5. The TOY Machine Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 3// 5:3 AM!
What is TOY? An imaginary machine similar to:! Ancient computers.! Today's microprocessors. 4
Why Study TOY? Machine language programming.! How do Java programs relate to computer?! Key to understanding Java references.! Still situations today where it is really necessary. multimedia, computer games, scientific computing, SSE, AVX Computer architecture.! How does it work?! How is a computer put together? TOY machine. Optimized for simplicity, not cost or performance. 5
Data and Programs Are Encoded in Binary Each bit consists of two states:! or ; true or false.! Switch is on or off; wire has high voltage or low voltage. Everything stored in a computer is a sequence of bits.! Data and programs.! Text, documents, pictures, sounds, movies, executables, TOY machine. Data and programs stored in memory. 6
Binary Encoding How to represent integers?! Use binary encoding. Dec Bin Dec Bin! Ex: 6375 = 8 9 3 4 5 3 6 4 7 5 5 4 3 9 8 7 6 5 4 6 3? 6375 = 6375 = + + + 7 + 6 + 5 + + + 496 +48 +8 +64 +3 +4 + + 7
Hexadecimal Encoding How to represent integers?! Use hexadecimal encoding. Dec Bin Hex Dec Bin Hex! Ex: 6375 = 8 8 = 8E7 6 9 9 binary code, 4 bits at a time 3 3 A B 4 4 C 5 5 3 D 6 6 4 E 7 7 5 F 5 4 3 9 8 7 6 5 4 6 3? 8 E 7 6375 =! 6 3 + 8! 6 + 4! 6 + + 7! 6 = 496 + 48 + 4 + + 7 8
Inside the Box Switches. Input data and programs. Lights. View data. Memory.! Stores data and programs.! 56 6-bit "words."! Special word for stdin / stdout. Registers.! Fastest form of storage.! Scratch space during computation.! 6 6-bit registers.! Register is always. Arithmetic-logic unit (ALU). Manipulate data stored in registers. Program counter (PC).! An extra 8-bit register.! Next instruction to be executed. Standard input, standard output. Interact with outside world. MEMORY R E G ALU
TOY Machine "Core" Dump Core dump. Contents of machine at a particular place and time.! Record of what program has done.! Completely determines what machine will do. Registers pc Memory R R R R3 : 8 5 8: R4 R8 RC R5 R9 RD R6 RA RE R7 RB RF index of next instruction data program : 8: : 8:.. E8: 8A 8B CAB 9C variables F: F8:
A Sample Program A sample program. Adds 8 + 5 = D. TOY memory (program and data) comments RA RB RC pc : 8 8 : 5 5 : Registers Program counter : 8A RA " mem[] : 8B RB " mem[] : CAB RC " RA + RB 3: 9C mem[] " RC 4: halt add.toy
A Sample Program Program counter. The pc is initially, so the machine interprets 8A as an instruction. RA RB RC pc : 8 8 : 5 5 : Registers index of next instruction to execute Program counter : 8A RA " mem[] : 8B RB " mem[] : CAB RC " RA + RB 3: 9C mem[] " RC 4: halt add.toy 3
Load Load. [opcode 8]! Loads the contents of some memory location into a register.! 8A means load the contents of memory cell into register A. RA RB RC pc : 8 8 : 5 5 : Registers Program counter : 8A RA " mem[] : 8B RB " mem[] : CAB RC " RA + RB 3: 9C mem[] " RC 4: halt add.toy 5 4 3 9 8 7 6 5 4 6 3? 8 6 A 6 6 opcode dest d addr 4
Load Load. [opcode 8]! Loads the contents of some memory location into a register.! 8B means load the contents of memory cell into register B. RA RB RC pc : 8 8 : 5 5 : 8 Registers Program counter : 8A RA " mem[] : 8B RB " mem[] : CAB RC " RA + RB 3: 9C mem[] " RC 4: halt add.toy 5 4 3 9 8 7 6 5 4 6 3? 8 6 B 6 6 opcode dest d addr 5
Add Add. [opcode ]! Add contents of two registers and store sum in a third.! CAB means add the contents of registers A and B and put the result into register C. RA RB RC pc : 8 8 : 5 5 : 8 5 Registers Program counter : 8A RA " mem[] : 8B RB " mem[] : CAB RC " RA + RB 3: 9C mem[] " RC 4: halt add.toy 5 4 3 9 8 7 6 5 4 6 3? 6 C 6 A 6 B 6 opcode dest d source s source t 6
Store Store. [opcode 9]! Stores the contents of some register into a memory cell.! 9C means store the contents of register C into memory cell. RA RB RC pc : 8 8 : 5 5 : 8 5 Registers D 3 Program counter : 8A RA " mem[] : 8B RB " mem[] : CAB RC " RA + RB 3: 9C mem[] " RC 4: halt add.toy 5 4 3 9 8 7 6 5 4 6 3? 9 6 C 6 6 opcode dest d addr 7
Halt Halt. [opcode ]! Stop the machine. RA RB RC pc : 8 8 : 5 5 : D D 8 5 Registers D 4 Program counter : 8A RA " mem[] : 8B RB " mem[] : CAB RC " RA + RB 3: 9C mem[] " RC 4: halt add.toy 8
Program and Data Instructions Program. Sequence of 6-bit integers, interpreted one way. Data. Sequence of 6-bit integers, interpreted other way. Program counter (pc). Holds memory address of the next "instruction" and determines which integers get interpreted as instructions. 6 instruction types. Changes contents of registers, memory, and pc in specified, well-defined ways. : halt : add : subtract 3: and 4: xor 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 9
TOY Instruction Set Architecture TOY instruction set architecture (ISA).! Interface that specifies behavior of machine.! 6 register, 56 words of main memory, 6-bit words.! 6 instructions. Each instruction consists of 6 bits.! Bits -5 encode one of 6 instruction types or opcodes.! Bits 8- encode destination register d.! Bits -7 encode: [Format ] source registers s and t [Format ] 8-bit memory address or constant add, subtract, load, store, 5 4 3 9 8 7 6 5 4 6 3? Format Format opcode dest d source s source t opcode dest d addr
Interfacing with the TOY Machine To enter a program or data:! Set 8 memory address switches.! Set 6 data switches.! Press Load: data written into addressed word of memory. To view the results of a program:! Set 8 memory address switches.! Press Look: contents of addressed word appears in lights.
Interfacing with the TOY Machine To execute the program:! Set 8 memory address switches to address of first instruction.! Press Look to set pc to first instruction.! Press Run to repeat fetch-execute cycle until halt opcode. Fetch-execute cycle.! Fetch: get instruction from memory.! Execute: update pc, move data to or from memory and registers, perform calculations. Fetch
Flow Control Flow control.! To harness the power of TOY, need loops and conditionals.! Manipulate pc to control program flow. if (boolean expression) { statement ; statement ; } while (boolean expression) { statement ; statement ; } Branch if zero. [opcode C]! Changes pc depending on whether value of some register is zero.! Used to implement: for, while, if-else. Branch if positive. [opcode D]! Changes pc depending on whether value of some register is positive.! Used to implement: for, while, if-else. 3
An Example: Multiplication Multiply. Given integers a and b, compute c = a! b. TOY multiplication. No direct support in TOY hardware. Brute-force multiplication algorithm:! Initialize c to.! Add b to c, a times. int a = 3; int b = 9; int c = ; while (a!= ) { c = c + b; a = a - ; } brute force multiply in Java Issues ignored. Slow, overflow, negative numbers. 4
Multiply A: 3 3 B: 9 9 C: D: E: inputs output constants : 8AA RA " mem[a] a : 8BB RB " mem[b] b : 8CD RC " mem[d] c = 3: 8E R " mem[e] always loop 4: CA8 if (RA == ) pc " 8 while (a!= ) { 5: CCB RC " RC + RB c = c + b 6: AA RA " RA - R a = a - 7: C4 pc " 4 } 8: 9CC mem[c] " RC 9: halt multiply.toy 5
Step-By-Step Trace R RA RB RC : 8AA RA " mem[a] 3 : 8BB RB " mem[b] 9 : 8CD RC " mem[d] 3: 8E R " mem[e] 4: CA8 if (RA == ) pc " 8 5: CCB RC " RC + RB 9 6: AA RA " RA R 7: C4 pc " 4 4: CA8 if (RA == ) pc " 8 5: CCB RC " RC + RB 6: AA RA " RA R 7: C4 pc " 4 4: CA8 if (RA == ) pc " 8 5: CCB RC " RC + RB B 6: AA RA " RA R 7: C4 pc " 4 4: CA8 if (RA == ) pc " 8 8: 9CC mem[c] " RC 9: halt multiply.toy 6
A Little History Electronic Numerical Integrator and Calculator (ENIAC).! First widely known general purpose electronic computer.! Conditional jumps, programmable.! Programming: change switches and cable connections.! Data: enter numbers using punch cards. 3 tons 3 x 5 x 8.5 ft 7,468 vacuum tubes 3 multiply/sec John Mauchly (left) and J. Presper Eckert (right) http://cs.swau.edu/~durkin/articles/history_computing.html ENIAC, Ester Gerston (left), Gloria Gordon (right) US Army photo: http://ftp.arl.mil/ftp/historic-computers 7
Basic Characteristics of TOY Machine TOY is a general-purpose computer.! Sufficient power to perform any computation.! Limited only by amount of memory and time. Stored-program computer. [von Neumann memo, 944]! Data and program encoded in binary.! Data and program stored in same memory.! Can change program without rewiring. John von Neumann Outgrowth of Alan Turing's work. (stay tuned) All modern computers are general-purpose computers and have same (von Neumann) architecture. Maurice Wilkes (left) EDSAC (right) 8
Harvard vs. Princeton Harvard architecture.! Separate program and data memories.! Can't 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. Q. What's the difference between Harvard and Princeton? A. At Princeton, data and programs are the same. 9
TOY Reference Card Format Format 5 4 3 9 8 7 6 5 4 6 3 opcode dest d source s source t opcode dest d addr # Operation Fmt Pseudocode : halt exit() : add R[d] " R[s] + R[t] : 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 B: store indirect C: branch zero R[d] " mem[r[t]] mem[r[t]] " R[d] if (R[d] == ) pc " addr Register always reads. Loads from mem[ff] from stdin. Stores to mem[ff] to stdout. D: branch positive E: jump register F: jump and link if (R[d] > ) pc " addr pc " R[d] R[d] " pc; pc " addr 6-bit registers. 6-bit memory. 8-bit program counter. 3