计算机系统基础-汇编指令学习
1.1 传送指令
push %ebp
mov %esp,%ebp
and $0xfffffff0,%esp //边界对齐指令,16字节对齐
sub $0x20,%esp
movl $0xa,0x10(%esp) //立即数10
赋值给偏移量为
0x10的基址寄存器espmovl $0x9,0x14(%esp)?//立即数9赋值
mov 0x10(%esp),%eax?
mov %eax,0x18(%esp)?//立即数10赋值给0x18(%esp)
mov 0x14(%esp),%eax
mov %eax,0x1c(%esp)?//立即数9赋值给0x1c(%esp)
mov 0x1c(%esp),%eax
mov %eax,0x8(%esp) //立即数9赋值给0x8(%esp)
mov 0x18(%esp),%eax
mov %eax,0x4(%esp) //立即数10赋值给0x4(%esp)
movl $0x0,(%esp)
call 41 <main+0x41>
leave
ret
参考代码:
#include"stdio.h"
int main(){
? ? int a,b,c,d;
? ? a=10,b=9;
? ? printf("评测结果:成功\n评测脚本:C\n返回结果:");//此行不在汇编代码中
? ? //**********Begin**********
? ? c=a;
? ? d=b;
? ? //**********End**********
? ? printf("%d,%d\n",c,d);
}
1.2 传送指令
mov %esp,%ebp
and $0xfffffff0,%esp
sub $0x20,%esp
movl $0xa,0x18(%esp)?//立即数10
赋值给偏移量为
0x18的基址寄存器espmov 0x18(%esp),%eax
mov %eax,0x1c(%esp)
mov 0x1c(%esp),%eax
mov %eax,0x8(%esp)?//立即数10
赋值给
0x8(%esp)
mov 0x18(%esp),%eax
mov %eax,0x4(%esp)
movl $0x0,(%esp)
call 31 <main+0x31>
leave
ret
参考代码:?
#include"stdio.h"
int main(){
? ? int a=10;
? ? int b;
? ? printf("评测结果:成功\n评测脚本:C\n返回结果:");//此行不在汇编代码中
? ? //**********Begin********
? ? b=a;
? ? printf("%d,%d\n",a,b);
? ? //**********End**********
}
2.1 加减运算指令
push %ebp
mov %esp,%ebp
and $0xfffffff0,%esp
sub $0x20,%esp movl
movl $0x6,0x10(%esp)?//立即数6
赋值给偏移量为
0x10的基址寄存器espmovl $0x64,0x14(%esp) //16进制 立即数为100
mov 0x14(%esp),%eax?
mov 0x10(%esp),%edx
sub %eax,%edx?//将edx-eax的值赋值给edx
mov %edx,%eax
mov %eax,0x18(%esp)?//将edx的值赋值给c
mov 0x14(%esp),%eax
mov 0x10(%esp),%edx
add %edx,%eax?//将eax+edx的值赋值给edx
mov %eax,0x1c(%esp)
mov 0x1c(%esp),%eax
mov %eax,0x8(%esp)?//将eax的值赋值给c
mov 0x18(%esp),%eax
mov %eax,0x4(%esp)
movl $0x0,(%esp)
call 4f <main+0x4f>
leave
ret
参考代码 :
#include"stdio.h"
int main(){
? ? int a,b,c,d;
? ? a=6,b=100;
? ? printf("评测结果:成功\n评测脚本:C\n返回结果:");//此行不在汇编代码中
? ? //**********Begin**********
? ? c=a-b;
? ? d=a+b;
? ? //**********End**********
? ? printf("%d,%d\n",c,d);
}
3.1?整数的乘法指令
push %ebp
mov %esp,%ebp
and $0xfffffff0,%esp
sub $0x20,%esp
movl $0x3,0x10(%esp)?//立即数3
赋值给偏移量为
0x10的基址寄存器espmov 0x10(%esp),%eax
sub $0x1,%eax //将eax的值-1赋给eax
imul 0x10(%esp),%eax?//将eax与
0x10(%esp)内的值相乘赋给eax
mov %eax,0x14(%esp)?//赋值给b
mov 0x10(%esp),%eax?
imul 0x14(%esp),%eax?//将c与0x10(%esp)内的值相乘赋给eax
mov %eax,0x18(%esp)?//赋给c
mov 0x18(%esp),%eax
add $0x2,%eax?//d+2的值赋给eax
imul 0x10(%esp),%eax?//eax与0x10(%esp)内的值相乘赋给eax
mov %eax,0x1c(%esp)?//赋给d
mov 0x1c(%esp),%eax
mov %eax,0x8(%esp)//赋给d
mov 0x18(%esp),%eax
mov %eax,0x4(%esp)?
movl $0x0,(%esp)
call 56 <main+0x56>
leave
ret
?参考代码:
#include"stdio.h"
int main(){
? ? int a,b,c,d;
? ? a=3;
? ? printf("评测结果:成功\n评测脚本:C\n返回结果:");//此行不在汇编代码中
? ? //**********Begin**********
? ? b=a*(a-1);
? ? c=a*b;
? ? d=a*(c+2);
? ? //**********End**********
? ? printf("%d,%d\n",c,d);
}
4.1 控制转移指令
00000000 <sum>:
0: 55? ? ? ? ? ? ? push %ebp
1: 89 e5? ? ? ? ? ? mov %esp,%ebp
3: 83 ec 10? ? ? ? ? sub $0x10,%esp //分配十字节空间
6: c7 45 fc 00 00 00 00? movl $0x0,-0x4(%ebp) //s?=0
d: c7 45 f8 00 00 00 00? movl $0x0,-0x8(%ebp) //i?=0
14: eb 18? ? ? ? ? ?jmp 2e <sum+0x2e>
16: 8b 45 f8? ? ? ? ?mov -0x8(%ebp),%eax //
19: 8d 14 85 00 00 00 00 lea 0x0(,%eax,4),%edx //a[i]的值赋给edx
20: 8b 45 08? ? ? ? ?mov 0x8(%ebp),%eax //
23: 01 d0? ? ? ? ? ?add %edx,%eax
25: 8b 00? ? ? ? ? ?mov (%eax),%eax
27: 01 45 fc? ? ? ? ?add %eax,-0x4(%ebp)?//s的值+a[i]赋给s
2a: 83 45 f8 01? ? ? ?addl $0x1,-0x8(%ebp)?//i的值+1赋给i
2e: 8b 45 f8? ? ? ? ?mov -0x8(%ebp),%eax //i的值赋给eax
31: 3b 45 0c? ? ? ? ?cmp 0xc(%ebp),%eax //比较i跟n的值;
34: 7c e0? ? ? ? ? ?jl 16?<sum+0x16>
36: 8b 45 fc? ? ? ? ?mov -0x4(%ebp),%eax
39: c9? ? ? ? ? ? ?leave
3a: c3 ret
?参考代码:
#include"stdio.h"
int sum(int a[],int n){
? ? //**********Begin**********
? ? int s = 0;
? ? for (int i = 0;i<n; i++) {
? ? ? ? s += a[i];?
? ? }
? ?return s;
? ? //**********End**********
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!