multipling 64bit data type in assembly
Hi all
I have to multiply a 64-bit Qword data type using assembly language on a 32bit processor: now we know that mul instruction, uses that edx register to store the portion of the result which does not fit in the eax register. hence edx:eax makeup the result of an multiplication now if the data type is 64 bit I but the upper 32 bit in edx and the lower 32 bit in eax when I multiply say by 10 the edx register is overwritten and I only get the result of eax*10 not edx:eax *10 so how should i go about it? |
You can't do this with just two 32-bit registers. Result of multiplying two 64 bit numbers may be up to 128 bits. It means four registers only for the result. You need to divide it into four multiplications (the same way you use to multiply long numbers on paper, but this time for hexadecimals) and add the results in the right way.
Code:
A B |
show me how?
|
It's quite simple. You ahev two 64 bit values. One you represent as A B (A is higher 32 bit, B lower 32 bit), second as C D. You can multiply them this way:
Code:
A B lowest byte of the result = lower 32 bit of B*D second byte of the result = lower 32 bit of (A*D+B*C+higher 32 bit of B*D) third byte of the result = lower 32 bit of (A*C+higher 32 bit of (A*D+B*C+higher 32 bit of B*D)) fourth byte (highest 32 bits) of the result = higher 32 bit of (A*C+higher 32 bit of (A*D+B*C+higher 32 bit of B*D)) So you'd need to make four multiplications: A*C, A*D, B*C, B*D. Their results will be in two registers each. When you have them (8 registers total, so you'd probably need to use stack), calculate the result using the formulas as above. Please check them, so I might have made a mistake somewhere. I hope that the idea is clear. |
All times are GMT -5. The time now is 07:32 PM. |