我正在编写有关汇编中基本输入和输出的教程。我使用的是 64 位 Linux 发行版 (Ubuntu)。在教程的第一部分中,我讨论了基本输出并创建了一个简单的程序,如下所示:
global _start
section .text
_start:
mov rax,1
mov rdi,1
mov rsi,message
mov rdx,13
syscall
mov rax,60
xor rdi,rdi
syscall
section .data
message: db "Hello, World", 10
效果很好。系统打印字符串并干净退出。对于教程的下一部分,我只想从键盘读取一个字符。从我对此的理解web site http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/我们改变rdi注册为0对于 sys_read 调用。
我首先从当前值中减去8rsp然后将该地址加载到rsi登记。 (这就是我想存储字符的地方)。当我编译并运行我的程序时,它似乎可以工作......但终端似乎模仿我再次输入的输入。
这是程序:
global _start
section .text
_start:
sub rsp,8 ; allocate space on the stack to read
mov rdi,0 ; set rdi to 0 to indicate a system read
mov rsi,[rsp-8]
mov rdx,1
syscall
mov rax,1
mov rdi,1
mov rsi,message
mov rdx,13
syscall
mov rax,60
xor rdi,rdi
syscall
section .data
message: db "Hello, World", 10
这就是我的终端中发生的事情......
matthew@matthew-Precision-WorkStation-690:~/Documents/Programming/RockPaperScissors$ nasm -felf64 rps.asm && ld rps.o && ./a.out
5
Hello, World
matthew@matthew-Precision-WorkStation-690:~/Documents/Programming/RockPaperScissors$ 5
5: command not found
matthew@matthew-Precision-WorkStation-690:~/Documents/Programming/RockPaperScissors$
输入 5 被重复返回到终端after程序已退出。使用 NASM 和 Linux x64 读取单个字符的正确方法是什么?