简体   繁体   English

在汇编程序 AT&T 上下文中: movl 在此指定行中做了什么?

[英]In Assembler AT&T context: What does movl do in this specified line?

i have a some simple lines of code in C and wanted to disassemble it:我在 C 中有一些简单的代码行,想反汇编它:

#include <stdio.h>
int main(){
  int i=42;
}

After compiling it and starting gdb, i simply cant find my value=42 in the corresponding place:编译启动gdb后,在相应的地方根本找不到我的value=42: 在此处输入图像描述

Its not just that i get the value 0, but what exactly does它不仅是我得到值 0,而且到底是什么

movl $0x2a, -0x4(%rbp)

mean.意思。 I know that 0x2a is 42 in hex, but the next part is cryptic to me;我知道 0x2a 是十六进制的 42,但下一部分对我来说很神秘; should it mean, that 42 gets saved into register rbp?这是否意味着 42 被保存到寄存器 rbp 中? and what about the -0x4?那-0x4 呢? And where is my 42:O?我的 42:O 在哪里?

Each variable in C either gets a set position in memory (called the stack ) or a register . C 中的每个变量要么在 memory 中得到一组 position(称为堆栈),要么得到一个寄存器 In fact, the compiler will often move variables between these two places for performance.事实上,编译器为了性能经常会在这两个地方之间移动变量。

MOVL moves a 32-bit number (your int,) from one register to another, while MOV moves an entire register, even if your program doesn't use that part of the register. MOVL将 32 位数字(您的 int,)从一个寄存器移动到另一个寄存器,而MOV移动整个寄存器,即使您的程序不使用寄存器的那部分。

PUSH and POP add and remove items from the stack. PUSHPOP在堆栈中添加和删除项目。 It's often used by the C compiler to save registers.它经常被 C 编译器用来保存寄存器。 As a function calling main() , you have no idea what it does, and how to clean up after all the memory main() uses, which is why it is main() 's responsibility to clean up after itself, leaving the program exactly as it started with it.作为一个 function 调用main() ,你不知道它做了什么,也不知道在 memory main()使用之后如何清理,这就是为什么main()有责任自行清理,离开程序就像它开始时一样。 (except, of course, for the results of the operation) (当然,操作结果除外)

EAX is a common register, and is typically used for the results of functions. EAX是一个通用寄存器,通常用于函数的结果。

With this background, let's rewrite your program in a slightly more readable form:在此背景下,让我们以更具可读性的形式重写您的程序:

  1. push %rbp Move the stack pointer to the stack itself (so we can clean up after all of our junk memory) push %rbp堆栈指针移动到堆栈本身(这样我们就可以清理所有垃圾内存)
  2. mov %rsp, %rbp Resize the stack to 0 , Preventing the main() from accidentally reading junk from other functions mov %rsp, %rbp将堆栈大小调整0防止main()意外地从其他函数读取垃圾
  3. movl $0x2a, -0x4(%rbp) Move 42 to the first slot in the stack (note: Since an int is 4 bytes big, this is actually the -4th space!) movl $0x2a, -0x4(%rbp)42移动到堆栈中的第一个位置(注意:由于 int 有 4 个字节大,这实际上是第 -4 个空间!)
  4. Wait-- We never actually resized the stack to tell other programs that our special number is there.等等——我们从来没有真正调整堆栈的大小来告诉其他程序我们的特殊号码就在那里。 This is because the compiler optimized it away, as it saw that we were increasing the size when we were going to reset it in a few instructions anyways.这是因为编译器优化了它,因为它看到我们在增加大小时无论如何都要用几条指令重置它。
    • Thank you Peter Cordes, for reminding me the Red Zone exists.谢谢 Peter Cordes,提醒我红区的存在。 This is a special area of memory inside of the stack which does not require the stack to be expanded before writing to it, In the case of this program.这是堆栈内部 memory 的特殊区域,在写入之前不需要扩展堆栈,在这个程序的情况下。 this is likely what is happening.这可能是正在发生的事情。
  5. MOV $0x0, %eax Move 0 to the result register ( EAX ) MOV $0x0, %eax0移动到结果寄存器 ( EAX )
  6. POP %RBP Clean Up after our mess by restoring the old stack pointer. POP %RBP通过恢复旧的堆栈指针来清理我们的混乱。 This means that, even though we reset the size of the stack, the program above us will still have all of their memory intact.这意味着,即使我们重置了堆栈的大小,我们上面的程序仍将完整地保留其所有 memory。 Remember--We did this to prevent main() from accessing other function's memory. Great!请记住——我们这样做是为了防止main()访问其他函数的 memory。太好了!
  7. RETQ Return and say goodbye;( RETQ回来说再见;(

If you wanted to retrieve your 42 , you would need to change your code to say return 42 , which would means the compiler would place 42 in EAX , and it would get passed up to your friends above:)如果你想检索你的42 ,你需要将你的代码更改为return 42 ,这意味着编译器会将42放在EAX中,并且它会传递给你上面的朋友:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM