linux - 等待按键 组装 NASM,Linux

标签 linux assembly nasm x86-64 system-calls

我正在为 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/

相关文章:

c - 撤回非法指令

linux - iwlist 输出到列表或变量

regex - 查找具有 2 个或更多 * 的字符

linux - Windows 机器中的 UNIX/LINUX 本地主机服务器

NASM x86 程序集中的 C float

assembly - NASM x86 16 位寻址模式

node.js - 将 KillAll 发送到多 Node 进程并等待响应

assembly - 函数参数未正确传入 MASM

c - 如何在程序集中访问 C 结构值?

c++ - gcc-c++ 是否没有为当前的 x86-64 处理器优化原子操作