为什么 Masm32 只给出 1 到 100 的加法和减法运算结果,除此之外我得到了错误的答案?

Why Masm32 only give 1 to 100 result for add and subtraction operation and beyond that I got wrong answer?

我是汇编语言的新手。我刚刚编写了这段代码并且 运行 到目前为止没有任何错误,除了它只会给出 1 - 100 的结果,这是我的代码。

这是一个简单的数学运算,就是加减法。 我试过像 num1 db 10 dup (?) 中那样分析代码 我试过将 10 更改为 100 但它仍然无法正常工作。我错过了什么吗?

.386
.model flat, stdcall
option casemap :none
include \masm32\include\masm32rt.inc

.data
    msgEC db "Choose Operation", 00ah, 0
    msgAdd db "Addition 1", 00ah, 0
    msgSub db "Subtraction 2", 00ah, 0
    msgEx db "Exit 3", 00ah, 0
    msg db "Addition", 00ah, 0
    msgSub2 db "Subtraction", 00ah, 0
    msg1 db "Enter 1st Number:", 00ah, 0
    msg2 db "Enter 2nd Number:", 00ah, 0
    msg3 db "Sum is:", 00ah, 0
    msgdf db "Diff is:", 00ah, 0
    endl db 00ah, 0
    msg4 db "Try Again[1/0]:", 0
.data?

    num1 db 10 dup (?)
    num2 db 10 dup (?)
    res sdword ?
    choice db 10 dup (?)
    choice2 db 10 dup (?)

.code
start:
    invoke StdOut, addr msgAdd
    invoke StdOut, addr msgSub 
    invoke StdOut, addr msgEx
    invoke StdOut, addr msgEC
    invoke StdIn, addr choice2, 10
    mov[choice2 + eax-3], 0
    invoke StripLF, addr choice2
    invoke atodw, addr choice2

    .if eax == 1
    jmp _addition
    .elseif eax == 2
    jmp _subtraction
    .elseif eax == 3 || eax > 3
    jmp _exit
    .endif


_addition:

    invoke ClearScreen
    invoke StdOut, addr msg
    invoke StdOut, addr msg1
    invoke StdIn, addr num1, 10
    mov[num1 + eax-3], 0
    invoke StripLF, addr num1
    invoke atodw, addr num1
    mov ebx, eax

    invoke StdOut, addr msg2
    invoke StdIn, addr num2, 10
    mov[num2 + eax-3], 0
    invoke StripLF, addr num2
    invoke atodw, addr num2
    add ebx, eax   

    invoke dwtoa, ebx, addr res
    invoke StdOut, addr msg3
    invoke StdOut, addr res
    invoke StdOut, addr endl

    invoke StdOut, addr msg4
    invoke StdIn, addr choice, 10
    mov[choice + eax-3], 0
    invoke StripLF, addr choice
    invoke atodw, addr choice

    .if eax == 1
    jmp _addition
    .else
    .endif

_subtraction:

    invoke ClearScreen
    invoke StdOut, addr msgSub2
    invoke StdOut, addr msg1
    invoke StdIn, addr num1, 100
    mov[num1 + eax-3], 0
    invoke StripLF, addr num1
    invoke atodw, addr num1
    mov ebx, eax

    invoke StdOut, addr msg2
    invoke StdIn, addr num2, 100
    mov[num2 + eax-3], 0
    invoke StripLF, addr num2
    invoke atodw, addr num2
    sub ebx, eax   

    invoke dwtoa, ebx, addr res
    invoke StdOut, addr msgdf
    invoke StdOut, addr res
    invoke StdOut, addr endl

    invoke StdOut, addr msg4
    invoke StdIn, addr choice, 10
    mov[choice + eax-3], 0
    invoke StripLF, addr choice
    invoke atodw, addr choice

    .if eax == 1
    jmp _subtraction
    .else 
    .endif

_exit:

invoke ExitProcess, 0

end start

编辑:示例输出是,如果我添加 100 + 5,它会显示错误的结果,即 5,但如果我添加较小的数字,如 90 + 5,它会输出正确的结果 95。

您代码中的所有这些操作都不正确:

mov[num1 + eax-3], 0

StdIn returns 读取的字符数,不包括 NUL 终止符。因此,如果您输入 100eax 将为 3,因此您会将第一个字节设置为 0,从而使 num1 成为一个空字符串。更糟糕的是,如果您输入一位或两位数字,您将在 num1 之外写入,因为 eax-3 将为负数。

幸运的是,StdIn 会为您去除以 CRLF 和 NUL 结尾的字符串。因此,您可以简单地删除所有这些 mov[foo + eax-3], 0 指令以及 invoke StripLF, addr foo 调用。