Page 1 of 1
assembly code - multiplication
Posted: Tue Apr 05, 2005 1:50 pm
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
Re:assembly code - multiplication
Posted: Tue Apr 05, 2005 2:57 pm
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]
Re:assembly code - multiplication
Posted: Wed Apr 06, 2005 3:04 am
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.
Re:assembly code - multiplication
Posted: Wed Apr 06, 2005 5:52 am
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.
Re:assembly code - multiplication
Posted: Wed Apr 06, 2005 2:16 pm
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.
Re:assembly code - multiplication
Posted: Wed Apr 06, 2005 3:07 pm
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.