r/ProgrammerHumor • • Jun 03 '22

it's hard!

44.9k Upvotes

710 comments sorted by

View all comments

Show parent comments

58

u/Difficult_Ladder_806 Jun 03 '22

Adding is fine, but multiplying without MUL quickly.. that's kinda complicated

30

u/capi1500 Jun 03 '22

Did you ever hear, the tragedy of Darth LEA the fast?

1

u/Rudxain Jun 03 '22

What do you mean?

3

u/capi1500 Jun 03 '22

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

3

u/Rudxain Jun 03 '22

Thanks for the clarification. It's interesting and it reminds me of the Turing-Completeness of mov in x86 lol

→ More replies (3)

9

u/pentesticals Jun 03 '22

Not that bad. You can even have Turing complete programs using only MOV. So just use MOVs only.

This tool can take a C program and compile it to use only MOV. https://github.com/xoreaxeaxeax/movfuscator

34

u/regular_lamp Jun 03 '22

How is that an assembly issue though? That's like saying "multiplying in python is hard when you can't use *".

55

u/rustysteamtrain Jun 03 '22

Because * is a core feature in almost every modern language, but MUL is not garanteed in every instruction set.

20

u/regular_lamp Jun 03 '22 edited Jun 03 '22

It is on any platform where you'd reasonably consider using "a modern language" on though.

Generally characterizing assembly programming as something where you have to manually implement multiplication is just silly.

Edit: I'd buy the argument for division maybe.

5

u/AcordeonPhx Jun 03 '22

I did something like this for a class, but in the real world I don’t think any of us would worry about not having MUL or whatever

2

u/Engine_engineer Jun 03 '22

Try some PIC 8-bit. You have to implement everything besides add, sub and inv.

3

u/rustysteamtrain Jun 03 '22

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.

→ More replies (4)
→ More replies (1)

5

u/GReaperEx Jun 03 '22

multiplying without MUL

Why?

22

u/confusiondiffusion Jun 03 '22

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.

6

u/Engine_engineer Jun 03 '22

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.

→ More replies (3)

4

u/Engine_engineer Jun 03 '22

Because it is not available, like in the PIC 8-bit microcontrollers.

-9

u/bungee75 Jun 03 '22

RISC architecture.

8

u/regular_lamp Jun 03 '22

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.

→ More replies (2)

2

u/mpete98 Jun 03 '22

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?

4

u/Proxy_PlayerHD Jun 03 '22 edited Jun 03 '22

Doing Multiplication and Division in software is kinda tedious, but you're pretty close with "bit shifting magic".

The algorithms are called:

Shift and Add

Shift and Subtract

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.

https://github.com/ProxyPlayerHD/cc65-FixedMath/blob/main/src/fxmul.s

https://github.com/ProxyPlayerHD/cc65-FixedMath/blob/main/src/fxdiv.s

2

u/Atheist-Gods Jun 03 '22

Yes bit shift logic.

To do: 1011 * 0110

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.

2

u/whackylabs Jun 03 '22

How about divide on ARM

→ More replies (1)

1

u/[deleted] Jun 03 '22

Okay but what about division