Presentation is loading. Please wait.

Presentation is loading. Please wait.

10-1 Programming Remember: –programming language –how to program (conceptually) –intro to programming the “ROBOT” computer In this lecture: –programming.

Similar presentations


Presentation on theme: "10-1 Programming Remember: –programming language –how to program (conceptually) –intro to programming the “ROBOT” computer In this lecture: –programming."— Presentation transcript:

1 10-1 Programming Remember: –programming language –how to program (conceptually) –intro to programming the “ROBOT” computer In this lecture: –programming the “ROBOT” computer –intro to programming the Pencil and Paper Computer

2 10-2 The ROBOT 2 Computer: Programs and Algorithms Our first example of the computer: The ROBOT 2 computer. The ROBOT’s domain –The room is empty. –The room is rectangular. –There may be one or more open doorways in the walls. –The floor is paved with square tiles with lines between them. (Such that the lines are easy for us to see) –The size of the room is unknown to us at any given time. –The size of the room does not change during the execution of a program. –Doorways will never be located in corners. In the future there will be robots

3 10-3 The ROBOT 2 Computer: Hardware Features: –Locomotion: Forward motion from one square to an adjacent square within its domain (STEP). Pivot: Only to the right, 90 degrees (TURN). –Arms (two - one at each side) Can be raised (RAISE) and lowered (LOWER). Extension: arms reach to the far side of the next square. Sensors: –At tips of arms: Can sense if a wall is in front of it if arms are raised and a SENSE command is given.

4 10-4 The ROBOT 2 Computer: Hardware: ROBOT 2 ’s program Memory: –Located on the ROBOT’s torso. –32 memory locations numbered 0 to 31. –Each memory location is a set of 8 toggle switches. On = 1 Off = 0 –Each location is capable of storing one ROBOT instruction. –Loading a program: setting the switches. (fat finger?)

5 10-5 The ROBOT 2 Computer: ROBOT Hardware: –Fetch: Electronic circuits cause it to fetch (or retrieve) instructions from memory one at a time, and usually in the order in which they are stored. –Decode: An Instruction Decoder is a set of circuits which causes the appropriate actions to be taken based on the particular binary number instruction that is received as input. ROBOT instructions are split into two parts. Opcode (Comma nd) Operand (Address)

6 10-6 The ROBOT 2 ’s Instruction Set The sense light will turn off, returning the ROBOTs ability to follow GOTO instructions. [Has no effect if the light is already off] LIGHT110 The ROBOT shuts off its own powerSTOP111 The ROBOT takes the next command out of normal order. The Operand, the last 5 bits of the instruction, tells which memory location is to be performed next [If the light is currently on, this command is ignored] GOTO101 The ROBOT, with its arms in raised position, can detect if it is one step away from the wall it is facing. IF IT IS, the sense light will turn on. [When the light is on, GOTO statements will be ignored] SENSE100 The ROBOT lowers its arms if they are raised.LOWER011 The ROBOT raises its arms if possible. If can’t RAISE there MUST be a wall directly in front of the ROBOT, but the ROBOT is unaware that its arms cant be raised. This is generally bad for the Robot’s gears RAISE010 The ROBOT pivots 90 degrees. The Operand, the last 5 bits of the instruction, may be RIGHT = 00000 or LEFT = 00001 TURN001 The ROBOT takes one STEP forward (it might bash itself into the wall) – note, the robot is expensive! Don’t bash it into the wall! STEP000 The action taken by the ROBOT, in English mnemonicOpcode

7 10-7 The ROBOT 2 Computer : Programs and Algorithms Task: Destroy the robot. –How?: Cause the ROBOT to walk to the wall until it stops working. (Assume we ARE facing a wall already) GOTO 01 STEP0 OperationMemory Location

8 10-8 The ROBOT 2 Computer : Programs and Algorithms Program: Defend the room! Problem: Cause the ROBOT to walk to the wall it is initially facing and then stand with its back to the wall and arms raised to scare anyone who enters. (Assume the ROBOT is not initially facing an open doorway) Remember: We have NO IDEA how big the room is! –We CAN’T just tell it to STEP X-number of times! First, find the Algorithm: –The solution to the problem should be general. –We must break the problem up into smaller tasks to solve (“divide and conquer”)

9 10-9 The ROBOT 2 Computer : Turn about and raise arms If we were at the wall, how would we turn around and raise our arms? 0TURN RIGHT 1TURN RIGHT 2RAISE

10 10-10 The ROBOT 2 Computer: find the wall Let’s program finding the wall – what do we need to do? What do we need to know before we can write our program? Are there assumptions we need to make? Can we avoid making assumptions?

11 10-11 The ROBOT 2 Computer: find the wall Does this solution get the ROBOT to the wall? 0 RAISE 1 SENSE 2 STEP 3 GOTO 1 4 LIGHT 5 STOP

12 10-12 The ROBOT 2 Computer: Programs and Algorithms In some ways this a better solution to the problem of finding the wall in front of the ROBOT... 0 RAISE 1 SENSE 2 LOWER 3 STEP 4 GOTO 0 5 LIGHT 6 STOP

13 10-13 The ROBOT 2 Computer: Programs and Algorithms Yet, there may be an even better (“best”) solution that we've yet to discover...

14 10-14 Conversion to machine language Programming the ROBOT - Taking the “English” steps and writing them in the language the ROBOT understands (Machine Language). –Machine Language - Written in binary code, the program is in the form the computer understands. “English” Version Machine Language Version 0 RAISE010 00000 1 SENSE100 00000 2 LOWER011 00000 3 STEP000 00000 4 GOTO 0101 00000 5 LIGHT110 00000 6 STOP111 00000

15 10-15 The ROBOT Computer: Programs and Algorithms What do we need to add to cause the ROBOT to walk around the perimeter of the room? What do we need to know before we can write our program? Are there assumptions we need to make?

16 10-16 The ROBOT Computer: Programs and Algorithms What do we need to add to cause the ROBOT to walk around the perimeter of the room? – RAISE – SENSE – LOWER – STEP – GOTO 0 – LIGHT – TURN LEFT – GOTO 0 – STOP Does the program ever stop?

17 10-17 The ROBOT Computer: Programs and Algorithms Challenge: get the ROBOT from point A to point B A B

18 10-18 Conceptual Computers The Pencil and Paper computer is a mathematical computer –It features (almost exclusively) numerical capacity –Instructions are focused on mathematic operations: reading numbers from input units into a “memory location” printing numbers to output units into “memory location” adding from a “memory location” into the accumulator subtracting a “memory location” from the accumulator moving bits in “memory” to/from the accumulator deciding the next step based on a numerical test

19 10-19 Visualizing Memory Variables are “place holders” where the values are memorized. Each variable has a name and a value –A variable's “name” names the slot in memory – it is an alias to a particular memory location (an abstraction for a memory address) A variable may have different values at different times during the execution of the program X = 19 Y = 23 Z = 16 name=“Michael” email=mlong@cs.bu.edu ….

20 10-20 Visualizing Memory Consider a third- generation programming language construct to add two variables and store the value into a third X := Y + Z X = 19 Y = 23 Z = 16 name=“Michael” email=mlong@cs.bu.edu ….

21 10-21 Visualizing Memory X := Y + Z X = 39 Y = 23 Z = 16 X = 19 Y = 23 Z = 16 before after

22 10-22 Visualizing Memory Y := Y + 1 X = 39 Y = 24 Z = 16 X = 39 Y = 23 Z = 16 before after


Download ppt "10-1 Programming Remember: –programming language –how to program (conceptually) –intro to programming the “ROBOT” computer In this lecture: –programming."

Similar presentations


Ads by Google