Koray Tugay
Koray Tugay

Reputation: 23800

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?

Upvotes: 12

Views: 686

Answers (4)

David Hoelzer
David Hoelzer

Reputation: 16361

@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!

Upvotes: 2

John Hascall
John Hascall

Reputation: 9416

The linux terminal driver is reading the characters and buffering them in its own memory space. Because your terminal is in 'line' mode, this buffering continues until the ENTER key ends a line. At this point the data can be transferred into the buffer you supplied. You only asked for 1 character, so that's what you get and the remaining characters sit in the terminal driver's memory for your next read request.

Upvotes: 1

glglgl
glglgl

Reputation: 91109

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.

Upvotes: 5

Sep Roland
Sep Roland

Reputation: 39306

If you asked for 1 byte then the input function will never store any extra bytes at the memory at buff. Linux will only store the a at buff but certainly not the c

Upvotes: 1

Related Questions