subl在这里做什么? [英] What does subl do here?

查看:143
本文介绍了subl在这里做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...我正在用gcc -S -O2 -m32编译成汇编器:

So... I'm compiling into assembler, with gcc -S -O2 -m32:

void h(int y){int x; x=y+1; f(y); f(2); }

它给了我以下内容:

.file   "sample.c"
.text
.p2align 4,,15
.globl h
.type   h, @function
 h:
pushl   %ebp
movl    %esp, %ebp
subl    $24, %esp
movl    8(%ebp), %eax
movl    %eax, (%esp)
call    f
movl    $2, 8(%ebp)
leave
jmp f
.size   h, .-h
.ident  "GCC: (GNU) 4.4.3 20100127 (Red Hat 4.4.3-4)"
.section    .note.GNU-stack,"",@progbits

现在我知道pushl和movel是什么:它们将当前帧指针存储到堆栈上,然后将帧指针寄存器的值设置为堆栈指针的值.

Now I know what pushl and movel: they store the current frame pointer onto the stack and then set the value of the frame pointer register to the value of the Stack Pointer.

  1. 但是我不知道 subl $ 24,%esp 是什么.我知道它会将堆栈指针向下移动24个字节.正确吗?
  2. 顺带一提是什么?
  3. 为什么移动8(%ebp),%eax 使用8?是8个字节吗?这是否适合h的返回值+参数 y ?还是我完全不在这里.因此,这意味着从堆栈指针回溯8个字节?
  4. 移动$ 2,8(%ebp)会做什么?它将内容2复制到帧指针之前8个字节的位置.当我们调用f时,帧指针是否发生了变化?如果是,则8(%ebp)指向f的参数位置.
  5. 请假有什么作用?如何删除"堆栈框架?我的意思是你不能只删除一块内存.在文档中说它确实 mov(esp,ebp),弹出ebp .
  1. But I have no idea what the subl $24, %esp is. I understood that it moves the stack pointer down by 24 bytes. Correct?
  2. What is immed by the way?
  3. Why does movl 8(%ebp), %eax use 8? Is it 8 bytes? Is this to accommodate for return value + argument y to h? Or am I completely off here. So this means look back 8 bytes from the stack pointer?
  4. What does movl $2, 8(%ebp) do? It copies contant 2 to the location 8 bytes before the frame pointer. Did the frame pointer change when we called f? If yes - then 8(%ebp) points to the argument location for f.
  5. What does leave do? How can it "remove" a stack frame? I mean you cant just remove a piece of memory. In the doc it says it does mov(esp, ebp), pop ebp.

谢谢!

推荐答案

编译器正在为本地人及其可能需要的其他需求在堆栈上保留空间.我不确定为什么要保留24个字节(似乎并不需要全部使用).

The compiler is reserving space on the stack for locals and whatever other needs it might have. I'm not sure offhand why it's reserving 24 bytes (it doesn't seem to need or use it all).

当调用函数f()时,它不使用推入指令将参数放在堆栈上,而是使用简单的movl到保留的最后一个位置:

When calling function f(), instead of using a push instruction to put the parameter on the stack, it uses a simple movl to the last location it reserved:

movl    8(%ebp), %eax    ; get the value of `y` passed in to `h()`
movl    %eax, (%esp)     ; put that value on the stack for call to `f()`

在我看来,更有趣的事情是编译器如何处理对f(2)的调用:

A more interesting (in my opinion) thing happening here is how the compiler is handling the call to f(2):

movl    $2, 8(%ebp)      ; store 2 in the `y` argument passed to `h()`
                         ;     since `h()` won't be using `y` anymore
leave                    ; get rid of the stackframe for `h()`
jmp f                    ; jump to `f()` instead of calling it - it'll return
                         ;     directly to whatever called `h()`


要回答您的问题,顺便说一下?" -指令引用用来指示该值是在指令操作码中编码的,而不是放在寄存器或内存位置之类的其他地方.


To answer your question, "immed by the way?" - that is what the instruction reference uses to indicate that the value is encoded in the instruction opcode instead of coming somewhere else like a register or memory location.

这篇关于subl在这里做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆