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] = "0x00000000\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)