【问题标题】:x86 ax register getting a different value?x86 ax 寄存器得到不同的值?
【发布时间】:2018-06-06 15:39:38
【问题描述】:

我想将 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

【问题讨论】:

标签: assembly x86 masm32


【解决方案1】:

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

StdIn 从控制台接收 text 输入并将其放入缓冲区 需要作为参数。函数在 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

【讨论】:

    【解决方案2】:

    尝试使用

    mov ax, input
    

    而不是

    lea ax, input
    

    Lea 加载指向您正在寻址的项目的指针(类似于 0x12345678),而 mov 在该地址加载实际值。

    【讨论】:

    • 表示指令操作数无效。我认为您不能使用 mov 将数据从变量移动到寄存器。
    • mov ax, word ptr [input] .. 当然可以。作为您的整个代码,它仍然没有意义,因为输入很可能是 ASCII 字符串,而不是数字,因此您只需将第一个字符与数字的 ASCII 代码进行比较,例如cmp al,'5',但无论如何,使用lea 只是表明您应该先阅读一些书籍/教程。此外,您应该使用调试器来观察真正发生的事情。 (在汇编程序中没有像高级语言那样真正的“变量”,它只是内存,可以按字节寻址...... MASM 会给你一些“变量”的概念,但是嗯..)
    猜你喜欢
    • 2012-02-17
    • 1970-01-01
    • 2017-04-23
    • 2022-01-13
    • 1970-01-01
    • 2014-12-26
    • 2020-02-01
    • 2015-11-01
    • 2019-01-02
    相关资源
    最近更新 更多