【问题标题】:Assember prompt for user input doesn't work用户输入的汇编程序提示不起作用
【发布时间】:2018-11-10 19:57:36
【问题描述】:

今天我开始在 Linux 上使用 NASM 学习 x86_64 汇编。 我成功编写了一个 hello world 程序。 现在我想编写另一个简单的程序。 程序应该询问用户他的名字,然后打印“hi [name]”。 我的问题是该程序不要求名称。如果我启动程序,它不会打印任何内容并停止而不会出现错误。 这是我的代码:

section .data
    msg1 db "Type in ur Name? ", 10
    len1 equ $ - msg1       ; Get the Size of msg1

    msg2 db "Hi, "
    len2 equ $ - msg2       ;Get the Size of msg2

section .bss
    name resb 16            ;16 Bytes for name

section .text
    global _start

_start:

    ;Call Functions
    call _printMsg1
    call _getName
    call _printMsg2
    call _printName

    mov eax, 60
    mov ebx, 0
    int 0x80


_printMsg1:
    mov eax, 1
    mov ebx, 1
    mov ecx, msg1
    mov edx, len1
    int 0x80
    ret


_printMsg2:
    mov eax, 1
    mov ebx, 1
    mov ecx, msg2
    mov edx, len2
    int 0x80
    ret


_printName:
    mov eax, 1
    mov ebx, 1
    mov ecx, name
    mov edx, 16     ; reserve 16 Bytes for the name
    int 0x80
    ret


_getName:
    mov eax, 0      ;Syscall 0 = User Input
    mov ebx, 0
    mov ecx, name
    mov edx, 16     ;16 Bytes for the name
    int 0x80
    ret

感谢您的帮助!

编辑:我发现了问题。 如果我将以下寄存器替换为: eax 到 rax ebx 转 rdi ecx 到 rsi edx 转 rdx

好像我用的是假寄存器。

【问题讨论】:

标签: linux assembly nasm x86-64


【解决方案1】:

x86-32 和 x86-64 系统调用在数量、寄存器和系统调用指令方面有很大不同。

x86-32 系统调用使用int 80h 并且这个数字和寄存器:http://www.lxhp.in-berlin.de/lhpsysc0.html

x86-64 系统调用使用syscall 并且这个数字和寄存器: http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/

您正在使用 x86-32 系统调用,因此请相应地更改 EAX 中的数字。

【讨论】:

    【解决方案2】:

    快速修复
    您对 32 位和 64 位寄存器以及系统调用编号感到困惑
    我只是为 32 位架构更改了错误的寄存器值

    section .data
        msg1 db "Type in ur Name? ", 10
        len1 equ $ - msg1       ; Get the Size of msg1
    
        msg2 db "Hi, "
        len2 equ $ - msg2       ;Get the Size of msg2
    
    section .bss
        name resb 16            ;16 Bytes for name
    
    section .text
        global _start
    
    _start:
    
        ;Call Functions
        call _printMsg1
        call _getName
        call _printMsg2
        call _printName
    
        mov eax, 1
        mov ebx, 0
        int 0x80
    
    
    _printMsg1:
        mov eax, 4
        mov ebx, 1
        mov ecx, msg1
        mov edx, len1
        int 0x80
        ret
    
    
    _printMsg2:
        mov eax, 4
        mov ebx, 1
        mov ecx, msg2
        mov edx, len2
        int 0x80
        ret
    
    
    _printName:
        mov eax, 4
        mov ebx, 1
        mov ecx, name
        mov edx, 16     ; reserve 16 Bytes for the name
        int 0x80
        ret
    
    
    _getName:
        mov eax, 3      ;Syscall 3 = Read from stdin
        mov ebx, 0  
        mov ecx, name
        mov edx, 16     ;16 Bytes for the name
        int 0x80
        ret
    

    用 nasm 和 ld 编译为 32 位

    nasm -f elf32 test.asm -o test.o
    ld -m elf_i386 test.o -o test
    

    【讨论】:

    • 您的_getName 有问题。它从 stderr (fd = ebx=2) 读取。这通常会起作用,因为通常 fds 0、1 和 2 都引用终端的相同读写打开文件描述,但如果用户重定向或关闭 stderr 而不是 stdin,它会中断。
    • 普通皮特可怜不是我的职责! _getName 函数中 ebx 的正确值应该是多少?
    • 你抱怨我什么?您是破坏 OP 代码部分的人,该部分正确使用 STDIN_FD (0) 作为 read 系统调用的第一个参数。您是否不知道您应该从标准输入读取输入,或者不知道标准输入的文件描述符编号?我假设您知道 EBX 是 read(int fd, void *buf, size_t len) 的 fd arg。顺便说一句,如果您要发布这样的答案,通过使用 ; read(0, name, 16) 之类的 cmets 对其进行评论来为代码添加一些价值会很有用。
    • 你实际上还没有编辑你的答案来解决这个问题。但我想提出另一点:在代码中留下未纠正的问题是一件坏事。 Stack Overflow 的未来读者应该假设问题中的代码会被破坏。但是答案中的代码应该可以工作。通过将代码复制到您的答案中,您认可它是一个值得效仿的好例子。这就是为什么我对“代码不起作用”问题的回答通常会解决不相关的问题,甚至改进事情以做更好的事情。 (例如使用 RIP 相对寻址,或将常量放入 .rodata)
    • TL:DR:问题中的错误不是答案中错误的有效理由。这违背了 Stack Overflow 的目的:高质量的答案。从 stderr 读取并不总是 100% 错误,因此它并不明显属于这一类。例如我想我见过一些现实世界的程序,比如less,如果他们的标准输入没有连接到终端,则可以替代打开/dev/tty。 (如foo | less)。但是,如果您不做特定于终端的工作,那么从 stdin 以外的 FD 读取仍然不好。但如果没有大评论,这不是什么大事。
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2013-10-01
    相关资源
    最近更新 更多