Download TOY Machine: Understanding Two's Complement and Memory Operations and more Slides Data Representation and Algorithm Design in PDF only on Docsity!
TOY II
7
What We've Learned About TOY
Data representation. Binary and hex.
TOY.
Box with switches and lights.
16-bit memory locations, 16-bit registers, 8-bit pc.
4,328 bits = (255 × 16) + (15 × 16) + (8) = 541 bytes!
von Neumann architecture.
TOY instruction set architecture. 16 instruction types.
TOY machine language programs. Variables, arithmetic, loops.
8
What We Do Today
Data representation. Negative numbers.
Input and output. Standard input, standard output.
Manipulate addresses. References (pointers) and arrays.
TOY simulator in Java.
9
Negative Numbers
10
How to add and subtract binary numbers
Binary addition facts:
1 + 1 + 1 = 11 (needed for carries)
Bigger numbers example:
OK, but: subtract?
Subtract by adding a negative integer (e.g., 6 - 4 = 6 + (-4))
OK, but: negative integers?
carries 11
Representing Negative Integers
TOY words are 16 bits each.
We could use 16 bits to represent 0 to 2^16 - 1.
We want negative integers too.
Reserving half the possible bit-patterns for negative seems fair.
Highly desirable property. If x is an integer, then the representation
of -x, when added to x, is zero.
x 0 0 1 1 0 1 0 0 +(-x) +???????? 0 0 0 0 0 0 0 0 0 x 0 0 1 1 0 1 0 0
- 1 1 0 0 1 0 1 1 +(-x) 1 1 1 1 1 1 1 1
- 1 0 0 0 0 0 0 0 0 0 flip bits and add 1 12
“Two's Complement” Integers
Properties:
Leading bit (bit 15) signifies sign.
Negative integer -N represented by 2^16 - N.
Trick to compute -N:
- Start with N. 1 1 1 1 1 1 1 1 1 1 1 1? 1 0 1 1
- Flip bits. -4 1 1 1 1 1 1 1 1 1 1 1 1? 1 1 0 0
- Add 1. 13
Two's Complement Integers
0 1 1 1 1 1 1 1 1 1 1 1? 1 1 1 1 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0? 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0? 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0? 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0? 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0? 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1? 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1? 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1? 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1? 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0? 0 0 0 0 7FFF 0004 0003 0002 0001 0000 FFFF FFFE FFFD FFFC 8000
dec hex binary
18 Standard Input and Output 19
Standard Output
Standard output.
Writing to memory location FF sends one word to TOY stdout.
Ex. 9AFF writes the integer in register A to stdout.
10: 8A00 RA ← mem[00] a = 0 11: 8B01 RB ← mem[01] b = 1 do { 12: 9AFF write RA to stdout print a 13: 1AAB RA ← RA + RB a = a + b 14: 2BAB RB ← RA - RB b = a - b 15: DA12 if (RA > 0) goto 12 } while (a > 0) 16: 0000 halt 22
Standard Input
Standard input.
Loading from memory address FF loads one word from TOY stdin.
Ex. 8AFF reads an integer from stdin and store it in register A.
Ex: read in a sequence of integers and print their sum.
In Java, stop reading when EOF.
In TOY, stop reading when user enters 0000.
while (!StdIn.isEmpty()) { a = StdIn.readInt(); sum = sum + a; } StdOut.println(sum);
10: 8C00 RC <- mem[00] 11: 8AFF read RA from stdin 12: CA15 if (RA == 0) pc ← 15 13: 1CCA RC ← RC + RA 14: C011 pc ← 11 15: 9CFF write RC 16: 0000 halt 00AE 0046 0003 0000 00F 23
Standard Input and Output: Implications
Standard input and output enable you to:
Put information from real world into machine.
Get information out of machine.
Process more information than fits in memory.
Interact with the computer while it is running.
Information can be instructions!
Booting a computer.
Sending programs over the Internet
Sending viruses over the Internet
24 Pointers 25 addr
Load Address (a.k.a. Load Constant)
Load address. [opcode 7]
Loads an 8-bit integer into a register.
7A30 means load the value 30 into register A.
Applications.
Load a small constant into a register.
Load a 8-bit memory address into a register.
716 A 16 316 016
opcode dest d a = 0x30; Java code register stores "pointer" to a memory cell 26
Arrays in TOY
TOY main memory is a giant array.
Can access memory cell 30 using load and store.
8C30 means load mem[30] into register C.
Goal: access memory cell i where i is a variable.
Load indirect. [opcode A]
AC06 means load mem[R6] into register C.
Store indirect. [opcode B]
BC06 means store contents of register C into mem[R6].
for (int i = 0 ; i < N; i++) a[i] = StdIn.readInt(); for (int i = 0 ; i < N; i++) StdOut.println(a[N-i- 1 ]); a variable index a variable index 30 31 32 33 34 35 36 37 … … 0000 0001 0001 0002 0003 0005 0008 000D … … TOY memory 27
TOY Implementation of Reverse
TOY implementation of reverse.
Read in a sequence of integers and store in memory 30 , 31 , 32 , …
Stop reading if 0000.
Print sequence in reverse order.
10: 7101 R1 ← 0001 constant 1 11: 7A30 RA ← 0030 a[] 12: 7B00 RB ← 0000 n while(true) { 13: 8CFF read RC c = StdIn.readInt(); 14: CC19 if (RC == 0) goto 19 if (c == 0) break; 15: 16AB R6 ← RA + RB memory address of a[n] 16: BC06 mem[R6] ← RC a[n] = c; 17: 1BB1 RB ← RB + R1 n++; 18: C013 goto 13 } read in the data
32
Q. Work all day to develop operating system. How to save it?
A. Write short program dump.toy and run it to dump contents of
memory onto tape.
Dumping
00: 7101 R1 ← 0001
01: 7210 R2 ← 0010 i = 10 02: 73FF R3 ← 00FF do { 03: AA02 RA ← mem[R2] a = mem[i] 04: 9AFF write RA print a 05: 1221 R2 ← R2 + R1 i++ 06: 2432 R4 ← R3 - R 07: D403 if (R4 > 0) goto 03 } while (i < 255) 08: 0000 halt dump.toy 33
Q. How do you get it back?
A. Write short program boot.toy and run it to
read contents of memory from tape.
Booting
00: 7101 R1 ← 0001
01: 7210 R2 ← 0010 i = 10 02: 73FF R3 ← 00FF do { 03: 8AFF read RA read a 04: BA02 mem[R2] ← RA mem[i] = a 05: 1221 R2 ← R2 + R1 i++ 06: 2432 R4 ← R3 - R 07: D403 if (R4 > 0) goto 03 } while (i < 255) 08: 0000 halt boot.toy 34 TOY Simulator 35
TOY Simulator
Goal. Write a program to "simulate" the behavior of the TOY machine.
TOY simulator in Java.
public class TOY { public static void main(String[] args) { int pc = 0x10; // program counter int[] R = new int[ 16 ]; // registers int[] mem = new int[ 256 ]; // main memory // READ IN .toy FILE while (true) { // FETCH INSTRUCTION and DECODE ... // EXECUTE ... } } } % java TOY add-stdin.toy A 002B A03D
36
TOY Simulator: Fetch
Fetch. Extract destination register of 1CAB by shifting and masking.
int inst = mem[pc++]; // fetch and increment int op = (inst >> 12 ) & 15 ; // opcode (bits 12-15) int d = (inst >> 8 ) & 15 ; // dest d (bits 08-11) int s = (inst >> 4 ) & 15 ; // source s (bits 04-07) int t = (inst >> 0 ) & 15 ; // source t (bits 00-03) int addr = (inst >> 0 ) & 255 ; // addr (bits 00-07) 0 0 0 1 1 1 0 0 1 0 1 0 1 0 1 1 116 C 16 A 16 B 16 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 016 016 1 C 16 inst inst >> 8 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 016 016 016 F 16 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 016 016 0 C 16 15 (inst >> 8) & 15 37
TOY Simulator: Execute
if (op == 0 ) break; // halt switch (op) { case 1 : R[d] = R[s] + R[t]; break; case 2 : R[d] = R[s] - R[t]; break; case 3 : R[d] = R[s] & R[t]; break; case 4 : R[d] = R[s] ^ R[t]; break; case 5 : R[d] = R[s] << R[t]; break; case 6 : R[d] = R[s] >> R[t]; break; case 7 : R[d] = addr; break; case 8 : R[d] = mem[addr]; break; case 9 : mem[addr] = R[d]; break; case 10 : R[d] = mem[R[t]]; break; case 11 : mem[R[t]] = R[d]; break; case 12 : if (R[d] == 0 ) pc = addr; break; case 13 : if (R[d] > 0 ) pc = addr; break; case 14 : pc = R[d]; pc; pc = addr; break; case 15 : R[d] = pc; pc = addr; break; } 38
TOY Simulator: Omitted Details
Omitted details.
Register 0 is always 0.
- reset R[0]=0 after each fetch-execute step
Standard input and output.
- if addr is FF and opcode is load (indirect) then read in data
- if addr is FF and opcode is store (indirect) then write out data
TOY registers are 16-bit integers; program counter is 8-bit.
- Java int is 32-bit; Java short is 16-bit
- use casts and bit-whacking
Complete implementation. See TOY.java on booksite.
41
Simulation
Consequences of simulation.
Test out new machine or microprocessor using simulator.
- cheaper and faster than building actual machine
Easy to add new functionality to simulator.
- trace, single-step, breakpoint debugging
- simulator more useful than TOY itself
Reuse software from old machines.
Ancient programs still running on modern computers.
Ticketron.
Lode Runner on Apple IIe.