There is a thing called a “one instruction set computer - OISC”. IIRC one of the instructions that works - I.e. an instruction you can write any program with - is “decrement and jump if not equal to zero”.
Perhaps this CPU is a “fun” one with jumps, branches, and adding one (succ).
I only saw the Clang assembly, maybe gcc did better, but I was saying that there isn’t the usual optimizations that convert it to just an add instruction.
lea in this case is more efficient than an add because it can specify the output register in the same instruction. If you wanted to use an add, you also needed a mov of one of the arguments in eax, because add will always use the first register as the output. So one instruction with lea > 2 instructions with add.
unsigned int add(unsigned int m, unsigned int n) {
goto c;
a:
int i = 0;
b:
if (n == 0) return m;
m = succ(m);
goto d;
c:
goto a;
d:
i++;
n--;
goto b;
}
Honestly though I do appreciate the use of goto, I never understood how it worked in the examples, and this shit code helped it click that it falls through like a switch statement.
There is a separate function for 1 decimal place, and another with 2 decimal places and so on. You import the function for addition to the amount of decimal places necessary
227
u/_PM_ME_PANGOLINS_ May 20 '23
This has to be a deliberate troll. The
goto c;is entirely pointless.118
u/Pepineros May 20 '23
This is someone explaining how you would add two numbers in Assembly without using Assembly.
22
u/TTachyon May 20 '23
https://godbolt.org/z/MdcKcce4z
44
u/HeWhoShantNotBeNamed May 20 '23
Rule #1 of this Subreddit. No intentionally bad code.
13
u/robottron45 May 20 '23
succ(x) definitely reminds me of theory courses ._.
12
u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 20 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
9
u/fried_green_baloney May 20 '23
This is a not-so-clear implementation of a description of addition.
and
Obviously practical CPUs don't do this.
6
u/Effective-Attorney33 May 20 '23
This seems to be kinda like how addition is done using the peano axioms.
6
u/igeorgehall45 evil May 20 '23
Not even recursive, smh
4
u/Martsadas May 20 '23
this code succs
3
u/SimpleG404 May 20 '23
i haven’t seen a goto in a long time
2
u/Rahu888 May 20 '23
The forbidden jutsu
2
u/TheTimBrick May 21 '23
And I have a revolutionary answer:
no.
2
u/[deleted] May 20 '23
This is a masterpiece.
1
u/sixtyfifth_snow May 20 '23
Quite impressive, actually :)
1
u/koobzar May 21 '23
This is probably the most cursed thing I've seen
1
u/[deleted] May 21 '23
(x+y)
1
u/BlurredSight May 21 '23
Intel 8051 training
1
u/Inochigake May 23 '23
Good old spaghetti code. Takes me back to the days of what the hell were we thinking
0
u/TheZipCreator May 21 '23
r/deftruefalse is for deliberately bad code.
-1
u/WittyGandalf1337 May 20 '23
My favorite part is the two parameters having the same name.
This is legendary.
-4
u/lonay_the_wane_one May 20 '23
This code breaks when dealing with decimals or negatives...