movq (%rsp), %rsp 程序集堆栈指针加载?

发布于 2024-09-26 03:00:36 字数 72 浏览 6 评论 0原文

我正在阅读一些代码,但不确定这一行的作用:

movq (%rsp), %rsp

I was reading some code and was not sure what this line does:

movq (%rsp), %rsp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最终幸福 2024-10-03 03:00:37

它是一个 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" in movq which is quad and quad is 64bit.

There can be other example such as movl in which l 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.

沙与沫 2024-10-03 03:00:36

movq(假设您正在谈论 x86)是四字(64 位值)的移动。这个特定的指令:

movq (%rsp), %rsp

看起来非常像将遍历堆栈帧的代码。该特定指令获取当前堆栈指针指向的四字,并将其加载到堆栈指针中,并覆盖它。

举例来说,此代码序列(基于真实代码,采用 Intel 而不是 AT&T 格式)将连续从其内容加载堆栈指针,直到超出其 16 个字节的值为 0。

576  cmpq    [rsp+0x10],0x0
582  jz      594
588  movq    rsp,[rsp]
592  jmp     576
594  ...

它可能不是堆栈-frame 步行代码,但它很不寻常,因为它会取代堆栈指针来代替通常不使用的东西。

不寻常的是,向上移动堆栈帧通常涉及堆栈指针和基指针,但这通常仅用于向上一级(即从函数返回)。

对于上面显示的那种想要向上移动多个级别的代码,使用堆栈指针可能会更快,直到到达需要的位置,然后弹出基指针(调用约定通常会推送当前基指针)更改它之前的指针,以便简单的弹出将恢复旧值)。

movq (assuming you're talking about x86) is a move of a quadword (64-bit value). This particular instruction:

movq (%rsp), %rsp

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.

576  cmpq    [rsp+0x10],0x0
582  jz      594
588  movq    rsp,[rsp]
592  jmp     576
594  ...

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文