System call
Direct use of system call Output Hello Generate ELF file $ LL ASM-ROOT 1202 JAN 25 18:59 ASM only 1202 bytes, and use PrintF to generate> 10K file source code
asm.c
#include
Int errno;
_SysCall3 (int, Write, Int, FD, Char *, DATA, INT, LEN);
_syscall1 (int, exit, int, status);
_Start () {Write (0, "Hello / N", 6); EXIT (0);
In /usr/include/asm/unistd.h, there is a macro definition of the system call, with the program related to #define __nr_exit 1 # define __nr_write 4 # define __syscall_return (type, res) / do {/ f ((unsigned long) (RES)> = (unsigned long) (- 125)) {/ errno = - (res); / res = -1; /} / return (r) (}); /} while (0)
#define _syscall1 (Type, Name, Type1, Arg1) / Type Name (Type1 Arg1) / {/ long __res; / __ ASM__ Volatile ("INT $ 0x80" /: "= a" (__res) /: "0" (__nr_ ## Name), "B" ((line))); / __ syscall_return (Type, __ res); /}
#define _syscall3 (Type, Name, Type1, Arg1, Type2, Arg2, Type3, Arg3) / Type Name (Type1 Arg1, Type2 Arg2, Type3 Arg3) / {/ long __res; / __ ASM__Volatile ("INT $ 0x80" /: "= a" (__res) /: "0" (__nr _ ## name), "B" ((long)), "C" ((arg2)), / "d" ((Long) ) (arg3))))); / __ syscall_return (Type, __ res); /}
Embedded assembly can refer to the "GCC Compilation C language" exit system call number 1 and Write is 4, the system call number is placed in EAX, and the Linux system calls can have 6 parameters to use EBX. , ECX, EDX, ESI, EDI, EBP (need to make a stack protection), can view the macro replacement code with the gcc -e command, the following code is convenient to change the format
$ cc -e asm.c # 1 "asm.c" # 1 "/usr/include/asm/unistd.h" 1 3 # 2 "ASM.C" 2
Int errno;
INT WRITE (INT FD, CHAR * DATA, INT LEN) {long __res; __asm__volatile ("INT $ 0x80": "= a" (_RES): "0" (4), "B" ((long) (FD) )), "DATA)," D "((LON))); Do {IF ((unsigned long) (__ rES)> = (UNSigned long) (- 125)) {Errno = - (__ res); __RES = -1;} return (int);} while (0);}; int exit (int stat (int status) {long __res; __asm__volatile ("INT $ 0x80": " = a "(__res):" 0 "(1)," B "((long)));
DO {IF (__ rES)> = (- 125)) {errno = - (__ res); __RES = -1;} return (int);} while (0); }
_Start () {Write (0, "Hello / N", 6); EXIT (0);
Compilation and running [Fifan @ Fifan Fifan] $ cc -c asm.casm.c: in function `exit ': asm.c: 6: warning: function declared` NoreTurn' HAS a `Return 'Statement [fifan @ fifan fifan] $ ld -o asm asm.o [fifan @ fifan fifan] $ ./asmhello[fifan@fifan fifan】 $