0

I'd like to know if it's possible to get keystrokes in realtime using x86 assembly and if yes, how would that look. My goal is to make a program that continuously runs and executes some code only when the user presses a certain key, but doesn't stop until he does so. Until now I've only found solutions that either didn't work or stopped until the user pressed the key. I'm using x86 assembly with NASM as the assembler, my OS is Linux, I compile my code using these commands:

nasm -f elf64 hello_world.asm
ld -s -o hello_world hello_world.o

and a hello world example for me would look like this:

section .text
   global _start
    
_start:
   mov  edx,len
   mov  ecx,msg
   mov  ebx,1
   mov  eax,4
   int  0x80
    
   mov  eax,1
   mov  ebx,0
   int  0x80

section .data
   msg db 'Hello, world!', 0xa
   len equ $ - msg

I've found a lot of people using int 16h, but that's not possible on Linux.

Thanks in advance,
Konstantin

SkilLP
  • 1
  • 3
    Look up "terminal raw mode" and "asynchronous I/O". I suggest getting it working in C or another higher-level language first, then converting to asm. – Nate Eldredge Jul 24 '20 at 20:09
  • If you're willing to have the stop key be Ctrl-C, then you can instead handle the SIGINT signal and save some trouble. – Nate Eldredge Jul 24 '20 at 20:11
  • 2
    The fact that it's x86 asm is basically irrelevant. What matters is that you're running under Linux, getting input through Linux system calls. Not via DOS or BIOS calls. The CPU doesn't have a keyboard, and being x86 only tells you what CPU instructions you can use. – Peter Cordes Jul 24 '20 at 20:15
  • 2
    Fortunately there was an exact duplicate of your question recently, including the problem of using `int 0x80` instead of `syscall` in 64-bit mode. – Peter Cordes Jul 24 '20 at 20:18
  • Here's some good info, but it's in C - should be pretty easy to figure out how to reimplement it in ASM. https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html – flashbang Jul 24 '20 at 20:30
  • @PeterCordes That's not really a duplicate as OP wants to process the key stroke asynchronously. Some approach using either asynchronous IO or threads might work. – fuz Jul 25 '20 at 11:16
  • @fuz: Oh I see. They want that plus `select` or `poll`, or polling with manual reads on an fd with `O_NONBLOCK` would be like one of the `int 16h` BIOS function I think? Anyway, found some duplicates. – Peter Cordes Jul 25 '20 at 11:30

0 Answers0