Intoduction First we’ll talk in general about Visual X-Toy Then we’ll input and run a program...

20
Intoduction • First we’ll talk in general about Visual X-Toy • Then we’ll input and run a program using switches • Then we’ll look at the more advanced editor. www.cs.princeton.edu/introcs/50machine/index.html
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    0

Transcript of Intoduction First we’ll talk in general about Visual X-Toy Then we’ll input and run a program...

Intoduction

• First we’ll talk in general about Visual X-Toy

• Then we’ll input and run a program using switches

• Then we’ll look at the more advanced editor.

www.cs.princeton.edu/introcs/50machine/index.html

• Memory.– Stores data and programs.– 256 "words." (16 bits each)

• FF is ‘code’ for stdin / stdout.• Program counter (PC).

– An extra 8-bit register.– Keeps track of next instruction

to be executed.• Standard input, standard output.

– Interact with outside world.

• Registers.Fastest form of storage.

•Scratch space during computation.•16 registers. (16 bits each)• Register 0 is always 0.

•Arithmetic-logic unit (ALU).•Manipulate data stored in registers.

REMEMBER

The lights are the computer talking to us. Here it says “The Program counter is currently pointing to memory location (1) 0x10 and its contents are (2) 0x0000”

The switches (3) are ways for us to communicate with the computer. When we flick switches the lights don’t change (only when we press LOAD | LOOK | ENTER and then the computer echoes back our inputs using the lights)

11

33

22

33

In general, what we see first...

11

I want to save ‘CAFE’ in ‘00:’ so I flick the switches with (1) the ADDR of 0x0000 and (2) the DATA 0xCAFE

Notice the lights do not reflect my changes as I flick switches.

22

22

After I press (1) LOAD the computer “tells me” (2) location 0x00

has (3) instruction 0xCAFE.

(i.e. The computer ‘echoes’ back what I just asked it to LOAD)

33

11

Overview of a program

• We’re going to enter ‘raw’ binary instructions (i.e. a program) using hexadecimal representation into the computer the hard way (with the switches)

• The program – reads user input (two binary

numbers from from stdin)– performs an addition

calculation – and then prints out the result

(to stdout)

What you can do in ‘Sim mode’

1) We can see the contents of memory

2) We can see the state of the registers and program counter

3) By setting the (ADDR) address switches and clicking ‘LOOK’ we can see the contents of a memory location in the (DATA) lights.

4) We can ‘Load’ data into a particular address

3311

22

44

Exercise 1: Enter the program into memory starting at 10:

****

1) When you have the code entered press RESET

2) The computer ‘talks’ to us using the lights and shows us where the (PC) program counter will start and (3) the instruction (we have to assume its an instruction and not data!) at this location

Note: One of the essential characteristics of the TOY machine is that it stores computer programs as numbers, and both data and programs are stored in the same main memory (aka the von Neumann architecture)

11

22

33

1) Run the program by single stepping through it

2) The first instruction (8AFF) tells the computer to look for input from (a) ‘stdin’. Notice that the (b) INWAIT light is now lit.

3) Set the ‘Data’ switches to 0x0007

4) Press ENTER to load this data into the Stdin.

11

2a2a

33

44

2b2b

Please note:

(1) Even though we’ve ENTERed the value 0x0007 into Stdin

(2) the program counter has not moved to the next instruction.

(3) We need to now press STEP to move forward with this program.

11

2233

We’ve entered data using the switches.

However we could be more sophisticated and enter the data using punch cards

1) We complete out program by stepping through each instruction until the computer is told to halt (0x0000). Then we see the result displayed on the standard output (0x0007 + 0x0008 = 0x00F). Please note there are three instances of stdout: a, b and c.

11

aa

bb

cc

Review

• We used the switches to enter the program instructions at particular memory locations.

• The computer can communicate with us using the lights

• We can interact with our running program using Stdin and Stdout

• Using the switches is tedious• Deciphering the lights is tedious

What you can do in ‘edit mode’

1) We have a nice editor

2) We have a language reference card

3) We can easily type into stdin and queue up input as required

4) We can type in trial hex instructions and get X-Toy to tell us what they mean

11

22

33

44

Exercise: Enter the same program using the editor.

11

1) Note that you can auto insert comments into your code using CTRL+SHIFT+A or Tools menu

SEE NEXT SLIDE

22

1) You can type in (decimal) values and queue them up for stdin

2) Or using an external editor read them from a file and queue them up for stdin.

3) Or copy the stdout to a file as well

11

33

add.toy

subtract.toy

Also shows flow control