x86 linux系统调用的调用约定? [英] x86 linux system call calling convention?
问题描述
给出此代码:
section .text
global _start
_start: ;tell linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
## mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string
内核如何知道 msg
在 ecx
中,在 len
在 edx
中,等等?我们不向内核"传递参数?
How does the kernel know that msg
is in ecx
, len
in edx
, etc? We do not pass arguments to "kernel"?
推荐答案
这就是您传递参数的方式.系统调用的调用约定说明哪些寄存器保存参数.内核希望它们在那里,您应该将它们放在那里.另请参见此参考.
That is how you pass the arguments. The calling convention for system calls says which registers hold the arguments. The kernel expects them there, and you should put them there. See also this reference.
虽然通常的32位 cdecl
约定使用堆栈来传递参数,但在用户模式下也存在类似的约定(特别是 fastcall
),它们也使用寄存器来传递参数.x86-64的标准约定也可以做到这一点.
While the usual 32 bit cdecl
convention uses the stack to pass arguments, there are similar conventions in user mode (notably fastcall
) that also use registers to pass arguments. The standard conventions for x86-64 do that as well.
这篇关于x86 linux系统调用的调用约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!