[英]understanding some assembly code
I am trying to learn some assembly code, so I read in some tutorial that the assembly code for 我正在尝试学习一些汇编代码,所以我在一些教程中读到了该汇编代码
int proc(void)
{
int x,y;
scanf("%x %x", &y, &x);
return x-y;
}
is 是
1 proc:
2 pushl %ebp
3 movl %esp, %ebp
4 subl $40, %esp
5 leal -4(%ebp), %eax
6 movl %eax, 8(%esp)
7 leal -8(%ebp), %eax
8 movl %eax, 4(%esp)
9 movl $.LC0, (%esp)
10 call scanf
Diagram stack frame at this point
11 movl -4(%ebp), %eax
12 subl -8(%ebp), %eax
13 leave
14 ret
If I well understood, the instructions of line 5 to 8 store some addresses that will be used to store the values of scanf
's input. 如果我很了解,第5至8行的指令将存储一些地址,这些地址将用于存储
scanf
的输入值。 So is it right to say that scanf
uses systematically the address %esp
plus a certain number of bytes (depending on the sizeof
the input) to fetch the address at which is the data will be stored ? 因此,可以说
scanf
系统地使用地址%esp
加上一定数量的字节(取决于输入的sizeof
)来获取将存储数据的地址吗?
What's happening here is that a stack frame is built up to pass arguments to scanf
. 这里发生的是建立了一个堆栈框架以将参数传递给
scanf
。 subl
is used to allocate space for the new stack frame and the movl
is used with offsets from the stack pointer, %esp
, to write values for the arguments on the freshly allocated stack frame. subl
用于为新的堆栈帧分配空间, movl
与堆栈指针%esp
偏移量一起使用,以将参数的值写入新分配的堆栈帧。
A more thorough explanation on x86 calling conventions and cdecl in particular can be found here . 在这里可以找到有关x86调用约定(尤其是cdecl)的更详尽的解释。 Understanding the high-level structure of the stack frame and the cdecl convention will help you make sense of the intent of this code snippet.
了解堆栈框架和cdecl约定的高级结构将帮助您理解此代码段的意图。
Calling convention of scanf
is cdecl
. scanf
调用约定为cdecl
。 It passes its arguments to stack pointed by esp
. 它将参数传递给
esp
指向的堆栈。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.