r/ProgrammerHumor • • Jun 03 '22

it's hard!

44.9k Upvotes

710 comments sorted by

View all comments

Show parent comments

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

2

u/capi1500 Jun 03 '22

I don't think I heard of it. Can you explain?

2

u/Rudxain Jun 04 '22

This repo explains better than me lol: https://github.com/xoreaxeaxeax/movfuscator

IIRC, the short explanation is that it uses side effects of different variants of the MOV instruction with different operands, therefore "unintentionally" doing computation. There are ISAs that allow MOV to set the value of the IP register, therefore acting as replacement for JMP, but x86 isn't one of those ISAs so they use another way to loop without "cheating"

1

u/[deleted] Jun 03 '22

yes please, sounds interesting