我正在为 x86-64 开发一个 Hello World in Assembly。
我已经成功创建了一个在按下 Enter 键时完成的任务,但我必须在按下任何键时完成它。
这是等待 ENTER 键的代码:
mov rax, 0
mov rdi, 0
mov rdx, 1
syscall
我不能使用任何 int xh 或类似的东西。仅系统调用。
谢谢!
最佳答案
我已回复a similar question before ,并给出了可以直接与系统调用一起工作的 C 代码,以完成您想要的操作。
这是该代码到 nasm 的翻译,稍作更改以反射(reflect)您只是检查是否按下了任意键,而不是特定键:
fwait:
; fetch the current terminal settings
mov rax, 16 ; __NR_ioctl
mov rdi, 0 ; fd: stdin
mov rsi, 21505 ; cmd: TCGETS
mov rdx, orig ; arg: the buffer, orig
syscall
; again, but this time for the 'new' buffer
mov rax, 16
mov rdi, 0
mov rsi, 21505
mov rdx, new
syscall
; change settings
and dword [new+0], -1516 ; ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON)
and dword [new+4], -2 ; ~OPOST
and dword [new+12], -32844 ; ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN)
and dword [new+8], -305 ; ~(CSIZE | PARENB)
or dword [new+8], 48 ; CS8
; set settings (with ioctl again)
mov rax, 16 ; __NR_ioctl
mov rdi, 0 ; fd: stdin
mov rsi, 21506 ; cmd: TCSETS
mov rdx, new ; arg: the buffer, new
syscall
; read a character
mov rax, 0 ; __NR_read
mov rdi, 0 ; fd: stdin
mov rsi, char ; buf: the temporary buffer, char
mov rdx, 1 ; count: the length of the buffer, 1
syscall
; reset settings (with ioctl again)
mov rax, 16 ; __NR_ioctl
mov rdi, 0 ; fd: stdin
mov rsi, 21506 ; cmd: TCSETS
mov rdx, orig ; arg: the buffer, orig
syscall
ret
基本思想是您必须编辑终端设置、读取字符并重置设置。
关于linux - 等待按键 组装 NASM,Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32193374/