22
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

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

Embed Size (px)

Citation preview

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

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

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

10-2

The ROBOT2 Computer: Programs and Algorithms

• Our first example of the computer: The ROBOT2

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

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

10-3

The ROBOT2 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.

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

10-4

The ROBOT2 Computer:

• Hardware: ROBOT2’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?)

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

10-5

The ROBOT2 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)

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

10-6

The ROBOT2’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

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

10-7

The ROBOT2 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

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

10-8

The ROBOT2 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”)

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

10-9

The ROBOT2 Computer : Turn about and raise arms

• If we were at the wall, how would we turn around and raise our arms?

0 TURN RIGHT

1 TURN RIGHT

2 RAISE

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

10-10

The ROBOT2 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?

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

10-11

The ROBOT2 Computer: find the wall

• Does this solution get the ROBOT to the wall?

0 RAISE1 SENSE2 STEP3 GOTO 14 LIGHT5 STOP

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

10-12

The ROBOT2 Computer: Programs and Algorithms

• In some ways this a better solution to the problem of finding the wall in front of the ROBOT...

0 RAISE1 SENSE2 LOWER3 STEP4 GOTO 05 LIGHT6 STOP

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

10-13

The ROBOT2 Computer: Programs and Algorithms

• Yet, there may be an even better (“best”) solution that we've yet to discover...

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

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 RAISE 010 000001 SENSE 100 000002 LOWER 011 000003 STEP 000 000004 GOTO 0 101 00000

5 LIGHT 110 00000 6 STOP 111 00000

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

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?

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

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?

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

10-17

The ROBOT Computer: Programs and Algorithms

• Challenge: get the ROBOT from point A to point B

A

B

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

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

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

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 protected]

….

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

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 protected]

….

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

10-21

Visualizing Memory

X := Y + Z

X = 39

Y = 23

Z = 16

X = 19

Y = 23

Z = 16

before after

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

10-22

Visualizing Memory

Y := Y + 1

X = 39

Y = 24

Z = 16

X = 39

Y = 23

Z = 16

before after