gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:39:39 26 4
gpt4 key购买 nike

有人告诉我,如果我使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com