计算机系统基础-汇编指令学习

1.1 传送指令

  1. push %ebp
  2. mov %esp,%ebp
  3. and $0xfffffff0,%esp //边界对齐指令,16字节对齐
  4. sub $0x20,%esp
  5. movl $0xa,0x10(%esp) //立即数10赋值给偏移量为0x10的基址寄存器esp
  6. movl $0x9,0x14(%esp) //立即数9赋值
  7. mov 0x10(%esp),%eax 
  8. mov %eax,0x18(%esp) //立即数10赋值给0x18(%esp)
  9. mov 0x14(%esp),%eax
  10. mov %eax,0x1c(%esp) //立即数9赋值给0x1c(%esp)
  11. mov 0x1c(%esp),%eax
  12. mov %eax,0x8(%esp) //立即数9赋值给0x8(%esp)
  13. mov 0x18(%esp),%eax
  14. mov %eax,0x4(%esp) //立即数10赋值给0x4(%esp)
  15. movl $0x0,(%esp)
  16. call 41
  17. leave
  18. 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 传送指令

  1. mov %esp,%ebp
  2. and $0xfffffff0,%esp
  3. sub $0x20,%esp
  4. movl $0xa,0x18(%esp) //立即数10赋值给偏移量为0x18的基址寄存器esp
  5. mov 0x18(%esp),%eax
  6. mov %eax,0x1c(%esp)
  7. mov 0x1c(%esp),%eax
  8. mov %eax,0x8(%esp) //立即数10赋值给0x8(%esp)
  9. mov 0x18(%esp),%eax
  10. mov %eax,0x4(%esp)
  11. movl $0x0,(%esp)
  12. call 31
  13. leave
  14. 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 加减运算指令

  1. push %ebp
  2. mov %esp,%ebp
  3. and $0xfffffff0,%esp
  4. sub $0x20,%esp movl
  5. movl $0x6,0x10(%esp) //立即数6赋值给偏移量为0x10的基址寄存器esp
  6. movl $0x64,0x14(%esp) //16进制 立即数为100
  7. mov 0x14(%esp),%eax 
  8. mov 0x10(%esp),%edx
  9. sub %eax,%edx //将edx-eax的值赋值给edx
  10. mov %edx,%eax
  11. mov %eax,0x18(%esp) //将edx的值赋值给c
  12. mov 0x14(%esp),%eax
  13. mov 0x10(%esp),%edx
  14. add %edx,%eax //将eax+edx的值赋值给edx
  15. mov %eax,0x1c(%esp)
  16. mov 0x1c(%esp),%eax
  17. mov %eax,0x8(%esp) //将eax的值赋值给c
  18. mov 0x18(%esp),%eax
  19. mov %eax,0x4(%esp)
  20. movl $0x0,(%esp)
  21. call 4f
  22. leave
  23. 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 整数的乘法指令

  1. push %ebp
  2. mov %esp,%ebp
  3. and $0xfffffff0,%esp
  4. sub $0x20,%esp
  5. movl $0x3,0x10(%esp) //立即数3赋值给偏移量为0x10的基址寄存器esp
  6. mov 0x10(%esp),%eax
  7. sub $0x1,%eax //将eax的值-1赋给eax
  8. imul 0x10(%esp),%eax //将eax与0x10(%esp)内的值相乘赋给eax
  9. mov %eax,0x14(%esp) //赋值给b
  10. mov 0x10(%esp),%eax 
  11. imul 0x14(%esp),%eax //将c与0x10(%esp)内的值相乘赋给eax
  12. mov %eax,0x18(%esp) //赋给c
  13. mov 0x18(%esp),%eax
  14. add $0x2,%eax //d+2的值赋给eax
  15. imul 0x10(%esp),%eax //eax与0x10(%esp)内的值相乘赋给eax
  16. mov %eax,0x1c(%esp) //赋给d
  17. mov 0x1c(%esp),%eax
  18. mov %eax,0x8(%esp)//赋给d
  19. mov 0x18(%esp),%eax
  20. mov %eax,0x4(%esp) 
  21. movl $0x0,(%esp)
  22. call 56
  23. leave
  24. 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 控制转移指令

  1. 00000000 :
  2. 0: 55              push %ebp
  3. 1: 89 e5            mov %esp,%ebp
  4. 3: 83 ec 10          sub $0x10,%esp //分配十字节空间
  5. 6: c7 45 fc 00 00 00 00  movl $0x0,-0x4(%ebp) //s =0
  6. d: c7 45 f8 00 00 00 00  movl $0x0,-0x8(%ebp) //i =0
  7. 14: eb 18           jmp 2e
  8. 16: 8b 45 f8         mov -0x8(%ebp),%eax //
  9. 19: 8d 14 85 00 00 00 00 lea 0x0(,%eax,4),%edx //a[i]的值赋给edx
  10. 20: 8b 45 08         mov 0x8(%ebp),%eax //
  11. 23: 01 d0           add %edx,%eax
  12. 25: 8b 00           mov (%eax),%eax
  13. 27: 01 45 fc         add %eax,-0x4(%ebp) //s的值+a[i]赋给s
  14. 2a: 83 45 f8 01       addl $0x1,-0x8(%ebp) //i的值+1赋给i
  15. 2e: 8b 45 f8         mov -0x8(%ebp),%eax //i的值赋给eax
  16. 31: 3b 45 0c         cmp 0xc(%ebp),%eax //比较i跟n的值;
  17. 34: 7c e0           jl 16 
  18. 36: 8b 45 fc         mov -0x4(%ebp),%eax
  19. 39: c9             leave
  20. 3a: c3 ret

 参考代码:

#include"stdio.h"

int sum(int a[],int n){

    //**********Begin**********

    int s = 0;

    for (int i = 0;i

        s += a[i]; 

    }

   return s;

    //**********End**********

}

你可能感兴趣的:(计算机系统基础,汇编,学习)