1.C language Inline compilation 1.1 inline compilation syntax
1.2 Inline assembly Examples
#include <stdio.h>int main(){ int result = 0; int input = 1; int a = 1; int b = 2; asm volatile ( "movl %1, %0\n" // 通过占位符指定交互的变量 : "=r"(result) // 输出变量,与汇编交互 : "r"(input) // 输出变量,与汇编交互// 这里的r指示编译器自动将通用寄存器关联到变量 ); printf("result = %d\n", result); printf("input = %d\n", input); asm volatile ( "movl %%eax, %%ecx\n" "movl %%ebx, %%eax\n" "movl %%ecx, %%ebx\n" : "=a"(a), "=b"(b) // 这里指明a变量使用a寄存器 : "a"(a), "b"(b) ); printf("a = %d\n", a); printf("b = %d\n", b); return 0;}
1.3 What the compiler did
For the example above, the compiler does the following:
1. Associating result to an appropriate register
2. Associating input to an appropriate register
3. Indirect operation of variables via general register
Attention:
Assembly language does not support memory-to-memory direct operations, and registers are used as intermediate roles.
1.4 Common Restrictions Description
1.5 Using System Services
System services can be used directly through an inline assembly. Using the kernel service with int 80H
1.INT instructions for using the Linux kernel Service (interrupt instruction)
2.80H is an interrupt vector number that is used to perform system calls
3. Specific system calls and their parameters can be specified via registers (e.g. Sys_write service)
1.6 Using system services to finish printing
char* s = "D.T.Software\n";int l = 13;asm volatile( "movl $4, %%eax\n" // "movl $1, %%ebx\n" "movl %0, %%ecx\n" "movl %1, %%edx\n" "int $0x80 \n" : : "r"(s), "r"(l) : "eax", "ebx", "ecx", "edx");
1.7 Exiting using the system service execution program
asm volatile( "movl $1, %eax\n" "movl $42, %ebx\n" "int $0x80 \n" );
Attention:
1. When the assembly is embedded, the remaining parameters can be omitted except the assembly template.
2. When the omitted argument is in the middle, the corresponding delimiter ":" must not be omitted
3. When the reserved list is omitted, the corresponding delimiter ":" Can be omitted
4. When an optional parameter is omitted, a single% prefix is used before the register
When an optional parameter is present, the register uses two percent-percent as a prefix
C Language Inline compilation