movq (%rsp), %rsp 程序集堆栈指针加载?
我正在阅读一些代码,但不确定这一行的作用:
movq (%rsp), %rsp
I was reading some code and was not sure what this line does:
movq (%rsp), %rsp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是一个 64 位值
mov
。它是 64 位,因为 movq 中的“q”是四元,而四元是 64 位。还可以有其他示例,例如
movl
,其中l
是 32 位。但在
movq (%rsp), %rsp
使用 ATT 语法的情况下。movq (%rsp), %rsp
-> movq 称为操作码,(%rsp)
(左操作数)称为源或 src,%rsp
(右操作数)称为目标或 dst。它的作用是在寄存器
%rsp
中查找,获取其值并转到该值的内存[括号“()”表示进入内存值],然后将其分配给%rsp
。虽然两者是相同的寄存器,但区别在于
%rsp
的值发生了变化。EG:假设
%rsp
的值为 22。但是%rsp
的内存为 30。使用此指令
movq (%rsp), %rsp
%rsp
的新值为 30。再次因为(%rsp)
获取 %rsp 的值,假设为 22,然后(%rsp)< /code> 转到内存值 30,然后将其分配给目标上的
%rsp
,即%rsp
本身。It is a 64 bit value
mov
. It is 64 bit because of the "q" inmovq
which is quad and quad is 64bit.There can be other example such as
movl
in whichl
is 32 bit.But in the case of
movq (%rsp), %rsp
using ATT syntax..The
movq (%rsp), %rsp
-> movq is called opcode,(%rsp)
(left operand) is called source or src and%rsp
(right operand) is called the destination or the dst.What it does is that it looks up in register
%rsp
gets its value and goes to the memory [the bracket "()" means going into memory value] of that value and then assigns it to%rsp
.While both are same register the difference is that the value of
%rsp
changes.EG:lets say
%rsp
has value 22. But the memory of%rsp
is 30.Using this instruction
movq (%rsp), %rsp
the new value of
%rsp
is 30. Again because(%rsp)
gets the value of %rsp which is assume 22 and then(%rsp)
goes to the memory value 30 and then assigns it to%rsp
on the destination, which is%rsp
itself.movq
(假设您正在谈论 x86)是四字(64 位值)的移动。这个特定的指令:看起来非常像将遍历堆栈帧的代码。该特定指令获取当前堆栈指针指向的四字,并将其加载到堆栈指针中,并覆盖它。
举例来说,此代码序列(基于真实代码,采用 Intel 而不是 AT&T 格式)将连续从其内容加载堆栈指针,直到超出其 16 个字节的值为 0。
它可能不是堆栈-frame 步行代码,但它很不寻常,因为它会取代堆栈指针来代替通常不使用的东西。
不寻常的是,向上移动堆栈帧通常涉及堆栈指针和基指针,但这通常仅用于向上一级(即从函数返回)。
对于上面显示的那种想要向上移动多个级别的代码,使用堆栈指针可能会更快,直到到达需要的位置,然后弹出基指针(调用约定通常会推送当前基指针)更改它之前的指针,以便简单的弹出将恢复旧值)。
movq
(assuming you're talking about x86) is a move of a quadword (64-bit value). This particular instruction:looks very much like code that will walk up through stack frames. This particular instruction grabs the quadword pointed to by the current stack pointer, and loads it into the stack pointer, overwriting it.
By way of example, this code sequence (based on real code, and in Intel rather that AT&T format) will continuously load the stack pointer from its contents until the value 16 bytes beyond it is 0.
It's possible it may not be stack-frame walking code but it's be unusual since it would be suborning the stack pointer for something it's not usually used for.
It is unusual in that moving up stack frames usually involves stack pointer and base pointer but that's usually for just going up one level (i.e., a return from a function).
For the sort of code shown above where you want to move up multiple levels, it's probably faster to just use the stack pointer until you get where you need to be, then pop the base pointer off then (calling conventions will often push the current base pointer before changing it, so that a simple pop will recover the old value).