views:

39

answers:

1

What is an OpenSolaris syscall calling convention (x86)?

F.e. I'd like to write a program in 32bit assembly which displays a string to the console. For this I'd like to use "write" syscall (no. 4). C definition for write is:

ssize_t write(int fildes, const void *buf, size_t nbyte)

what registers should hold fildes, buf and nbyte arguments? Which interrupt should I call?

+1  A: 

You could write a C program which calls write,compile it to assembly language (use -S option). And then examine the output to see how the compiler does it.

Edit:

OpenSolaris libc does it like this:

First when you call write it check some things and then calls __write:

pushl  0x10(%ebp)
pushl  0xc(%ebp)
pushl  0x8(%ebp)
call   c2730 <__write>

and __write then looks like:

<__write>:
call c2735 <__write+0x5>
pop    %edx
mov    $0x4,%eax
mov    %esp,%ecx
add    $0x10,%edx
sysenter
jae    c2751 <__write+0x21>
cmp    $0x5b,%eax
je     c2730 <__write>
jmp    2e0d0 <__cerror>
ret

It puts the write syscall number in eax (4), stack pointer in ecx and the return address in edx. And the arguments to the write syscall have been already pushed on the stack.

Jaka
The compiler calls the 'write' function from libc. Same thing when I use 'syscall' function to call a syscall - it calls the 'syscall' function from libc.I'll analyze the libc's implementation of these functions when I'll have some spare time, but that time didn't come yet :)
antonone
Lol, I have the answer. Here it is - http://bit.ly/baMJVo :)
antonone