How A Computer Works
1 Learning Objectives
By the end of the lesson…
- All Should Be able to describe how a CPU carries out instructions via the LMC.
- Most Should Be able to write a program in assembly language to subtract two numbers.
- Some Could Be able to create an algorithm to simulate the Fibonacci Sequence.
- Some Could Be able to write a program in assembly language from their Fibonacci Sequence algorithm.
2 More than just data
Learn It
- In the last lesson you saw how we can write bits of data to RAM.
- So if we have an address that is 8 bits in length we can write values up to 255 (11111111) to any given address in RAM.
- In reality, modern computers have far larger address spaces, and 64bit of data (and more) can be written to a single address.
Learn It
- The bits that are written to an address are not always considered data though.
- Sometimes when a CPU is accessing RAM, the bits in an address represent an instruction rather than just a number.
3 The Little Man Computer
Learn It
- Below is a simulation of how a CPU can carry out instructions.
- For now, ignore the section on the left titled Assembley Language Code.
- The section titled RAM is where we'll focus.
- When the program starts, the Program Counter is set to 0. The CPU then fetches an instruction from address 0
- After each instruction is executed, the program counter is increased by 1.
- This means the next address location is read.
- Sometimes an instruction can branch, meaning the program counter jumps to another address.
- Here are a list of the instructions that can be accepted.
Instruction | What it does |
---|---|
1xx | Add value in address xx to the value in the accumulator |
2xx | Subtract value in address xx from the value in the accumulator |
3xx | Store the value of the accumulator to address xx |
5xx | Load the value from address xx to the accumulato |
6xx | Set the program counter to address xx |
7xx | If the accumulator contains the value 0 then set the counter to address xx |
8xx | If the accumulator contains a value greater than 0 then set the counter to address xx |
901 | Store user input into the accumulator |
902 | Output the value in the accumulator |
000 | Halt the program |
Try It
- Lets write a little program using these instructions and see what happens.
- Fill in the RAM with the instructions as shown in the table below:
Address | Instruction | What it will do |
---|---|---|
00 | 901 | Take input and load it into the accumulator |
01 | 902 | Output the value from the accumulator |
-It should look like this
- You must hit
ENTER
after inputing each code into the RAM address.
- Run your program by hitting the
RUN
button and see what it does. - Watch the little Android. She'll tell you what is happening.
- If you want to run it again, you'll need to hit the
RESET
, or you can change the Program Counter to0
4 Assessment
Badge It - Silver
- Write down what happens when you run the simple program. You can use the list below as a start.
- The value in the
PROGRAM COUNTER
is sent toARITHMETIC UNIT
and to that address in RAM. - The value in the
ARITHMETIC UNIT
is increased by 1 and sent back to thePROGRAM COUNTER
- The value at address 0 in RAM is sent to the the
INSTRUCTION REGISTER
and theADDRESS REGISTER
- The value in the
5 Some Basic Maths.
Try It
- Let's try something a little more complicated now.
- Add these values into the RAM addresses and hit
RUN
Address | Instruction | What it will do |
---|---|---|
00 | 901 | Take input and load it into the accumulator |
01 | 350 | Store the input value in address 50 |
02 | 901 | Take input and load it into the accumulator |
03 | 150 | Add the value in address 50 to the value in the accumulator |
04 | 902 | Output the value from the accumulator |
- It should look like this
- Can you explain what is happening?
6 Assessment
Badge It - Gold
- Can you write a program that will subtract two numbers.
- So if the first value A is provided, then a second value B is provided, then A-B will be output.
- Try it with the first value entered of 10 and the second of 6, and make sure that 4 is output.
7 Counting with the LMC
- Let's try counting with the LMC down from ten.
Address | Instruction | What it will do |
---|---|---|
00 | 901 | Take input and load it into the accumulator |
01 | 350 | Store the input in address 50 |
02 | 550 | Load the data from address 50 |
03 | 251 | Subtract the value in address 51 |
04 | 350 | Store the value at address 50 |
05 | 902 | Output value |
06 | 802 | If value is greater than zero, set PC to address 02 |
51 | 1 | Value to be subtracted |
8 Assessment
Badge It - Platinum
- The Fibonacci Sequence can be produced by starting with two number 1s
1,1
- You then add these numbers to get the next value in the sequence.
1,1,2
- You can now add 1 + 2 to get the next.
1,1,2,3
- And keep going.
1,1,2,3,5,8,13...
- Can you write an LMC program that outputs the Fibonacci Squence.
- (HINT the code 6 will reset the
PROGRAM COUNTER
to what ever address you like. For instance 600 will reset it to address 000)