【问题标题】:NASM Assembly; jumps not working in this codeNASM 大会;跳转在此代码中不起作用
【发布时间】:2015-11-20 17:56:47
【问题描述】:

在这段代码中,我试图让编译器识别字符串中的第二个字符。比如我输入haris;然后我希望能够迭代到字符串“haris”中的“a”。我使用 lea 获取指向寄存器存储“haris”的指针。然后我增加了寄存器eax,它用作指针,最初指向“h”,所以它应该指向“a”——对吗?但是,我的代码似乎没有按预期工作,因为如果它工作,跳转到 _works 应该工作。

extern _printf
extern _scanf
extern _printf
extern _system
extern _strcmp

segment .data


szPAUSE db'PAUSE', 0
szStrIn db '%s',0
szName db 0,0
szNworks db 'Doesnt work', 10,0
szworks db 'Does work', 10,0


segment .code

global _main

_main:
enter 0,0

;get name. I input "haris" for test
push szName 
push szStrIn
call _scanf
add esp, 8

;;test to see if name was inputted
mov esi, szName
push esi
call _printf
add esp, 4

;;get the address of the second character in szName, which in my case is "a"
lea eax, [esi+1]

;; compare the second character with "a"
cmp eax, 0x61
je _works

;; if the character is not equal to "a"
push szNworks
call _printf
add esp, 4

push szPAUSE
call _system
add esp, 4

leave 
ret

;;if the character is equal to "a"
_works:
push szworks
call _printf
add esp, 4

push szPAUSE
call _system
add esp, 4

leave 
ret

【问题讨论】:

    标签: assembly nasm


    【解决方案1】:

    正如您的评论所说,您将 地址 加载到 eax,而不是字符本身。而不是lea,您应该使用movzx eax, byte [esi+1] 来加载具有零扩展名的字符(因为您稍后会与eax 进行比较)。如果您保留lea,则需要在之后插入movzx eax, byte [eax]。如果以后注意只使用低 8 位,则可以使用简单的mov,例如:

    mov al, [esi+1]
    cmp al, 0x61
    

    【讨论】:

    • 你能告诉我mov和movzx的区别吗?我们还没有在课堂上介绍过 movzx。谢谢。
    • @haris: movzx eax, byte [esi+1] 将从[esi+1] 加载一个字节并将值零扩展为eax(即24 个最高有效位都设置为零)。
    • @haris:英特尔有一个很好的 PDF 文档,记录了所有 x86 指令,以及确切它们的作用。请参阅stackoverflow.com/tags/x86/info 的链接。
    猜你喜欢
    • 2015-03-31
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2016-04-06
    • 2014-02-01
    • 2017-01-09
    • 2018-04-01
    相关资源
    最近更新 更多