assembly - 我可以使用 rsp 作为通用寄存器吗?

标签 assembly x86-64 cpu-registers stack-pointer

有人告诉我,如果我使用 rsp 作为通用寄存器,操作系统可能会将寄存器转储到它在中断时指向的位置,从而导致出现问题的行为。

这是真的吗?如果不是,如果我不需要堆栈,我可以使用 rsp 作为通用寄存器吗?

编辑:在用户空间中运行。

最佳答案

Aren't you screwed if an interrupt occurs?

Those of you who have programmed in DOS are likely squirming at this point about the possibility of interrupts. Ordinarily, reusing the stack pointer like this is a really bad idea because you have no idea when an interrupt might strike, and when one does, the CPU dutifully pushes the current program counter and flags onto the stack. If you have reused ESP, this would cause random data structures to be trashed. In this kind of environment, ESP must always point to valid and sufficient stack space to service an interrupt, and whenever this does not hold, interrupts must be disabled. Running with interrupts disabled for a long time lowers system responsiveness (lost interrupts and bad latency), and isn't practical for a big routine.

However, we're running in protected mode here.

When running in user space in Win32, interrupts do not push onto the user stack, but onto a kernel stack instead. If you think about it, it isn't possible for the user stack to be used. If the thread were out of stack space, or even just had an invalid stack, when the CPU tried to push EIP and EFLAGS, it would page fault, and you can't page fault in an interrupt handler. Thus, the scheduler can do any number of context switches while a no-stack routine is running, and any data structures that are being pointed to be ESP will not be affected.

来自http://www.virtualdub.org/blog/pivot/entry.php?id=85

关于assembly - 我可以使用 rsp 作为通用寄存器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211227/

相关文章:

assembly - 如何在 XMM 寄存器之间移动 128 位值?

c++ - 程序集中无法访问的代码

assembly - 该程序集访问该字符串常量有什么问题?

loops - x86_64 - 汇编 - 循环条件和乱序

c - 在 x64 Visual Studio 中内联汇编函数

architecture - 如果指令是 C 指令,在这个 (nand2tetris) CPU 实现中会发生什么?

assembly - 在 MIPS 中,什么是 HI 和 LO

assembly - x86汇编abs()实现?

linux - 通过 ASM (x86) 处理系统调用(需要 ebp)

assembly - 64位nasm除法idiv