等待按键组装NASM,Linux [英] Wait for keypress Assembly NASM, Linux

查看:69
本文介绍了等待按键组装NASM,Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为x86-64开发一个Hello World in Assembly.

I'm working on a Hello World in Assembly for x86-64.

我设法创建了一个在按Enter键时完成的操作,但是我必须在按ANY键时完成了操作.

I have managed to create one that finishes when Enter key is pressed, but I have to finish it when ANY key is pressed.

这是等待ENTER键的代码:

This is the code for waiting the ENTER Key:

mov rax, 0
mov rdi, 0
mov rdx, 1
syscall

我不能使用任何int xh或类似的东西.仅系统调用.

I can't use any int xh or something like that. Only syscalls.

谢谢!

推荐答案

我已经回答了之前的类似问题,并提供了可直接与系统调用配合使用的C代码,以完成您想要的事情.

I've answered a similar question before, and gave C code that would work directly with system calls to do what you wanted.

这是该代码到nasm的翻译,稍有变化以反映您只是在检查是否按下了任何键,而不是特定的键:

Here's a translation of that code to nasm, with slight changes to reflect that you're just checking that any key is pressed, not a specific key:

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

基本思想是,您必须编辑终端设置,读取字符并重置设置.

The basic idea is that you have to edit the terminal settings, read a character, and reset the settings.

这篇关于等待按键组装NASM,Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆