我对汇编编程非常陌生,只是编写简单的小程序并做很多教程来尝试自学。我一天的大部分时间都在使用 bash 和 python,但汇编在我的工作领域具有很大的优势,而且我对它着迷不已。
我正在编写一个小程序来接受用户输入的“y”或“n”进行比较并输出两条消息之一。它非常简单,但是我无法弄清楚我的跳转逻辑在哪里失败,并且想知道是否有人可以指出我正确的方向。
我在 Windows 7 x64 中使用 NASM,此代码编译时没有问题,并且我已将问题追溯到我的跳转指令。下面是我的代码。我的问题是,输入 y 或 n 后,它似乎没有正确调用 yes_msg 或 no_msg,并且在 google 和此处搜索了一个下午后,我一直无法弄清楚。如果有人能提供帮助,我将不胜感激。
谢谢。
;compile with nasm.exe -f elf tutorial4.asm
;gcc -m32 tutorial4.o -o tutorial4.exe
global _main
extern _printf
extern _scanf
global _yes_msg
global _no_msg
section .data
msg db "Please say yes or no? [y/n] ", 0
msgy db "You said yes! ", 0
msgn db "You said no! ", 0
yes db 'y', 0
no db 'n', 0
fmt db "%s", 0
section .bss
inpt resb 200
section .text
_yes_msg:
push msgy
call _printf
add esp, 4
_no_msg:
push msgn
call _printf
add esp, 4
_main:
push ebp
mov ebp, esp
push msg
call _printf
add esp, 4
push inpt
push fmt
call _scanf
mov eax, inpt
mov edx, 'y'
cmp eax, edx
jne _no_msg
jmp _yes_msg
add esp, 8
mov esp, ebp
pop ebp
ret
最佳答案
您的指令mov eax, inpt
将缓冲区的地址移动到EAX寄存器中的inpt处。您需要此位置的第一个字节,因此必须编写 mov al, [inpt]
。请记住这是 NASM!
现在您可以比较内容:
mov dl, 'y'
cmp al, dl
您将 _scanf 调用的清理工作置于绝对跳转下方,因此它永远不会被执行。将其移至调用本身下方:
call _scanf
add esp, 8
mov al, [inpt]
正如 @Netch 所说,您可能应该更改 _yes_msg 和 _no_msg 处的代码,因为现在您连续收到两条消息,并且不会像这样退出程序应该。
关于assembly - 如果函数相等则 NASM 程序集跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34237892/