[英]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. PUSH
和POP
在堆栈中添加和删除项目。 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:在此背景下,让我们以更具可读性的形式重写您的程序:
push %rbp
Move the stack pointer to the stack itself (so we can clean up after all of our junk memory) push %rbp
将堆栈指针移动到堆栈本身(这样我们就可以清理所有垃圾内存)mov %rsp, %rbp
Resize the stack to 0
, Preventing the main()
from accidentally reading junk from other functions mov %rsp, %rbp
将堆栈大小调整为0
,防止main()
意外地从其他函数读取垃圾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 个空间!)MOV $0x0, %eax
Move 0
to the result register ( EAX
) MOV $0x0, %eax
将0
移动到结果寄存器 ( EAX
)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.main()
from accessing other function's memory. Great!main()
访问其他函数的 memory。太好了!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.