Download presentation
Presentation is loading. Please wait.
1
Little Man Computer (continued)
2
All Instructions 1XX – add the value in mailbox XX to calc
2XX – subtract the value in mailbox xx from calc 3XX – put the value of calc into mailbox XX 5XX – take the value of mailbox XX and put it into calc 6XX – set the instruction counter to XX 7XX – set the instruction counter to XX if calc is 0, otherwise nap 8XX – set the instruction counter to XX if calc is positive, otherwise nap 901 – grab the top item from input and put it in calc 902 – put calc into output 000 – stop and go home
3
Conditional Execution
What if we want the our mail man to skip instructions? For example, let’s say that you are an acturary You need to write a program to calculate how many years an individual has left to live You decide to offload this task to the mail man The formula you decide to use is 80 - <input> What happens if the user says they are -5 years old?
4
Conditional Execution
If the user enters a negative value, we want to output -1 instead of <input>
5
Conditional Execution Example
0 – 901 – read a # from the input and put it in calc 1 – 209 – subtract the # in mailbox 9 from calc 2 – 706 – if calc is now 0, set the instruction counter to 10, otherwise rest 3 – 512 – copy the # mailbox 12 to calc 4 – 902 – write the value to calc to the output 5 – 0 – go home 6 – 511 – copy the # mailbox 11 to calc 7 – 902 – write the value to calc to the output 8 – 0 – go home 9 – 5 10 – 6 11 – 1 12 – -1
6
Looping Sometimes, you need to keep doing a set of instructions until a value reaches 0 For example, remember to simulate multiplication using repeated addition, we stopped once the number we were multiplying by reached 0
7
Looping Example 0 – 901 – copy # from input to calc 1 – 399 – copy calc to mailbox 99 2 – 901 – copy # from input to calc 3 – 398 – copy calc to mailbox 98 4 – 598 – copy mailbox 98 to calc 5 – 712 – if calc is 0 go to XX 6 – 215 – calc – mailbox XX 7 – 398 – copy calc to mailbox 98 8 – 597 – copy mailbox 97 to calc 9 – 199 – calc + mailbox – 397 – copy calc to mailbox – 604 – go to 4 12 – 597 – copy mailbox 97 to calc 13 – 902 – copy from calc to out 14 –
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.