第八周课上测试ch03

测试-1-ch03

  1. 任务详情
  • 通过输入gcc -S -o main.s main.c,将下面c程序“week04学号.c”编译成汇编代码
    int g(int x){
        return x+3;
    }
    int f(int x){
        int i = 学号后两位;
        return g(x)+i;
     }
     int main(void){
        return f(8)+1;
     }
    
  • 删除汇编代码中 . 开头的代码,提交f函数的汇编代码截图,图中用矩形标出函数栈帧的形成和销毁的代码
  1. 知识点
  1. 测试截图
    image

测试-2-ch03

  1. 任务详情
  • 通过输入gcc -S -o main.s main.c,将下面c程序“week04学号.c”编译成汇编代码
    int g(int x){
        return x+3;
    }
    int f(int x){
        int i = 学号后两位;
        return g(x)+i;
     }
     int main(void){
        return f(8)+1;
     }
    
  • 参考http://www.cnblogs.com/lxm20145215----/p/5982554.html,使用gdb跟踪汇编代码,在纸上画出f中每一条语句引起的eip(rip),ebp(rbp),esp(rsb),eax(rax)的值和栈的变化情况。提交照片,要有学号信息。
  1. 知识点
  • 参考GDB调试汇编堆栈过程分析,使用gcc - g XX.c -o XX -m32指令在64位的机器上产生32位汇编
  • 使用gdb XX指令进入gdb调试器
  • gdb相关指令
    • r:运行
    • b n:在n行设置断点
    • i r:查看各寄存器的值
    • disassemble:获取汇编代码
  1. 过程
    • 查看f函数的汇编代码、
      image
    • 入栈
      image
    • 将上一个函数的基址入栈,从当前%esp开始作为新基址
      image
    • 为传参做准备
      image
    • 实参入栈
      image
    • 计算short+int
      image
    • pop %ebp指令将栈顶弹到%ebp中,同时%esp增加4字节
      image
    • ret指令将栈顶弹给%eip
      image
    • 用leave指令恢复
      image
  2. 变化情况
指令 eip ebp esp eax
push %ebp 0x80483e6 0xbfffef58 0xbfffef50 0xb7fbcdbc 0x8048411 0x8 0x0
mov %esp,%ebp 0x80483e7 0xbfffef58 0xbfffef4c 0xb7fbcdbc 0xbfffef58 0x8048411 0x8 0x0
sub $0x10,%esp 0x80483e9 0xbfffef4c 0xbfffef4c 0xb7fbcdbc 0xbfffef58 0x8048411 0x8 0x0
movl $0xc,-0x4(%ebp) 0x80483ec 0xbfffef4c 0xbfffef3c 0xb7fbcdbc 0x8048441 0xb7fbb3dc 0x80481ec 0x8048429
pushl 0x8(%ebp) 0x80483f3 0xbfffef4c 0xbfffef3c 0xb7fbcdbc 0x8048441 0xb7fbb3dc 0x80481ec 0x1e
call 0x80483db 0x80483f6 0xbfffef4c 0xbfffef38 0xb7fbcdbc 0x8 0x8048441 0xb7fbb3dc 0x80481ec
add $0x4,%esp 0x80483fb 0xbfffef4c 0xbfffef38 0xb 11 0x8 0x8048441 0xb7fbb3dc 0x80481ec
mov %eax,%edx 0x80483fe 0xbfffef4c 0xbfffef3c 0xb 11 0x8048441 0xb7fbb3dc 0x80481ec 0x1e
mov -0x4(%ebp),%eax 0x8048400 0xbfffef4c 0xbfffef3c 0xb 11 0x8048441 0xb7fbb3dc 0x80481ec 0x1e
add %edx,%eax 0x8048403 0xbfffef4c 0xbfffef3c 0x1e 30 0x8048441 0xb7fbb3dc 0x80481ec 0x1e
leave 0x8048406 0xbfffef58 0xbfffef50 0x29 41 0x8048411 0x8 0x0
ret 0x8048417 0xbfffef58 0xbfffef58 0x2a 42 0x0
原文地址:https://www.cnblogs.com/besty-zyx/p/9859360.html