Comment by Hardass_McBadCop on 08/03/2025 at 14:51 UTC

2 upvotes, 2 direct replies (showing 2)

View submission: Ask Anything Wednesday - Engineering, Mathematics, Computer Science

Maybe this is a bit too basic, but can anyone explain to me how programming works? Like on a mechanical level. How does binary on a screen become real - become actual electricity and switches in a machine? How does code actually act its instructions out?

Like for example, let's say I wrote a short segment that just stored a number in memory. Say, 2. So in binary that's 10 and the memory register would have one switch open and one closed for the two bits needed to store that. But how does that code, after it's compiled into binary, open/close switches, create logic gates, or interact with the CPU to do calculations?

For a metaphor of what I'm trying to really get at, if I wanted to turn on a light switch: I think about it and my brain uses electricity to cause my muscles to contract in a way that makes my arm move and physically turn the switch on/off. So if my thought is the "code" and my brain is the "compiler" that turns it into something the rest of my body can act on, then how does the rest work that leads to the real world, physical action of opening and closing the switch?

I hope that made sense.

Replies

Comment by youngeng at 09/03/2025 at 16:12 UTC*

2 upvotes, 1 direct replies

how does that code, after it's compiled into binary, open/close switches, create logic gates, or interact with the CPU to do calculations

The CPU operates on a cycle known as Fetch-Decode-Execute.

First, it fetches the instruction to be executed.

Then, it decodes it. So, if the instruction is, say, add, it "switches on" the sum circuit and looks for two terms (other data stored somewhere) to be added.

Finally, it executes the instruction.

This is a cycle because it happens continuously and because the output of the execute phase may lead to another instruction to be fetched.

As a loose analogy, imagine a kitchen with a single inexperienced cook.

Let's say someone orders mashed potatoes.

The cook reads the order and opens the recipe book.

Then he follows the recipe. He starts cutting potatoes, he puts them somewhere to boil them, and so on. At the end, they put all the food somewhere (on a dish) and somewhere else (the waiter) brings it to the customer.

The cook himself doesn't know all the recipes, but he knows how to read and he understands the basic recipe "building blocks" (cooking, cutting, searing,...).

In this analogy, the cook is the CPU and the basics of cooking are CPU instructions.

I hope this helps.

Comment by atomfullerene at 10/03/2025 at 04:27 UTC

1 upvotes, 0 direct replies

So, to boil things down to their very most basic level, a transistor is a switch that opens when a small current is applied to (or grounded from) the base. In other words, it's a switch that electricity can open or close. Memory is stored in a bunch of different ways, but basically that memory supplies some voltage. That is then fed into the rest of the computer and opens and or closes a bunch of other switches, which goes on and runs the rest of the system.

This is simplified and a bit inaccurate, but hopefully it gives you a very broad picture.

If you want the details, I highly recommend https://nandgame.com/[1] and Ben Eater's playlist on building an 8 bit computer from scratch

1: https://nandgame.com/

https://www.youtube.com/watch?v=HyznrdDSSGM&list=PLowKtXNTBypGqImE405J2565dvjafglHU