我应该使用哪些Linux系统调用来读取stdin中的原始字符?

我试图将我的stdreplstdrepl到FASM的学习目的。 我知道GNU readline库已经做了我想做的事情,但是我想学习如何在汇编中编写非平凡的程序。

在node.js中,我可以通过编写来轻松创build一个tty:

 var stdin = process.stdin; stdin.setEncoding("utf8"); stdin.setRawMode(true); stdin.resume(); 

我如何在纯assembly中达到相同的结果。 我尝试从一个循环中读取stdin中的一个字节,如下所示,但是在按下某个键之后,它不会立即返回字节:

 oct db ? mov eax, 3 xor ebx, ebx mov ecx, oct mov edx, 1 

请注意, oct的数据定义不是循环的一部分,所以请不要为此而伤害我。 我知道如何构build一个汇编程序。

对不起,延迟(我真的应该“注册”在这里 – 这将让我“通知”,对不对?)。 正如我所说,这是基本的和不完善的。 一些“通常”的东西可能在其他地方定义,但我想你可以弄清楚如何组装。 只要调用它 – 没有参数 – 钥匙是返回在al 。 希望对你有用处!

 ;----------------------------- ; ioctl subfunctions %define TCGETS 0x5401 ; tty-"magic" %define TCSETS 0x5402 ; flags for 'em %define ICANON 2 ;.Do erase and kill processing. %define ECHO 8 ;.Enable echo. struc termios alignb 4 .c_iflag: resd 1 ; input mode flags .c_oflag: resd 1 ; output mode flags .c_cflag: resd 1 ; control mode flags .c_lflag: resd 1 ; local mode flags .c_line: resb 1 ; line discipline .c_cc: resb 19 ; control characters endstruc ;--------------------------------- getc: push ebp mov ebp, esp sub esp, termios_size ; make a place for current kbd mode push edx push ecx push ebx mov eax, __NR_ioctl ; get current mode mov ebx, STDIN mov ecx, TCGETS lea edx, [ebp - termios_size] int 80h ; monkey with it and dword [ebp - termios_size + termios.c_lflag], ~(ICANON | ECHO) mov eax, __NR_ioctl mov ebx, STDIN mov ecx, TCSETS lea edx, [ebp - termios_size] int 80h xor eax, eax push eax ; this is the buffer to read into mov eax, __NR_read mov ebx, STDIN mov ecx, esp ; character goes on the stack mov edx, 1 ; just one int 80h ; do it ; restore normal kbd mode or dword [ebp - termios_size + termios.c_lflag], ICANON | ECHO mov eax, __NR_ioctl mov ebx, STDIN mov ecx, TCSETS lea edx, [ebp - termios_size] int 80h pop eax ; get character into al pop ebx ; restore caller's regs pop ecx pop edx mov esp, ebp ; leave pop ebp ret ;-------------------------