-1

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 
Comedy
  • 23
  • 5
  • You could do a `sys_read` to get a single byte from the input stream. – h0r53 May 25 '21 at 15:36
  • You tagged this [osdev]. Does that mean you're writing kernel code, specifically a keyboard driver? (If so, for what hardware? legacy possibly-emulated PS/2 keyboard controller? USB (much more complex), or something like UEFI calls to take care of it for you?) If you're not writing a kernel / bare-metal / uefi application, then you're just going to want to ask the kernel to read input into your memory. It will be ASCII text representing digits which you can turn into a binary integer with [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) – Peter Cordes May 25 '21 at 23:25
  • Also note that `cha dw` reserves no bytes. Perhaps you mean `section .bss` / `cha resw 1` to reserve 2 bytes? – Peter Cordes May 25 '21 at 23:28

1 Answers1

0

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.

h0r53
  • 3,034
  • 2
  • 16
  • 25
  • 1
    `cha dw` has an empty list of word initializers, so it reserves 0 bytes. Normally you'd just use stack space for a small fixed size, but you want at least `dw 0`. Also, one way of reading the question would imply they want [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) to save a binary integer into .data. And it's not clear if they're actually doing [osdev]; if so they may be trying to write a keyboard driver. – Peter Cordes May 25 '21 at 23:29