DEV Community

Cover image for The Ground Beneath Your Code
Curious_Soul
Curious_Soul

Posted on

The Ground Beneath Your Code

As programmers, coding and problem solving is an active part of our life.

But think for yourself — if someone comes and asks you "What is a Program?", can you answer that correctly?

In reality most can't. Since we're used to mugging up the important things which can land us a job, we never try understanding how things actually work — not just the surface level code, but the whole work going on behind it.

So before jumping into C++, let us understand what happens behind the screen and how things work — starting from the very beginning.
The Working Behind It All

You have used programs all your life. WhatsApp, YouTube — all of these are programs. But have we ever asked what they actually are?

Not how they work. Not how to use them.

What are they.

Is it a file? A set of instructions? Code? And what even is code? What is the machine actually doing when it runs it?

Most people use the tools without understanding the ground they're standing on.

This series is about the ground.

And the ground starts with something much smaller — Sand.
From Sand to Switch

Sand gets purified into silicon. Silicon then eventually gets shaped into what we call a transistor.

A transistor controls the flow of current and inside a CPU it's used as a tiny switch — on or off. That's it.

A CPU has billions of these tiny electronic switches. And everything you observe on your system — the pixels on your screen, the sounds from your speakers — all of it ultimately comes down to these switches turning on and off in the right order at the right time.

We represent these ON and OFF states using 1s and 0s. This pattern of 1s and 0s is called binary — and it becomes the only thing the CPU physically responds to.

Now the question is — how do you go from switches flipping to running applications?
Switches That Make Decisions

For switches to do something useful, they need to work together in patterns.

Engineers wired these switches in clever combinations. Wire two switches so electricity flows when both are on — you just made a decision. Wire them differently and you get a different decision.

These combinations are called logic gates — AND, OR, NOT.

Simple rules. But combine enough of them together and you can build a circuit that adds two numbers. In electricity. Automatically. No human doing the math.
The Machine That Could Calculate

So we found a machine that could calculate.

Before machines, humans did all calculations by hand. Armies employed rooms full of people who were literally called "computers" — their only job was to compute artillery tables. One wrong digit and the shells landed on your own soldiers.

A machine that could calculate automatically — without fatigue, without distraction, without error — changed everything.

But it calculates and immediately forgets. The moment electricity stops, the result is gone.

So we built Memory — tiny circuits that could be forced into an on or off state and hold it. A billion of these grouped together is what we call RAM today.
The CPU — One Job, Forever

Now we have calculation and memory. Put those together and we get a CPU.

And the CPU does exactly one job without stopping —

It fetches an instruction from memory. Decodes what that instruction means. Executes it. Then fetches the next one.

This cycle of fetching, decoding and executing is repeated billions of times per second.

But there's an issue.

The only instructions it responds to are binary — patterns of 0s and 1s — specific patterns of electricity its circuits are physically wired to respond to.

So how does a human tell it what to do?
The Birth of Programming

That's where programming was born. And the story starts with people writing instructions as raw 0s and 1s — by hand — which was a nightmare.

Writing long code in binary was extremely difficult. You needed to remember patterns for everything and one error made it all wrong.

So we came up with Assembly Code — a small relief.

It was still a hectic task since it had one word for one CPU task. But it was much better because at least it used words instead of binary. Instead of 00000101 you could now just write ADD.

And slowly with time came higher level languages — words and expressions that almost looked like maths and English. And a new kind of program was born alongside them — the compiler. A program whose only job was to read human friendly text and translate it down to binary automatically. Humans could now think in human terms and the compiler handled the translation.
So — What Actually Is a Program?

Now we can actually answer the question we started with.

What is a program?

A program starts as a text file. Written by a human. In a language human enough to think in — like C++. That gets handed to a compiler, which translates it down through layers into binary — the only thing the CPU physically responds to.

That binary gets loaded into RAM. And the CPU fetches it one instruction at a time. Decodes it. Executes it. Billions of times per second.

And while all of this is happening underneath, those instructions ultimately become patterns of electrical activity — billions of tiny transistor switches turning ON and OFF in the right order at the right time.

That's all a program is.

WhatsApp running on your phone is just binary in RAM. That binary came from source code a human wrote. That source code went through a compiler before it became what it is today.

There's no magic. Code is not a spell getting things magically done. It's just switches. Patterns. Layers of translation built by humans so we don't have to think in binary.

You have been using the top layer your whole life — but in this series we'll explore every layer beneath it.
What's Coming Next

In the next part we'll see what exactly happens between you writing C++ code and your program actually running.

We'll start with the preprocessor — the program that runs before anything else, before the compiler even opens your file.

And once you see what it actually does — you'll never look at a #include the same way again.

Top comments (0)