tags:

views:

2722

answers:

9

As the title states, I need to multiply two 64bit numbers in Assembly, or two 16digit hexadecimal numbers.

Help please? I'm only allowed to use registers %eax, %ebx, %ecx, %edx, and the stack.

EDIT: Oh, I'm using ATT Syntax on the x86 EDIT2: Not allowed to decompile into assembly...

A: 

You may want to specify what assembly you're using. General techniques are cross-applicable (usually), but the mnemonics are almost always different between platforms. :-)

Daniel Spiewak
Oh, ATT Syntax for the x86? I'm sorry for not adding that info... *Looks for edit button on title*
Kawarazu
+1  A: 

http://sandpile.org/

Wedge
I don't actually understand enough to be able to parse through this website easily... But thank you for sending me a website I can use as I learn more about assembly.
Kawarazu
A: 

It depends what language you are using. From what I remember from learning MIPS assembly, there is a Move From High command and a Move From Lo command, or mflo and mfhi. mfhi stores the top 64bits while mflo stores the lower 64bits of the total number.

Dave
+5  A: 

Use what should probably be your course textbook, Randall Hyde's "The Art of Assembly Language":

http://webster.cs.ucr.edu/AoA/Linux/HTML/AdvancedArithmetica2.html#1007619

Michael Burr
I think this explanation will do nicely. Thank you very much, I will definitely look over this very carefully
Kawarazu
A: 

ah assembly, been awhile since i've used it. so i'm assuming the real problem here is that the microcontroller (what i used to write code for in assembly anyways) you're working on doesn't have 64 bit registers? if that's the case, you're going to have the break the numbers you're working with apart and perform multiple multiplications with the pieces.

this sounds like it's a homework assignment from the way you've worded it, so i'm not gonna spell it out much further :P

+1  A: 

Since you're on x86 you need 4 mull instructions. Split the 64bit quantities into two 32bit words and multiply the low words to the lowest and 2nd lowest word of the result, then both pairs of low and high word from different numbers (they go to the 2nd and 3rd lowest word of the result) and finally both high words into the 2 highest words of the result. Add them all together not forgetting to deal with carry. You didn't specify the memory layout of the inputs and outputs so it's impossible to write sample code.

jjrv
I think the high*high part is unneeded as it overflows no matter what
BCS
A: 

Just do normal long multiplication, as if you were multiplying a pair of 2-digit numbers, except each "digit" is really a 32-bit integer. If you're multiplying two numbers at addresses X and Y and storing the result in Z, then what you want to do (in pseudocode) is:

Z[0..3] = X[0..3] * Y[0..3]
Z[4..7] = X[0..3] * Y[4..7] + X[4..7] * Y[0..3]

Note that we're discarding the upper 64 bits of the result (since a 64-bit number times a 64-bit number is a 128-bit number). Also note that this is assuming little-endian. Also, be careful about a signed versus an unsigned multiply.

Adam Rosenfield
That's missing the high bits from the first part
BCS
Wait, you've confused me-- You've said to go ahead and get rid of the upper 64 bits of the result? Why would that be... well, rational...?
Kawarazu
A: 

Find a C compiler that supports 64bit (GCC does IIRC) compile a program that does just that, then get the disassembly. GCC can spit it out on it's own and you can get it out of object file with the right tools.

OTOH their is a 32bX32b = 64b op on x86

a:b * c:d = e:f
// goes to
e:f = b*d;
x:y = a*d;  e += x;
x:y = b*c;  e += x;

everything else overflows

(untested)

Edit Unsigned only

BCS
That's cheating, can't do it that way [else of course I would...]But thank you, as under normal circumstances that would work out
Kawarazu
how is it cheating?
BCS
Asking the compiler is cheating but asking StackOverflow isn't?
I. J. Kennedy
the 32x32=64 asm might be the cheat.
BCS
A: 

I'm betting you're a student, so see if you can make this work: Do it word by word, and use bit shifts. Think up the most efficient solution. Beware of the sign bit.

Kevin Conner
You indeed are correct, and I wish I had the time to do that-- Next time I'll definitely be more intelligent about this and correctly work out the time to think about this assignment
Kawarazu