;PROGRAM GOES HERE;11:34 2023/4/9如果使用子程序写应该更方便,这个代码有点繁杂了.
;FOLLOWED BY DESCRIPTIVE PHRASES
;EQU STATEMENTS GO HERE 1.从键盘输入两位十六进制数,并检查其合法性,大小写都可以,不合法重新输入.
ST_SEG SEGMENT STACK ;2.合法就将其转为二进制数存入地址a(一个字节)中,再将其显示在屏幕上.
DB 20 DUP('STACK') ;3.将地址 A 中(一字节)的二进制数转换成压缩的 BCD 码,存入地址 B 开始的两字节,依次显示.
ST_SEG ENDS
;****************
DATAREA SEGMENT ;DEFINE CODE SEGMENT
buffer1 db 20 dup('$')
pif DB "PLEASE INPUT FILENAME:$ " ;DATA GOES HERE
yf DB "YOUR FILENAME:$ "
yn db "[Y/N]?$ "
pip DB "PLEASE INPUT PASSWORD:$ "
ok1 db "OK!$"
changeline db 13,10,'$'
xuehao db '20201050'
buffer db 3,32 dup(?)
blank db ' ','$'
ERROR DB 'PLEASE WRITE AGAIN!',10,13,'$'
a db 32 dup(?)
b db 32 dup(?)
DATAREA ENDS
;*****************
PROGRAM SEGMENT
;-------------------
MAIN PROC FAR ;MAIN PART OF PROGRAM
ASSUME CS:PROGRAM,DS:DATAREA,SS:ST_SEG
;SAVE IT ON STACK,SAVE OLD DATA SEGMENT
START:
MOV AX,DATAREA ;SET DS REGISTER TO CURRENT DATA SEGMENT,DATEREA SEGMENT ADDR
MOV DS,AX ;INTO DS REGISTER
;MAIN PART OF PROGRAM GOES HERE
;-----------------输入两位十六进制数
JMP write1
WRITE:MOV AH,09H
lea dx,ERROR
INT 21H
write1:mov ah,0ah
lea dx,buffer
int 21h
;---------------------------检查合法性,不合法重新输入,合法就存入地址a(一个字节)中,并显示.
lea di,buffer
inc di
inc di ;跳转到缓冲区第一个输入的字符(字节型数据)
;----------------比较第一个字符
mov cl,[di]
cmp cl,30h
jb write ;小于30h(0)则重新输入
cmp cl,39h
jna access1 ;0~9跳转到数字输出
cmp cl,41h
jb write ;小于A重新写
cmp cl,46h
jna access2; 大写A~F转到相应输出
cmp cl,61h
jb write; 小于a重写
cmp cl,66h
jna access3; 小写a~f转到相应输出
jmp write ;其余全部跳转重写
;--------------存入地址,要学会屏蔽高四位或者低四位,目的是为了先显示高位,后显示低位;
access1:sub cl,30h
lea si,buffer1
mov [si],cl
jmp two;这里之前的错误是没有写跳转指令导致一直顺序执行,也就是分支结构没有写好
;-------------
access2:sub cl,37h
lea si,buffer1
mov [si],cl
jmp two;这里之前的错误是没有写跳转指令导致一直顺序执行,也就是分支结构没有写好
;-------------
access3:sub cl,57h
lea si,buffer1
mov [si],cl
jmp two;这里之前的错误是没有写跳转指令导致一直顺序执行,也就是分支结构没有写好
;----------------比较第二个字符
two:
lea di,buffer
inc di
inc di
inc di
mov cl,[di]
cmp cl,30h
jb write ;小于30h(0)则重新输入
cmp cl,39h
jna access4;
cmp cl,41h
jb write ;小于A重新写
cmp cl,46h
jna access5;
cmp cl,61h
jb write; 小于a重写
cmp cl,66h
jna access6;
jmp write ;其余全部跳转重写
;--------------
access4:sub cl,30h
lea si,buffer1
inc si
mov [si],cl
jmp three
;-------------
access5:sub cl,37h
lea si,buffer1
inc si
mov [si],cl
jmp three
;-------------
access6:sub cl,57h
lea si,buffer1
inc si
mov [si],cl
jmp three
;--------------------
three:mov ah,09h
mov dx,offset changeline
int 21h ;changeline
;------------------显示部分,一位十六进制的数变为四位二进制形式的数.
lea si,buffer1
mov al,[si]
mov cl,4
ror al,cl
inc si
mov bl,[si]
add al,bl
lea di,a
mov [di],al;数据存入地址a中
mov ch,9
jmp display_binary
bk:mov ah,09h
lea dx,blank
int 21h
pop ax
display_binary:push ax;入栈保护ax(al)
dec ch
cmp ch,4
jz bk
cmp ch,-1 ;或者设置断点再跳回到这个地方
jz display_b
and al,80h
cmp al,80h
jz print1
jnz print0
print1:mov dl,31h
mov ah,02h
int 21h
pop ax
rol al,1
jmp display_binary
print0:mov dl,30h
mov ah,02h
int 21h
pop ax
rol al,1
jmp display_binary
;-----------将地址 A 中(一字节)的二进制数转换成压缩的 BCD 码,存入地址 B 开始的两字节,依次显示.
display_b:lea si,a
mov al,[si]
and al,0fh
cmp al,09h
ja above_9;如果超过9,高四位为0001
mov dl,00h
add dl,al
lea si,b
mov [si],dl
jmp gaowei
above_9:mov dl,10h
sub al,0ah
add dl,al
lea si,b
mov [si],dl
gaowei: lea si,a
mov al,[si]
mov cl,4
rol al,cl
and al,0fh
cmp al,09h
ja above9;如果超过9,高四位为0001
mov dl,00h
add dl,al
lea si,b
inc si
mov [si],dl
jmp huanhang
above9:mov dl,10h
sub al,0ah
add dl,al
lea si,b
inc si
mov [si],dl
;-----------------------
huanhang: mov ah,09h
mov dx,offset changeline
int 21h
;------------------
;---------------------------------------BCD显示
lea si,b
mov ax,[si]
mov ch,17
jmp display1
bk1:mov ah,09h
lea dx,blank
int 21h
pop ax
display1: push ax;入栈保护ax(al)
dec ch
cmp ch,12
jz bk1
cmp ch,7
jz bk1
cmp ch,2
jz bk1
cmp ch,-3
jz exit
and ax,8000h
cmp ax,8000h
jz print_1
jnz print_0
print_1:mov dl,31h
mov ah,02h
int 21h
pop ax
rol ax,1
jmp display1
print_0:mov dl,30h
mov ah,02h
int 21h
pop ax
rol ax,1
jmp display1
;----------------------------以下为结束部分
exit:
MOV AH,4CH
INT 21H
;RETURN TO DOS
MAIN ENDP ;END OF MAIN PART OF PROGRAM
;-------------
SUBRL PROC NEAR ;DEFINE SUBPROCEDURE
;SUBROUTINE GOES HERE
SUBRL ENDP ;END SUBPROCEDURE
;-------------------
PROGRAM ENDS ; END OF CODE SEGMENT
;******************
END START; END ASSEMBLY