问题
I need some easy way to divide 64b unsigned integers in assembler for x86. My number is saved in two 32b registers EDX:EAX and I need to put result back to EDX:EAX. Factor is in 32b integer. Some code, please?
回答1:
If I interpret your question correctly (particularly the part Factor is in 32b integer
), you want to divide a 64-bit dividend by a 32-bit divisor and get a 64-bit quotient.
If that interpretation is correct, then it's actually easy to do in 32-bit code.
The idea is that you divide both "halves" of the dividend by the divisor and reuse the remainder from the first division for the second division.
C code illustrating how to do it:
#include <stdio.h>
#include <limits.h>
#define C_ASSERT(expr) extern char CAssertExtern[(expr)?1:-1]
#if UINT_MAX >= 0xFFFFFFFF
typedef unsigned int uint32;
#else
typedef unsigned long uint32;
#endif
typedef unsigned long long uint64;
typedef unsigned long ulong;
// Make sure uint32=32 bits and uint64=64 bits
C_ASSERT(sizeof(uint32) * CHAR_BIT == 32);
C_ASSERT(sizeof(uint64) * CHAR_BIT == 64);
int div64by32eq64(uint64* dividend, uint32 divisor)
{
uint32 dividendHi = (uint32)(*dividend >> 32);
uint32 dividendLo = (uint32)*dividend;
uint32 quotientHi;
uint32 quotientLo;
if (divisor == 0)
return 0;
// This can be done as one 32-bit DIV, e.g. "div ecx"
quotientHi = dividendHi / divisor;
dividendHi = dividendHi % divisor;
// This can be done as another 32-bit DIV, e.g. "div ecx"
quotientLo = (uint32)((((uint64)dividendHi << 32) + dividendLo) / divisor);
*dividend = ((uint64)quotientHi << 32) + quotientLo;
return 1;
}
int main(void)
{
static const struct
{
uint64 dividend;
uint32 divisor;
} testData[] =
{
{ 1 , 0 },
{ 0xFFFFFFFFFFFFFFFFULL, 1 },
{ 0xFFFFFFFFFFFFFFFFULL, 2 },
{ 0xFFFFFFFF00000000ULL, 0xFFFFFFFFUL },
{ 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFUL },
};
int i;
for (i = 0; i < sizeof(testData)/sizeof(testData[0]); i++)
{
uint64 dividend = testData[i].dividend;
uint32 divisor = testData[i].divisor;
printf("0x%016llX / 0x%08lX = ", dividend, (ulong)divisor);
if (div64by32eq64(÷nd, divisor))
printf("0x%016llX\n", dividend);
else
printf("division by 0 error\n");
}
return 0;
}
Output (ideone):
0x0000000000000001 / 0x00000000 = division by 0 error
0xFFFFFFFFFFFFFFFF / 0x00000001 = 0xFFFFFFFFFFFFFFFF
0xFFFFFFFFFFFFFFFF / 0x00000002 = 0x7FFFFFFFFFFFFFFF
0xFFFFFFFF00000000 / 0xFFFFFFFF = 0x0000000100000000
0xFFFFFFFFFFFFFFFF / 0xFFFFFFFF = 0x0000000100000001
And now the equivalent division code in assembly (NASM syntax) without checking for division by 0:
; 64-bit dividend
mov edx, 0xFFFFFFFF
mov eax, 0xFFFFFFFF
; 32-bit divisor
mov ecx, 0xFFFFFFFF
push eax
mov eax, edx
xor edx, edx
div ecx ; get high 32 bits of quotient
xchg eax, [esp] ; store them on stack, get low 32 bits of dividend
div ecx ; get low 32 bits of quotient
pop edx ; 64-bit quotient in edx:eax now
; edx:eax should now be equal 0x0000000100000001
回答2:
What about using the div instruction?
回答3:
Quick terminology recap: numerator/divisor = result + remainder/divisor
First check if the divisor is zero (abort if it is).
test eax,eax
jne .ok
test edx,edx
je .divisionByZero
.ok:
Shift the divisor left until the MSB is set while keeping track of how many shifts you did:
xor ebp,ebp ;ebp = bits shifted so far
test edx,(1 << 31)
jne .l2
.l1:
shld edx,eax,1
shl eax,1
inc ebp
test edx,(1 << 31)
jne .l1
.l2:
Set the current result to zero:
xor esi,esi
xor edi,edi
Now shift the divisor back to its original position; while subtracting current divisor from remaining numerator and setting a bit in the result whenever current divisor is less than current numerator:
.nextBit:
shld edi,esi,1
shl esi,1
cmp ecx,edx
jb .doneBit
ja .subtract
cmp ebx,eax
jb .doneBit
.subtract:
sub ecx,edx
sbb ebx,eax
or esi,1
.doneBit:
sub ebp,1
jnc .nextBit
At this point EDX:EAX is the same value it was, EDI:ESI is the result and ECX:EBX is the remainder.
WARNING: All of the above is completely untested. It's just an example/description.
NOTE: If the numbers are signed you need to remove the sign bit from the numerator and divisor first; then set the sign bit in the result and remainder later (sign = numerator_sign XOR divisor_sign
).
来源:https://stackoverflow.com/questions/12965098/assembler-64b-division