系统调用实现Hello world!
最常见的一种:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 3 int main(void) 4 { 5 printf("Hello world!\n"); 6 return 0; 7 }
C系统调用:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <unistd.h> 2 3 int main(void) 4 { 5 write(1,"Hello world!\n",14); 6 return 0; 7 }
利用syscall直接调用:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <unistd.h> 2 #include <sys/syscall.h> 3 4 int main(void) 5 { 6 syscall(SYS_write,1,"Hello world!\n",14); 7 return 0; 8 }
利用trap异常(0x80)进行调用:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 .section .data 2 string: 3 .ascii "Hello world!\n" 4 string_end: 5 .equ len, string_end - string 6 7 .section .text 8 .globl main 9 main: 10 #First call write(1,"Hello world!\n",14) 11 movl $4,%eax #system call number 4 12 movl $1,%ebx #stdout has descriptior 1 13 movl $string,%ecx #Hello world string 14 movl $len,%edx #string length 15 int $0x80 #system call code 16 17 #Next, call exit(0); 18 movl $1,%eax #system call number 1 19 movl $0,%ebx #argument is 0 20 int $0x80 #system call code
所有的到Linux系统调用的参数都是通过通用寄存器而不是栈传递的。按照惯例,寄存器%eax包含系统调用号,寄存器%ebx、%ecx、%edx、%esi、%edi、%ebp包含最多六个任意的参数(64位可能还包含r8、r9)。