键盘输入两个一位十进制数(有提示输入),编程计算他们的和,并将结果分别以二进制、十进制和十六进制的形式显示到CRT上。
(扩展:做一个菜单,选1,2,3,4分别输出及退出)
data segment
input1 db 'please input a number x:',0dh,0ah,'$'
input2 db 'please input a number y:',0dh,0ah,'$'
menu db "1 print b", 0dh, 0ah ;定义打印菜单
db "2 print d", 0dh, 0ah
db "3 print h", 0dh, 0ah
db "4 exit", 0dh, 0ah, '$' ;0dh回车,0ah换行
crlf db 0aH,0dH,"$";回车换行
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov dx, offset input1;打印请输入x
mov ah,9
int 21h
mov ah,1
int 21h
mov bh,al;输入的从al送入bh
lea dx, crlf
mov ah, 09h
int 21h
mov dx ,offset input2;打印请输入y
mov ah,9
int 21h
mov ah,1
int 21h
mov bl,al ;输入的从al送入bl
lea dx, crlf
mov ah, 09h
int 21h
mov dx, offset menu;打印菜单
mov ah,9
int 21h
mov ah,1
int 21h
;输入选择1,2,3,4
lea dx, crlf
mov ah, 09h
int 21h
add bl,bh ;x+y的值送入dl
sub bl,60h
mov dl,bl
cmp al,'1' ;选择菜单,跳转。
jz b
cmp al, '2'
jz d
cmp al, '3'
jz h
jmp eexit
b:
mov al,dl
mov bl,8
l1:
mov dl,0
shl al,1 ;逻辑左移
mov BH,AL
adc dl,30h ;显示数据到屏幕
mov ah, 02
int 21h
MOV AL,BH
dec bl ;8位完否
jnz l1
mov dl,0dh ;加回车,换行符
mov ah, 02
int 21h
mov dl,0ah
mov ah, 02
int 21h
jmp start
d:
MOV AL,dl
CALL B8_TO_D
jmp start
B8_TO_D PROC NEAR;8位二进制转十进制子程序
;AL中要转换的二进制数
mov cl,0
AND AH,0
MOV BL,100
DIV BL
MOV CL,AH
ADD AL,30H
MOV DL,AL
MOV AH,02H
INT 21H
MOV AL,CL
MOV BL,10
AND AH,0
DIV BL
ADD AL,30H
MOV DL,AL
MOV CL,AH
MOV AH,02H
INT 21H
MOV AL,CL
ADD AL,30H
MOV DL,AL
MOV AH,02H
INT 21H
RET
B8_TO_D ENDP
h:
mov al,dl
MOV BH,AL
mov cl,4 ;每次循环右移动4位,先处理高4位
shr al,cl
;
cmp al,0ah
jb next ;AL内容>=A,加37H
add al,7h
next:
add al,30h ;否则,加30H
mov dl,al ;输出字符的ASCII码送DL
mov ah,2 ;02H号DOS功能调用
int 21h
mov al,bh
and al,0fh ;屏蔽高4位,再先处理低4位
cmp al,0ah
jb next1 ;AL内容>=A,加37H
add al,7h
next1:
add al,30h ;否则,加30H
mov dl,al ;输出字符的ASCII码送DL
mov ah,2 ;02H号DOS功能调用
int 21h
mov dl,0dh ;加回车,换行符
mov ah, 02
int 21h
mov dl,0ah
mov ah, 02
int 21h
jmp start
eexit:
mov ax, 4c00H
int 21H
code ends
end start