问题:我们给了一个16位的十进制数字,我们必须以十六进制格式打印该数字。
例子:
Input: d1 = 999
Output: 3E7
Input: d1 = 123
Output: 7B
解释:
- 将存储的值加载到寄存器中
- 将值除以16可将其转换为十六进制
- 将其余部分推入堆栈
- 增加数量
- 重复这些步骤,直到寄存器的值大于0
- 直到计数大于零
- 弹出堆栈
- 如果栈顶的值大于9
- 然后再向该值加7,以使其对应于十六进制字符A,B,C,D,E,F
- 将48添加到顶部元素以将其转换为ASCII
- 使用中断打印字符
- 减少计数
程序:
;8086 program to convert a 16 bit decimal number to Hexadecimal
.MODEL SMALL
.STACK 100H
.DATA
d1 dw 999
.CODE
MAIN PROC FAR
MOV AX,
@DATA
MOV DS,
AX
;load the value stored;
in variable d1
mov ax,
d1
;convert the value to Hexadecimal;
print the value
CALL PRINT
;interrupt to exit
MOV AH,
4CH INT 21H
MAIN ENDP
PRINT PROC
;initialize count
mov cx,
0 mov dx, 0 label1:
;if
ax is zero
cmp ax,
0 je print1
;initialize bx to 16 mov bx, 16
;divide it by 16
;to convert it to Hexadecimal
div bx
;push it in the stack
push dx
;increment the count
inc cx
;set dx to 0
xor dx,
dx
jmp label1
print1:
;check if count
;is greater than zero
cmp cx,
0 je exit
;pop the top of stack
pop dx
;compare the value
;with 9 cmp dx, 9 jle continue
;if
value is greater than 9
;then add 7 so that after
;adding 48 it represents A
;for example 10 + 7 + 48 = 65
;which is ASCII value of A
add dx,
7
continue:
;add 48 so that it
;represents the ASCII
;value of digits
add dx,
48
;interrupt to print a;
character
mov ah,
02h int 21h
;decrease the count
dec cx
jmp print1
exit : ret
PRINT ENDP
END MAIN
输出:
3E7
注意:该程序无法在在线编辑器上运行,请使用MASM运行该程序,并使用dos框运行MASM,您可以使用任何8086仿真器运行该程序