linux平台学x86汇编(十八):内联汇编
asm("movl $1, %eax\n\t" "movl $0,%ebx\n\t" "int $0x80");
#include <stdio.h> int result = 10; int main(int argc, const char *argv[]) { asm("addl $1, result\n\t" "subl $2, result\n\t"); printf("the result is %d\n", result); return 0; }
.file "inline-as.c" .globl result .data .align 4 .type result, @object .size result, 4 result: .long 10 .section .rodata .LC0: .string "the result is %d\n" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp andl $-16, %esp subl $16, %esp #APP # 7 "inline-as.c" 1 addl $1, result subl $2, result # 0 "" 2 #NO_APP movl result, %edx movl $.LC0, %eax movl %edx, 4(%esp) movl %eax, (%esp) call printf movl $0, %eax leave ret .size main, .-main .ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-3)" .section .note.GNU-stack,"",@progbits
$ ./inline-as the result is 9 $
__asm__("addl $1, result\n\t" "subl $2, result\n\t");
<span style="font-family:Microsoft YaHei;">asm ("as code": output location : input operands : changed registers);</span>
#define CAL ({ asm("addl $1, result\n\t" "subl $2, result\n\t"); })这里asm语句必须要在一对花括号中,以便指出语句的开头和结尾,否则编译器会生成错误信息。
#include <stdio.h> #define CAL ({ asm("addl $1, result\n\t" "subl $2, result\n\t"); }) int result = 10; int main(int argc, const char *argv[]) { CAL; printf("the result is %d\n", result); return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。