使用汇编语言,从键盘上输入0-65535之间的十进制数,将其转换成十六进制数,并在屏幕上显示出

越简单越好
当按下<ESC>键后显示“OK”并退出程序。

;输入不超过65535的10进制数,将其以16位的形式显示
code  segment
      assume cs:code
      org 100h
start:
      jmp bbb
x     dw 0
msg1  db 10,13,'please input a number:','$'
msg2  db 10,13,'     ','$'

bbb:
      push cs
      pop ds
      lea dx,msg1
      mov ah,9
      int 21h
      
      lea bx,x
      mov ax,0
      push ax
again1:
      mov ah,1
      int 21h
      cmp al,13
      je line0
      sub al,30h
      mov ah,0
      mov si,ax
      pop ax
      mov cl,10
      mov ch,0
      mul cx
      add ax,si
      push ax
      jmp again1
line0:
      pop ax
      mov word ptr[x],ax
      lea di,msg2
      inc di
      inc di
      mov cx,4
again2:
      mov ax,word ptr[x]
      rol ax,1
      rol ax,1
      rol ax,1
      rol ax,1
;      rol ax,4
      mov word ptr[x],ax
      and ax,000Fh
      cmp al,9
      jg line1
      add al,30h
      mov byte ptr[di],al
      jmp line2
line1:
      add al,37h
      mov byte ptr[di],al
line2:
      inc di
      loop again2
      
      lea dx,msg2
      mov ah,9
      int 21h
      mov ah,8
      int 21h

      mov ah,4ch
      int 21h
code  ends
      end start

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-05-10
没太明白。
一般情况下,输入完数据后,按回车,结束输入。
你这按下ESC键,是当什么情况下按下?
难道是:比方说,输入12,这时按下ESC,屏幕上输出OK?
下面是不考虑按ESC情况的程序。
;read a Decimal number, print it Hex
dseg segment
n  dw 0
f  db 0
t  db '0123456789ABCDEF'
msg0 db 'Please input decimal number between 0-65535:$'
msg1 db 'Conversion result is:$'
msg2 db 0dh,0ah,'Input error!',0dh,0ah,'$'
dseg ends

cseg segment
assume cs:cseg, ds:dseg
start:
mov ax, dseg
mov ds, ax

lea dx, msg0
mov ah,9
int 21h

lea si, n
call read10

cmp f,1
je ex0

lea dx, msg1
mov ah,9
int 21h

mov ax,n
call print
ex0:
mov ah,4ch
int 21h

;read a Decimal number
;store it in [si]
;end flag: char not in '0'-'9'
;readflag  f:0-success, 1:error
read10 proc near
xor dx,dx
mov bx,10
rd0:
mov ah,1
int 21h

cmp al,'0'
jb rt0
cmp al,'9'
ja rt0

and ax,0Fh
xchg ax,[si]
mul bx
add ax,[si]
jc re0
xchg ax,[si]

jmp rd0
re0:
mov f,1
lea dx,msg2
mov ah,9
int 21h
rt0:
ret
read10 endp

;print (ax) in Hex
print proc near
mov bx,16
xor cx,cx
Q0:
xor dx,dx
div bx
or dx,0e00h
push dx
inc cx
cmp ax,0
jnz Q0

lea bx,t
Q1:
pop ax
xlat
int 10h
loop Q1

mov al,'H'
int 10h
ret
print endp

cseg ends

end start

相似回答