LMC Lesson 2 Objectives
Understand how the CPU processes data
Write a simple program which outputs a number input by a user using the LMC
Write a simple program which outputs a number input by a user using the LMC
New Learning
The LMC is a very powerful tool for tackling a whole range of Computing topics in a practical fashion.
•Assembly Language / 2nd generation programming
•Machine Code / 1st generation programming
•The Fetch/Execute Cycle
•The Stored Program Concept
•Registers We will be programming the Little Man Computer using Assembly Language but taking a look at the Machine Code too.
How to create programs
An “executable file” ie program can be crafted from scratch using machine code (1st level language), which looks like this...
0100100100010010111101110111110111101010010010010000101110100101
and is completely unintelligible!
But it is far more usual to develop software as source code in a high-level language (3rd level language) easily understood by humans, and then “compile” it into an EXE file, ie into machine code.
In some cases assembly language (2nd level language), which is more complex for humans, can be used to create programs from scratch.
Assembly language uses MNEMONICS and numbers to replace the 1's and 0's. What an assembler does is convert these MNEMONICS and numbers into their binary equivalent machine code.
•Assembly Language / 2nd generation programming
•Machine Code / 1st generation programming
•The Fetch/Execute Cycle
•The Stored Program Concept
•Registers We will be programming the Little Man Computer using Assembly Language but taking a look at the Machine Code too.
How to create programs
An “executable file” ie program can be crafted from scratch using machine code (1st level language), which looks like this...
0100100100010010111101110111110111101010010010010000101110100101
and is completely unintelligible!
But it is far more usual to develop software as source code in a high-level language (3rd level language) easily understood by humans, and then “compile” it into an EXE file, ie into machine code.
In some cases assembly language (2nd level language), which is more complex for humans, can be used to create programs from scratch.
Assembly language uses MNEMONICS and numbers to replace the 1's and 0's. What an assembler does is convert these MNEMONICS and numbers into their binary equivalent machine code.
There are two ways to program the Little Man Computer - Assembly Language and Machine Code. Assembly Language written codes of mnemonics (e.g. INP means input, STA means store). Machine Code is a numeric representation of the same codes, called opcodes (e.g. 901 means input, 399 means store*). You can program the LMC by typing lines of mnemonics into the message box or by typing the numeric codes directly into the memory blocks on the left hand side. Every processor type has its own type of assembly language - most of which are significantly more complex than the LMC, which is designed as an educational tool rather than a programming platform. * Technically 399 means store in memory address 99
(Virtual) Hardware for the LMC - 5 components:
INPUT TRAY: Where you give numerical input to the Little Man.
OUTPUT TRAY Where the Little Man leaves a number for you.
CALCULATOR The Arithmetic Logic Unit (ALU) where numbers are accessed, added, and subtracted. All numbers must go through the ALU on their way to any other location.
MEMORY Where the program instructions and data are stored.
PROGRAM Keeps track of the location where the Little
COUNTER Man must look for the next instruction. The Little Man
ALWAYS STARTS by getting the number in the counter, then incrementing the counter.
The 'Little Man' performs the following steps to execute a program:
1.Check the Program Counter so it knows the memory address to look at.
2.Fetch the instruction from the memory address that matches the program counter.
3.Increment the Program Counter (so that it contains the memory address of the next instruction).
4.Decode the instruction (includes finding the memory address for any data it refers to).
5.If required by the instruction code, fetch the data from the memory address found in the previous step.
6.Execute the instruction and if necessary set the Program Counter to match any branch instructions.
INPUT TRAY: Where you give numerical input to the Little Man.
OUTPUT TRAY Where the Little Man leaves a number for you.
CALCULATOR The Arithmetic Logic Unit (ALU) where numbers are accessed, added, and subtracted. All numbers must go through the ALU on their way to any other location.
MEMORY Where the program instructions and data are stored.
PROGRAM Keeps track of the location where the Little
COUNTER Man must look for the next instruction. The Little Man
ALWAYS STARTS by getting the number in the counter, then incrementing the counter.
The 'Little Man' performs the following steps to execute a program:
1.Check the Program Counter so it knows the memory address to look at.
2.Fetch the instruction from the memory address that matches the program counter.
3.Increment the Program Counter (so that it contains the memory address of the next instruction).
4.Decode the instruction (includes finding the memory address for any data it refers to).
5.If required by the instruction code, fetch the data from the memory address found in the previous step.
6.Execute the instruction and if necessary set the Program Counter to match any branch instructions.
Task
INP
STA first
INP
STA second
LDA first
ADD second
OUT
HLT
first DAT
second DAT
1)What does this code do?
2)Load Notepad / Notepad++ and add these mnemonics above to your new file.
3)NB - if there is an error in the program then the compiler will fail and the code will be lost.
4)Copy (CTRL-C) and Paste (CTRL-V) the program from Notepad++ into the assembly code in the LMC
5)Then “Compile Program”
6)Click Slow to run it slowly
7)OK?
8)Write LMC code that asks the user for three numbers, adds them and prints out the answer.
Line By Line
INP <-- Prompt the user for an input [stored in accumulator temporarily]
STA first <-- Store the answer [currently in accumulator] in a variable called first
INP <-- Prompt the user for an input [stored in accumulator temporarily]
STA second <-- Store the answer [currently in accumulator] in second
LDA first <-- Load the number in variable first back into the accumulator
ADD second <-- Add the contents of second to whatever is in the accumulator
OUT <-- Put the contents of the accumulator into the out-box
HLT <-- End of program
first DAT <-- Declare that first is data [i.e. a variable]
second DAT <-- Declare that second is data [i.e. a variable]
The LMC compiler (technically an assembler) converts each mnemonic into an opcode.
INP <-- 901 [Input]
STA first <-- 308 [Store in memory address 09]
INP <-- 901 [Input]
STA second <-- 309 [Store in memory address 10]
LDA first <-- 508 [Load the data from memory address 09]
ADD second <-- 109 [Load the data from memory address 10]
OUT <-- 902 [Output]
HLT <-- 0 [End of program]
first DAT <-- [Declare first as a variable]
second DAT <-- [Declare second as a variable]
General Notes
Points to Note
Step Through
By clicking the step button the program will execute one line at a time. You can watch the following boxes to see how the registers inside the processor work.
Accumulator <-- Temporarily stores the most current piece of data
Program Counter <-- Keeps track of which instruction to carry out next
MEM Address <-- Points to the currently addressed memory block
MEM Data <-- Loads the contents of the addressed memory
In-Box <-- Used for data input
Out-Box <-- Used for data output
- The LMC is not case sensitive. It is a good idea to write mnemonics and variables using different cases to make it easier to read, but the compiler does not notice the difference.
- Variables are declared at the end of the program, rather than the start. This is so the compiler can assign memory addresses immediately after the program instructions - it doesnʼt know which addresses are free initially.
- If there is an error in the program then the compiler will fail and the code will be lost. It is therefore a good idea to copy (CTRL-C) the program before you compile. This is especially true with more complex programs.
- Each mnemonic is converted to one opcode - there is a 1:1 relationship. This is one of the major differences between an assembler and a compiler.
Step Through
By clicking the step button the program will execute one line at a time. You can watch the following boxes to see how the registers inside the processor work.
Accumulator <-- Temporarily stores the most current piece of data
Program Counter <-- Keeps track of which instruction to carry out next
MEM Address <-- Points to the currently addressed memory block
MEM Data <-- Loads the contents of the addressed memory
In-Box <-- Used for data input
Out-Box <-- Used for data output
INP
STA FIRST
INP
STA SECOND
LDA FIRST
SUB SECOND
BRZ SAME
LDA ONE
OUT
HLT
SAME OUT
HLT
FIRST DAT
SECOND DAT
ONE DAT 1
1)What does this code do?
2)IF statements work by having a logical test (e.g. is 7 > 3) and then sending the program down one of two routes. In the LMC we use the branch command to achieve this outcome . There are three branch commands
1)Branch If Zero (BRZ)
2)Branch If Zero Or Positive (BRP)
3)Branch Always (BRA)
3)In this program we want to find if two numbers are equal. If they are then subtracting them will give the answer 0
4)The word SAME is used a label, to point where the branch will go
5)Both branches require a HLT command
6)It is possible to use multiple branch statements to create multiple IF/ELSE statements or CASE statements
7)Planning programs with a Flowchart is recommended!
STA FIRST
INP
STA SECOND
LDA FIRST
SUB SECOND
BRZ SAME
LDA ONE
OUT
HLT
SAME OUT
HLT
FIRST DAT
SECOND DAT
ONE DAT 1
1)What does this code do?
2)IF statements work by having a logical test (e.g. is 7 > 3) and then sending the program down one of two routes. In the LMC we use the branch command to achieve this outcome . There are three branch commands
1)Branch If Zero (BRZ)
2)Branch If Zero Or Positive (BRP)
3)Branch Always (BRA)
3)In this program we want to find if two numbers are equal. If they are then subtracting them will give the answer 0
4)The word SAME is used a label, to point where the branch will go
5)Both branches require a HLT command
6)It is possible to use multiple branch statements to create multiple IF/ELSE statements or CASE statements
7)Planning programs with a Flowchart is recommended!
Task
Draw the above code as a flow chart in powerpoint. Try not to look at the solution below if you can.
solution_to_flow_chart.png | |
File Size: | 37 kb |
File Type: | png |
Further Independent Tasks
Create some more complicated programs using the LMC Write LMC programs to complete the following tasks. You will need to use the BRZ, BRP and BRA commands here as well now....
1.Start off with your JUMP.LMC code. Edit it to ask the user for two numbers. If they are the same then print 0, otherwise add them and print out the answer. ZERO.LMC (Hint - use BRZ to find out if they are the same)
2.Ask the user for two numbers and print out the biggest. BIG.LMC (Hint – use BRP to work out which is largest)
1.Start off with your JUMP.LMC code. Edit it to ask the user for two numbers. If they are the same then print 0, otherwise add them and print out the answer. ZERO.LMC (Hint - use BRZ to find out if they are the same)
2.Ask the user for two numbers and print out the biggest. BIG.LMC (Hint – use BRP to work out which is largest)
Extention
3.EXTENSION - Ask the user for three numbers and print out the biggest one. BIGGEST.LMC
"Little Man Computer" by Mark Clarkson is licensed under CC BY 2.0
Little Man Computer presentation slides by John Palmer is licensed under CC BY 2.0
"Little Man Computer" by Mark Clarkson is licensed under CC BY 2.0
Little Man Computer presentation slides by John Palmer is licensed under CC BY 2.0