NASM:大数除以小数
NASM手册关于DIV
说:
- 对于DIV r/m32,EDX:EAX除以给定的操作数;商存储在 EAX 中,余数存储在 EDX 中。
如果 EDX:EAX
是 259 左右的大数并且除法器是 3
该怎么办?商显然无法放入 EAX
中。假设我不关心其余的。我想要进行划分的最佳实践。
考虑分步骤划分高 32 位和低 32 位。我想我可以找出一些丑陋的结果,但我会对好的结果感兴趣。快速检查 EAX 可能保留商的情况,从而避免复杂的魔术。
解决方案:drhirsch的答案转换为NASM语法:
; this divides edx:eax by ebx, even if the result is bigger than 2^32.
; result is in edx:eax, ecx,esi are used as spare registers
mov ecx, eax ;save lower 32 bit
mov eax, edx
xor edx, edx ;now edx:eax contains 0:hi32
div ebx
mov esi, eax ;hi 32 bit of result, save to esi
mov eax, ecx ;now edx:eax contains r:lo32, where r is the remainder
div ebx
mov edx, esi ;restore hi32
NASM manual says on DIV
:
- For DIV r/m32, EDX:EAX is divided by the given operand; the quotient is stored in EAX and the remainder in EDX.
What if EDX:EAX
is a large number around 259 and the divider is 3
? The quotient clearly cannot fit into EAX
. Let's say I do not care about the remainder. I would like to have a best practice for doing the division.
Thinking about dividing the upper and lower 32 bits in separate steps. I think I could figure out some ugly result, but I would be interested in a good one. With a quick check for the case when EAX
is likely to hold the quotient thus avoiding the complicated magic.
Solution: drhirsch's answer converted to NASM syntax:
; this divides edx:eax by ebx, even if the result is bigger than 2^32.
; result is in edx:eax, ecx,esi are used as spare registers
mov ecx, eax ;save lower 32 bit
mov eax, edx
xor edx, edx ;now edx:eax contains 0:hi32
div ebx
mov esi, eax ;hi 32 bit of result, save to esi
mov eax, ecx ;now edx:eax contains r:lo32, where r is the remainder
div ebx
mov edx, esi ;restore hi32
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码未经测试。它应该计算 (d*2^32 + a)/b:
This code is untested. It should calculate (d*2^32 + a)/b: