0

我有这个代码:

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

1FEer 第一个十六进制数:按任意键继续。. .

可以看到,prompt1 的一部分,er first hex number卡住了 sum 的值1FE,为什么会这样

程序循环edx寄存器3次第一次值是E,第二次是F,第三次是1,然后写入writeString打印输出,这似乎是程序应该做的,此时edx值突然跳到00405911,为什么会这样?

提前感谢您的帮助

4

1 回答 1

0

将 NULL 终止符添加到转换后的字符串。您可以在convertDigit循环之前执行此操作:

mov byte ptr [arry+4],0


div顺便说一句,当你除以 2 的幂(比如 16)时 ,你不需要。您可以使用更快的按位与和移位指令:

mov dx,ax
and dx,15   ; dx = ax & 15 (== ax % 16)

shr ax,4    ; ax = ax >> 4 (== ax / 16)
于 2013-05-21T05:23:24.293 回答