Professional Documents
Culture Documents
What is TOY?
What is TOY?
Pentium
Celeron
Memory.
Registers.
!
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
to be executed.
15
14
13
12
11
10
46 3
0? 0
637510 =
6375 =
+212 +211
4096 +2048
+2
+1
Shorthand Notation
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
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
+ 2 7 ! 160
+2
+22
R9
next
instruction
+ 14 ! 161
10
15
Main Memory
pc
R3
variables
+ 7
8
00:
08:
10:
18:
20:
28:
.
.
E8:
F0:
F8:
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
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
Halt. (opcode 0)
Stop the machine.
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
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
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
Programming in TOY
Fetch-execute cycle.
FETCH: get instruction from memory.
EXECUTE: update PC, move data to or from
memory and registers, perform calculations.
Fetch
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
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
RC
0009
0000
0001
" 18
0009
0002
" 18
0012
0001
" 18
001B
0000
" 18
multiply.toy
24
25
Harvard architecture.
Separate program and data memories.
Can't load game from disk (data) and execute (program).
Used in some microcontrollers.
27