r/explainlikeimfive • • 13d ago

Technology [ Removed by moderator ]

[removed] — view removed post

0 Upvotes

30 comments sorted by

•

u/explainlikeimfive-ModTeam 11d ago

Please read this entire message


Your submission has been removed for the following reason(s):

  • ELI5 requires that you search the ELI5 subreddit for your topic before posting.

Please search before submitting.

This question has already been asked on ELI5 multiple times.

If you need help searching, please refer to the Wiki.


If you would like this removal reviewed, please read the detailed rules first. If you believe this was removed erroneously, please use this form and we will review your submission.

24

u/WellsLikeWellsFargo 13d ago

the logic is in how the transistors are wired, not in the bios. a few transistors wired one way make an AND gate (output is on only if both inputs are on), wired another way they make an OR or NOT gate, and the cpu is just billions of those gates arranged to do things like add numbers and compare them. so the “if/then” paths are literally baked into the physical layout.

the bios is separate, it’s a small program stored on a chip on the motherboard that just wakes everything up when you turn the pc on and hands off to the operating system. every program after that is just a list of simple instructions (add, compare, jump) that the cpu’s gates already know how to do, it doesn’t need to be taught.

16

u/primalbluewolf 13d ago

Is this what's known as the BIOS (Basic Input Output System)? 

No, BIOS is a bit further up the chain. 

Is that BIOS code (instructions) stored in a ROM (read only memory) on the motherboard? 

BIOS is typically stored in flash on the motherboard, not read only. That way, it can be updated. 

The process of a CPU handling instructions and generating output is in hardware: the physical layout of the CPU wafers determines its behaviour. 

If so, how does the BIOS know to how to use the CPU to complete computations with the transistors...?  Are the transistors laid out with particular paths for "if/then/or" logic ? 

The transistors are laid out with particular paths for binary logic, and if you're careful with it, you can build if/then/or logic into that. 

There's actually a pretty neat game on Steam called Turing Complete that walks you through the building blocks of computers, starting with individual logic gates and working up to a full computer, then continuing on to writing your own assembly language and software. There's also many textbooks on computer architecture that would go into this. 

4

u/GalFisk 13d ago

There's also a free browser game at nandgame.com where you do the same. I've been replaying it recently. It was fun to assemble the arithmetic unit and the logic unit, and then wire them together into an arithmetic logic unit or ALU, which is what performs the actual logic that you write in your code.

2

u/paulstelian97 13d ago

And then there’s nand2tetris, essentially a practical course you can do for free (though you probably can pay for the certification)

1

u/GalFisk 13d ago

Yup, Nandgame is a subset of Nand2tetris.

1

u/paulstelian97 13d ago

Optional levels are an interesting extension

4

u/jacekowski 13d ago edited 13d ago

BIOS is the first code that CPU executes (stored in some sort of memory on motherboard - exact type changed over the years)

Transistors are used to build more complicated logic circuits, gates and then more complex instruction decoders (if-then logic to decide what to do with this particular instruction), execution units, and everything needed for modern CPU.

Combinational sequential logic is what you are looking for to understand basics of cpu design (this https://www.cl.cam.ac.uk/teaching/0910/DigElec/Digital_Electronics_09_pdf.pdf is a good place to start and books referenced there)

4

u/rupertavery64 13d ago edited 13d ago

Logic circuits are built using logic gates, most notably AND, OR, and NOT. (As an aside, NAND and NOR gates are particularly common in hardware because they are functionally complete and can often be manufactured more efficiently than implementing the equivalent combinations of AND, OR, and NOT gates)

These logic gates are built using transistors. Functionally, two transistors each can be used to make AND and OR gates and one can be used to make a NOT gate.

AND gates switch the output ON when all the inputs are ON, OR gates switch the output ON when one of the inputs are ON, and NOT gates switch the output ON when the input is OFF.

Combining these gates in certain configurations can do things that are useful for calculations.

When you add two bits, the Sum is 1 if one of the inputs is 1, otherwise it carries over and the output of the sum is 0.

ABSumCarry
0000
0110
1010
1101

For example, 1 + 1 = 10 in binary. The sum is 0 and the carry moves to the next place value.

Sum is perfectly represented by an XOR (exclusive or) gate while Carry can be represented by one AND gate

The XOR gate can be built with NAND gates, or but the actual logic can be expressed using AND, OR and NOT gates.

Another collection of gates that is useful is the D-type flipflop. This captures the input and stores it, a sort of memory cell. D-type flipflops are used in registers, which is the scratchpad of the CPU.

So now you can do some basic math and store data, and of course do things like AND, OR, NOT which is the foundation of logic itself. So how does a CPU actually do anything?

A CPU needs a clock (which is a pulse that goes on/off continuously), data lines, address lines. It also uses those registers I mentioned. One (called the Program Counter) stores the current "address" that is being executed, others store temporary data between operations, a "stack pointer", an "index register", and others depending on the CPU.

The CPU is connected to other devices like memory and other hardware through the data lines and address lines. In order to communicate (send or recieve data) with the devices, it places a number on the address line, which activates a device within the address range, then the data line can be used to read or write data to the device.

Memory (RAM) is used to store programs in the form of machine code or instructions. These are just bytes of data, but the bytes themselves control what the CPU does.

The CPU basically does a couple of things:

  • fetch - load data from memory at the current address (Program Counter)

  • decode - use the instruction byte(s) to activate the appropriate circuits to do the desired operation

  • execute - perform the operation by running the data through the circuit

Each instruction byte or bytes is basically like a key that unlocks circuits using combinations of AND/OR/NOT logic which eventually results in an ON or OFF signal

Here is the instruction set for the Nintendo Entertainment System CPU, the 6502:

https://www.masswerk.at/6502/6502_instruction_set.html

There are less than 256 instructions (some of them are undefined). and they are basically just performing addition, loading / storing data from memory, performing AND/OR/NOT logic, manipulating registers (rotating bits), and updating the program counter (branching)

Branching (if) is done by comparing the result of an operation with zero.

All higher programming logic such as loops, if/else is based on these basic instructions.

Things like input/output (reading from a keyboard/gamepad, performing graphics stuff) is reading and writing to memory addresses that the devices are "connected" to.

The CPU needs to be set to some predetermined address, and perhaps surprisingly, this is not 0000. What happens is, the CPU has a "reset vector" or a hard-coded address that the program counter will be loaded with on initial boot or when reset.

So the CPU will set the address to this reset vector, then load the value in memory at that address to the program counter. This lets the external hardware to determine where the program will start.

So for the NES hardware, the cartridge is plugged directly into the address and data lines of the CPU, and when the CPU resets, it loads whatever is found at the reset vector in the cartridge. So the game can tell the CPU where to start loading from, which can be a bit different from game to game, depending on the size of that game.

The BIOS is just a memory device that the CPU will "reset" to when booting, just like a NES cartridge. It is usually a programmable ROM that can be updated depending on hardware or software changes.

4

u/OldAd9280 13d ago

https://nandgame.com/ is a really good explanation of how you go from basic transistors, to logic gates, to adders, to a cpu

7

u/IMovedYourCheese 13d ago

The CPU has logic gates.

  • NOT - 1 becomes 0 (and vice versa)

  • AND - outputs 1 when all inputs are 1

  • OR - outputs 1 if at least one input is 1

You can write every algorithm in existence with a combination of these gates.

BIOS is a higher level system, and is part of the motherboard's firmware. It helps the OS boot up. It doesn't have much to do with the CPU itself.

2

u/paulstelian97 13d ago

The CPU _runs_ the BIOS and other code.

3

u/DragonFireCK 13d ago

Typically, the only gate used at the bottom level is the NOR gate. This one outputs 1 if, and only if, both inputs are 0.

From that single gate, you can produce every other piece of logic possible.

2

u/CheezitsLight 13d ago

NANDs are equivalent. Depending on the technology they may be more efficient. In TTL it would be NAND. The moon landing computers were built entirely out of the the first integrated circuits which were NOR because the technology then was different and NORs were more efficient.

1

u/bebopbrain 13d ago

This is the way. Understanding a FET as a switch. Building a gates (NOR, NAND) out of FETs. Building flip flops out of gates. Building adders and counters. Then the sky's the limit and you're building an LLM that can talk to you.

2

u/Shartyshartfast 13d ago

Watch the YouTube series on building an 8 bit computer from scratch, by Ben Eater.

Unfortunately this won’t explain it like you’re 5, it will explain it like you’re 15. But that’s as good as it gets; it’s a little involved.

1

u/Vert354 13d ago

The logic part comes in the form of what called "Boolean Algebra" which is a whole category of math that deals with true/false values. 1=true 0=false Boolean Algebra lets you boil some very complex states down to being either true or false then the system will do one thing if the state is true and something else if its false.

1

u/DBDude 13d ago

Modified copypasta from an earlier post:

You have wires and electrical components. You apply a voltage going in and see what voltage comes out. That's a logic gate. Here are some. 1 means high voltage, 0 means low.

AND: If both are 1, the result is 1. If either one or both are 0, the result is 0.

OR: If one or the other or both are 1, the result is 1.

XOR (exclusive OR): If one or the other (but not both) are 1, then the result is 1.

NOT: Operates on one bit, if it's 1 the result is 0, if it's 0 the result is 1.

NAND: combined NOT and AND, outputs 1 as long as both inputs aren't 1

For example, an AND gate is an electronic circuit that accepts two voltage inputs and has one voltage output. It has just a few electronic components (resistors and wires and stuff, you could easily make one yourself) arranged so that if both inputs are high voltage, the output is high voltage. If one or both of the inputs is low voltage, the output is low voltage. We then interpret high voltage as 1, low voltage as 0, and we can do an AND on two bits. The same works for the next two, just the electronic components are arranged a bit differently to achieve the other results. The NOT gate just inverts the one input, low to high, high to low voltage.

We can use these to do things. Let's start building.

A half-adder gives you the sum of the two inputs plus the carry. So 1 + 1 = 0 with a 1 carry (like 5+5 is 0 with 1 carry to be 10). You make this half-adder by hooking an AND gate and an XOR gate together, that's it. You apply high voltage to both inputs (1+1), you get high low voltage out on the add line (0) plus high voltage out on the carry line (1).

But that's too basic. We need to add numbers we can use, notice we didn't do anything with that carry. So let's make a full adder which accepts the numbers to be added and can take that carry from a previous adder. This is made by combining two half-adders with another XOR gate.

Now this is still for one bit, but now we have something we can chain together since each adder accepts the carry from the previous. If you have 8 bits, you string 8 full adders together so each can get the other's carry (carry out wire connected to the carry in wire on the next one) and output the result. Put in two 8-bit numbers (remember, this is just voltage sent to wires), and you'll get the 8-bit result plus a carry, if any. We can now add any two numbers 0-255, and we'll be told if we need to carry a 1.

So now we can add. Subtraction, multiplication, and division all work the same way, just a different number of those basic logic gates arranged differently.

And now we can do any math by controlling what voltages get sent where. You could do this at home with a circuit board, some transistors and resistors. Put two rows of 8 flip switches to describe the first and second input numbers (up = high voltage = 1, down = low voltage = 0) where each switch feeds high or low voltage into one of your 8 full adders. Now hook a set of lights to the output of each full adder plus another light connected to the carry output. As you flip the switches, the lights will change to represent your two added numbers.

Ta-da, you just made a basic binary adding calculator. Now instead of just adders, you could put in the components for a subtractor, a multiplier, and a divider (above), and have another switch that controls which of those is hooked up to the input switches and output lights. Now you've made a full-blown binary calculator.

Now let's automate this. Let's run your circuit board on a clock that sends a pulse of electricity every second. We can also call a particular arrangement of configuration switches a "program" that can control how the electricity flows to multiple parts of the calculator. We need memory! No problem, throw in two NAND gates connected to teach other the right way and you can set and read one bit by applying and detecting electricity. Sixteen NAND gates let you store one 8-bit byte, so instead of your calculator result just flashing lights, it can be stored in these NAND gates, and it can be used later.

Congratulations, you have a primitive computer. A modern microprocessor is the same thing, except we have billions of components and run at billions of pulses per second. Instead of transistors, resistors, and wires on a circuitboard you can touch, they are printed on silicon wafers using a type of photography, where one AND gate is only several billionths of a meter in size.

1

u/BitOBear 13d ago

All the big parts are made out of almost identical small parts.

The core of the CPU is a bunch of math. The ability to add and multiply subtract and divide and perform the basic Boolean logic operations like AND and OR

Now all of those basic operations have been plumbed with a certain side effect sensitivity. A series of observations that are made whenever any of these math operations take place.

Different means exist but in the Intel bloodline that's called the flags register.

In the Intel flags register there's a bit that says whether the last operation return zero as a result. There's another flag that says whether the last result was negative. There's another flag that says whether the last operation overflowed or underflowed (adding one to the largest integer that computer CPU can naturally handle is an overflow for example.)

So after every transformation or analysis operation there's this short list of things that the computer observes.

Now the way a computer actually executes instructions is that the instructions are just codes in memory. Literally integers with specific meaning. And every time it looks at an instruction the piece of logic that says where to look for instructions increments by one. So it goes to the next instruction.

And that's super cool.

I thought we can take all this and develop a way to make decisions. If we add an instruction that says hey if the flag that says the last result is zero is true then instead of adding one to the instruction counter add this other bigger number or substitute this other number so that you can start running code that's way the hell over there instead of right here.

So if I say if A is less than B then go to something else. That becomes a subtraction of B from A followed by the instruction that says if the last operation was negative in the next instruction to execute is way the hell over there.

So once you have disability to understand equality and greater than and less than and stuff like that. And you have the mechanism necessary to stop what you're doing here and start doing something over there when that's the case that's the core of logical branching.

Out of these very basic operations we can construct arbitrarily complex logic. We can have B less than A and C equals D as to consecutive applications of this conditional type logic.

Now to get the most efficient ways to make these analyzes you often engage in what I call logical origami. You decide the best order to make these inquiries in order to get the answer as fast as possible having done is least work as possible etc.

Now we just need to change the meanings of A, B, C, and D. So like if the family in question is my family is just C&D hanging out with different names if I've got some numeric way to represent family identities.

So it's like a question of how you can make a castle out of Lego bricks when all the Lego bricks are a little rectangles of various sizes.

To continue the Lego analogy, later CPUs have more specialized sets so that you can make things but look more and more like wings and wheels and windows and things so that instead of making a big blocky Castle you can make something that looks like a specific building or artwork.

But it's all the same stuff just scaled up in complexity by iteration.

1

u/SkullLeader 13d ago

Go research how transistors are used to construct logic gates.

1

u/Great-Powerful-Talia 13d ago

It's not the BIOS. The BIOS is a set of instructions, not transistors. Its purpose is to kickstart the operating system. But that OS, like the BIOS that grabs and activates it, is itself a program, so it can't do anything that programs can't do. It's only responsible for higher-level things like starting other programs (with appropriate safety restrictions), giving them permission to use specific areas of the computer's RAM, operating a filesystem, etc.

-

Transistors can do if-then logic all on their own. Otherwise, they wouldn't be able to do different things when given different instructions.

Any CPU will have, in addition to instructions to add and multiply and stuff like that, some instructions that can jump straight to another part if the program if a specific thing is true.

It doesn't really matter what, exactly, you can test for- you just need to have something. If you can't directly test for two numbers being equal, you can probably subtract them and test if the result is zero. If you can't directly test if a number is zero, you can probably set another number to 0 and test if those two are equal.

With these jump instructions, you can write out logic like,

if a is equal to b:
  do Thing
otherwise:
  do OtherThing

And when this is compiled into computer-readable 'language', that's a series of instructions like:

if a is equal to b, jump forward by 10 instructions 
[9 instructions of doing OtherThing]
no matter what, jump forward by 7 instructions
[7 instructions of doing Thing]

If you think through this, you can see that it's the same as the original logic.

If a is equal to b, then the computer skips doing OtherThing and the second jump instruction- none of that stuff happens at all- and it just does Thing and moves on

If a isn't equal to b, then the first jump instruction does nothing, the computer does OtherThing, and then it skips right over doing Thing and moves on.

-

You can build any if-then logic, no matter how complicated, by just doing this over and over.

You can also have looping logic, where something happens until some condition is met- the key is jumping backwards, which can re-run the same code repeatedly.

And that's all you need to make basically everything computers do.

1

u/iamthelowercase 13d ago

If you just want to mess around with the concepts everyone else has been mentioning, "NANDgame" and "NAND to tetris" are both free.

1

u/LagrangianMechanic 13d ago

Find and read a copy of Charles Petzold’s book “Code”.

It will lead you through all this starting with the very basics.

1

u/GalFisk 13d ago

When you write logic in software, and it's turned into machine code patterns of ones and zeroes, what you're really doing is (very simplified) putting a logical 1 on the physical wire that turns on the ALU (arithmetic logic unit). The unit has a few more wires for deciding which logic gates to use and where to fetch the data for those gates.

A software program is really a list of which parts of the CPU should be on or off at any specific time, and by carefully orchestrating these you can move and process data as you desire. Each instruction does very little, but a modern CPU can process billions of them every second.

1

u/egoalter 12d ago

No electronic circuitry knows anything. It's us human who are taking advantage of physics when it comes to electronics, and using that to create functionality out of controlling voltage flow.

There's quite a few YouTube folks who have created full series where you start from Scratch and you end up programming it. Ben Eater is the one I think started it, and his series is very good - and you can buy a kit from his website where you get everything to make your own. Ben starts with transistors - so it starts from where your question comes from.

On the very fundamental level, and it should be said that modern systems have found shortcuts to some of this. The transistors used in computers can be seen as a simple switch with a twist. Fundamentally one way to use a transistor is applying a small voltage to the base/gate and it will conduct electricity between two pins. Most transistors have 3 pins. What we humans care about are the signals, and with a single transistor you can create a NOT (inverter), meaning when you open the gate, the output signal turns off.

     +5V
      |
      >
      <  Rc 1kΩ
      >
      |
      +-----------o  Q = NOT A
      |

Rb / Q1 A -//-| 10kΩ
v | GND

This is a poor attempt using ascii chart. The transistor is on the bottom. When the transistor does not conduct (A is LOW) the Q is high. When it conducts, it's low. And yes, you will need some understanding of electrical circuits at this level, so again I recommend googling building 8bit computers and Ben Eater will most likely be a top hit.

We can create other basic calculations ("not" is one arithmetic computer program uses all the time) using transistors (more than one often) providing features such as AND (two or more inputs must be high for the output to be high. An OR gate is high if at least one of the inputs is high. With this, we can create a NOR gate - combining a OR and NOT gate, which returns high if all inputs are low, and that allows us to design another fundamental setup - a flipflop (specifically the SR FlipFlop).

     ______

R ------\ -. ) >o-------+------ Q +---/______.-' | | | +---------. .--------+ \ / X / \ +---------' '--------+ | | | ______ | +---\-. | ) >o-------+------ Q' S ------/______.-'

So in the above the symbols are NOR gates. This is how static memory can be implemented. The output Q stays high or low even with the input S changes. R resets the flipflop and it will use S to set the output.

So addition, subtraction etc are all built in a similar way. It's just electrical signals, and this is how two inputs can be added together:

            ______

A ----+-------\ `-. | )) >-------- Sum | +----//_.-' | | B ----|--+ | | | | _____ | +-----|
| | )-------- Carry +--------|/

This uses another type of gate XOR - which outputs high only if one or the other of the inputs are high. This is how we typically document logic gates using a truth-table:

A | B | Sum | Carry ---+---+-----+------- 0 | 0 | 0 | 0 0 | 1 | 1 | 0 1 | 0 | 1 | 0 1 | 1 | 0 | 1

So remember, the output is 2 bits, because when both A and B are high, we need to represent 2 in binary, and that requires 2 bits.

This has been the fundamental building block of computers when they became electronic instead of mechanical. Quantum is changing this, but that's another response. By combining gates we create memory and math. There's then a "controller" we call microcode stored in the CPU, so when a instruction for OR is met, that it will open inputs to the OR gate in the circuit passing in the inputs and on the next clock it will read the output from the OR gate. The microcode is signals of high/low that opens connections to the specific function that represents the desired functionality.

Lastly, making decisions on a computer is done with "if" statements, but at this level it's a jump - meaning an instruction that sets where the next instruction is instead of just going to the next. Jump instructions can be conditional using a "flag" to guide if something was true or false. So the programmer asks if A>B, the OP come basically subtracts B from A, and if the result does not set the carry (meaning it's a positive (above 0) result) the jump statement changes the location of the next instruction.

Again, this is very simplified but just saying that the electrical signals can prepresent a logical (human) model doesn't really show the basics. And realize that pretty much all electronics you have today have some kind of computer in it, your graphics card has similar processing internally but has special circuitry to do a tons of stuff in parallel. The basic 8bit computer cannot do that using Ben Eater's example, but if you head to James Sharman on YouTube he creates a pipelined CPU that allows instructions to be processed in parallel, something most CPUs does today (just a lot more advanced). What is great about James Sharman is that he's a programmer by day - of games. So you will see he's created graphical games with joystick inputs and more on his 8bit computer. And what both James and Ben do, is using LEDs to show you exactly what's going on. So as you manually step through an instruction, you can see with your eyes exactly what happens to the signals, and you can then trace the diagram and verify that it's exactly what you expected.

This is long - but hopefully the illustrations albeit crude, will help with the understanding.

1

u/Adorable_Ice_2963 12d ago

If there is a voltage (=on signal) at the "gate" of the transistor, it lets current flow.

With one transistor alone, you can do 2 things: start a current by open the path from the power supply to the signal line, amplyfing the Signal, or diverting the current by Open a path from the power supply to ground, making it "on" at the "off" signal, and "Off" at the "on" signal (Inverter/NOT-Gate).

Its not logic, yet 

By adding a 2nd transistor in series, you now need 2 signal lines to be on to make current flow from the power supply to the signal line.

You can also do that from the signal line to ground (like an inverter, but 2 transisors in series), pulling the signal down when both are on, making it Off.

These are AND (on when both signals are on, else off) and NOT AND. (off when both signals are on, else on.

Similar with OR Gates, with the difference that the transistors are in parallel.

Either on if at least one signal is on (OR), Off if at least one Signal is ON.

Every logic gate are amplifier and/or Inverters combined.

Since Diagrams can get quite messy, you leave out the Transistors and voltage supply and ground, only showing signal lines as input and Outputs, and a shape as body to ease up reading/avoiding mistakes.

By combining them, you can make simple yes/no logic.

Like: ON if Power Button is ON AND if emergency Button is OFF

You can also use logic gates to make signals go to different logic blocks, or go through them in a different order. You can also choose that way what logic to fire. Thats basically an CPU.

1

u/Designer-Ad-2136 11d ago

Read the short guidebook: "But how do it know?". That will actually build your intuition

1

u/Aluxanatomy 13d ago

You build a rock that, when you interact with it one way, can only possibly give a certain response. Now multiply that by a bazillion, and you get a computational algorithm. The electrical response that CPUs produce are the only possible responses they can produce for a given electrical input.

Any "meaning" produced by that response is entirely within the human. The magic rock isn't alive and doesn't care what responses it is giving.

0

u/Lumpy-Notice8945 13d ago

BIOS and all that are software, these are already zeroes and ones that need to be run by the CPU.

The CPU is physical, its electricity flowing through logic gates. It might sound dumb but you can look up minecraft for examples of how logic gates work because its simple and visual.

Logic gates are circuits called "XOR" "NOT" "AND" and so on. They do simple operations like compare if two binary values(1 or 0) are the same or flip the value from a zero to a one or back.

From these you can build instructions like addition, by using like 100 logic gates you can add two 8 bit numbers(compare each digit, if the result is 2 carty over to the next digit otherwise return the sum).

Thats what you vould call assembly instructions or an instruction set for CPUs and thats what a compiler produces from code, a long list of these simple instructions like "add two numbers" or multiplication or run another insteuction if a value is zero. A CPU has these instructions literaly baked into it as physical wires and capacitors that carry electricity.