Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly happens when I hit the Enter button in terms of system_read interrupt, assembly?

I have this code:

section .bss
    buff    resb 1
readfromkeyboard:
    mov     eax,3       ;specify system read
    mov     ebx,0       ;specify standard in -> keyboard
    mov     ecx,buff    ;where to store what is read
    mov     edx,1       ;read 1 byte
    int     0x80        ;tell linux to do everything above

    mov     eax,4       ;sys_write
    mov     ebx,1       ;Standard output
    mov     ecx,buff    ;what to print          
    mov     edx,1       ;how long to print
    int     0x80        ;tell linux to do everything above

which works fine.

When I start the process the cursor will start to blink in terminal and I am free to enter characters. At this point I am free to enter as many characters as I want, except when I hit "ENTER" 1 byte will be read and it will be printed in the terminal.

My question is, what is happening internally as I enter characters and as I hit Enter.. So I hit 'a' in my keyboard, and say 'c', where is this data stored at the moment? Are they already in the memory space addressed by 'buff' in my code? Why does Linux read when I hit Enter?

like image 213
Koray Tugay Avatar asked Jan 11 '15 10:01

Koray Tugay


2 Answers

There is a long way from inputting to the application:

  • Hardware
  • Driver layer
  • Console layer
  • reading functions

Somewhere therein happens the treatment of lines, I think it is at the console layer. There you can input data which is processed on in lines.

If an application comes along and reads, it gets as many characters as it asks for, the remaining ones are kept for the next reading call.

If there are none remaining, it will wait until the next line is complete - or if the user presses ^D, which means to terminate the current read() call. If no data were entered before, read() returns 0, denoting EOF. In all other cases, read() returns the number of bytes read so far.

like image 151
glglgl Avatar answered Sep 19 '22 12:09

glglgl


@glglgl has a good answer there, but here's a more direct answer: The other characters are sitting in the read input buffer waiting for processing.

Since you appear to be talking about Linux here, the kernel has a buffer set aside specifically for this that is created when a character devices is registered. If you'd really like to dig into this deeply, I'd strongly suggest this article. When you get there, search for vfs_read and start reading. It's a very good write up!

like image 34
David Hoelzer Avatar answered Sep 17 '22 12:09

David Hoelzer