深入理解计算机系统第三章笔记 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值