Search code examples
assemblyx86-64osdev

How to get keyboard ASCII input and save it to a value in .data in x86_64 Assembly?


So as my question states, how do you get the ASCII key code from keyboard input as an integer value, then I want to save that value in a dataword inside of .data so I can then place the dataword into a different function.

input:
    ; get ASCII for keyboard input
    ; save ASCII into cha
    push rbp
    mov rdi, cha
    call kernel_input
    pop rbp

section .data
    cha dw 

Solution

  • You should be able to store a single byte by calling x64's SYS_READ system call. Here is some modified code based on your example.

    input:
        ; get ASCII for keyboard input
        ; save ASCII into cha
        push rbp
        mov  rdx, 1             ; max length
        mov  rsi, cha           ; buffer
        mov  rdi, 0             ; stdin
        mov  rax, 0             ; sys_read
        syscall
        pop rbp
    
    section .data
        cha dw 0
    

    I recommend looking up system calls in Linux for additional details.