200 likes | 322 Views
Computer program. A sequence of step-by-step instructions for the computer to follow. Computer instructions. 1. OPERATION: Evaluate expression, set output, read input, ... 2. JUMP: Jump to instruction tagged by a label 3. BRANCH: Evaluate a condition and jump if condition true
E N D
Computer program A sequence of step-by-step instructions for the computer to follow
Computer instructions 1. OPERATION: Evaluate expression, set output, read input, ... 2. JUMP: Jump to instruction tagged by a label 3. BRANCH: Evaluate a condition and jump if condition true 4. LOOP: Repeat section specified number of times
A program 1. Do this 2. Do that 3. Jump to instruction 6 4. Do the other thing 5. All done, sleep 6. If switch closed, do that thing you do 7. Jump to instruction 4
Pen Computer PEN_UP Lift pen off paper PEN_DOWN Lower pen onto paper MOVX dMove pen a distance d in x direction MOVY dMove pen a distance d in y direction PEN_UP MOVX 8 MOVY 8 PEN_DOWN MOVX 4 MOVY 4 MOVX -4 MOVY -4 PENUP
PEN_UP MOVX 8 MOVY 8 PEN_DOWN MOVX 4 MOVY 4 MOVX -4 MOVY -4 PENUP
Some complexity: Loops FOR B0 = 1 TO n Repeatinstructions n times instructions NEXT B0
Program 1 start: pen_down movx 20 movy 16 movx -20 movy -16 pen_up FOR B0 = 1 TO 3 movx 4*B0 movy 4*B0 pen_down movx 4 movy 4 movx -4 movy -4 pen_up movx -4*B0 movy -4*B0 NEXT B0 end:
More complexity N = N + 1 New val = old val + 1 IF expr THEN addressEvaluate and branch if true GOTO addressJump immediately GOSUB addressJump to subroutine at address RETURN Go back to instruction after GOSUB
Program 2 start: pen_up movx 10 movy 8 pen_down N = 0 part1: movy 1 N = N + 1 IF N = 5 THEN part2 GOTO part1 part2: GOSUB part3 pen_up movx -2 movy -2 pen_down FOR N = 1 TO 4 movx 1 NEXT N pen_up GOTO end part3: pen_up movx 4 pen_down movy 4 pen_up movx -4 movy -4 RETURN end:
Body Computer HIGH limbRaise body part LOW limbLower body part TOGGLE limbBody part to opposite state SHOUT “text”Shout words SHOUT B0Shout value of variable PAUSE millisecondsDelay for specified time SLEEP Sleep until next power-up
Practice Programs (1) HIGH right_hand LOW right_hand SHOUT “Go” SHOUT “Gophers” HIGH left_hand PAUSE 2000 LOW left_hand
Practice Programs (2) HIGH right_foot PAUSE 5000 TOGGLE left_hand PAUSE 2000 TOGGLE right_foot PAUSE 5000 LOW left_hand SHOUT “Done”
Practice Programs (3) start: HIGH right_hand PAUSE 6000 IF (light on) THEN hammer SHOUT “My hand is up” LOW right_hand GOTO end hammer: LOW right_hand SHOUT “My hand is down” end: END
start: HIGH right_hand PAUSE 2000 LOW right_hand PAUSE 2000 SHOUT “Go Gophers” HIGH left_foot PAUSE 2000 LOW left_foot PAUSE 2000 SHOUT “Why am I doing this?!” IF (last name of person closest to you starts with B, C, D, H, M, N or S) THEN apples FOR N = 1 TO 2 HIGH right_foot PAUSE 500 LOW right_foot PAUSE 500 SHOUT N NEXT N HIGH left_foot GOTO oranges apples: FOR N = 1 TO 3 HIGH left_hand PAUSE 2000 LOW left_hand PAUSE 2000 SHOUT N NEXT N HIGH right_hand GOTO plums oranges: PAUSE 3000 IF (hand of person closest to you is up) THEN pears FOR N = 1 to 2 TOGGLE right_hand PAUSE 2000 SHOUT “Minnesota” TOGGLE right_hand NEXT N pears: LOW left_foot SHOUT “Finally” plums: HIGH right hand IF (light is on) THEN bananas TOGGLE left_hand TOGGLE left_foot PAUSE 3000 TOGGLE left_foot PAUSE 5000 IF (TA is standing) THEN blueberries GOTO end bananas: FOR N = 1 TO 2 GOSUB pineapples PAUSE 2000 NEXT N GOTO plums blueberries: FOR N = 1 TO 2 GOSUB peaches PAUSE 2000 NEXT N GOTO plums peaches: SHOUT “Go” RETURN pineapples: SHOUT “Gophers” RETURN end: LOW left_hand LOW left_foot LOW right_hand LOW right_foot SHOUT “I’m done” END
Body Performance: Run your program. One step per clock tick • Computer A • HIGH right_hand • LOW right_hand • SHOUT “Go Gophers” • HIGH left_foot • LOW left_foot • IF person to my right has last name that starts with L thru Z THEN Line 9 • CLAP • GOTO Line 11 • WAIT • SHOUT “Go” • WAIT • END • Computer B • HIGH right_hand • LOW right_hand • SHOUT “Go Gophers” • HIGH right_foot • LOW right_foot • IF person to my right has last name that starts with L thru Z THEN Line 9 • CLAP • GOTO Line 11 • WAIT • WAIT • SHOUT “Gophers” • END