What is TOY? An imaginary machine similar to: ! Ancient computers. ! Today's microprocessors.

Similar documents
! Ancient computers. ! Today's microprocessors. Memory. ! Stores data and programs. ! 256 "words." (16 bits each) ! Special word for stdin / stdout.

11. A Computing Machine

TOY II LINC LINC. !1 Introduction to Computer Science Sedgewick and Wayne Copyright 2007

5. The TOY Machine II

12/11/ The TOY Machine II. Data Representation. What We've Learned About TOY. What We Do Today. Adding and Subtracting Binary Numbers

! Binary and hex. ! Box with switches and lights. ! 4,328 bits = (255 " 16) + (15 " 16) + (8) = 541 bytes! ! von Neumann architecture.

Lecture A2: X-TOY Programming

Computer Architecture /

COS 126 Midterm 1 Written Exam Spring 2015

CS.17.A.MachineI.Overview

COS 126 General Computer Science Fall Exam 1

COS 126 Written Exam 2 (Spring 2015)

COS 126 General Computer Science Fall Midterm 1

Computer Science. 18. von Neumann Machines. Computer Science COMPUTER SCIENCE. Sections

CIS 110 Introduction to Computer Programming. 17 December 2012 Final Exam

2. Computer Evolution and Performance

Lecture Topics. Announcements. Today: The MIPS ISA (P&H ) Next: continued. Milestone #1 (due 1/26) Milestone #2 (due 2/2)

Chapter 4 The Von Neumann Model

CS 31: Intro to Systems Digital Logic. Kevin Webb Swarthmore College February 3, 2015

CS 31: Intro to Systems Digital Logic. Kevin Webb Swarthmore College February 2, 2016

C++ to assembly to machine code

Chapter 4 The Von Neumann Model

Virtual machines. Virtual machines. Abstractions for computers. Abstractions for computers. Virtual machines

18. Machine Language. Computer Systems. COMP1917: Computing 1. Machine Language Programming. History of Computer Technology

EC-801 Advanced Computer Architecture

COS 126 Written Exam 2 Spring 18

Introduction to Computer Engineering. CS/ECE 252 Prof. Mark D. Hill Computer Sciences Department University of Wisconsin Madison

COS 126 General Computer Science Fall Exam 1

Chapter 4 The Von Neumann Model

CSEE 3827: Fundamentals of Computer Systems

Lecture 4: MIPS Instruction Set

CIS 110 Introduction to Computer Programming. 7 May 2012 Final Exam

Advanced Parallel Architecture Lesson 3. Annalisa Massini /2015

LC-3 Architecture. (Ch4 ish material)

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Computing Layers

Computer Organization II CMSC 3833 Lecture 33

6.1 Combinational Circuits. George Boole ( ) Claude Shannon ( )

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

COS 126 General Computer Science Spring Written Exam 1

Agenda EE 224: INTRODUCTION TO DIGITAL CIRCUITS & COMPUTER DESIGN. Lecture 1: Introduction. Go over the syllabus 3/31/2010

Computer Architecture Review. ICS332 - Spring 2016 Operating Systems

11/22/1999 7pm - 9pm. Name: Login Name: Preceptor Name: Precept Number:

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

THE MICROPROCESSOR Von Neumann s Architecture Model

Notes: The Marie Simulator

Designing an Instruction Set

CS 24: INTRODUCTION TO. Spring 2018 Lecture 3 COMPUTING SYSTEMS

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

von Neumann Architecture Basic Computer System Early Computers Microprocessor Reading Assignment An Introduction to Computer Architecture

Basic Computer System. von Neumann Architecture. Reading Assignment. An Introduction to Computer Architecture. EEL 4744C: Microprocessor Applications

Instruction Set Design

Lecture 2: RISC V Instruction Set Architecture. Housekeeping

Hardware Design I Chap. 10 Design of microprocessor

Typical Processor Execution Cycle

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Chapter 2A Instructions: Language of the Computer

RISC Architecture Ch 12

Lecture 10: Simple Data Path

ISA and RISCV. CASS 2018 Lavanya Ramapantulu

Software and Hardware

Unsigned Binary Integers

Unsigned Binary Integers

William Stallings Computer Organization and Architecture 8 th Edition. Chapter 2 Computer Evolution and Performance

Outline. EEL-4713 Computer Architecture Designing a Single Cycle Datapath

The von Neumann Architecture. IT 3123 Hardware and Software Concepts. The Instruction Cycle. Registers. LMC Executes a Store.

are Softw Instruction Set Architecture Microarchitecture are rdw

Introduction to Computer Science. Homework 1

The Modern Computer. Exploring Computer Science

Lecture 2: RISC V Instruction Set Architecture. James C. Hoe Department of ECE Carnegie Mellon University

Programmable Machines

Computer Architecture

Programmable Machines

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

2.2 THE MARIE Instruction Set Architecture

Welcome to COS151! 1.1

361 datapath.1. Computer Architecture EECS 361 Lecture 8: Designing a Single Cycle Datapath

9. Programmable Machines

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 01, SPRING 2013

Introduction to Digital Logic

Chronological History of the Pre-Computer Developments

Chapter 4. The Processor

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Fundamental concepts of Information Technology

Computer Systems. Binary Representation. Binary Representation. Logical Computation: Boolean Algebra

Chapter 4. The Processor

EE 3170 Microcontroller Applications

CENG 3420 Computer Organization and Design. Lecture 06: MIPS Processor - I. Bei Yu

Computer Organization. Structure of a Computer. Registers. Register Transfer. Register Files. Memories

CSCI 402: Computer Architectures. Instructions: Language of the Computer (1) Fengguang Song Department of Computer & Information Science IUPUI

The MIPS Processor Datapath

Instruction Set Architecture. "Speaking with the computer"

ELEC 5200/6200 Computer Architecture and Design Spring 2017 Lecture 4: Datapath and Control

CENG 3420 Lecture 06: Datapath

Topic Notes: MIPS Instruction Set Architecture

A Processor. Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University. See: P&H Chapter , 4.1-3

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

CS 61C: Great Ideas in Computer Architecture RISC-V Instruction Formats

9/14/17. Levels of Representation/Interpretation. Big Idea: Stored-Program Computer

( input = α output = λ move = L )

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Transcription:

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