assembly - 如果函数相等则 NASM 程序集跳转

标签 assembly x86 nasm

我对汇编编程非常陌生,只是编写简单的小程序并做很多教程来尝试自学。我一天的大部分时间都在使用 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/

相关文章:

c++ - Thunking __stdcall 函数与堆栈调整

c - MOV 和 MOV ptr 的区别

assembly - 8086 汇编中文本用户界面的字符大小错误

x86 - 支持SSE4的处理器是否支持SSSE3指令?

assembly - 使用 ES 寄存器会出现错误

linux - 理解以下 x86 汇编代码

c++ - FastCGI 应用程序 - 如何获取监听套接字句柄?

c - C 中的 switch 语句会清空 x86 管道吗?

assembly - x86 汇编中 "align"关键字/指令的语法是什么?

c - 当我尝试从 C 程序调用已编译的 NASM 函数时出现 undefined reference 错误