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
assembly code - multiplication
Re:assembly code - multiplication
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]
[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
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.
It's one of my most freqently used sources.
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:assembly code - multiplication
beside the ancient "mul <reg>", you have the extended "imul" instructions, which allows you to tell x := x * y
e.g.
The plain old "mul" thing is still useful if you're generating possibly overlarge values.
e.g.
Code: Select all
imul eax,ecx ; eax now contains eax*ecx
imul ecx,[variable]
imul edx,13
Re:assembly code - multiplication
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.
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
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.
There's a damn good reason x86 is CISC.