You are on page 1of 7

What is TOY?

Lecture 8: The TOY Machine

COS126: General Computer Sci ence

An imaginary machine similar to:


Ancient computers.
!

http://w w w .cs.Pri nceton.EDU/~cos126

What is TOY?

What is TOY?

An imaginary machine similar to:


Ancient computers.
Today's microprocessors.

An imaginary machine similar to:


Ancient computers.
Today's microprocessors.

Why study TOY?


Machine language programming.
how do Java programs relate to computer?
key to understanding Java references (and C pointers)
Computer architecture.
how is a computer put together?
how does it work?
Optimized for understandability, not cost or performance.
!

Pentium

Celeron

Inside the Box


Switches. Input data and programs.
Lights. View data.

Memory.

Each bit consists of two states:


Switch is ON or OFF.
High voltage or low voltage.
1 or 0.
True or false.

Registers.
!

Data and Programs Are Encoded in Binary

Fastest form of storage.

Dec

Bin

Dec

Bin

0000

1000

0001

1001

0010

10

1010

0011

11

1011

0100

12

1100

0101

13

1101

0110

14

1110

0111

15

1111

Scratch space during computation.

16 registers. (16 bits each)


Register 0 is always 0.

Stores data and programs.


256 "words." (16 bits each)

Arithmetic-logic unit (ALU). Manipulate

FF for stdin / stdout.

data stored in registers.

How to represent integers?


Use binary encoding.
Ex: 637510 = 00011000111001112
!

Program counter (PC).


!

An extra 8-bit register.

Standard input, standard output.

Keeps track of next instruction

Interact with outside world.

to be executed.

15

14

13

12

11

10

46 3

0? 0

637510 =
6375 =

+212 +211

+27 +26 +25

4096 +2048

+128 +64 +32

+22 +21 +20


+4

+2

+1

Shorthand Notation

Machine "Core" Dump

Use hexadecimal (base 16) representation.


Binary code, four bits at a time.
Ex: 637510
= 00011000111001112

Dec

Bin

Hex

Dec

Bin

Hex

= 18E716

0000

1000

0001

1001

0010

10

1010

0011

11

1011

0100

12

1100

0101

13

1101

0110

14

1110

0111

15

1111

Machine contents at a particular place and time.


Record of what program has done.
Completely determines what machine will do.
!

Registers
R0

R1

R2

14

13

12

11

10

46 3

0? 0

1
637510 =
=

8
1 ! 163
4096

+ 8 ! 162
+ 2048

R4
R8

R5

R6

R7

+ 224

RA

data

RB

RC

RD

RE

RF

program

0000 0000 0000 0000

+ 2 7 ! 160
+2
+22

R9

next
instruction

0000 0000 0000 0000

+ 14 ! 161

10

0000 0000 0000 0000


0000 0000 0000 0000

15

Main Memory

pc
R3

variables

+ 7
8

00:

0008 0005 0000 0000 0000 0000 0000 0000

08:

0000 0000 0000 0000 0000 0000 0000 0000

10:

8A00 8B01 1CAB 9C02 0000 0000 0000 0000

18:

0000 0000 0000 0000 0000 0000 0000 0000

20:

0000 0000 0000 0000 0000 0000 0000 0000

28:

0000 0000 0000 0000 0000 0000 0000 0000

.
.
E8:

0000 0000 0000 0000 0000 0000 0000 0000

F0:

0000 0000 0000 0000 0000 0000 0000 0000

F8:

0000 0000 0000 0000 0000 0000 0000 0000


9

Why do they call it core?

A Sample Program
A sample program.
Adds 8 + 5 = D.
!

RA

RB

RC

pc

0000

0000

0000

10

Registers

add.toy

00: 0008
01: 0005
02: 0000

8
5
0

10:
11:
12:
13:
14:

RA " mem[00]
RB " mem[01]
RC " RA + RB
mem[02] " RC
halt

8A00
8B01
1CAB
9C02
0000

Memory

Since PC = 10, machine interprets 8A00 as an instruction.


Reference: http://w w w .columbi a.edu/aci s/hi story/core.html
10

11

Load

Load

Load. (opcode 8)
Loads the contents of some memory location into a register.
8A00 means load the contents of memory cell 00 into register A.

Load. (opcode 8)
Loads the contents of some memory location into a register.
8B01 means load the contents of memory cell 01 into register B.

RA

RB

RC

pc

0000

0000

0000

10

00: 0008
01: 0005
02: 0000
10:
11:
12:
13:
14:

Registers

8A00
8B01
1CAB
9C02
0000

add.toy

8
5
0

RA " mem[00]
RB " mem[01]
RC " RA + RB
mem[02] " RC
halt

RA

RB

RC

pc

0008

0000

0000

11

Registers

add.toy

00: 0008
01: 0005
02: 0000

8
5
0

10:
11:
12:
13:
14:

RA " mem[00]
RB " mem[01]
RC " RA + RB
mem[02] " RC
halt

8A00
8B01
1CAB
9C02
0000

15

14

13

12

11

10

46 3

15

14

13

12

11

10

46 3

0? 0

0? 0

816

A16

0016

opcode

dest d

addr
12

816

B16

0116

opcode

dest d

addr
13

Add

Store

Add. (opcode 1)
Add contents of two registers and store sum in a third.
1CAB means add the contents of registers A and B and put the
result into register C.

Store. (opcode 9)
Stores the contents of some register into a memory cell.
9C02 means store the contents of register A into memory cell 02.

RA

RB

RC

pc

0008

0005

0000

12

00: 0008
01: 0005
02: 0000
10:
11:
12:
13:
14:

Registers

15

14

13

12

11

10

8A00
8B01
1CAB
9C02
0000

add.toy

8
5
0

RA " mem[00]
RB " mem[01]
RC " RA + RB
mem[02] " RC
halt

46 3

0? 1

RA

RB

RC

pc

0008

0005

000D

13

Registers

add.toy

00: 0008
01: 0005
02: 0000

8
5
0

10:
11:
12:
13:
14:

RA " mem[00]
RB " mem[01]
RC " RA + RB
mem[02] " RC
halt

8A00
8B01
1CAB
9C02
0000

15

14

13

12

11

10

46 3

0? 0

116

C16

A16

B16

opcode

dest d

source s

source t

916

C16

0216

opcode

dest d

addr

14

15

Halt

Program and Data

Halt. (opcode 0)
Stop the machine.

Program: Sequence of instructions.

RA

RB

RC

pc

0008

0005

000D

14

Registers

00: 0008
01: 0005
02: 000D
10:
11:
12:
13:
14:

8A00
8B01
1CAB
9C02
0000

8
5
D

16 instruction types:
16-bit word (interpreted one way).
Changes contents of registers, memory, and
PC in specified, well-defined ways.

add.toy

1: add
2: subtract

3: and

4: xor

Data:
16-bit word (interpreted other way).

RA " mem[00]
RB " mem[01]
RC " RA + RB
mem[02] " RC
halt

Instructions
0: halt

Program counter (PC):


Stores memory address of "next instruction."
!

5: shift left
6: shift right
7: load address
8: load
9: store
A: load indirect
B: store indirect
C: branch zero
D: branch positive
E: jump register
F: jump and link

16

17

TOY Instruction Set Architecture

Interfacing with the TOY Machine

TOY instruction set architecture (ISA).


Interface that specifies behavior of machine.
16 register, 256 words of main memory, 16-bit words.
16 instructions.

To enter a program or data:


Set 8 memory address switches.
Set 16 data switches.
Press LOAD.
data written into addressed word of memory

Each instruction consists of 16 bits.


!

To view the results of a program:


Set 8 memory address switches.
Press LOOK: contents of addressed word appears in lights.

Bits 12-15 encode one of 16 instruction types or opcodes.


Bits 8-11 encode destination register d.

Bits 0-7 encode:

Format 1: source registers s and t

Format 2: 8-bit memory address or constant


15

14

13

12

11

10

4 6 3

0 ? 0

Format 1

opcode

dest d

Format 2

opcode

dest d

source s

source t
addr

18

19

Using the TOY Machine: Run

Programming in TOY

To run the program:


Set 8 memory address switches to address of first instruction.
Press LOOK to set PC to first instruction.
Press RUN button to repeat fetch-execute cycle until halt opcode.

Hello, World. Add two numbers.

To harness the power of TOY, need loops and conditionals.


Manipulate PC to control program flow.

Fetch-execute cycle.
FETCH: get instruction from memory.
EXECUTE: update PC, move data to or from
memory and registers, perform calculations.

Branch if zero. (opcode C)


Changes PC depending of value of some register.
Used to implement: for, while, if-else.

Fetch

Branch if positive. (opcode D)


Analogous.

Execute

20

21

An Example: Multiplication

Multiply

Multiply.
No direct support in TOY hardware.
Load in integers a and b, and store c = a ! b.
Brute-force algorithm:
initialize c = 0
int a = 3;
add b to c, a times
!

0A: 0003
0B: 0009
0C: 0000

3
9
0

inputs

0D: 0000
0E: 0001

0
1

constants

10: 8A0A
11: 8B0B
12: 8C0D

RA " mem[0A]
RB " mem[0B]
RC " mem[0D]

13: 810E

R1 " mem[0E]

always 1

14:
15:
16:
17:

if
RC
RA
pc

while (a != 0) {
c = c + b
a = a - 1
}

output

int b = 9;
int c = 0;
while (a != 0) {
c = c + b;
a = a - 1;
}
loop

Java

Issues ignored: slow, overflow, negative numbers.

CA18
1CCB
2AA1
C014

18: 9C0C
19: 0000

a
b
c = 0

(RA == 0) pc " 18
" RC + RB
" RA - R1
" 14

mem[0C] " RC
halt
multiply.toy

22

23

Step-By-Step Trace

R1
10:
11:
12:
13:
14:
15:
16:
17:
14:
15:
16:
17:
14:
15:
16:
17:
14:
18:
19:

8A0A
8B0B
8C0D
810E
CA18
1CCB
2AA1
C014
CA18
1CCB
2AA1
C014
CA18
1CCB
2AA1
C014
CA18
9C0C
0000

RA " mem[0A]
RB " mem[0B]
RC " mem[0D]
R1 " mem[0E]
if (RA == 0) pc
RC " RC + RB
RA " RA R1
pc " 14
if (RA == 0) pc
RC " RC + RB
RA " RA R1
pc " 14
if (RA == 0) pc
RC " RC + RB
RA " RA R1
pc " 14
if (RA == 0) pc
mem[0C] " RC
halt

A Little History

RA
0003

RB

Electronic Numerical Integrator and Calculator (ENIAC).


First widely known general purpose electronic computer.
30 tons, 30 x 50 x 8.5 ft, 17,468 vacuum tubes, 300 multiply/sec.
Conditional jumps, programmable.
Programming: change switches and cable connections.
Data: enter numbers using punch cards.

RC

0009

0000
0001

" 18

0009

0002
" 18
0012
0001
" 18
001B
0000
" 18

multiply.toy
24

John Mauchly (left) and J. Presper Eckert (ri ght)


http://cs.sw au.edu/~durki n/arti cles/hi story_computi ng.html

ENIAC, Ester Gerston (left), Glori a Gordon (ri ght)


US Army photo: http://ftp.arl.mi l/ftp/hi stori c-computers

25

Basic Characteristics of TOY Machine

Harvard vs. Princeton

TOY is a general-purpose computer.


Sufficient power to perform ANY computation.
Limited only by amount of memory and time.

Harvard architecture.
Separate program and data memories.
Can't load game from disk (data) and execute (program).
Used in some microcontrollers.

Stored-program computer. (von Neumann memo, 1944)


Data and instructions encoded in binary.
Data and instructions stored in SAME memory.
Can change program without rewiring.
!

John von Neumann

Von Neumann architecture.


Program and data stored in same memory.
Used in almost all computers.

Outgrowth of Alan Turing's work. (stay tuned)

All modern computers are general-purpose computers


and have same (von Neumann) architecture.

Q. What's the difference between Harvard and Princeton?


A. At Princeton, data and programs are the same.
Maurice Wilkes (left)
EDSAC (right)
26

27

You might also like