x86 linux system call calling convention?

1k views Asked by At

Given this code :

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

How does the kernel know that msg is in ecx, len in edx, etc? We do not pass arguments to "kernel"?

1

There are 1 answers

0
Jester On BEST ANSWER

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.

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.