问题
I am looking through some demos of assembly (using NASM on a Mac, I am new to assembly) and seeing things like this:
; read a byte from stdin
mov eax, 3 ; 3 is recognized by the system as meaning "read"
mov ebx, 0 ; read from standard input
mov ecx, variable ; address to pass to
mov edx, 1 ; input length (one byte)
int 0x80 ; call the kernel
I am beginning to understand that eax
, ebx
, etc. are "general registers", which is where you store common things. Still have more to learn there but I get the gist of it.
But I am confused as to where the values like 3
(recognized by the system as meaning "read") and 0
(read from standard input) are coming from. How do you know that 0
means "standard input"? Is there a list of such integer values, or some book or standard reference?
回答1:
You're conflating system call numbers with the system call arguments.
The system call numbers (e.g. "3 = read") are OS-specific (well, kernel-specific), and sometimes version-specific. For example, see the system call numbers for Linux on x86 here and on x86_64 here. How the arguments are passed, how the system call is invoked, and what the system call numbers mean are all architecture- and kernel-specific.
The number "0" for "standard input" on the other hand is a UNIX standardized value, STDIN_FILENO
.
来源:https://stackoverflow.com/questions/27592937/where-are-the-system-codes-coming-from-in-x86-64-assembly