2

我正在尝试将我的stdrepl库移植到 FASM 以用于学习目的。我知道 GNU readline 库已经做了我想做的事情,但我想学习如何在汇编中编写重要的程序。

在 node.js 中,我可以通过以下方式轻松创建 tty:

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

如何在纯组装中获得相同的结果。我尝试在循环中一次读取一个字节,stdin如下所示,但是在我按下一个键后它不会立即返回该字节:

oct db ?

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

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

4

1 回答 1

1

抱歉耽搁了(我真的应该在这里“注册” - 这会给我“通知”,对吧?)。正如我所说,它是初级的和不完善的。一些“通常”的东西可能在别处定义,但我认为你可以弄清楚如何组装它。只需调用它 - 没有参数 - 并且密钥以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
;-------------------------

于 2013-06-09T21:41:15.110 回答