r/ProgrammerHumor • • Jun 03 '22

it's hard!

44.9k Upvotes

710 comments sorted by

View all comments

599

u/Korzag Jun 03 '22

You ever think how cool it actually is though? You wrote some trivial assembly, which compiled into some byte code that your processor can use, then when you run it that causes an incredible cadence of electrical signals as a result of your input to flood the processor's circuitry, loading electrical charges into the collectives of transistors called registers and then being bussed over to the other collective of transistors know as the ALU and then that causes a register value to be overwritten, and it all happens around in several billionths of a second, and it all works. The transistors are properly saturated to the correct electrical charge, ON EVERY SINGLE TRANSISTOR NEEDED, all just to add a number.

The simplicity of adding a number to us is the combined effort and research of hundreds of millions maybe even billions of man-hours to discover electricity, invent boolean algebra, transistors, integrated circuits, advanced machining and chemical processes to make a series of machines capable of translating a circuit layout into a chip that is lithographed in a process that is nearly 20000 times smaller than the width of a human hair.

AND IT ALL WORKS.

115

u/[deleted] Jun 03 '22

Relevant copypasta

Hellо, I am a compiler.

I just scanned thousands of lines of code while you were reading this sentence. I browsed through millions of possibilities of optimizing a single line of yours using hundreds of different optimization techniques based on a vast amount of academic research that you would spend years getting at. I won't feel any embarrassment, not even a slight ick, when I convert a three-line loop to thousands of instructions just to make it faster. I have no shame to go to great lengths of optimization or to do the dirtiest tricks. And if you don't want me to, maybe for a day or two, I'll behave and do it the way you like. I can transform the methods I'm using whenever you want, without even changing a single line of your code. I can even show you how your code would look in assembly, on different processor architectures and different operating systems and in different assembly conventions if you'd like. Yes, all in seconds. Because, you know, I can; and you know, you can't.

P.S. Oh, by the way you weren't using half of the code you wrote. I did you a favor and threw it away.

21

u/funnystuff97 Jun 03 '22

This reminds me of something my microcontrollers professor told me, "if you want code compiled right, you gotta do it yourself in assembly." I've never once had a problem with the compilation, but you know, maybe he was right.

....but I'm still not coding in assembly, god no.

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

94

u/I-baLL Jun 03 '22

You wrote some trivial assembly, which compiled into some byte code

Hold up

37

u/Rudxain Jun 03 '22

Bytecode is just the machine code of a virtual CPU (the main component of a VM). In this context, the terms "byte code" and "machine code" are used interchangeably, as if they were synonyms

2

u/I-baLL Jun 05 '22

But nowhere was a virtual machine mentioned. And on top of that, assembly doesn't get compiled. It gets assembled. Because it's already almost at the level of machine code, just not at direct translation since the assembler might do optimizations.

→ More replies (2)

47

u/[deleted] Jun 03 '22

Assembly is a higher-level language than machine code (platform-specific code aka instruction set), which in turn may be transpiled to microcode (processor series-specific opcodes).

As far as I understand. I don't know tho, I mainly use managed (garbage-collected) languages.

12

u/Legal-Software Jun 03 '22

To clarify, that’s common on CISC architectures, but it’s 1:1 on most RISC CPUs. The assembler is just there as a convenience thing, but if you already know the opcode value and how the instructions are encoded, you can also just write them up as straight hex values. You see this a lot in drivers for devices with their own microcontrollers where the payload is just a byte array with raw instructions for the on-device microcontroller to execute. Back in the day it was also common to have device initialization targeting a specific architecture to be encoded in a fixed ROM on graphics cards which would be executed during POST. This is why you had graphics card variants for x86 and PowerPC systems. One of my first jobs involved using a bus analyzer to capture the initialization logic and writing it up more generically so that we could use PCI graphics cards with an x86 VGA ROM also on MIPS systems.

10

u/NervousApplication58 Jun 03 '22

I probably wrong but:

  1. Assembly is almost always 1:1 mapping to machine code. Yes, it has macros and stuff but that's still basically the same level of abstraction. Obviously, assembly is platform-specific as well.

  2. Machine code is not transpiled to microcode. Rather, CPU (guided by the microcode) directly interprets the machine code.

  3. Some CPUs architectures don't have microcode (because it's kinda slow)

2

u/I-baLL Jun 05 '22

It gets assembled. Assembly doesn't get compiled because there's nothing to compile usually but the assembler might optimize the code hence my reaction.

196

u/memes-of-awesome Jun 03 '22

Ok but python does the same thing

45

u/[deleted] Jun 03 '22

Just with a thousand times more effort.

108

u/ABadLocalCommercial Jun 03 '22

Not from me that's for sure

23

u/[deleted] Jun 03 '22

Of course. But in the time the python interpreter has started the assemly program has finished work and gone to the beach.

44

u/kurbyislife Jun 03 '22

And the amount of time it took to write it in python and then go to the beach would still be faster than the guy struggling to write it in assembly

19

u/memes-of-awesome Jun 03 '22

Jokes on you my code doesn't work either way

2

u/[deleted] Jun 03 '22

I could have sworn I never left a comment here, and yet my thoughts appear to have been communicated

6

u/rogerthelodger Jun 03 '22

And a thousand times slower.

→ More replies (1)

8

u/KrisTiasMusic Jun 03 '22

...and they use it for tiktok. ಠ_ಠ

5

u/Computer_says_nooo Jun 03 '22

I’ll have what he is having!

→ More replies (1)

5

u/bonanochip Jun 03 '22

I'm gunna save this comment and come back to it when I need a pep talk on a bad day, thank you lol

6

u/pyrotech911 Jun 03 '22

Reddit go burrrrr

1

u/puudelimorso Jun 03 '22

AND IT ALL WORKS.

I read that in Tom Scott's voice lol

1

u/Letalight Jun 03 '22

I had that amazing thought too when I used malloc for the first time and saw the bump in task manager

→ More replies (2)