Re: calling syscalls directly from asm

2007-07-15 Thread Marcus Watts
 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
movl8(%esp),%ebx# fd
movl12(%esp),%ecx   # buffer
movl16(%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



Re: calling syscalls directly from asm

2007-07-15 Thread Vincent GROSS

On 7/15/07, Marcus Watts [EMAIL PROTECTED] wrote:

 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
movl8(%esp),%ebx# fd
movl12(%esp),%ecx   # buffer
movl16(%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


Okay, so I did missed a big, fat, screaming obvious point ...
Thanks a lot for your answers.

--
Vincent GROSS
GUIs normally make it simple to accomplish simple actions and
impossible to accomplish complex actions. --Doug Gwyn (22/Jun/91 in
comp.unix.wizards)



calling syscalls directly from asm

2007-07-14 Thread Vincent GROSS

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 ...


#include sys/types.h
#include unistd.h

char hexstr[12] = 0x\n ;

int main(int argc, char *argv[]){
 unsigned int stack_ptr ;
 unsigned int str_addr ;
 int *page_start ;
 int page[1024] ;
 int i ;
 int __ret ;
 asm(movl %%ebp, %0 : =r(stack_ptr)) ;
 str_addr = (unsigned int)hexstr ;
 page_start = (int *)(stack_ptr  ~0xFFF) ;
 for (i = 0 ; i  8 ; i++){
   switch ((stack_ptr  (i*4))  0xf){
   case 0 :
 hexstr[9-i] = '0' ;
 break ;
   case 1 :
 hexstr[9-i] = '1' ;
 break ;
   case 2 :
 hexstr[9-i] = '2' ;
 break ;
   case 3 :
 hexstr[9-i] = '3' ;
 break ;
   case 4 :
 hexstr[9-i] = '4' ;
 break ;
   case 5 :
 hexstr[9-i] = '5' ;
 break ;
   case 6 :
 hexstr[9-i] = '6' ;
 break ;
   case 7 :
 hexstr[9-i] = '7' ;
 break ;
   case 8 :
 hexstr[9-i] = '8' ;
 break ;
   case 9 :
 hexstr[9-i] = '9' ;
 break ;
   case 10 :
 hexstr[9-i] = 'a' ;
 break ;
   case 11 :
 hexstr[9-i] = 'b' ;
 break ;
   case 12 :
 hexstr[9-i] = 'c' ;
 break ;
   case 13 :
 hexstr[9-i] = 'd' ;
 break ;
   case 14 :
 hexstr[9-i] = 'e' ;
 break ;
   default :
 hexstr[9-i] = 'f' ;
   }
 }
 /*
 write(1, hexstr, 11) ;
 */
 asm volatile (\n\tint $0x80
: =a(__ret)
: 0(4), b(1), c(hexstr), d(11));

 /*
 for (i = 0 ; i  1024 ; i++){
   page[i] = page_start[i] ;
 }
 write(1, (char *)page, 4096) ;
 */
 exit(0) ;
}


--
Vincent GROSS
GUIs normally make it simple to accomplish simple actions and
impossible to accomplish complex actions. --Doug Gwyn (22/Jun/91 in
comp.unix.wizards)