简单C与汇编
C Swap
#include<stdio.h> #include<stdlib.h> void swap(long *a,long *b) { long temp=*a; *a=*b; *b=temp; } int main(int argc,char *argv[])//Unix标准C语言main函数 //argc代表参数数量,argv是字符串数组,argv[0]是可执行文件的名字 { long a=atol(argv[1]); long b=atol(argv[2]); //long a=atol("2"); //long b=atol("5"); swap(&a,&b); printf("%ld %ld\n",a,b); printf("%s\n",argv[0]); return 0; } //执行命令 gcc -o first.out first.c //得到first.out可执行文件 //执行./first.out 2 5
汇编
执行命令:gcc -Og -S first.c 得到汇编结果first.s
.file "first.c" .text .globl swap .type swap, @function swap: .LFB38: .cfi_startproc movq (%rdi), %rax movq (%rsi), %rdx movq %rdx, (%rdi) movq %rax, (%rsi) ret .cfi_endproc .LFE38: .size swap, .-swap .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "%ld %ld\n" .text .globl main .type main, @function main: .LFB39: .cfi_startproc pushq %rbx .cfi_def_cfa_offset 16 .cfi_offset 3, -16 subq $32, %rsp .cfi_def_cfa_offset 48 movq %rsi, %rbx