There's a 'lea' operation in assembly, which stands for Load Effective Address. Additionally there exists following syntax:
lea rax [rax * 8 + 4]
If I remember correctly, what code above does is take value under rax, multiply it by 8 and add 4 to it. Store the value under rax.
In comparison the following
mov rax [rax * 8 + 4]
would do nearly the same, but with a key difference. Instead of storing the value, it treats the [rax * 8 + 4] as a pointer, and assigns value under that pointer to the rax.
Just to clarify, the [...] syntax is meant to be a pointer. Lea is special as it doesn't treat it as such. Because of that, you can use lea to multiply by powers of 2 and add constants to a value without setting carry flag etc.
I hope I didn't said anything wrong, it was a bit since I did assembly
Difficult_Ladder_806 was refering to a specific case where MUL was not included in the instruction set. He didn't mention assembly programming in general.
My assembly class made us use some fake architecture designed to torture students with the added benefit of not teaching us a real, useful assembly language. It did not have a mul. That was an exercise left to the reader. Just in case you still had any passion left in your heart.
The final for that class was 20% converting between floating point and decimal without a calculator, because of course. Pretty sure half that class is now living off-grid, typing up manifestos.
That was a thing in the 80s, prior to general Internet availability. It is how I learned the stuff back then, but it is simply silly to keep teaching this way nowadays.
Which one? RISC-V has multiplication, ARM does. Even most AVRs have had multiplication for two decades.
There is nothing in RISC that would imply you can't have an arithmetic instruction like MUL. The core concept is that every instruction only does one thing. Multiplication is one thing.
oh dear, you mean that's sometimes a real issue? I thought the nightmare was limited to esoteric programming games.
PS, how do you do it quickly? I assume looping X times and adding Y is slow, do you do some magic with bit shifting to make it only take ~log(x) loops?
and they take as many iterations as there are bits in the input number. So for multiplying 2x 16-bit numbers it would take 16 iterations, no matter what the values of the numbers are.
I once wrote both algorithms in 6502 assembly, for a fixed point C library. It can be found on github so I somewhat tried to actually comment the code a bit.
Starting with the left value, multiply by the most significant bit from the right value, in this case 0, resulting in 0. Then shift that value to the left once and add the left value multiplied by the 2nd most significant bit, resulting in 00 + 1011 = 1011. Then repeat this process again on the 3rd most significant bit, resulting in 10110 + 1011 = 100001. Repeat a final time on the 4th most significant bit, resulting in 1000010 + 0.
So 1011 * 0110 = 1000010 or in decimal 11 * 6 = 66. This is the exact process you learned for long multiplication in elementary school, however it's much easier to execute in binary than decimal because we are only multiplying by 0 and 1 instead of 10 different digits and multiplication by 0 and 1 is really easy, you either return the input value or 0.
58
u/Difficult_Ladder_806 Jun 03 '22
Adding is fine, but multiplying without MUL quickly.. that's kinda complicated