首页 > 解决方案 > x86 ax 寄存器得到不同的值?

问题描述

我想将 ax 与 5 进行比较,如果值大于 5,它将显示一个错误框。如果没有,它只会打印数字。但是即使我输入1,它总是显示值大于5。问题出在哪里?

.386 
.model flat,stdcall 
option casemap:none 

include c:\masm32\include\windows.inc
include \masm32\include\kernel32.inc 
includelib \masm32\lib\kernel32.lib 
include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib

.data 
msg db "Enter Number", 0
msg1 db "The value is too large", 0

.data? 
input db 150 dup(?)
output db 150 dup(?)
.code 


start: 
push offset msg
call StdOut 

push 100
push offset input
call StdIn

lea ax, input
cmp ax, 5
jg Toolarge


exit:
    push 0
    call ExitProcess 

 Toolarge:
     push offset msg1
     call StdOut
     jmp start

end start

标签: assemblyx86masm32

解决方案


MASM32 附带一个帮助文件:\masm32\help\masmlib.chm. 它说:

StdIn 从控制台接收文本输入并将其作为参数放入所需的缓冲区中。该函数在按下 Enter 时终止。

我标记了相关的单词“文本”。所以,你会得到一个 ASCII 字符串,而不是一个适合AX. 您必须先将其转换为“整数”,然后才能将其与cmp. 您可以使用 MASM32 功能atol

.386
.model flat,stdcall
option casemap:none

include c:\masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib

.data
    msg db "Enter Number", 0
    msg1 db "The value is too large", 0

.data?
    input db 150 dup(?)
    output db 150 dup(?)
.code

start:

    push offset msg
    call StdOut

    push 100
    push offset input
    call StdIn

    push offset input
    call atol

    cmp eax, 5
    jg Toolarge

exit:

    push 0
    call ExitProcess

Toolarge:

    push offset msg1
    call StdOut
    jmp start

end start

推荐阅读