> Date: Sat, 14 Jul 2007 18:48:46 +0200 > To: misc@openbsd.org > From: "Vincent GROSS" <[EMAIL PROTECTED]> > Subject: calling syscalls directly from asm > > Hi folks, > > I would like to call write(2) without going through the libc functions. I > wrote > this little thing to test, it does not print anything, but friends say > it works just > fine with linux. I did check the addresses and operands in the resulting > binary with objdump, everything has the correct values. What am I doing > wrong ? Feel free to cluebat me to death if I missed some obvious point ... ...
I don't know what you hope to accomplish by avoiding the use of libc. But if you really want to do that, you'll need to know that the system call interface is completely different on linux than openbsd. Here's assembly code to call write on linux: .globl write write: pushl %ebx movl 8(%esp),%ebx # fd movl 12(%esp),%ecx # buffer movl 16(%esp),%edx # count movl $4,%eax # __NR_write int $128 popl %ebx testl %eax,%eax jl cerror_ ret cerror_: negl %eax movl %eax,errno movl $-1,%eax movl $-1,%edx ret In linux, parameters are passed in the registers, & the error code is returned as a negative number. Here's assembly code to call write on openbsd: .globl write write: movl $4,%eax # SYS_write int $128 jb cerror_ ret cerror_: movl %eax,errno movl $-1,%eax movl $-1,%edx ret On OpenBSD, parameters are passed on the stack. The kernel cleverly copies stuff from the stack just where the C calling conventions left them, which is why you don't see any code here to muck with that. An error is indicated by setting the carry flag. Incidently, there are better ways to do hexadecimal conversion. That is, assuming you really don't want to use libc. For instance, consider how you might use this: *--cp = "0123456789abcdef" [ n & 15 ]; -Marcus Watts