我有这段代码:
INCLUDE Irvine32.inc
.data
arry BYTE ?
prompt1 BYTE "Enter first hex number: ",0
prompt2 BYTE "Enter second hex number: ",0
prompt3 BYTE "The sum is ",0
prompt4 BYTE "The sum is out of range ",0
prompt5 BYTE "Convert again? [y/n]: ",0
prompt6 BYTE "First number is invalid ",0
prompt7 BYTE "Second number is invalid ",0
.code
main PROC
ReadInput:
L1:
mov edx, OFFSET prompt1
call writeString
mov edx, 0
call readHex
call Crlf
mov ecx, eax
jmp L3
L2:
mov eax, 0
mov edx, OFFSET prompt2
call writeString
mov edx, 0
call readHex
call Crlf
mov ebx, eax
mov eax, 0
jmp L4
L3:
cmp ecx, 0FFFFh
JA L5
JBE L2
L4:
cmp ebx, 0FFFFh
JA L6
JBE addInt
L5:
mov edx, OFFSET prompt6
call writeString
mov edx, 0
call Crlf
jmp L1
L6:
mov edx, OFFSET prompt7
call writeString
mov edx, 0
call Crlf
jmp L2
addInt:
clc
mov ax, cx
add ax, bx
JC printError
jmp convert
; convert hex to string
convert:
mov ecx, 0
mov esi, 0
mov si, 4
mov cx, 10h
convertDigit:
dec si
mov dx , 0
div cx
cmp dx, 9h
JA convertLetter
add dx, 30h
jmp printSucess
convertLetter:
add dx, 37h
jmp printSucess
printError:
mov edx, OFFSET prompt4
call writeString
call Crlf
printSucess:
mov arry[si], dl
cmp ax , 0
JNE convertDigit
mov edx, OFFSET arry
add dx, si
call writeString
call Crlf
exit
main ENDP
END main
当我尝试打印输出时,我得到了这个
输入第一个十六进制数:ff
输入第二个十六进制数:ff
1个第一个十六进制数: 按任意键继续 。 。
正如您所看到的,有一部分prompt1,er first hex number
与1FE
的值相符,为什么会发生这种情况
程序循环edx寄存器3次第一次值E
,第二次是F
,第三次是1
,然后它到writeString打印输出,这似乎是程序假设要做的,此时edx值突然跳到00405911
,为什么会发生这种情况?
提前感谢您的帮助
答案 0 :(得分:0)
为转换后的字符串添加NULL终结符。你可以在convertDigit
循环之前做到这一点:
mov byte ptr [arry+4],0
顺便说一句,当你除以2的幂(如16)时,你不需要div
。您可以使用更快的按位AND和移位指令:
mov dx,ax
and dx,15 ; dx = ax & 15 (== ax % 16)
shr ax,4 ; ax = ax >> 4 (== ax / 16)