Profile icon
IMayBeMe

### Introduction.css-1q3m8ps{margin-left:var(--space-4);margin-right:var(--space-4);display:none;}

I'm not too big of a fan of introductions as I feel it takes away from the content of the tutorial and is more so just padding to make it longer. So going with this theme, I'll just explicitly list the objective of this tutorial down below:

To explain how a computer runs a simple program on a very low level (lower than assembly).

This may seem lame at first but trust me, there is a surprising amount of complexity, just to run a simple program which I hope to explore to the fullest extent.

### The Computer (specifications).css-1q3m8ps{margin-left:var(--space-4);margin-right:var(--space-4);display:none;}

Now that we have moved past the boring part, let's get into explaining the computer on which we will be running this program. One of the major issues with trying to use a modern computer is just the sheer complexity and size, something that is very hard for a beginner to wrap their head around. Keeping that in mind, I have devised a simple hypothetical computer, which I'll refer to as YAX (thought it would sound cool, but no actual purpose behind the name) for the rest of the tutorial.

YAX is a basic computer consisting of 4 special registers, 4-bit memory addresses, and a total of 4 opcodes. This could be a lot to unpack for many of you so let me take some time to walk through this step by step.

First up we have the 4 special registers. These are essentially 1 byte (8 bits) wide and are used for temporarily storing data throughout the program. For this tutorial, let's call them registers A, B, C, and D.

Second, we have the 4-bit memory addresses. For those of you not familiar with binary, 4 bit means that we can have only 4 bits in each address, making 1111 (15) the biggest unsigned binary number that we can use. Based on this we will have a total of 15 memory addresses, each containing 1 byte of data.

Lastly, we have the opcodes which for now will be limited to four simple instructions. These four have been listed down below in a table:

| Opcode | Instruction | Function |
: ---------|:------------------|:---------|
| 0001| LOAD_A \$7|Loads the memory address 7 into register A
| 0010| LOAD_B \$8|Loads the memory address 8 into register B
| 0011| ADD_A #B|Adds the contents of register B into register A
| 0100| STORE_A \$9|Stores the value of register A into memory address 9

Technically speaking, the memory addresses and the registers would be accepted as changeable arguments, but once again for the simplicity of the tutorial I have chosen to stick with just plain arguments.

### The Computer Again (graphical).css-1q3m8ps{margin-left:var(--space-4);margin-right:var(--space-4);display:none;}

Hopefully, by now you have read the last section of the tutorial in which I attempted to explain YAX, more so from a technical standpoint than a graphical one. I get how difficult this could be for someone to wrap their head around so considering that I have decided to also create a diagram of YAX below:

So to walk you through the diagram, you can see the 4 special registers labeled at the top all connected to a data wire stemming from the RAM. Now underneath the registers, there is an ALU that essentially does all the math and logic on the computer. I don't want to go too deep into the ALU but for the sake of this tutorial, let's just say it will be doing the math. Next to we have the control unit which is like the brain of a computer. The control unit is in charge of running the programs and interacting with the RAM which is outside of the CPU. Personally, I like to think of it as a hub for all the information to be processed through and sent to its destination.

I could go on a lot further with this but I'm trying to keep it as a mini-tutorial so I better move on to the program itself.

### The Program.css-1q3m8ps{margin-left:var(--space-4);margin-right:var(--space-4);display:none;}

The program as the name suggests is what we will be running on YAX. Long story short the program will pretty much be adding 2 numbers together and storing them in a memory address. It would look something like this:

```.css-19sk4h4{position:relative;}.css-1bu6gr6{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:relative;}.css-1n2m10r{padding:var(--space-8);border-radius:var(--border-radius-4);background-color:var(--background-higher);}.css-1hwur6u{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;padding:var(--space-8);border-radius:var(--border-radius-4);background-color:var(--background-higher);}.css-1ubbl1f{padding:var(--space-4);padding-left:var(--space-4);padding-right:var(--space-2);font-family:var(--font-family-code);font-size:14px;line-height:var(--line-height-small);overflow-x:auto;word-break:break-word;white-space:break-spaces;overflow-wrap:anywhere;}```LOAD_A \$7
STORE_A \$9``````

As far as the symbols in the code above go, any number after a `\$` signifies a memory address while anything after a `#` shows a register.

Taking that into consideration we can tell that the first line of code loads the contents of memory address 7 into register A. The second line of code does the same thing except with register B and a different memory address.

The third line of code which is `ADD_A #B` is a bit more complex but is in essence adding the contents of register B into A thus changing the value of register A but not B.

The final line is hopefully pretty self-explanatory but all it does is store whatever is in register A into the memory.

I also feel like it's pretty important to give you a look at the memory before we run the program so here it is:

| Address | Content |
: ---------|:-------
|4|STORE_A \$9|
|5|0|
|6|0|
|7|3|
|8|5|
|9|0|
|10|0|
|11|0|
|12|0|
|13|0|
|14|0|
|15|0|

Moving on let's get to running the program.

### Running the Program.css-1q3m8ps{margin-left:var(--space-4);margin-right:var(--space-4);display:none;}

I think that the actual running of the program would be simplest to do in bullet form, so I think I'll go with that.

Line 1 `LOAD_A \$7`:

• The control unit reads from address 1 and determines the opcode.
• The control unit then enables the read wire on the RAM and uses the multiplexer to select the contents of address 7.
• These contents rea passed along the data wire until it reaches and is stored in register A.

Line 2 `LOAD_B \$8`:

• The control unit reads from address 3 and determines the opcode.
• The control unit then enables the read wire on the RAM and uses the multiplexer to select the contents of address 8.
• These contents rea passed along the data wire until it reaches and is stored in register B.

And yes the second line is just the first one copied and pasted.

Line 3 `ADD_A #B`:

• The control unit reads from address 3 and determines the opcode.
• The control unit then passed the values stores in registers A and B into the 2 inputs of the ALU.
• The control unit then sets the operation on the ALU to add which will require no extra processing.
• The ALU will then proceeds to add the 2 values together before being instructed to pass the sum into register A.

Line 4 `STORE_A \$9`:

• The control unit reads from address 4 and determines the opcode.
• The control unit then enables the write wire on the RAM and uses the multiplexer to select the contents of address 9.
• The contents of register A are then passed along the data wire and into address 9, therefore, achieving our sum of 8.

### Conclusion.css-1q3m8ps{margin-left:var(--space-4);margin-right:var(--space-4);display:none;}

That's all for today and I hope you enjoyed this mini tutorial and maybe even learned something new about computers from it. Please comment with any sort of feedback/comments and questions are always appreciated.

You are viewing a single comment. View All
Profile icon
IMayBeMe

@Xshoury this is the exact feedback I was looking for, thank you! I will definitely get around to making more tutorials on CPUs and will probably have an ALU one come out first as it makes it easier for me to build onto it.