使用x64 linux syscalls从键盘读取输入(汇编) [英] Reading input from keyboard with x64 linux syscalls (assembly)

查看:118
本文介绍了使用x64 linux syscalls从键盘读取输入(汇编)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何在汇编中使用linux 64位syscall.

I'm trying to learn how to use linux 64bits syscall in assembly.

我正在编写一些用于读取键盘的代码,然后将其直接打印在屏幕上按下的键上:我正在使用sys_read.

I was writing some code for reading keyboard and simply print it on screen the pressed keys: i'm using sys_read.

代码:

section .text

global _start

_start:
;write startmsg
mov rax, 1
mov rdi, 1
mov rsi, startmsg
mov rdx, sizestart
syscall
;using sys_read
mov ax, 0
mov rdi, 0
mov rsi, key
mov rdx, 2
syscall
;trying to see if esc is pressed then exit
mov rbx, 0x1b
cmp rbx, key
je _exit

_exit:  
mov rax, 60
mov rdi, 0
syscall

section .bss
key resw 1

section .data
startmsg db 'Press a key', 10
sizestart equ $-startmsg

现在发生两件事:1)它会自动在屏幕上打印按键(D :)2)当我按esc时它不会退出

Now two things happen: 1) it automatically prints on screen the keys (D:) 2) it doesn't exit when i press esc

推荐答案

它会自动在屏幕上打印按键

it automatically prints on screen the keys

这是Linux中的默认设置(与编程语言无关):

This is the default setting in Linux (independent of the programming language):

  • 键盘输入被打印到屏幕上
  • sys_read将等到按下回车键(回车)

要更改此行为,必须调用 tcsetattr() 函数(在 C 中).您应该在存储当前设置之前调用tcgetattr()函数,并在退出程序之前恢复它们.

To change this behaviour the tcsetattr() function (in C) must be called. You should call the tcgetattr() function before to store the current settings and restore them before leaving the program.

如果要直接使用系统调用:tcsetattr和tcgetattr都使用一些sys_ioctl.要找出使用了哪个ioctl()代码,您可以编写一个执行tcsetattr和tcgetattr的C程序,并使用"strace"来找出要调用的系统调用.

If you want to use system calls directly: tcsetattr and tcgetattr both use some sys_ioctl. To find out which ioctl() code is used you may write a C program doing tcsetattr and tcgetattr and use "strace" to find out which syscalls are called.

当我按esc时它不会退出

it doesn't exit when i press esc

文件中存在三个问题:

  1. 据我了解,您每次调用sys_read时都会读取两个字节-这意味着两次击键
  2. sys_read将一直等到按下返回键(见上文)
  3. 您将64位值与只有一个(或两个)字节长的内存进行比较.

您应该只使用sys_read读取一个字节.然后,您应该按字节进行比较,而不是64位比较:

You should read only one byte using sys_read. Then you should do a bytewise compare instead of a 64-bit compare:

cmp bl,key

代替:

cmp rbx,key

这篇关于使用x64 linux syscalls从键盘读取输入(汇编)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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