assembly code - multiplication

Programming, for all ages and all languages.
Post Reply
Poseidon

assembly code - multiplication

Post by Poseidon »

sorry for this stupid question, guys... i can't find anywhere on google a clear example of how to multiplicate numbers in assembly code. can anyone explain/give an example?

thanks :)
Ushma

Re:assembly code - multiplication

Post by Ushma »

For "mul reg/mem" (unsigned multiply):

[table]
[tr]
[td]Size[/td]
[td]Multiplicand with reg/mem[/td]
[td]Result[/td]
[/tr]
[tr]
[td]byte[/td]
[td]al[/td]
[td]ax[/td]
[/tr]
[tr]
[td]word[/td]
[td]ax[/td]
[td]dx:ax[/td]
[/tr]
[tr]
[td]double word[/td]
[td]eax[/td]
[td]edx:eax[/td]
[/tr]
[/table]
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:assembly code - multiplication

Post by bubach »

You should really get and print a copy of Paul Carter's assembly tutorial from: http://www.drpaulcarter.com/pcasm/
It's one of my most freqently used sources. ;)
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:assembly code - multiplication

Post by Pype.Clicker »

beside the ancient "mul <reg>", you have the extended "imul" instructions, which allows you to tell x := x * y

e.g.

Code: Select all

imul eax,ecx ;  eax now contains eax*ecx
imul ecx,[variable]
imul edx,13
The plain old "mul" thing is still useful if you're generating possibly overlarge values.
Schol-R-LEA

Re:assembly code - multiplication

Post by Schol-R-LEA »

For floating point values, you would need to use the FPU instruction fmul. Mind you, FP math in assembly is a real pain for anything that isn't utterly trivial; unless you are writing a compiler and/or library, it's better to leave it to a higher-level language. Though it's not nearly as bad as having to emulate floating-point in software...

Here is a page of sample assembly code in NASM, which covers basic integer and FP arithmetic in the NASM dialect. If you are using another assembler, you may need to translate it a bit, but the instruction mnemonics should be the same in most cases.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:assembly code - multiplication

Post by Candy »

just to add to confusion, you can also do the multiplies in parallel or using SIMD hardware, using mulps, mulpd, pmaddwd and loads more instructions. See intel manuals 2a and 2b for details.

There's a damn good reason x86 is CISC.
Post Reply