LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   multipling 64bit data type in assembly (https://www.linuxquestions.org/questions/programming-9/multipling-64bit-data-type-in-assembly-262688/)

irfanhab 12-04-2004 08:58 PM

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?

Mara 12-05-2004 02:31 PM

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
  x C  D
--------


irfanhab 12-06-2004 12:45 AM

show me how?

Mara 12-06-2004 04:41 PM

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
      x C    D
----------------
      AD  BD
  + AC BC
----------------
AC  AD+BC  BD

Seems simple but there's one problem. Every multiplication of two 32 bit values can give max 64 bit result. So AC, BD etc are not just 32 bit values. You'd need to correct the result. It will be:
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.