gpt4 book ai didi

c - 使用 "pushf"并弹出到 32 位寄存器是否会破坏堆栈?

转载 作者:行者123 更新时间:2023-12-02 04:30:54 25 4
gpt4 key购买 nike

我想通过将 CPU 标志压入堆栈来读取它们,然后将它们弹出到寄存器中,如下所示:

uint32_t getEflags() {
uint32_t eflags;
asm ("pushf");
asm ("pop %eax");
asm ("mov %%eax, %0" : "=r"(eflags));
return eflags;
}

现在我发现这是一个错误,因为 pushf 只压入 EFLAGS 的低 16 位,然后我将它们弹出到 32 位寄存器中(我需要 pushfd )。

这意味着我弹出的内容基本上比最初推送的内容要多 - 返回此处时我的堆栈会发生什么情况?

最佳答案

指令集引用是这样说的:

Decrements the stack pointer by 4 (if the current operand-size attribute is 32) and pushes the entire contents of the EFLAGS register onto the stack

它还说:

The PUSHF (push flags) and PUSHFD (push flags double) mnemonics reference the same opcode. The PUSHF instruction is intended for use when the operand-size attribute is 16 and the PUSHFD instruction for when the operand-size attribute is 32. Some assemblers may force the operand size to 16 when PUSHF is used and to 32 when PUSHFD is used. Others may treat these mnemonics as synonyms (PUSHF/PUSHFD) and use the current setting of the operand-size attribute to deter- mine the size of values to be pushed from the stack, regardless of the mnemonic used.

所以没问题。不过,您的汇编已损坏,它应该更像是这样的:

__asm__ __volatile__(
"pushf\n\t"
"pop %0"
: "=rm" (eflags));

关于c - 使用 "pushf"并弹出到 32 位寄存器是否会破坏堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23176165/

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