深入理解计算机系统第三章笔记 gcc

随便写个最简单程序
然后gcc -S
看汇编
在gcc -C 变成.o文件
使用
objdump -d
反汇编

 2010  vim code.c
 2011  ls
 2012  gcc -O1 -S code.c 
 2013  ls
 2014  cat code.s 
 2015  gcc  -S code.c 
 2016  cat code.s 
 2017  gcc -O1 -S code.c 
 2018  cat code.s 
 2019  gcc -O1 -c code.c
 2020  ls
 2021  cat code.o 
 2023  objdump -d code.o 

root@ubuntu:~/algorithm# cat code.c 
#include <stdio.h>
int accum=0;
int sum(int x,int y){
    int t=x +y;
    accum+=t;
    return t;
}

root@ubuntu:~/algorithm# cat code.s
        .file   "code.c"
        .text
        .globl  sum
        .type   sum, @function
sum:
.LFB22:
        .cfi_startproc
        movl    8(%esp), %eax
        addl    4(%esp), %eax
        addl    %eax, accum
        ret
        .cfi_endproc
.LFE22:
        .size   sum, .-sum
        .globl  accum
        .bss
        .align 4
        .type   accum, @object
        .size   accum, 4
accum:
        .zero   4
        .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
        .section        .note.GNU-stack,"",@progbits
root@ubuntu:~/algorithm# 

root@ubuntu:~/algorithm# objdump -d code.o 

code.o:     file format elf32-i386


Disassembly of section .text:

00000000 <sum>:
   0:   8b 44 24 08             mov    0x8(%esp),%eax
   4:   03 44 24 04             add    0x4(%esp),%eax
   8:   01 05 00 00 00 00       add    %eax,0x0
   e:   c3                      ret    
root@ubuntu:~/algorithm# 

----------------------------
root@ubuntu:~/algorithm# gcc -O1 -o prog code.o main.c
root@ubuntu:~/algorithm# cat main.c 
int main(){
    return sum(1,3);
}
root@ubuntu:~/algorithm# 

gcc -O1 -S -masm=intel simple.c
root@ubuntu:~/algorithm# cat simple.s 
        .file   "simple.c"
        .intel_syntax noprefix
        .text
        .globl  simple
        .type   simple, @function
simple:
.LFB22:
        .cfi_startproc
        mov     edx, DWORD PTR [esp+4]
        mov     ecx, DWORD PTR [edx]
        mov     eax, ecx
        add     eax, DWORD PTR [esp+8]
        add     ecx, eax
        mov     DWORD PTR [edx], ecx
        ret
        .cfi_endproc
.LFE22:
        .size   simple, .-simple
        .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
        .section        .note.GNU-stack,"",@progbits
root@ubuntu:~/algorithm# 

gcc -O1 -S  simple.c
root@ubuntu:~/algorithm# cat simple1.s 
        .file   "simple.c"
        .text
        .globl  simple
        .type   simple, @function
simple:
.LFB22:
        .cfi_startproc
        movl    4(%esp), %edx
        movl    (%edx), %ecx
        movl    %ecx, %eax
        addl    8(%esp), %eax
        addl    %eax, %ecx
        movl    %ecx, (%edx)
        ret
        .cfi_endproc
.LFE22:
        .size   simple, .-simple
        .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
        .section        .note.GNU-stack,"",@progbits
root@ubuntu:~/algorithm#

两种汇编的比较
ATT与Intel

你可能感兴趣的:(gcc)