//x86平台 x86指令集,intel风格test.c
#include
int add(void) {
long eax=4;
long ebx=2;
__asm__ __volatile__ ("addl %1, %0"
: "=b"((long)ebx)
: "a"((long)eax), "b"((long)ebx)
: "1"
);
printf("ebx=%d\n", ebx);
return 0;
}
int main(){
int a=12;
char *hello="hello,world\n";
__asm__(
".intel_syntax noprefix\n"
"mov eax,4\n"
"mov ebx,1\n"
"mov ecx,%0\n"
"mov edx,%1\n"
"int 0x80\n"
".att_syntax\n"
:
:"r"(hello),"r"(a)
:"eax","ebx","ecx","edx"
);
add();
exit(0);
}
-------------------------------------------------
//x86平台 x86指令集 ,at&t风格test.c
#include
int sum(int a, int b)
{
int r;
__asm__(
"sub $0x4, %%esp\n"
"movl 0xc(%%ebp), %%eax\n"
"addl 0x8(%%ebp),%%eax\n"
" movl %%eax, 0xfffffffc(%%ebp)\n"
" movl 0xfffffffc(%%ebp), %%eax\n"
" movl %%eax, %%eax\n"
: "=a"(r)
);
return r;
}
int main()
{
int x = sum(10,2);
printf("x = %d\n", x);
return 0;
}
gcc -o test test.c
-------------------------------------------------
//arm 平台,用arm平台的指令集 ,test.c
int main()
{
//printf("starting...\n");
//直接内嵌汇编
__asm__(
"ldr r0, =0x30000000\n"//别忘记加个等号
"mov r1,#1\n"
"str r1,[r0]\n"
);
}
arm-linux-gcc -o test test.c
阅读(1094) | 评论(0) | 转发(0) |