You are on page 1of 7

What is TOY?

Lecture A1: The TOY Machine

An imaginary machine similar to:

Ancient computers.

Todays microprocessors.

Why study?

Princeton University

COS 126

General Computer Science

Fall 2002

Machine language programming.


how do C programs relate to computer?
still (a few) situations today where it is really necessary
Computer architecture.
how is a computer put together?
how does it work?
Simplified machine.
captures essence of real computers

http://www.Princeton.EDU/~cs126

Inside the Box


Switches.

Input data and programs.

Program counter (PC).

Lights.

Data and Programs Are Encoded in Binary


Each bit consists of two states:

An extra 8-bit register.


Keeps track of next
instruction to be executed.

View data.

Switch is ON or OFF.

High voltage or low voltage.

1 or 0.

True or false.

Dec
0
1
2
3
4
5
6
7

Memory.
Registers.

Fastest form of storage.


Use as scratch space during
computation.
16 registers.
each stores 16 bits
Register 0 is always 0.

Store data and programs.

How to represent integers?

256 "words."
TOY word is 16 bits

FF for stdin / stdout.

Arithmetic-logic unit (ALU).

Use binary encoding.


Ex: 637510 = 00011000111001112

Manipulate data.

Dec
8
9
10
11
12
13
14
15

15 14 13 12 11 10

46 3

0? 0

637510 =
6375 =
5

Bin
0000
0001
0010
0011
0100
0101
0110
0111

+212 +211
4096 +2048

+27 +26 +25

+22 +21 +20

+128 +64 +32

+4 +2 +1

Bin
1000
1001
1010
1011
1100
1101
1110
1111

Shorthand Notation

Machine "Core" Dump

Use hexadecimal (base 16) representation.

Binary code, four bits at a time.


Ex: 637510

EVERYTHING is encoded in binary.

Dec
0
1
2
3
4
5
6
7

= 00011000111001112
= 18E716

Bin Hex
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7

Dec
8
9
10
11
12
13
14
15

Bin Hex
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

46 3

0? 0

1
637510 =
=

8
1 163

+ 8 162

4096

+ 224

Text.

pc

Reals.

10

...

Registers
3
4
5

0000 0788 B700 0010 0401 0002 0003 00A0

Main Memory
Machine contents at a
particular place and time.

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

+ 27 160
+2
+22

0000 0788 B700 0010 0401 0002 0003 00A0

Machine instructions.

+ 14 161

+ 2048

Integers.

15 14 13 12 11 10

+ 7

00:
08:
10:
18:
20:
28:
.
.
E8:
F0:
F8:

0000
0000
9222
0000
0008
0000

0000
0000
9120
0001
0009
0000

0000
0000
1121
0002
000A
0000

0000
0000
A120
0003
000B
FE10

0000
0000
1121
0004
000C
FACE

0000
0000
A121
0005
000D
CAFE

0000
0000
7211
0006
000E
ACED

0000
0000
0000
0007
000F
CEDE

1234 5678 9ABC DEF0 0000 0000 F00D 0000


0000 0000 EEEE 1111 EEEE 1111 0000 0000
B1B2 F1F5 0000 0000 0000 0000 0000 0000

Program and Data

TOY Instruction Set Architecture

Instructions
0: halt

Program:

Sequence of instructions.

16 instruction types:

16-bit word (interpreted one way).


Changes contents of registers, memory, and
PC in specified, well-defined ways.

TOY instruction set architecture (ISA).

1: add

Interface that specifies behavior of machine.

3: and

16 register, 256 words of main memory, 16-bit words.

4: xor

16 instructions.

2: subtract

5: shift left

Each instruction consists of 16 bits.

6: shift right

7: load address
Data:

16-bit word (interpreted other way).

8: load

9: store
A: load indirect

Program counter (PC):

Stores memory address of "next instruction."

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

B: store indirect

Format 2: 8-bit memory address or constant

15 14 13 12 11 10 9 8
1 0 1 1 1 0 1 0
Format 1
opcode
dest d
Format 2
opcode
dest d

C: branch zero
D: branch positive
E: jump register

7
0

6 5 46 3 2 1 0
0 0 0? 0 1 0 0
source s
source t
addr

F: jump and link


9

10

Load Address (a.k.a. Load Constant)

Load, Store

Load address. (opcode 7)

Load. (opcode 8)

Loads an 8-bit integer into a register.

Format 2.
7A04 means:
load the value 0004 into register A
R[A] 0004

C code

Loads the contents of some memory location into a register.


8A04 means:
load the contents of memory location 04 into register A
R[A] mem[04]
load-store.toy

a = 4;
Store. (opcode 9)

15 14 13 12 11 10 9 8
0 1 1 1 1 0 1 0
716
A16
opcode
dest d

7
0

6 5
0 0
016

46 3
0? 0

2 1
1 0
416

0
0

Store the contents of a register


into main memory.

15 14 13 12 11 10 9 8
1 0 0 0 1 0 1 0
816
A16
opcode
dest d

addr

04: 1111
05: 0000

Opposite of load.

10: 8A04
11: 9A05
12: 0000

7
0

6 5
0 0
016

R[A] mem[04]
mem[05] R[A]
halt

46 3
0? 0

2 1
1 0
416

addr

11

12

Add, Subtract

Using the TOY Machine: Input, Output

Add. (opcode 1)

To enter a program or data:

Add contents of two registers and store sum in a third.

Set 8 memory address switches.

1CAB means:

Set 16 data switches.

add contents of registers


A and B
put result in register C
R[C] R[A] + R[B]

add.toy

Subtract. (opcode 2)

Analogous to add.

15 14 13 12 11 10 9 8
0 0 0 1 1 1 0 0
116
C16
opcode
dest d

0
0

00: 0005
01: 0008

5
8

10:
11:
12:
13:
14:

R[A]
R[B]
R[C]
mem[02]
halt

7
1

8A00
8B01
1CAB
9C02
0000

Press LOAD.
data written into addressed word of memory

To view the results of a program:

mem[00]
mem[01]
R[A] + R[B]
R[C]

Set 8 memory address switches.


Press LOOK.
contents of addressed word of memory appears in lights

6 5 46 3 2 1 0
0 1 0? 1 0 1 1
A16
B16
source s
source t
13

14

Using the TOY Machine: Run

Branch Zero, Branch Positive

To run the program:

Branch if zero. (opcode C)

Set 8 memory address switches to address of first instruction.


Press the RUN or STEP button.
loads PC from address switches
repeats fetch-execute cycle until halt instruction

Changes PC depending of value of some register.

Used to implement loops, if-else.

Multiply.

Fetch-execute cycle.

Fetch

FETCH.
get instruction from memory
EXECUTE.
update PC
move data to or from memory, registers
perform calculations

Execute

Load in integers a and b, and store c = a b.


Brute-force algorithm:
initialize c = 0
add b to c, a times

multiply.c
int a = 7, b = 8;
int c = 0;

Problems.
! Overflow.
! Slow.
! Negative integers.

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

Branch if positive. (opcode D)

Analogous.

15

16

Step-By-Step Trace of multiply.toy

Branch Zero, Branch Positive


Branch if zero. (opcode C)

R[1]
10:
11:
12:
13:
14:
15:
16:
17:
14:
15:
16:
17:
14:
15:
16:
17:
14:
18:
19:

multiply.toy

loop

0A: 0007
0B: 0008

7
8

10:
11:
12:
13:

8A0A
8B0B
7C00
7101

R[A]
R[B]
R[C]
R[1]

14:
15:
16:
17:

CA18
1CCB
2AA1
C014

if (R[A] == 0) pc 18
R[C] += R[B]
R[A] R[A] - R[1]
pc 14

18: 9C0C
19: 0000

mem[0A]
mem[0B]
00
01

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

mem[0C] R[C]
halt
17

8A0A
8B0B
7C00
7101
CA18
1CCB
2AA1
C014
CA18
1CCB
2AA1
C014
CA18
1CCB
2AA1
C014
CA18
9C0C
0000

R[A] mem[0A]
R[B] mem[0B]
R[C] 00
R[1] 01
if (R[A] == 0) goto
R[C] += R[B]
R[A]-goto 14
if (R[A] == 0) goto
R[C] += R[B]
R[A]-goto 14
if (R[A] == 0) goto
R[C] += R[B]
R[A]-goto 14
if (R[A] == 0) goto
mem[0C] R[C]
halt

R[A]
0003

R[B]

R[C]

0007
0000
0001
18
0007
0002
18
000E
0001
18
0015
0000
18

18

An Efficient Multiplication Algorithm

Binary Multiplication

Inefficient multiply.
Brute force multiplication algorithm loops a times.

Grade school binary multiplication algorithm


to compute c = a b.

In worst case, 65,535 additions!

"Grade-school" multiplication.

Always 16 additions to multiply 16-bit integers.

Initialize c = 0.

1 0 1 1

* 1 1 0 1

1 0 1 1 a << 0

Loop over i bits of b.


bi = ith bit of b
if bi = 0, do nothing
if bi = 1, shift a left i bits and
add to c

0 0 0 0
1 0 1 1
1 0 1 1

a << 2
a << 3

1 0 0 0 1 1 1 1
1 2 3 4

1 0 1 1
Binary

* 1 5 1 2

Decimal

2 4 6 8

Implement with built-in TOY shift instructions.

* 1 1 0 1
1 0 1 1

1 2 3 4

multiply-fast.c

0 0 0 0

6 1 7 0

int c = 0;
for (i = 15; i >= 0; i--) {
if ((b >> i) & 1)
c += (a << i);

1 0 1 1

1 2 3 4

1 0 1 1

0 1 8 6 5 8 0 8

1 0 0 0 1 1 1 1
19

Bitwise AND
Logical AND. (opcode B)

Logic operations are BITWISE.


B23516 & 000116 = 000116

0 1
B16

0 1
216

0 1
316

x
0
0
1
1

1? 0

y
0
1
0
1

20

Shift Left

AND
0
0
0
1

1 0
516

Shift left. (opcode 5)

discard

1
0

&
0

0 0
016

0 0
016

0 0
016

0? 0

0 0
016

0? 0

Move bits to the left, padding with zeros as needed.


123416 << 716 = 160016

0 0
016

0 0
116

0 0
116

0 1
216

0 1
316

<< 7 =

1? 0

1 0
416

pad with 0s

=
0

0 0
016

0 0
016

21

0 0
116

0 1
616

0 0
016

0? 0

0 0
016

22

Shift Right

Binary Multiplication
multiply-fast.toy

Shift right. (opcode 6)

Move bits to the right, padding with sign bit as needed.


123416 >> 716 = 012416
discard

sign bit

0 0
116

0 1
216

0 0
016

0 1
316

1? 0

1 0
416

1 0
416

>> 7 =

pad with 0s

0 0
116

0 1
216

0? 0

0A: 0007
0B: 0008

7
8

10:
11:
12:
13:
14:

8A0A
8B0B
7101
7210
7C00

R[A]
R[B]
R[1]
R[2]
R[C]

15:
16:
17:
18:
19:
1A:
1B:
1C:

C21D
2221
53A2
64B2
3441
C43A
1CC3
C015

if (R[2] == 0) goto 1D
R[2]-R[3] R[A] << R[2]
R[4] R[B] >> R[2]
R[4] R[4] & R[1]
if (R[4] == 0) goto 1C
R[C] += R[3]
goto 15

1D: 9C0C

input a
input b

<<<-

mem[0A]
mem[0B]
0001
0010
0000

always 1
i = 16
result

16-bit integers

while (i > 0) {
i-a << i
b >> i
bi = ith bit of b
if bi is 1
add a << i to sum
}

mem[0C] R[C]

23

24

A Little History

Basic Characteristics of TOY Machine

ENIAC. (Eckert and Mauchly, 1946)

TOY is a general-purpose computer.

First general purpose electronic computer.

Sufficient power to perform ANY COMPUTATION.

30 x 50 x 8.5 ft, 17,468 vacuum tubes.

Limited only by amount of memory (and time).

300 multiplications per second.

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

Conditional jumps, programmable.


code: set switches
data: punch cards

Data and instructions encoded in binary.

Data and instructions stored in SAME memory.

Can change program (control) without rewiring.


immediate applications
profound implications
EDSAC (Wilkes 1949).
first stored-program computer
Outgrowth of Turings work.

All modern computers are general-purpose


computers and have same (von Neumann) architecture.
25

26

Harvard vs. Princeton

Lecture A1: Extra Slides

Harvard architecture.

Separate program and data memories.

Cant load game from disk (data) and execute (program).

Used in some microcontrollers.

von Neumann architecture.

Program and data stored in same memory.

Used in almost all computers.

Whats the difference between Harvard and Princeton?


! At Princeton, data and programs are the same.

27

Princeton University

COS 126

TOY Cheat Sheet


Format 1
Format 2

15 14 13 12 11 10 9
opcode
dest d
opcode
#

Operation

Fall 2002

http://www.Princeton.EDU/~cs126

Jump absolute.

addr
Fmt

General Computer Science

Useful TOY "Idioms"

6
5
46 3
2
1
0
source s
source t

dest d

Pseudocode

0:

halt

exit(0)

1:

add

R[d] R[s] +

2:

subtract

R[d] R[s] -

R[t]

3:

and

R[d] R[s] &

R[t]

4:

xor

R[d] R[s] ^

R[t]

5:

shift left

R[d] R[s] << R[t]

6:

shift right

R[d] R[s] >> R[t]

7:

load addr

R[d] addr

8:

load

R[d] mem[addr]

9:

store

mem[addr] R[d]

A:

load indirect

R[d] mem[R[t]]

B:

store indirect

mem[R[t]] R[d]

C:

branch zero

if (R[d] == 0) pc addr

D:

branch positive

if (R[d] > 0)

E:

jump register

pc R[d]

F:

jump and link

R[d] pc; pc addr

Jump to a fixed memory address.


branch if zero with destination
register 0 is always 0

Jump absolute
17: C014

pc 14

R[t]

Register assignment.

No instruction that transfers contents of one register into another.


Pseudo-instruction that simulates assignment:
add with register 0 as one of
Register assignment
two source registers
17: 1230

No-op.

pc addr

Instruction that does nothing.


Plays the role of whitespace in C programs.
numerous other possibilities!
17: 1000

29

R[2] R[3]

No-op
no-op
30

You might also like