Assembly: file descriptor 0 or 2 for reading user

2019-03-02 06:15发布

问题:

I am learning assembly and there were different file descriptors used for reading user input.

For reading keyboard entry, I was expecting file descriptor 0 (stdin), but came across this article where file descriptor 2 (stderr) was used.

;Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num  
mov edx, 5          ;5 bytes (numeric, 1 for sign) of that information
int 80h

However, I've commonly seen ebx set as 0 instead:

;Read and store the user input
mov eax, 3
mov ebx, 0
mov ecx, num  
mov edx, 5          ;5 bytes (numeric, 1 for sign) of that information
int 80h

I have tried setting ebx to 0 and 2, and they both work fine without issues. Can you explain to me which is the better option to use? Or is there other approaches I should take for best practices?

回答1:

The better option is to use the STDIN file descriptor (No. 0). Nevertheless, you can use the STDERR streaM (No. 2) - actually an output stream - for reading:

The stderr stream is expected to be open for reading and writing

This is a practical workaround, if the STDIN stream is redirected.



标签: assembly nasm