How exactly do computers execute code?

  • #1
472
14

Main Question or Discussion Point

one thing that has always miffed me is how is it that the hardware carries out the coded instruction. the code is 1's and 0's, and if I have understood anything. 1's and 0's are on and off. but how is the computer flipping the circuits on and off? something must tell it to do that. but then what is telling it to follow those instructions. etc.

I am unfamiliar, but have friends to took circuits or logic circuits or something like that, and so I guess at the most basic level, logic circuits are used in a magical way to control everything. all code must carry with it instructions that activate those circuits?

I don't know. it's weird to me. almost as if there must be a brain in there deciphering the code and carry out the orders.
 

Answers and Replies

  • #2
berkeman
Mentor
57,281
7,270
What reading have you been doing so far on this? Seeing what you've been reading and not understanding will help us to answer your question the best that we can. Thanks. :smile:
 
  • Like
Likes Klystron
  • #3
8,479
5,356
A tutorial video like this might help. There are others on Youtube.


There is no magic. It is just boolean logic. I'm not sure the level of understanding you seek. Do you need a description of the CPU down to the level of each transistor?
 
  • #4
472
14
There is no magic. It is just boolean logic. I'm not sure the level of understanding you seek. Do you need a description of the CPU down to the level of each transistor?
I know there's no magic. I don't think that video is what I'm looking for. but probably, that's what I'm looking for. how the cpu works at the transistor level. not just a generic explanation you get in any course. but how exactly, physically, is the cpu processing.

I can understand how a computer worked back in the days when human involvement was required. manually plugging and unplugging as code. or with the punch cards. when computers required human intercession, and software was physically altering the system. which I don't understand how a cpu does.
 
  • #5
jim hardy
Science Advisor
Gold Member
2019 Award
Dearly Missed
9,839
4,869
i think you'll have to build one.
I learned by troubleshooting them. Hung a 'scope on every IC in it .
Of course that was in the days of 7400 logic IC's, when a CPU occupied three boards seventeen inches square and you could do that.
By 1974 they'd progressed to the point a CPU was now one IC the size of your thumb.

Hobbyists are building them for the exact reason you state.
https://eater.net/8bit/

good luck
old jim
 
  • Like
Likes grandpa2390 and OmCheeto
  • #6
8,479
5,356
If you are curious enough to study, I recommend choosing the famous 6502 chip as your object.

A simple google search for 6502 will point you to numerous books, articles, emulators, circuit diagrams, and even fan clubs of people who study and celebrate the 6502. The 6502 was a very capable microcomputer, but much much simpler than today's CPUs. Those 6502 groups and forums are filled with people who share your interests. They can probably help you with very specific suggestions.

Once upon a time, I had all the 6502 op codes memorized, and I could read code in binary. Those were fun times. :biggrin:
 
  • Like
Likes phinds, sysprog, grandpa2390 and 2 others
  • #8
rcgldr
Homework Helper
8,682
520
Typically the opcode and/or options are used to index the equivalent of tables, which in turn result in running a sequence of micro -operations, and/or more table lookups. In some simple processors, there's a single table lookup with a large number of bits per entry, 1 bit for every possible operation. I recall a 16 micro based on AMD 2900 family of chips that only had just under 80 possible combinations of operations, so it use a table that was 80 bits wide, with only 1 bit set per entry.
 
  • Like
Likes grandpa2390
  • #9
8,479
5,356
Typically the opcode and/or options are used to index the equivalent of tables, which in turn result in running a sequence of micro -operations, and/or more table lookups.
CPUs with micro code add another level of abstraction. That's why I recommended the 6502. No micro code.
 
  • Like
Likes grandpa2390
  • #10
Drakkith
Staff Emeritus
Science Advisor
20,799
4,536
how the cpu works at the transistor level. not just a generic explanation you get in any course. but how exactly, physically, is the cpu processing.
You can start at the transistor level and work up from there by looking into Digital Logic. In my undergrad Digital Logic class we actually designed a very, very basic CPU. Unfortunately you just won't be able to really understand this stuff without spending a significant amount of time and effort with a good textbook on the subject. You can watch as many videos as you want, but until you get down and dirty solving digital logic problems you won't truly understand.
 
  • Like
Likes sysprog, grandpa2390, QuantumQuest and 1 other person
  • #11
191
107
The punch card is just in memory now, as a bunch on/off bits. The clock is cycling, fetching, decoding, executing, it’s built in. You have your programs instructions loaded in memory, you have a number pointing you to where the next one is. That number increments, or an instruction can change it.

It seams highly complex, but all of the components individually are fairly simple. How they are combined to make more complex machinery is also relatively simple, at each level. The key to managing the complexity is abstraction.
 
  • Like
Likes Klystron
  • #12
1,596
943
You can start at the transistor level and work up from there by looking into Digital Logic. ... You can watch as many videos as you want, but until you get down and dirty solving digital logic problems you won't truly understand.
Exactly. Many things what you just can't understand by a book will become common and trivial once you start using them. Don't try to expand your understanding without any practical experience.
 
  • Like
Likes jim hardy
  • #13
RPinPA
Science Advisor
Homework Helper
568
319
I don't know. it's weird to me. almost as if there must be a brain in there deciphering the code and carry out the orders.
It seems that way because there IS a brain in there deciphering the code and carrying out the orders.

A processor has a set of instruction codes it is designed to understand. A common design feature is also to have "registers", which are a few memory locations right on the chip. Things done on registers are very fast. Let's say you have four registers A, B, C and D. Then a built-in instruction might be ADD A, B, C (encoded in some binary format) which the processor is wired to understand as "Add what's in register A to what's in register B and store the result in register C". When the processor is executing that instruction, it invokes special addition circuitry which is also right on the chip, as well as the circuitry which reads and writes things to the desired places.

Then it gets the next instruction and does whatever that says. And the next. And the next.

You might want to pick up a book on assembly language for some processor, any processor, just to see what those fundamental instructions look like. Assembly language is the text version of the commands. The processor of course encodes them as 1's and 0's. Maybe ADD is instruction 0101 and registers A, B, C, D are written as binary 00, 01, 10, 11 so ADD A, B, C to the computer really looks like 0101 00 01 10 for example.

The binary language, what the CPU actually understands, is called "machine language". There isn't really a good reason to learn or understand that unless you're really curious. And these days there is very little call for people to work in the slightly more-readable assembly language either.
 
  • Like
Likes sysprog, grandpa2390, Klystron and 1 other person
  • #14
rbelli1
Gold Member
930
341
And these days there is very little call for people to work in the slightly more-readable assembly language either.
It's good to have at least a passing understanding of assembly. If you ever get into trouble debugging you might get dumped to an assembly view. Also if your code is not working you can compare the assembly to the C and it will show you what part of the C language you violated.

BoB
 
  • Like
Likes grandpa2390, Klystron, jim hardy and 1 other person
  • #15
vela
Staff Emeritus
Science Advisor
Homework Helper
Education Advisor
14,618
1,253
one thing that has always miffed me is how is it that the hardware carries out the coded instruction. the code is 1's and 0's, and if I have understood anything. 1's and 0's are on and off. but how is the computer flipping the circuits on and off? something must tell it to do that. but then what is telling it to follow those instructions. etc.

I am unfamiliar, but have friends to took circuits or logic circuits or something like that, and so I guess at the most basic level, logic circuits are used in a magical way to control everything. all code must carry with it instructions that activate those circuits?

I don't know. it's weird to me. almost as if there must be a brain in there deciphering the code and carry out the orders.
You might want to look into what's called a finite state machine. Also, you should probably concentrate on the gate level, rather than the transistor level.
 
  • Like
Likes sysprog, grandpa2390 and Klystron
  • #16
harborsparrow
Gold Member
534
108
There are newer editions, but this Tanenbaum book from 1998 does a good job of explaining how it all happens, from start to finish. At least, it would help with a conceptual understanding. This older edition can be had for a mere $12 used on Amazon: https://www.amazon.com/dp/0130959901/?tag=pfamazon01-20
 
  • Like
Likes grandpa2390, Klystron and berkeman
  • #17
169
72
If you are curious enough to study, I recommend choosing the famous 6502 chip as your object.
I preferred the 6809. Ahh, assembly language programming on the Color Computer....
 
  • #18
Tom.G
Science Advisor
3,170
1,907
at the most basic level, logic circuits are used in a magical way to control everything. all code must carry with it instructions that activate those circuits?
Yup.

For a reasonably basic look starting at the transistor level:
http://www.circuitstoday.com/logic-gates
(above. and much more, found with: https://www.google.com/search?&q=logic+gate+circuit+diagram)

An early bible for the computer operation (and more) is THE ART OF COMPUTER PROGRAMMING, Volume 1, Fundamental Algorithms, by Donald E. Knuth, Addison-Wesley Publishing Company, ISBN 0-201-03809-9. (that's the Second edition, I believe there are later ones.)

Digital Logic is based on signals that are either On or Off (One or Zero, True or False); just like your refrigerator is either On or Off.

Then realize that any logical operation (or mathematical for that matter) can be created from the three logical operations of AND, OR, NOT. (can also be done with other operations, but these are the most used and simple enough to keep straight) (That's the field called Boolean Algebra, quite useful when you get in a bit deeper)

AND = output is True if all inputs are True
OR = output is True if any one or more inputs are True
NOT = output is True if input is False; output is False if input is True (i.e. the output is the complement of the input

Hope this helps.
Cheers,
Tom
 
  • Like
Likes grandpa2390, FactChecker and Klystron
  • #19
1,343
713
You can start at the transistor level and work up from there by looking into Digital Logic. In my undergrad Digital Logic class we actually designed a very, very basic CPU. Unfortunately you just won't be able to really understand this stuff without spending a significant amount of time and effort with a good textbook on the subject. You can watch as many videos as you want, but until you get down and dirty solving digital logic problems you won't truly understand.
The (free with little ad bar on its own screen only) Android app game Circuit Scramble (< direct download link from apkpure) is fun and instructive for neophytes in the workings of logic gates.
 
Last edited:
  • Like
Likes Drakkith

Related Threads on How exactly do computers execute code?

Replies
6
Views
5K
  • Last Post
Replies
14
Views
421
Replies
11
Views
2K
Replies
2
Views
610
  • Last Post
Replies
8
Views
2K
  • Last Post
Replies
2
Views
2K
Replies
4
Views
765
Replies
4
Views
24K
Replies
4
Views
25K
Replies
3
Views
11K
Top