首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Masm32在加减运算中只给出1到100的结果,然后我得到了错误的答案?

为什么Masm32在加减运算中只给出1到100的结果,然后我得到了错误的答案?
EN

Stack Overflow用户
提问于 2015-06-13 01:56:57
回答 1查看 206关注 0票数 0

我对汇编语言很陌生。我刚才做了这个代码,到目前为止没有错误地运行它,但是它只会给出1-100的结果,这是我的代码。

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

代码语言:javascript
复制
.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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-13 08:02:51

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

代码语言:javascript
复制
mov[num1 + eax-3], 0

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30814377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档