Jeff's notes on getting started with the little man computer (named when "man" was still being used in place of "mankind") Start with - Jeff's intro video - link coming soon How it works - good to read after having watched Jeff's intro video http://www.peterhigginson.co.uk/LMC/help.html The LMC web app - pretty interesting once you get the hang of it http://www.peterhigginson.co.uk/LMC/ LMC is a simple version of a computer, and has many properties that real computers have. - CPU can only perform a few basic tasks, doesn't actually "understand" anything but it's own assembly language, so Python programs ultimately need to be converted into CPU assembly language for them to run. - CPU does operations using data in its registers, which are separate from memory, to do something with data from memory the memory needs to be loaded into the CPU, and to save the results they have to be stored back into memory. - CPU keeps track of "next instruction" using a program counter register, that by default increases by 1 (goes to the next instruction). - Program instructions are stored in memory, and data for the program is also stored in memory ("stored program architecture") - CPU "fetch execute cycle" - CPU gets the next instruction, runs it. Then get the next instruction, run it. Etc. - Conditional logic in assembly happens using a branch (also called jump in some assembly language) instruction. - Looping in assembly happens using a branch instruction as well. - Functions would also be implemented using branch instructions. - Those are the basic parts of assembly language: arithmetic on data in registers, load from memory and store to memory, conditional branching. - There are additional instructions to deal with different types of hardware (screen, keyboard, etc.). We can translate LMC assembly code into Python as follows. - LMC's CPU accumulator register - Python variable ACC - LMC's 100 memory locations - Python list MEM that has 100 items - LMC's INP instruction - Python ACC = int(input()) - LMC's OUT instruction - Python print(ACC) - LMC's STA instruction - Python MEM[i] = ACC - LMC's LDA instruction - Python ACC = MEM[i] - LMC's ADD instruction - ACC = ACC + MEM[i] - LMC's SUB instruction - ACC = ACC - MEM[i] - LMC's BRA, BRZ, BRP instructions - like goto in other languages, nothing like it in Python Our first LMC assembly language program, and in Python. INP STA 99 INP ADD 99 OUT HLT And in Python that is this. This inputs two numbers, adds them, and prints. ACC = int(input()) MEM[99] = ACC ACC = int(input()) ACC = ACC + MEM[99] print(ACC) How about adding three numbers? LMC with a condition - input two numbers, output the larger of the two. In LMC... INP STA 90 INP STA 91 SUB 90 BRP HIGHER LDA 90 BRA DONE HIGHER LDA 91 DONE OUT HLT FIRST DAT SECOND DAT Note - HIGHER and DONE are "labels" for a place in the program to branch to with the branch instructions. These are used to do conditional execution (if/else). Note - FIRST and SECOND are "declared" with the DAT lines to be something like variable names - spots in MEM that will be used in the program. In the program below I have FIRST being at MEM[90] and SECOND being at MEM[91]. Convert that to Python... ACC = int(input()) MEM[90] = ACC ACC = int(input()) MEM[91] = ACC ACC = ACC - MEM[90] if ACC >= 0: # HIGHER ACC = MEM[91] print(ACC) else: # didn't go to HIGHER, keep going from before if ACC = MEM[90] # BRA DONE goes to the DONE code print(ACC) How about making the program print the smaller of the two numbers? How about making the program check if the two numbers are the same and printing 1 if yes and 0 if not? How about making the program input three numbers and printing the largest? LMC with the counting up loop... LDA ONE LOOP OUT SUB TEN BRZ DONE ADD TEN ADD ONE BRA LOOP DONE HLT TEN DAT 010 ONE DAT 001 How about making it count down from 10 to 1? How about making it count by 2's or 5's? How about having it ask the user for the start and stop to count between? What else? Something we have done in Python that is just basic math/loops/conditions...