Presentation is loading. Please wait.

Presentation is loading. Please wait.

Starter Read the Feedback Click on Add new Feedback Open Realsmart

Similar presentations


Presentation on theme: "Starter Read the Feedback Click on Add new Feedback Open Realsmart"— Presentation transcript:

1 Starter Read the Feedback Click on Add new Feedback Open Realsmart
Go to Computer Science Computer System Lesson 3 - LMC Read the Feedback Click on Add new Feedback Add the heading RESPONSES Response to the feedback

2 Copy LMC Resources Go to Computer Student Resources Computing Year 9
Computer Science Copy “Algorithms” Paste it your Computer Science / Computing folder in your area

3 The Little man computer
Mr A Jalloh

4 Instructions When a computer is instructed to run a program it is directed to the start address for these data and instructions. The CPU fetches the first instruction from this start location and decodes it to find out what to do next.

5 The contents of memory location 1101
Program Instructions A program instruction has two parts – Operator – This is the instruction part and , Operand – This the data part 1 1 1 Operator ADD to the accumulator Operand The contents of memory location 1101

6 Encoding Instructions
The two part to the instructions are further encoded to binary. Opcode - a binary value which represents the instruction Data – Could be null or will contain the data needed for the instruction in binary. When a instruction is passed to a micro-processor the first thing which happens is the opcode is decoded. This means the micro-processor will look the binary code up in a table. Once the instruction is found it will execute it.

7 What is the Little Man Computer?
The Little Man Computer (LMC) is a simulator which copies the basic features of a modern computer. It is based on the Von Neumann architecture featuring a central processing unit consisting of An Arithmetic Logic unit  Registers, A control unit containing an instruction register and program counter, Input and Output mechanisms and  Memory to store both data and instructions.

8 What is the Little Man Computer?
The LMC is based on the concept of a little man (shut in a small room or inside a computer) acting like the control unit of a CPU i.e.  Fetching instructions from memory, Decoding and Executing the instructions, and managing the input and output. The LMC can be programmed using either "Machine Code" or "Assembly Language". 

9 Which LMC? There are many implementations of the Little Man Computer (LMC). We will use the excellent web based one that can be found here: LMC/

10 Parts of the LMC Assembly instructions RAM with 100 memory locations
Output CPU with 4 registers Input

11 Parts of the CPU CPU register Function Accumulator Program Counter
This stores data that is being used in ALU to perform calculation. It can perform simple addition and subtraction. Accumulator This contains the memory address of the next instruction to be loaded. This automatically ticks to the next memory address when an instruction is loaded. It can be altered during the running of the program depending on the state of the accumulator. Program Counter An Instruction Register to hold the top digit of the instruction read from memory. Instruction Register An Address Register to hold the bottom two digits of the instruction read from memory. Address Register This registers allows the user to input numerical data to the LMC. Input Output This shows the data to output to the user.

12 How the LMC works The LMC will start to load the instruction from the memory address in the program counter. When the LMC first loads up this will set at zero. This memory location needs to be an instruction and will be dealt with as such. When the data is loaded the program counter is incremented to the next memory location.

13 LMC Instruction set The LMC processor understands 10 basic commands (plus 1 instruction to label data). The LMC only understands these instructions in a numerical form. This can be difficult for us to program in so there is a set of mnemonics we can use instead. This is known as assembly language. This will be converted into the LMC code before the program can run.

14 LMC Instruction set ADD 1xx SUB SUBTRACT 2xx STA STORE 3xx LDA LOAD
Mnemonic code Instruction Numeric code Description ADD 1xx SUB SUBTRACT 2xx STA STORE 3xx LDA LOAD 5xx INP INPUT 901 OUT OUTPUT 902 Add the value stored in mailbox xx to whatever value is currently on the accumulator (calculator). Subtract the value stored in mailbox xx from whatever value is currently on the accumulator (calculator). Store the contents of the accumulator in mailbox xx (destructive). Load the value from mailbox xx (non-destructive) and enter it in the accumulator (destructive). Go to the INBOX, fetch the value from the user, and put it in the accumulator (calculator) Copy the value from the accumulator (calculator) to the OUTBOX.

15 LMC Instruction set Mnemonic code Instruction Numeric code Description BRA BRANCH (unconditional) 6xx BRZ BRANCH IF ZERO (conditional) 7xx BRP BRANCH IF POSITIVE (conditional) 8xx HLT HALT DAT DATA Set the program counter to the given address (value xx). That is, value xx will be the next instruction executed. If the accumulator (calculator) contains the value 000, set the program counter to the value xx. Otherwise, do nothing. If the accumulator (calculator) is 0 or positive, set the program counter to the value xx. Otherwise, do nothing. Stop working. DAT can is used in conjunction with labels to declare variables. For example, DAT 984 will store the value 984 into a mailbox at the address of the DAT instruction.

16 Examples - input & output
This program simply asks the user for an input and then outputs what was input. INP OUT HLT The program has been assembled into RAM and you can see the numeric codes for the instructions in the first three memory locations. Try the example above and save a copy on Notepad as “Input and Output

17 Activity – LMC Instructions
Open the following files 1-LMC Instruction Activities and 2 - LMC Code Activities Complete all task

18 Using memory (Variable)
INP STA FIRST STA SECOND LDA FIRST OUT LDA SECOND HLT FIRST DAT 0 SECOND DAT 0 Try the example on the left and save a copy on Notepad as “Load Variable” This program asks the user to input a number. This is stored in a memory location defined by the DAT label. A second number is asked for and stored. These numbers are then loaded and output in order.

19 Using memory – Adding Two numbers together
INP STA FIRST STA SECOND LDA FIRST ADD SECOND STA ANSWER OUT HLT FIRST DAT 0 SECOND DAT 0 ANSWER DAT 0 Try the example on the left and save a copy on Notepad as “Adding two Variables” This program asks the user to input a number. This is stored in a memory location defined by the DAT label. A second number is asked for and stored. These numbers are then add together and the result is save in a variable called ANSWER The variable ANSWER is displayed the result

20 LMC Activity Using memory
This program asks the user to input a number. This is stored in a memory location defined by the DAT label. A second number is asked for and stored. These numbers are then add together After the addition one is subtracted and display the result NOTE: the value 1 in a variable called ONE Guess what this program does Try the example on the left and save a copy on Notepad as “Adding and Subtracting Variable” INP STA FIRST ADD FIRST STA SECOND SUB ONE OUT HLT FIRST DAT 0 SECOND DAT 0 ONE DAT 1

21 Set o Instruction Move (number of step ) Forward
Move (number of step ) Backward Move (number of step ) Right Move (number of step ) Left

22 Activity – LMC Instructions
Open the following files 3- LMC Matching and 4 - LMC CPU Worksheet Complete all task

23 Branches and loops in LMC
BRA – Branch Always This is the same a forever loop in Scratch. It is an unconditional loop. Try the example on the left and save a copy on Notepad as “Branch Always”. Add comments explaining what the code does This example will keep outputting the value which is stored in A forever. The program will not stop. LOOPTOP LDA A OUT BRA LOOPTOP A DAT 5 ONE DAT 1

24 Branches and loops in LMC
BRZ – Branch if Zero This allows us to branch our program if the value in the accumulator is zero. This can be used to create counting loops similar to the repeat loop in Scratch or a for loop in Python. LOOPTOP LDA A SUB ONE STA A OUT BRZ END BRA LOOPTOP END HLT A DAT 5 ONE DAT 1 Try the example on the left and save a copy on Notepad “Branch if Zero”. Add a comment describing what it does This example uses a BRA to create the loop and A BRZ to break the loop.

25 Branches and loops in LMC
BRP – Branch if Positive This also allows us to create a branch in our program, depending on whether the contents of the accumulator are positive or not. These can also be used to create loops. LOOPTOP LDA A SUB ONE STA A OUT BRP LOOPTOP HLT A DAT 5 ONE DAT 1 Try the example on the left and save a copy on Notepad as “Branch if Positive”. Add a comment describing what it does This example uses a BRP to both create and terminate the loop.

26 Activity – LMC Instructions
Open the following files 6 - While_Loops_in_LMC and 7 - CPU_LMC_Extension Worksheet Complete all task

27 Bigger INP STA FIRST STA SECOND SUB FIRST BRP FIRSTBIG LDA SECOND OUT HLT FIRSTBIG BRZ SAME LDA FIRST SAME LDA ZERO FIRST DAT 0 SECOND DAT 0 ZERO DAT 0 This program uses two branch commands to alter the path of the program. There is no greater than or less than command so we simply subtract the second number from the first. If it is positive then the first number must have been bigger so we branch if positive. The two numbers could be the same however so we need to check to see if the result is zero. We branch if it is. The biggest number is output or zero if they are both the same.

28 Points to note The instruction set is very limited so you often need to come up with a different way to perform things like multiplication or comparing two numbers. The LMC does not store decimals. The LMC does not have a loop structure but you can use a Branch Always command to redirect the code to an earlier command.

29 How to Write a Little man Computer program
Writing an LMC program can be quite a challenge. As the instruction set is very limited we often need to perform what seems to us to be a very simple task in an even simpler way. Using a Flow chart to help write the program is very helpful. When the flow chart is created we can simply look at each shape on the chart and think what instructions would we need to have for that shape. These will often be no more that a couple of lines of LMC code.

30 How to Write a Little man Computer program
In flow charts there are 4 symbols that we commonly use. Symbol Meaning LMC instuctions Start / Stop Start has no instruction but Stop is HLT. Input & Output OUT is the output command. It may need to be This could be a DAT command where we see variables initialised (e.g. counter = 0). addition and subtraction commands fit into this. A process such as X = X + Y would need to be done in the correct order. So we would Load X, Add Y and then store the result as X. This would be LDA X, ADD Y, STA X Process There are only two instructions that can have two alternatives. Branch if Positive and Branch if Zero. If the test is true then the program can branch to another part of the program. If not the program carries on. Decision

31 Using memory (Variable)
INP STA FIRST STA SECOND LDA FIRST OUT LDA SECOND HLT FIRST DAT 0 SECOND DAT 0 This program asks the user to input a number. This is stored in a memory location defined by the DAT label. A second number is asked for and stored. These numbers are then loaded and output in order.

32 Using memory (Variable)
Start FIRST = 0 First thing - create a flow chart to show what needs to be done. Be as detailed as you can be. SECOND= 0 INPUT FIRST INPUT SECOND LOAD FIRST OUTPUT FIRST LOAD SECOND OUTPUT SECOND End

33 Using memory (Variable)
Start FIRST = 0 Note any values you need to remember. These will be the variables. In LMC code they will become the DAT commands. Note if they have a start value. SECOND= 0 INPUT FIRST INPUT SECOND LOAD FIRST OUTPUT FIRST LOAD SECOND OUTPUT SECOND End

34 Using memory (Variable)
Start FIRST = 0 We start We have 2 variables FIRST DAT 0 SECOND DAT 0 NOTE: The variable is at the top of the Program in the Flowchart. SECOND= 0 INPUT FIRST INPUT SECOND LOAD FIRST OUTPUT FIRST LOAD SECOND OUTPUT SECOND End

35 Using memory (Variable)
Start FIRST = 0 Now start at the top and write down the commands for the instructions for the flow chart. Assigning values can be ignored so the first command in Input number The LMC command is INP If we need to store that we need to follow this with a store command and save it to memory using the DAT label we created SECOND= 0 INP STA FIRST STA SECOND INPUT FIRST INPUT SECOND LOAD FIRST OUTPUT FIRST LOAD SECOND OUTPUT SECOND End

36 Using memory (Variable)
Start FIRST = 0 INP STA FIRST STA SECOND LDA FIRST OUT Now we use the LDA command to load the value stored in FIRST Variable. We use the OUT command to output the value stored in FIRST Variable. SECOND= 0 INPUT FIRST INPUT SECOND LOAD FIRST OUTPUT FIRST LOAD SECOND OUTPUT SECOND End

37 Using memory (Variable)
Start FIRST = 0 INP STA FIRST STA SECOND LDA FIRST OUT LDA SECOND We do the same for the Second value We use the LDA command to load the value stored in SECOND Variable. We use the OUT command to output the value stored in SECOND Variable. SECOND= 0 INPUT FIRST INPUT SECOND LOAD FIRST OUTPUT FIRST LOAD SECOND OUTPUT SECOND End

38 Using memory (Variable)
Start FIRST = 0 Once we Output both FIRST and SECOND Value, We use HLT to end the program INP STA FIRST STA SECOND LDA FIRST OUT LDA SECOND HLT SECOND= 0 INPUT FIRST INPUT SECOND LOAD FIRST OUTPUT FIRST LOAD SECOND OUTPUT SECOND End

39 Activity – LMC Instructions
Open the following files 5 - More LMC Task and Complete all task For each task create a flowchart and write LMC codes Save a copy of your LMC code in Notepad

40 Example - The Problem We want a program to calculate averages.
We want to be able to keep entering values until we enter a zero. The average is then calculated and displayed.

41 PLANNING AND DESIGN First thing - create a flow chart to show what needs to be done. Be as detailed as you can be.

42 Note any values you need to remember.
These will be the variables. In LMC code they will become the DAT commands. Note if they have a start value.

43 We have 4 variables total DAT 0 count DAT 0 result DAT 0 num DAT

44 If we need to add on or subtract a specific value we need to be able to store that too.
We need to be able to add 1 do we can do this by having one DAT 1

45 Assigning values can be ignored so the first command in Input number
Now start at the top and write down the commands for the instructions for the flow chart. Assigning values can be ignored so the first command in Input number The LMC command is INP If we need to store that we need to follow this with a store command and save it to memory using the DAT label we created. INP STA num

46 Next we see if the user has entered a zero.
We can use Branch Zero to do this. If the accumulator is zero we will jump to another section of the code. We need to give this section a label. I will call this section CALCULATE. I will need to do that code later. INP STA num BRZ CALCULATE

47 INP STA num BRZ CALCULATE LDA total ADD num STA total
The next section of code happens if num does NOT equal zero. Now I need to add the num to the total. I will load the total and then add the num. LDA total ADD num This then needs to be saved back as the total. STA total INP STA num BRZ CALCULATE LDA total ADD num STA total

48 Now I need to add one to the count.
INP STA num BRZ CALCULATE LDA total ADD num STA total LDA count ADD one STA count Now I need to add one to the count. So i need to load count and then add one. The result needs to be saved as count

49 LOOPTOP INP STA num BRZ CALCULATE LDA total ADD num STA total
LDA count ADD one STA count BRA LOOPTOP The code now loops back to the Input command. We can use a Branch always command to do this. We need to label where we want the BRA command to jump to. I will call it LOOPTOP. I need to add this label to the INP command and use it in the BRA command.

50 Now we need to go back to out CALCULATE code.
LOOPTOP INP STA num BRZ CALCULATE LDA total ADD num STA total LDA count ADD one STA count BRA LOOPTOP Now we need to go back to out CALCULATE code. This code performs a division. LMC does not have a divide command.

51 The result will store this count.
LOOPTOP INP STA num BRZ CALCULATE LDA total ADD num STA total LDA count ADD one STA count BRA LOOPTOP We can perform a divide by repeatedly subtracting the count from the total until we get to zero. Keeping a count of how many times we successfully subtract the count will be the same as dividing. The result will store this count.

52 This code will run in a loop.
LOOPTOP INP STA num BRZ CALCULATE LDA total ADD num STA total LDA count ADD one STA count BRA LOOPTOP This code will run in a loop. We need to load total and then subtract the count. We then need to see if the count is below zero. If it is not we will add one to the result and then loop around.

53 BRA LOOPTOP CALCULATE LDA total
LOOPTOP INP STA num BRZ CALCULATE LDA total ADD num STA total LDA count ADD one STA count BRA LOOPTOP CALCULATE LDA total SUB count BRP DIVIDE So the first command is to load the total and subtract the count. LDA Total SUB count Then we Branch if the result is zero or higher, so we need BRP in order to add one to the result. I will give it the label DIVIDE and deal with that later.

54 BRA LOOPTOP CALCULATE LDA total
LOOPTOP INP STA num BRZ CALCULATE LDA total ADD num STA total LDA count ADD one STA count BRA LOOPTOP CALCULATE LDA total SUB count BRP DIVIDE LDA RESULT OUT HLT If the value in the accumulator is negative then the BRP does not run. We now need to load the result and output it to the user. Once we do that the program is done.

55 BRA LOOPTOP CALCULATE LDA total
LOOPTOP INP STA num BRZ CALCULATE LDA total ADD num STA total LDA count ADD one STA count BRA LOOPTOP CALCULATE LDA total SUB count BRP DIVIDE LDA RESULT OUT HLT DIVIDE LDA result STA result BRA CALCULATE Now we need to go back to what happens if the total - count is positive. Remember we jumped to a label called DIVIDE. We need to add one to the result and then start the loop again. We can use Branch always to jump back to the top of our loop. The loop already has a label, so we can use that.

56 All that remains is to add the DAT commands to the end of our program.
LOOPTOP INP STA num BRZ CALCULATE LDA total ADD num STA total LDA count ADD one STA count BRA LOOPTOP CALCULATE LDA total SUB count BRP DIVIDE LDA RESULT OUT HLT DIVIDE LDA result STA result BRA CALCULATE total DAT 0 count DAT 0 num DAT result DAT 0 one DAT 1 All that remains is to add the DAT commands to the end of our program.

57 Plenary Open Realsmart and Click on Computer Science
Click on Unit 1 - Computer System RAFL Under Lesson 3 - LMC Click on Add New Feedback Upload All work you completed in this lesson Write a short reflection on what you have covered by answering the following question; Explain what you have learned today Describe four things that went well in this lesson (SUCCESSES) Describe three thing that you could do better (TARGETS) Describe two things you will do differently next time (ACTION)?


Download ppt "Starter Read the Feedback Click on Add new Feedback Open Realsmart"

Similar presentations


Ads by Google