Mastering LMC Coding Part #1 Introduction to Low Level Languages Introduction to Little Man computer Simple examples (demos) with video tutorials included Further explanation and examples of LMC working examples Demonstration of INP, OUT, HLT Demonstration of LDA, STA (and how they work) Making sense of the numbers as instructions How to LOAD and SAVE How to ADD and SUBTRACT
What is LMC? (Little Man Computer) The Little Man Computer (LMC) is an instructional model of a computer, created by Dr. Stuart Madnick in assembly code The LMC is generally used to teach students, because it models a simple von Neumann architecture computer - which has all of the basic features of a modern computer. It can be programmed in machine code (in decimal rather than binary) or in assembly code.
REALLY exploring the inside of a computer Getting to grips with the LMC will help you really understand what goes on inside a computer processor – so it’ll be worth it! You’d be programming in a low level language (much closer to machine code; in fact each line translates directly into machine code) so it’ll look a little different!
Von Neumann Architecture? You may want to view the presentation on this topic, but as a brief summary, Von Neumann architecture describes the design of a computer that consists of: 1.A processing unit 2...which contains an ALU* and registers 3.Control Unit (containing instruction register) 4.Program Counter 5.A Memory to store both data and instructions 6.External mass storage 7.Input and output mechanisms *The definition has evolved to simply mean any stored-program computer in which an instruction fetch and data operation cannot occur at the same time (as they share the same bus). 1.A processing unit 2...which contains an ALU* and registers 3.Control Unit (containing instruction register) 4.Program Counter 5.A Memory to store both data and instructions 6.External mass storage 7.Input and output mechanisms *The definition has evolved to simply mean any stored-program computer in which an instruction fetch and data operation cannot occur at the same time (as they share the same bus).
Why is it called “Little man computer”? The LMC model is based on the idea of a little man shut in a small room (or a computer, in this scenario) This is the LMC Model we will be using for the purposes of this tutorial. Please follow the link to try the tasks and check it out for yourself: This is the LMC Model we will be using for the purposes of this tutorial. Please follow the link to try the tasks and check it out for yourself: At one end of the “room”, there are 100 mailboxes (memory), numbered 0 to 99, that can each contain a 3 digit instruction or data (ranging from 000 to 999) Furthermore, there are two mailboxes at the other end labelled INBOX and OUTBOX which are used for receiving and outputting data.
Accumulator and Program Counter? Think of the Accumulator like a ‘work area’ which contains a simple calculator. The program counter is a resettable counter and it holds the address of the next instruction that the little man will carry out. This Program Counter is normally incremented by 1 after each instruction is executed, allowing the Little Man to work through a program sequentially.
What about instructions and doing stuff!? Branch instructions allow iteration (loops) and conditional programming structures to be incorporated into a program. The latter would be implemented by setting the Program Counter to a non-sequential memory address if a particular condition is met (typically the value stored in the accumulator being zero or positive).
What do we need to look out for? Remember in Von Neumann architecture, memory contains both instructions and data. Care therefore needs to be taken to stop the Program Counter reaching a memory address containing data or the Little Man will attempt to treat it as an instruction Remember in Von Neumann architecture, memory contains both instructions and data. Care therefore needs to be taken to stop the Program Counter reaching a memory address containing data or the Little Man will attempt to treat it as an instruction
How to use the LMC? To use the LMC the user loads data into the mailboxes and then signals the Little Man to begin execution, starting with the instruction stored at memory address zero Resetting the Program Counter to zero effectively restarts the program (potentially in a different state.
Decimal or Binary … While the LMC does reflect the actual workings of binary processors, the simplicity of decimal numbers was chosen to minimize the complexity for students who may not be comfortable working in binary/hexadecimal.
Instruction Set in LMC Some LMC simulators are programmed directly using 3-digit numeric instructions and some use 3- letter mnemonic codes and labels. In either case, the instruction set is deliberately very limited (typically about ten instructions) to simplify understanding If the LMC uses mnemonic codes and labels then these are converted into 3-digit numeric instructions when the program is assembled. The first digit of a numeric instruction represents the command to be performed and the last two digits represent the memory address of the mailbox relevant to that command. Some typical instructions
A closer look at an instruction in LMC
Things we can do in the LMC Input and Output The Instruction set may seem limited, but you can do pretty much everything in the LMC Here are some of the things we will be looking at in these tutorials: Using Memory Adding and Subtracting Making Decisions Multiply two numbers This PowerPoint contains information on how to do all of the things in Red Watch Part 2 to find out how to use LOOPS in LMC, make decisions and multiply two numbers.
Watch the video on the right and let’s analyse what is happening LMC Part #1 Video 1
Analysis of the code and execution What you should do 1.Click on the "LMC Simulator Applet" link to start the LMC simulator. 2.Clear the Message Box and all of the LMC mailboxes -- click the "Clear Messages" button and the "Clear" button if necessary. 3.Copy the three-line program (INP, OUT, HLT) and paste it into the Message Box 4.Click on the "Compile Program" button. 5.Click on the "Run" button. 6.When prompted, enter a three-digit number in the "In-Box", and press the "Enter" button. What you should do 1.Click on the "LMC Simulator Applet" link to start the LMC simulator. 2.Clear the Message Box and all of the LMC mailboxes -- click the "Clear Messages" button and the "Clear" button if necessary. 3.Copy the three-line program (INP, OUT, HLT) and paste it into the Message Box 4.Click on the "Compile Program" button. 5.Click on the "Run" button. 6.When prompted, enter a three-digit number in the "In-Box", and press the "Enter" button. What you should see After the program is compiled, you should see 901 in mailbox 0 and 902 in mailbox 1. The Program Counter should start at 0 (click on "Reset" if necessary). When you click on "Run" or "Step", the Message Box will describe the actions of each instruction. After the INP instruction, the Accumulator has a copy of the value in the In Box. After the OUT instruction, the Out Box has a copy of the value in the Accumulator. The final value of the Program Counter is 2. This mailbox holds 0 - the machine code instruction that tells the LMC to stop executing your program. What you should see After the program is compiled, you should see 901 in mailbox 0 and 902 in mailbox 1. The Program Counter should start at 0 (click on "Reset" if necessary). When you click on "Run" or "Step", the Message Box will describe the actions of each instruction. After the INP instruction, the Accumulator has a copy of the value in the In Box. After the OUT instruction, the Out Box has a copy of the value in the Accumulator. The final value of the Program Counter is 2. This mailbox holds 0 - the machine code instruction that tells the LMC to stop executing your program. INPOUT
A few notes Program Information Running Program Program Counter : 0 Getting INPUT from user Input : 3--- Program Counter : 1 OUTPUTing accumulator (3) --- Program Counter : 2 HALT! Program Information Running Program Program Counter : 0 Getting INPUT from user Input : 3--- Program Counter : 1 OUTPUTing accumulator (3) --- Program Counter : 2 HALT! The final value of the Program Counter is 2. This mailbox holds 0 the machine code instruction that tells the LMC to stop executing your program. After the INP instruction, the Accumulator has a copy of the value in the In Box. After the OUT instruction, the Out Box has a copy of the value in the Accumulator. After the INP instruction, the Accumulator has a copy of the value in the In Box. After the OUT instruction, the Out Box has a copy of the value in the Accumulator.
You’ll never really understand it until you try it yourself. You’ll never really understand it until you try it yourself.
LMC – how to load and save instructions Program INP STA FIRST INP STA SECOND LDA FIRST OUT LDA SECOND OUT HLT FIRST DAT SECOND DAT Program INP STA FIRST INP STA SECOND LDA FIRST OUT LDA SECOND OUT HLT FIRST DAT SECOND DAT What you should do 1.Click on the "LMC Simulator Applet" link to start the LMC simulator. 2.Clear the Message Box and all of the LMC mailboxes -- click the "Clear Messages" button and the "Clear" button if necessary. 3.Copy the eleven line program above and paste it into the Message Box 4.Click on the "Compile Program" button. 5.Click on the "Run" button. 6.When prompted, enter three-digit numbers in the "In-Box", and press the "Enter" button. What you should do 1.Click on the "LMC Simulator Applet" link to start the LMC simulator. 2.Clear the Message Box and all of the LMC mailboxes -- click the "Clear Messages" button and the "Clear" button if necessary. 3.Copy the eleven line program above and paste it into the Message Box 4.Click on the "Compile Program" button. 5.Click on the "Run" button. 6.When prompted, enter three-digit numbers in the "In-Box", and press the "Enter" button. What you should see 1.After the program is compiled, you should see from mailbox 0 to 7 the instructions 901, 309, 901, 310, 509, 902, 510, 902. The Program Counter should start at 0 (click on "Reset" if necessary). 2.DAT is the tenth and eleventh instruction of your program, so they refer to mailboxes 9 and 10 (0-indexed counting). FIRST and SECOND are the identifiers that have been declared to represent these mailboxes in the assembly language program. 3.When you click on "Run" or "Step", the Message Box will describe the actions of each instruction. 4.After the first INP instruction, the Accumulator has a copy of the first value entered in the In Box. 5.After the STA instruction, the input value is copied from the Accumulator to mailbox means STORE and 09 refers to the mailbox to store into. 6.After the second INP instruction, the Accumulator has a copy of the second value entered into the In Box (use a different value than the first). 7.After the second STA instruction, the second input value is copied from the Accumulator to mailbox After the LDA instruction, the Accumulator is reset to the first input value. This value has been retrieved from mailbox means LOAD and 09 refers to the mailbox to load from. Note: since your Accumulator can only work on one value at a time, you have to repeatedly STORE and LOAD values from memory to keep them from getting erased by the next operation 9.After the OUT instruction, the Out Box has a copy of the value in the Accumulator -- the first input value. *All this will be further explained –don’t give up yet! What you should see 1.After the program is compiled, you should see from mailbox 0 to 7 the instructions 901, 309, 901, 310, 509, 902, 510, 902. The Program Counter should start at 0 (click on "Reset" if necessary). 2.DAT is the tenth and eleventh instruction of your program, so they refer to mailboxes 9 and 10 (0-indexed counting). FIRST and SECOND are the identifiers that have been declared to represent these mailboxes in the assembly language program. 3.When you click on "Run" or "Step", the Message Box will describe the actions of each instruction. 4.After the first INP instruction, the Accumulator has a copy of the first value entered in the In Box. 5.After the STA instruction, the input value is copied from the Accumulator to mailbox means STORE and 09 refers to the mailbox to store into. 6.After the second INP instruction, the Accumulator has a copy of the second value entered into the In Box (use a different value than the first). 7.After the second STA instruction, the second input value is copied from the Accumulator to mailbox After the LDA instruction, the Accumulator is reset to the first input value. This value has been retrieved from mailbox means LOAD and 09 refers to the mailbox to load from. Note: since your Accumulator can only work on one value at a time, you have to repeatedly STORE and LOAD values from memory to keep them from getting erased by the next operation 9.After the OUT instruction, the Out Box has a copy of the value in the Accumulator -- the first input value. *All this will be further explained –don’t give up yet!
How to load and save: Video demo Program INP STA FIRST INP STA SECOND LDA FIRST OUT LDA SECOND OUT HLT FIRST DAT SECOND DAT Program INP STA FIRST INP STA SECOND LDA FIRST OUT LDA SECOND OUT HLT FIRST DAT SECOND DAT LMC Part #1 Video 2
Program with Output 1.After the program is compiled, you should see from mailbox 0 to 7 the instructions 901, 309, 901, 310, 509, 902, 510, 902. The Program Counter should start at 0 (click on "Reset" if necessary). 2. DAT is the tenth and eleventh instruction of your program, so they refer to mailboxes 9 and 10 (0-indexed counting). FIRST and SECOND are the identifiers that have been declared to represent these mailboxes in the assembly language program. 3. After the first INP instruction, the Accumulator has a copy of the first value entered in the In Box 4. After the STA instruction, the input value is copied from the Accumulator to mailbox means STORE and 09 refers to the mailbox to store into
Look at the STA instruction more closely. INP = 901 (this refers to the first number that will be input by the user) STA FIRST = The code for ‘Store’ is 3. (that’s the first part of this instruction). The second part is 09 – and this is the memory location that LMC needs to store the INPUT number into. In this scenario, my input number was “200” – and as you can see it has been stored in memory location 09! STA SECOND = This is referring to the second INPUT number that the user enters (I had entered 100 as my second input). The 3 means ‘store’, and the second part is ‘10’ – meaning, store ‘100’ in memory location 10 – there it is!
Making sense of LDA and OUT After the LDA instruction, the Accumulator is reset to the first input value. This value has been retrieved from mailbox 09 This instruction is also made of two parts. [509] The 5 means LOAD and 09 refers to the mailbox to load from. Note: since your Accumulator can only work on one value at a time, you have to repeatedly STORE and LOAD values from memory to keep them from getting erased by the next operation After the OUT instruction, the Out Box has a copy of the value in the Accumulator -- the first input value.
Remember the numbers are instructions! English isn’t understood here! So the 5, in this case, means ‘Store’ and the 09 is the location where the input must be stored! As mentioned on the previous slide, if my INPUT was 200, then you’d see the INPUT (200) stored in Location 9. There it is …
A run through demonstrating the numbers linked to their corresponding instruction INP “200” is input STA First INP “100” is input STA Scnd LDA Frst *Load the first Number in 09 to the Accumulator OUT 200 is output! LDA Scnd *Load the second Number in 10 to the accumulator OUT HLT FIRST DAT SECOND DAT Remember in Von Neumann architecture, memory contains both instructions and data. Care therefore needs to be taken to stop the Program Counter reaching a memory address containing data or the Little Man will attempt to treat it as an instruction. THE HLT INSTRUCTION was useful! Remember in Von Neumann architecture, memory contains both instructions and data. Care therefore needs to be taken to stop the Program Counter reaching a memory address containing data or the Little Man will attempt to treat it as an instruction. THE HLT INSTRUCTION was useful!
Adding in LMC *try it yourself Program INP STA FIRST INP ADD FIRST OUT INP SUB FIRST OUT HLT FIRST DAT Program INP STA FIRST INP ADD FIRST OUT INP SUB FIRST OUT HLT FIRST DAT What you should do 1.Click on the "LMC Simulator Applet" link to start the LMC simulator. 2.Clear the Message Box and all of the LMC mailboxes -- click the "Clear Messages" button and the "Clear" button if necessary. 3.Copy the ten line program above and paste it into the Message Box 4.Click on the "Compile Program" button. 5.Click on the "Run" button. 6.When prompted, enter numbers in the "In-Box", and press the "Enter" button. What you should do 1.Click on the "LMC Simulator Applet" link to start the LMC simulator. 2.Clear the Message Box and all of the LMC mailboxes -- click the "Clear Messages" button and the "Clear" button if necessary. 3.Copy the ten line program above and paste it into the Message Box 4.Click on the "Compile Program" button. 5.Click on the "Run" button. 6.When prompted, enter numbers in the "In-Box", and press the "Enter" button. What you should see (for inputs 5 + 4)
Video: Adding in LMC *try it yourself Watch the video on the right and analyse what is happening Watch the video on the right and analyse what is happening
Step 1: ADDING in LMC Enter the code
Step 2: ADDING in LMC Assemble to RAM
Step 3: ADDING in LMC Run Program Input: 5 Run Program Input: 5 PC: 2 ACC:5 INPUT:5
Step 5: ADDING in LMC Second Input: 4 Second Input: 4 PC: 5 ACC: 9 INPUT: 4
Subtracting in LMC: Method 1 INP = 5 Store 5 in 08 INP = 3 Store 3 in 00 Load 5 into the ACC (5) Subtract second (3) OUPUT HALT Note: First DAT = 5 stored in location 08
Subtracting in LMC: Method 2 INP = 5 Store 5 in 08 INP = 3 Store 3 in 09 Load 5 into the ACC (5) Subtract second (3) OUPUT HALT *The key difference is that in Method 1, SECOND DAT was not specified and the second input was stored in 00. In this case, Input 2 is in 09.
Adding and Subtracting in LMC You’ll note that a solution can be achieved in different ways! Try and find the most elegant solution.
Running Program Program Counter : 0 Getting INPUT from user Input : 5--- Program Counter : 1 STore Accumulator contents (5) in box Program Counter : 2 Getting INPUT from user Input : 4--- Program Counter : 3 STore Accumulator contents (4) in box Program Counter : 4 LoaD Accumulator with contents of box 14 (5)--- Program Counter : 5 ADD contents of box 15 (4) to the accumulator--- Program Counter : 6 OUTPUTing accumulator (9)--- Program Counter : 7 STore Accumulator contents (9) in box 0--- Program Counter : 8 Getting INPUT from user Input : 3--- Program Counter : 9 STore Accumulator contents (3) in box Program Counter : 10 LoaD Accumulator with contents of box 0 (9)--- Program Counter : 11 SUBtract contents of box 16 (3) from the accumulator- Program Counter : 12 OUTPUTing accumulator (6)--- Program Counter : 13 HALT! Running Program Program Counter : 0 Getting INPUT from user Input : 5--- Program Counter : 1 STore Accumulator contents (5) in box Program Counter : 2 Getting INPUT from user Input : 4--- Program Counter : 3 STore Accumulator contents (4) in box Program Counter : 4 LoaD Accumulator with contents of box 14 (5)--- Program Counter : 5 ADD contents of box 15 (4) to the accumulator--- Program Counter : 6 OUTPUTing accumulator (9)--- Program Counter : 7 STore Accumulator contents (9) in box 0--- Program Counter : 8 Getting INPUT from user Input : 3--- Program Counter : 9 STore Accumulator contents (3) in box Program Counter : 10 LoaD Accumulator with contents of box 0 (9)--- Program Counter : 11 SUBtract contents of box 16 (3) from the accumulator- Program Counter : 12 OUTPUTing accumulator (6)--- Program Counter : 13 HALT! Adding and Subtracting in LMC Method 1
Adding and Subtracting in LMC *try it yourself Program INP STA FIRST INP ADD FIRST OUT INP SUB FIRST OUT HLT FIRST DAT Program INP STA FIRST INP ADD FIRST OUT INP SUB FIRST OUT HLT FIRST DAT What you should do 1.Click on the "LMC Simulator Applet" link to start the LMC simulator. 2.Clear the Message Box and all of the LMC mailboxes -- click the "Clear Messages" button and the "Clear" button if necessary. 3.Copy the ten line program above and paste it into the Message Box 4.Click on the "Compile Program" button. 5.Click on the "Run" button. 6.When prompted, enter three-digit numbers in the "In-Box", and press the "Enter" button. What you should do 1.Click on the "LMC Simulator Applet" link to start the LMC simulator. 2.Clear the Message Box and all of the LMC mailboxes -- click the "Clear Messages" button and the "Clear" button if necessary. 3.Copy the ten line program above and paste it into the Message Box 4.Click on the "Compile Program" button. 5.Click on the "Run" button. 6.When prompted, enter three-digit numbers in the "In-Box", and press the "Enter" button. What you should see 1.After the program is compiled, you should see from mailbox 0 to 7 the instructions 901, 309, 901, 109, 902, 901, 209, 902. The Program Counter should start at 0 (click on "Reset" if necessary). 2.DAT is the tenth instruction of your program, so it refers to mailbox 9 (0- indexed counting). FIRST is the identifier that has been declared to represent this mailbox in the assembly language program. 3.When you click on "Run" or "Step", the Message Box will describe the actions of each instruction. 4.After the first INP instruction, the Accumulator has a copy of the first value entered in the In Box. 5.After the STA instruction, the input value is copied from the Accumulator to mailbox 9. 6.After the second INP instruction, the Accumulator has a copy of the second value entered into the In Box (try values that will lead to three and four digit sums). 7.After the ADD instruction, the Accumulator value represents the sum of the two input values -- 1 means ADD and 09 refers to the mailbox where the value to be added to the Accumulator is stored. 8.After the third INP instruction, the Accumulator has a copy of the third value entered into the In Box (try values that will lead to positive and negative results). 9.After the SUB instruction, the Accumulator value represents the difference between the two input values -- 2 means SUBTRACT and 09 refers to the mailbox where the value to be subtracted from the Accumulator is stored. What you should see 1.After the program is compiled, you should see from mailbox 0 to 7 the instructions 901, 309, 901, 109, 902, 901, 209, 902. The Program Counter should start at 0 (click on "Reset" if necessary). 2.DAT is the tenth instruction of your program, so it refers to mailbox 9 (0- indexed counting). FIRST is the identifier that has been declared to represent this mailbox in the assembly language program. 3.When you click on "Run" or "Step", the Message Box will describe the actions of each instruction. 4.After the first INP instruction, the Accumulator has a copy of the first value entered in the In Box. 5.After the STA instruction, the input value is copied from the Accumulator to mailbox 9. 6.After the second INP instruction, the Accumulator has a copy of the second value entered into the In Box (try values that will lead to three and four digit sums). 7.After the ADD instruction, the Accumulator value represents the sum of the two input values -- 1 means ADD and 09 refers to the mailbox where the value to be added to the Accumulator is stored. 8.After the third INP instruction, the Accumulator has a copy of the third value entered into the In Box (try values that will lead to positive and negative results). 9.After the SUB instruction, the Accumulator value represents the difference between the two input values -- 2 means SUBTRACT and 09 refers to the mailbox where the value to be subtracted from the Accumulator is stored.
Program showing the Output Program INP STA FIRST INP ADD FIRST OUT INP SUB FIRST OUT HLT FIRST DAT Program INP STA FIRST INP ADD FIRST OUT INP SUB FIRST OUT HLT FIRST DAT INP Store INP in Location 09 INP ADD the first INPUT Method 1
Questions and Answers
Question 1 If INPUT 1 = 3 and INPUT 2 = 4 What would the output of this program be? If INPUT 1 = 3 and INPUT 2 = 4 What would the output of this program be? Answer Load Second (4) 4 SUB Second = 4 – 4 = 0 Answer Load Second (4) 4 SUB Second = 4 – 4 = 0 0 0
Question 2 Can you tell from analysing the LMC, what the INPUTS to this program are? Answer INPUT 1: 12 INPUT 2: 13 See Memory Box 1 and 3, which give their storage locations Answer INPUT 1: 12 INPUT 2: 13 See Memory Box 1 and 3, which give their storage locations
Question 3 Can you tell from analysing the LMC, what the INPUTS to this program are? Answer INPUT 1: 12 INPUT 2: 13 See Memory Box 1 and 3, which give their storage locations Answer INPUT 1: 12 INPUT 2: 13 See Memory Box 1 and 3, which give their storage locations
Question 4 What are the numbers 901 and 902 referring to? Answer 901 is referring to the instruction: INP (input) 902 which we see in location 6, is referring to OUT (output) Answer 901 is referring to the instruction: INP (input) 902 which we see in location 6, is referring to OUT (output)
Question 5 What is the meaning of the number 309 in memory location #3? Answer It is an instruction in two parts. The 3 means ‘store’ and the 09 is referring to where the number should be stored – in this case location 09. Answer It is an instruction in two parts. The 3 means ‘store’ and the 09 is referring to where the number should be stored – in this case location 09.
What’s next? #Part 2 LMC We’ve looked at commands like INP, HLT, OUT, STA, and LDA But using only the ones above don’t allow us to do everything we need. Looping (as you know from ordinary programming) is an important tool. In the next session we will look at MAKING DECISIONS in the LMC and how to Multiply (which is a great example of using a loop)