ESP是否与EAX一样通用? [英] Is ESP as general-purpose as EAX?

查看:152
本文介绍了ESP是否与EAX一样通用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在x86体系结构中,EAX可以做什么,而ESP不能做什么?忘记pushpopcall.

In the x86 architecture, what can be done with EAX but not with ESP? Forgetting about push and pop and call.

推荐答案

ESP被中断隐式使用.在现代操作系统中,这仅适用于内核堆栈,而不适用于用户空间堆栈.启用中断后,内核代码始终需要保持ESP有效,并假定每条指令之后它下面的空间都会被破坏.

ESP is implicitly used asynchronously by interrupts. In modern OSes, that only applies to the kernel stack, not the user-space stack. Kernel code always needs to keep ESP valid when interrupts are enabled, and assume the space below it is clobbered after every instruction.

用户空间中ESP的主要(唯一?)异步使用是信号处理程序,因此没有信号处理程序的进程不应使用ESP进行任何异步使用. (您甚至可以为内核设计一个新的ABI,以传递不会强制使用ESP的信号.)

The main (only?) asynchronous use of ESP in user-space is signal handlers, so a process with no signal handlers shouldn't have any asynchronous use of ESP. (You could even design a new ABI for the kernel to deliver signals that didn't force use of ESP).

因此在某些情况下,用户空间代码可以通过使用ESP作为关键循环中的第8个GP寄存器,否则将不得不溢出一些东西,但是正如该文章所指出的那样,它使在SEH要查找有效堆栈的Windows上进行调试变得不那么方便.使用MMX或XMM寄存器保存/恢复ESP,因为静态存储不是线程安全的,并且堆栈不可用(鸡肉/鸡蛋问题).从理论上讲,同样的论点适用于在64位代码中使用RSP,但是RSP以外的15 regs以及有保证的SSE2支持,使得这极不可能实现.

So user-space code can in some cases get away with using ESP as an 8th GP register in a critical loop that otherwise has to spill something, but as that article points out, it makes debugging less convenient on Windows where SEH wants to find a valid stack. Use an MMX or XMM register to save/restore ESP, because static storage wouldn't be thread-safe, and the stack isn't available (chicken/egg problem). The same argument in theory applies to using RSP in 64-bit code, but 15 regs other than RSP, and guaranteed SSE2 support, makes this extremely unlikely to be worth it.

此答案中的所有其他内容同样适用于64位模式下的RSP.

Everything else in this answer applies equally to RSP in 64-bit mode.

ESP不能做的所有其他寄存器只能做一件事:在寻址模式下,ESP不能是索引寄存器.

There's only one thing ESP can't do that every other register can: ESP can't be the index register in an addressing mode.

mov  edx, [esp + eax*4]        ; legal
mov  edx, [eax + esp*4]        ; not encodeable

mov  edx, [eax + esp]          ; assemblers will encode this with esp as the base reg, since neither reg is scaled.

如果我没记错的话,这是唯一无法将ESP用作普通操作数的情况.另一种特殊情况是,即使没有索引,ESP作为基址寄存器也始终需要一个SIB字节:

If I remember correctly, this is the only case where ESP just plain isn't available as an operand. The other special case is that ESP as a base register always requires a SIB byte, even when there's no index:

mov  edx, [eax]          ; 2 bytes: opcode + ModRM
mov  edx, [ebp]          ; 3 bytes: opcode + ModRM + disp8=0  (the other addressing-mode limitation, ebp/rbp and r13 as a base reg needs a displacement; the mode+M encoding that would mean this actually mean something else)
mov  edx, [esp]          ; 3 bytes: opcode + ModRM + SIB

mov  edx, [ebp + 4]      ; 3 bytes: opcode + ModRM + disp8
mov  edx, [esp + 4]      ; 4 bytes: opcode + ModRM + SIB + disp8

mov  edx, [ebp + 4 + eax]   ; 4 bytes: opcode + ModRM + SIB + disp8
mov  edx, [esp + 4 + eax]   ; 4 bytes: opcode + ModRM + SIB + disp8


还值得指出的是,即使与ECX之类的其他寄存器相比,EAX也有很多特殊之处.例如,它隐式地与stoscdq一起使用,并作为mul的操作数(并且此列表并不详尽). xchg eax, reg 也有一个1字节的编码(对于代码高尔夫来说是不错的选择,但对性能而言却不是. !),以及使用imm32进行的常见ALU操作(例如add eax, imm32add r/m32, imm32). (在在线或英特尔指令参考手册的原始PDF中查找这些ALU指令,请参见标记维基链接.)


It is also worth pointing out that there are a lot of things special about EAX, even compared to other registers like ECX. For example, it is implicitly used with stos, cdq, and as an operand for mul (and this list is not exhaustive). There is also a 1-byte encoding for xchg eax, reg (great for code golf but not performance!), and for the common ALU operation with an imm32 (like add eax, imm32 vs. add r/m32, imm32). (Look up these ALU instructions online or the original PDF of Intel's instruction reference manual—see the x86 tag wiki for links.)

EB8是EBX,它不是基数8的通用寄存器,它不是特殊的"或被任何通用指令隐含使用的.有关x86寄存器及其名称/传统用途的详细信息,请参见 http://www.swansontec .com/sregisters.html

The only one of the base 8 general-purpose registers that isn't "special" or used implicitly by any common instructions is EBX. For more info on x86 registers and where their names come from / traditional uses, see http://www.swansontec.com/sregisters.html

这篇关于ESP是否与EAX一样通用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆