Linux 汇编语言开发入门学习(1)
2016-02-27 22:32
381 查看
在Unix 和 Linux 系统中,汇编语言格式采用的较多的是 AT&T 格式。
C程序:
int g(int x)
{
return x + 27;
}
int f(int x)
{
return g(x);
}
int main(void)
{
return f(2016) + 2;
}
在Ubuntu 14.04 上汇编结果:
.file "main.c"
.text
.globl g
.type g, @function
g:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl %edi, -4(%rbp)
movl -4(%rbp), %eax
addl $27, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size g, .-g
.globl f
.type f, @function
f:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $8, %rsp
movl %edi, -4(%rbp)
movl -4(%rbp), %eax
movl %eax, %edi
call g
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size f, .-f
.globl main
.type main, @function
main:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $2016, %edi
call f
addl $2, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4"
.section .note.GNU-stack,"",@progbits
去掉用于链接和版本信息后的计算机会执行的汇编代码:
g:
pushq %rbp
movq %rsp, %rbp
movl %edi, -4(%rbp)
movl -4(%rbp), %eax
addl $27, %eax
popq %rbp
ret
f:
pushq %rbp
movq %rsp, %rbp
subq $8, %rsp
movl %edi, -4(%rbp)
movl -4(%rbp), %eax
movl %eax, %edi
call g
leave
ret
main:
pushq %rbp
movq %rsp, %rbp
movl $2016, %edi
call f
addl $2, %eax
popq %rbp
ret
实验截图:
作者:冯峰
原创作品转载请注明出处
《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
在Unix 和 Linux 系统中,汇编语言格式采用的较多的是 AT&T 格式。
C程序:
int g(int x)
{
return x + 27;
}
int f(int x)
{
return g(x);
}
int main(void)
{
return f(2016) + 2;
}
在Ubuntu 14.04 上汇编结果:
.file "main.c"
.text
.globl g
.type g, @function
g:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl %edi, -4(%rbp)
movl -4(%rbp), %eax
addl $27, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size g, .-g
.globl f
.type f, @function
f:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $8, %rsp
movl %edi, -4(%rbp)
movl -4(%rbp), %eax
movl %eax, %edi
call g
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size f, .-f
.globl main
.type main, @function
main:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $2016, %edi
call f
addl $2, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4"
.section .note.GNU-stack,"",@progbits
去掉用于链接和版本信息后的计算机会执行的汇编代码:
g:
pushq %rbp
movq %rsp, %rbp
movl %edi, -4(%rbp)
movl -4(%rbp), %eax
addl $27, %eax
popq %rbp
ret
f:
pushq %rbp
movq %rsp, %rbp
subq $8, %rsp
movl %edi, -4(%rbp)
movl -4(%rbp), %eax
movl %eax, %edi
call g
leave
ret
main:
pushq %rbp
movq %rsp, %rbp
movl $2016, %edi
call f
addl $2, %eax
popq %rbp
ret
实验截图:
作者:冯峰
原创作品转载请注明出处
《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000
相关文章推荐
- Linux精讲——atime、mtime、ctime
- (九)利用processing展示简单函数的使用
- Linux实用操作命令(不断更新)
- linux常用命令
- linux c 代码测试之内存越界及内存泄露
- linux(ubuntu)hadoop系统迁移或使用新主机产生问题
- linux c 编程模板总结(一)
- linux内核分析 第一周
- Linux Is Not Matrix——使用域名访问局域网网站
- linux(ubuntu)mysql自动安装
- Linux SSH安全策略限制IP登录方法(转)
- linux安全第一周总结
- CentOS 6.7下利用Rsyslog+LogAnalyzer+MySQL部署日志服务器
- Centos安装powerDNS和poweradmin
- eclipse4.5在Linux下标题栏显示过大的调整方法
- Linux#public key登陆问题
- 你知道哪些linux命令,能把文件上传到远程linux服务器
- CentOS下安装FTP服务器及配置3.html
- Linux内核分析第一周总结
- [国嵌攻略][077][Linux时间编程]