r/HFY • • Jan 14 '21

OC Human programming language

The classroom was silent; all eyes stared at the aged professor; eagerly awaiting his lecture. Viskardar took one last look at his students, creatures of all shapes and sizes, beings at widely different levels of experiences, all sat around the amphitheater. He can almost feel the anticipation. Guess its time to begin.

“Three-hundred-eighty-four students! Clearly an record for the university. Can’t believe y’all here just to listen to my life’s story.” the professor chucked at his teasing. “You see I was born at…”

The audience responded with a tired groan at Viskardar’s teasing.

“Sheesh. Tough crowd.” Viskardar grinned as his eyes grazed upon each student, seeing their faces shift from one of anticipation to annoyance. He spoke once more. “Now all of that awful tension is gone, we can finally start”.

The professor took a step back as the holo-projector above the stage sprung to life, engulfing the area with a faint blue glow; soon an three-dimensional depiction of a symbol manifested above the center of the theater.

Viskardar watched as a few students shot up with excitement upon seeing the character. Excited about recognizing a common symbol? They’re certainly eager.

“As you all know, the galactic union assigns symbols to represent fundamental theories and ideas; anything from evolution to algebra can all be communicated through a single character.” the professor paused in his explanation as his gaze wandered around the class once more “So, can anyone tell me what this character means?”

A few dozen appendages got lifted up.

“The idea that all programming languages should be as close to machine code as possible in order to maximize the efficiency and speed of the program.” a student loudly announced from behind Viskardar.

The professor quickly spun around and scanned for the perpetrator among the crowd. “Correct! However, next time please avoid interrupting.” Unable to find them, he returned to the lecture at large.

“Throughout the lifetime of the union, each species we’ve encountered has this rudimentary idea in one way or another.” the professor paused before lowering his voice, “All expect for one.”

The audience immediately lights up in anticipation. Viskardar can literally feel the sparks burning within each student’s eyes. Here we go

“The union made contact with humanity two standard cycles ago after the Isadrians discovered millions of signals sent from their home world: Earth. An important oddity that was quickly overshadowed by first contact. As the information exchange went on, both sides immediately went to develop a translation program. We expected that we’ll be the first to complete this, yet the humans returned with a fully programed prototype after mere weeks. What would’ve taken us months if not years is completed in just a matter of weeks. What the hell is going on?”

The professor took a deep breathe as his students enthusiastically waited for the lecture to continue.

“The only reasonable hypothesis we had at the time was that since humans are persistence hunters, it is likely that they have godly amounts of stamina and is in turn able to work on an project many times more than an average citizen of the union. However, while true, this wouldn’t end up being the answer as we’ll see much later on. Moving forwards, as integration of humanity into the union begun we realized that humanity didn’t treat computer software as an luxury good. Instead, it was considered an mass produced commodity! And before anyone asks, their programs aren’t incredibly basic either. They have software containing entire simulated worlds!”

Professor Viskardar looked around the amphitheater once more. Of course… Only a few students is shocked. All of this is common knowledge after all. Everyone is just here to know the secret to humanity’s software. Well enough stalling for time. Time to reveal humanity’s secret.

“There has to be an reason right?” Viskardar inquired

The holo-projector above the stage flickered before manifesting its projection as a four way screen, broadcasting to all sides of the theater; on the display, a single line of code is shown.

print("Hello World")

“Now”, Viskardar paused to gaze at the mixed crowd; some students shows signs of confusion, others disbelief at how simple human code is “could anyone tell me what this program does?”

A single appendage rose from the 5th row.

“I-it just outputs ‘Hello World’?’ the student stuttered out

“Yes! It’s just as simple as that” the professor proclaimed “now take a look at this”.

The screen switched once more. This time an complex program made out of nineteen lines of code is presented.

DEFAULT REL			

SECTION .rodata			
Hello:		db "Hello world!",10        
len_Hello:	equ $-Hello                 

SECTION .text

global _start
_start:
	mov eax, 1				
	mov edi, 1				
	lea rsi, [rel Hello]			
	mov rdx, len_Hello		
	syscall					    

	mov eax, 60				
	xor edi, edi				
	syscall					

“Could anyone tell me what this program does?”

…

Not a single soul rose their hands this time. Everyone stared with confusion at the strange new syntax.

…

“Good. What you just saw there is akin to our own programming languages, it is amount as close to machine code as possible. In fact, that variant of assembly — the name of the language — is designed to replicate the ancient human x86_64 CPU architecture's instruction set. None of us can comprehend what the code is actually doing without studying the instruction set and the language first. It’s too convoluted.”

The projected screen flickers for a moment before flickering away along with the dim blue light of the holo-projector.

“And that is the secret to human software. Their programming language abstracts away complexity and as a result makes it trivial to comprehend and program in.” the professor paused for a moment

A student thrusted their limbs up as soon as the professor stopped

“If humans abstract away from machine code how do they deal with the decreasing efficiency and speed of the program?” the pupil asked perplexed

Viskardar smiled “It’s simple really. The humans consider the performance trade-offs as a worthy expense. Besides, as a human compiler engineer once told me, ‘We just optimize the fuck out of it.’”.

The professor stepped back once more. Satisfied by the mixed emotions given to all of his students, from confusion to awe to even more confusion, his job was finished for the day.

“And that concludes our lecture for the day. Class dismissed and welcome to Human Computer Science.”

-------------

Well this is my first time writing in years. I think it’s actually fairly decent although the grammar and dialogue could use some work. In addition, I feel like I’ve skipped over providing reasons as to why the aliens take such a long time to develop anything. I'm fairly certain its implied but I feel like giving it explicitly would be better. It also feels quite rushed at the end of the story.

Anyhow, please excuse my technobabble when it comes to assembly and such. I pretty much only know basic python but hopefully at least some of it is semi correct.

The assembly code is taken from the Wikipedia page here.

1.6k Upvotes

143 comments sorted by

View all comments

286

u/rijento Jan 14 '21

Ah the joys of modern programming languages.

145

u/FogeltheVogel AI Jan 14 '21

Now I don't actually know anything about programming, but at the basic level, we still do that "as close to machine code as possible" stuff right?

It's just covered in a million levels of compilers that automatically translate it.

179

u/inser7name Jan 14 '21 edited Jan 14 '21

So yes that's sorta correct in the sense that it eventually does become machine code after compilation. The thing is, we don't use anything really close to that in what we write on a day to day basis usually. In fact, we let the compiler take some liberties with our code when it compiles for efficiency's sake.

As an example, there's a thing called loop unrolling. So let's say I'm writing this code:

int sum = 0;
for(int counter = 0; counter < 4; counter++) {
    sum += counter;
}

If I were to literally explain what I wrote, I would say first I made a variable called sum. Then I made a loop that has a counter in it that starts at 0 and goes up by 1 every time the loop goes around (counter++). As long as it's less than 4, keep going in the loop. While you're in the loop, take the counter and add it to sum. So what this should do is start sum at 0, then add 0, 1, 2 and 3 to it so at the end it equals 6.

Now with loop unrolling the compiler says "psha, who needs this counter variable and who needs to check if it's less than 4. I know that counter is going to be 0, then 1, then 2, then 3. I can analyze the code to see nothing is going to mess with this counter, so instead I can just copy and paste the body 4 times and it'll be the same thing." So the compiler will spit out something that corresponds to:

int sum = 0;
sum += 0;
sum += 1;
sum += 2;
sum += 3;

So why is this better? The computer never has to check if counter is less than 4, which removes 4 steps. It also never has to increment counter, which saves another 4 steps. So while I wrote this whole thing with a loop, the compiler can reduce it to "take a variable and set it to 0. Then add 0, 1, 2, and 3 to it."

So what we do gets further away from machine code. Not only in terms of layers of translation, but because we let the compiler mess with our logic a bit to get to more optimal, but equivalent, machine code.

Extra: the compiler could reduce this even more! sum += 0 doesn't do anything, so why not get rid of it? We'd get:

sum = 0;
sum += 1;
sum += 2;
sum += 3;

But wait! There's more! Because we know that in addition, (((0+1)+2)+3) is the same as 0+(1+2+3), and we know that 1+2+3=6, why not just say

sum = 0;
sum += 6;

And in fact, why even do the addition that we know will always be 0+6=6 when we could just set sum to 6 at the start?

sum = 6

96

u/DihydrogenM Jan 14 '21

You forgot to mention one of the biggest speed increases by loop unrolling. That you are also removing branches, which are time consuming. Since a computer is like a factory and does multiple things in parallel (called pipelines). However, when it has a choice for what to do the floor either comes to a halt, or may have to scrap a bunch of work.

47

u/inser7name Jan 14 '21

Yes! And not only this, but it also allows for the computer to parallelize each of the iterations! If each of the steps don't impact each other, the computer can even do them at the same time!

29

u/Mister_Myxlplyx Jan 15 '21

I'm gonna upvote... but I have no clue what was just said.

26

u/montyman185 AI Jan 15 '21

Basically, the programs that developers type their code in will, when translating the readable version to machine code, completely rewrite bits of code to make them faster automatically.

14

u/PrimeInsanity Jan 15 '21

Sometimes you can grasp enough to know it's out of your reach.

6

u/horser4dish Jan 15 '21

To greatly oversimplify: in modern processors, math is cheap when we're talking about "how long do things take," stuff that isn't math is relatively expensive. The compilers (programs that basically turn text code into runnable programs) know this, so they can simplify your code by throwing away the expensive parts ("is this number less than 4" for example) and leave only the cheap parts to greatly speed up the final program.

5

u/mafistic Jan 15 '21

smiles, nods and has a nose bleed

Ahuh, yup I know exactly what you are saying

25

u/LAB_Plague Jan 14 '21

It's just covered in a million levels of compilers that automatically translate it.

And that's where inefficiencies enter the picture. A gross simplification would be equating programming languages and their compilers to google translate, while machine code is being able to speak/write the language.

→ More replies (3)

30

u/[deleted] Jan 14 '21

Well at the end of the day executed code is machine code. But we don't do almost any of it, we just abstract on top of abstractions on top of abstractions on top of abstractions.

To take print("Hello, world") (feel free to skip to the end, I'm just geeking out and illustrating complexity with a wall of text):

  1. print("Hello, world!") is compiled into bytecode (like machine code but for an imaginary CPU). This makes things more efficient and streamlined, as the specific way the code is laid out doesn't matter anymore and some optimizations can be applied;
  2. That bytecode is then translated into machine code by the virtual machine;
  3. That machine code is actually just a long sequence of numbers; the virtual machine needs to tell the OS to load and execute it;
  4. Your CPU then loads that sequence of numbers into its pipeline;
  5. Millions of transistors then work in concerts to ingest those numbers, feeding them into the right combinatory circuits and storing the output where relevant.

That's not even touching on the magic that is that syscall thing (which actually tells the OS to do the printing, there's a whole rabbit hole in there!), how I/O works (putting a string into memory is already complicated enough, how does a GPU even work??), how the hell we even got compilers into the first place (bit of a chicken and egg problem as you can imagine), how a CPU works (modern CPUs are actually compiled from a Hardware Description Language, which is just a "programming language" that compiles to electronic circuitry instead of machine code, which is just a wonderfully recursive thing to think about), ...


AND YET we can just type print("Hello, world!") and trust that every level of abstraction beneath that will work as it should, printing "Hello, world!" to the screen. The implementation details are irrelevant, which allows programmers to stay entirely ignorant of them. Our brains can only juggle ~ 5 different things at a time, so we just keep on piling on abstractions until we can write code that doesn't require you to keep in mind more than a handful underlying concepts.

10

u/little_z Jan 15 '21

That's a good description of Java. Other languages work differently than this.

10

u/Krutonium Jan 15 '21

It's a good description of any code written in Java, Python, anything on .Net, Emulators that use JIT... The list goes on.

→ More replies (3)

7

u/Jannis_Black Jan 15 '21

With print("hello world!") Being the whole program I think it's a good guess to say it's python.

4

u/hearth-bursr AI Jan 15 '21

it is common knowledge that in python if the program is too hard to make or you are lasy, you can :

read(import(mail_from_boss.jpg))

return(python.solve_my_problem())

7

u/ketura Jan 14 '21

It's the million levels of compilers that makes it far away from the bare metal. It's the difference between shopping for yourself and sending a letter to your mother to do your shopping for you: yeah you'll still probably have approximately the same cart full of food at the end, but every step of the process (writing, letter shipping, mother reading) introduces potential misinterpretation and error.

"what do you mean you didn't want the twelve pack of watermelons? It says" watermelons" right there, how was I supposed to know how many you wanted? Well I'm not going back, just throw out 10 of them if it's too much. "

29

u/TaohRihze Jan 15 '21

Ahh the good old.

My wife said: "Please go to the store and buy a carton of milk and if they have eggs, get six."
I came back with 6 cartons of milk and she said, "why in the hell did you buy six cartons of milk".
I said "They had eggs".

3

u/jacktrowell Jan 16 '21

That's good programming, it followed the instructions perfectly

3

u/TaohRihze Jan 16 '21

Just glad she did not tell me. "While there see if they have eggs, get six".

Would either be stuck in a loop, or break it with an out of cash notification or out of milk notification.

Also was not sure if I should have gotten 7 cartons of milk.

3

u/little_z Jan 15 '21

The level of compiler abstraction doesn't affect the precision of the instruction. You can't write print("Hello world!) and have the output be 3 Stevens. It will always output Hello world!.

4

u/ketura Jan 15 '21

Sure! But perhaps one of those layers implements strings by pre-allocating 256 bytes to each string or something stupid like that. All subsequent compilation layers don't know that the 256 bytes was a stupid optimization, they just see the higher-level instructions for 256 bytes and follow instructions. You now have unintended side-effects where the storage size of "Hello world!" is (perhaps) larger than can fit in a lower-level register, so it has to be chopped up and shuffled around until the underlying assembly can be fully ran against it, discarding the excess wasted bytes and finally printing your desired "Hello world!".

You now have a program with assembly that does all sorts of twists and turns before finally arriving at the same destination as the theoretical hand-written assembly. The human running it notices nothing amiss, because they have a 4ghz processor and the difference was undetectably short. And yet.

4

u/primalbluewolf Jan 15 '21

Until, eventually, the issue becomes noticeable and finally it stops being "premature optimisation".

→ More replies (1)

3

u/little_z Jan 16 '21

I understand what you're saying. But your case is a bit contrived and leads a reader to believe that our compilers, lexers, etc are strewn with rookie mistakes like this. Which they are not. There are some extreme edge cases with reflection and complex abstraction resolution that can cause some unpredictability, yes. But an example like yours will certainly be caught immediately by anyone with a trained eye.

→ More replies (6)