IA-32装配部
有人可以向我解释以下汇编代码片段:
mydiv:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx ; get x
movl %edx, %eax
sarl $31, %edx ; divide by y
idivl 12(%ebp) ; return %eax
popl %ebp
ret
这相当于以下 C 函数:
int mydiv( int x, int y )
{
return x / y;
}
我无法理解的部分是 sarl
指令:为什么需要转移 edx
?
Can somebody explain to me the following snippet of assembly:
mydiv:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx ; get x
movl %edx, %eax
sarl $31, %edx ; divide by y
idivl 12(%ebp) ; return %eax
popl %ebp
ret
This is the equivalent to the following C function:
int mydiv( int x, int y )
{
return x / y;
}
The part which I'm having trouble understanding is the sarl
instruction: why do you need to shift edx
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是符号扩展。
idivl
有一个 64 位参数 (edx:eax
),因此您需要根据eax 的 msb 确保 MSB 包含正确的符号位
。因此,如果
eax
为正,则其msb将为0
,例如5 ->0000 ... 0101
。如果它是负数,则 MSB 将为1
,例如 -5 ->1111 ... 1011
。sarl
执行算术右移,因此edx
将是0000 ... 0000
或1111 ... 1111
代码>.It's sign extending.
idivl
has a 64-bit argument (edx:eax
), so you need to ensure that the MSB contains the correct sign bits, based on the msb ofeax
.So if
eax
is positive, it's msb will be0
, e.g. 5 ->0000 ... 0101
. If it's negative it's msb will be1
, e.g. -5 ->1111 ... 1011
.sarl
performs an arithmetic right-shift, soedx
will either be0000 ... 0000
or1111 ... 1111
.值得注意的是,有一条专门的指令根据EAX的符号位向EDX填充0或1。
或者在 AT&T 语法中,
cltd
(长到双宽?),但 GNU 汇编器接受任一助记符。在
idiv
之前使用它;它隐式读取 EAX 并写入 EDX,将 EAX 符号扩展为 EDX:EAX 作为被除数。在
div
之前,通常您希望通过xor %edx,%edx
将 EAX 零扩展为 EDX:EAX。还有其他可用宽度,例如用于将 AL 符号扩展为 AX 的
cbw
,以及用于将 RAX 符号扩展为 RDX:RAX 的cqo
。 cltq 在汇编中做什么? 有一个 AT&T 语法和 Intel 手册中的助记符之间的等效表。It is worth noting that there is a dedicated instruction to fill EDX with 0s or 1s based on the sign bit of EAX.
Or in AT&T syntax,
cltd
(long to double-width?), but the GNU assembler accepts either mnemonic).Use this before
idiv
; it implicitly reads EAX and writes EDX, sign-extending EAX into EDX:EAX as the dividend.Before
div
, normally you wantxor %edx,%edx
to zero-extend EAX into EDX:EAX.There are other widths available, like
cbw
to sign-extend AL into AX, andcqo
to sign-extend RAX into RDX:RAX. What does cltq do in assembly? has an equivalence table between AT&T syntax and the mnemonics in Intel's manuals.