LMC Worksheet 2
Branch Programs:
This program lets you try to guess the "lucky number" (hint, it is 42) if you are right, it says "89" (ASCII code for "Y"). If you are wrong it says "78" (ASCII code for "N"). Type (or copy/paste) the code into the LMC and watch it run. Step by step.
00 / 901 / Get Input to the accumulator01 / 310 / Store input to memory location 10
02 / 211 / Subtract value at location 11 (the number 42) from accumulator
03 / 707 / If accumulator is 0, go to instruction 07
04 / 512 / Load value from location 12 into accumulator
05 / 902 / Output accumulator to screen
06 / 000 / End
07 / 513 / Load value from location 13 into accumulator
08 / 902 / Output accumulator to screen
09 / 000 / End
10 / 000 / Data Storage
11 / 042 / Data Storage
12 / 078 / Data Storage
13 / 089 / Data Storage
How does it detect if you entered 42? (Since there is no "check if equal to 42" instruction) It subtracts 42 and then compares the answer to 0
Which instructions get executed ONLY if you enter 42?4,5,6
Which instructions get executed ONLY if you do NOT enter 42?7,8,9
Now try this program:
00 / 901 / Get Input to the accumulator01 / 310 / Store input to memory location 10
02 / 211 / Subtract value at location 11 (value is 10) from accumulator
03 / 807 / If accumulator is less than 0, go to instruction 07
04 / 510 / Load value from location 10 (the initial input) into accumulator
05 / 902 / Output accumulator to screen
06 / 000 / End
07 / 511 / Load value from location 11 (the 10) into accumulator
08 / 902 / Output accumulator to screen
09 / 000 / End
10 / 000 / Data Storage
11 / 010 / Data Storage
What number does your program compare your input to? 10
What happens if the input is greater? Outputs 10
What happens if the input is smaller?Outputs the number you input
You Try:
- Write a program that takes one input. If it is positive, print it back out. If it is negative, print 0.
0 / 901
1 / 310
2 / 804 / If >= Skip ahead to 4
3 / 506 / Must have been negative… load the 0 from location 06
4 / 902
5 / 000
6 / 000
Note - you do not have to do the store step… never need modify the input before we have to print it.
- Challenge:Takes in two numbers. Outputs the largest one.
Hints… get and store them both… subtract one from the other to decide which is bigger then use a branch to load the correct one
0 / 901
1 / 312
2 / 901
3 / 313
4 / 212
5 / 809
6 / 512
7 / 902
8 / 000
9 / 513
10 / 902
11 / 000
Loop Programs:
Try running this program:
0 / 505 / Load the value (0) that is stored in location 51 / 106 / Add the value from 6 (1)
2 / 902 / Output
3 / 601 / Always go back to step 1
4 / 000 / End
5 / 000 / Data Storage
6 / 001 / Data Storage
What does line 3 tell the computer to do?Go back to instruction 1
Step it a bunch of times. What pattern does it output?1,2,3,4…
Why can't it stop?The 601 is an always branch and it goes back to the start
Try running this program – input a small number like 5 or 6
0 / 901 / Get input1 / 902 / Output current value
2 / 206 / Subtract the value from 6 (1)
3 / 705 / If we got 0, go to instruction 5
4 / 601 / Always go back to step 1 (but we don't run this if the subtraction made 0)
5 / 000 / End
6 / 001 / Data Storage
Step it a bunch of times. What pattern does it output?Counts down from your input to 1
How does it stop?Once it hits 0, the 705 branch skips the “go back to the start” instruction (601)