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?
There is a long way from inputting to the application:
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.
@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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With