Register Machines. Connecting evaluators to low level machine code

Similar documents
Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

6.001 Recitation 23: Register Machines and Stack Frames

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

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

Special Section: Building Your Own Compiler

Programming Model 2 A. Introduction

Konzepte von Programmiersprachen

In this chapter you ll learn:

Microcomputer Architecture and Programming

The Eval/Apply Cycle Eval. Evaluation and universal machines. Examining the role of Eval. Eval from perspective of language designer

The MARIE Architecture

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Control & Execution. Finite State Machines for Control. MIPS Execution. Comp 411. L14 Control & Execution 1

Machine code. Nils Jansen December 12, 2017

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

Basic Processing Unit: Some Fundamental Concepts, Execution of a. Complete Instruction, Multiple Bus Organization, Hard-wired Control,

Problem with Scanning an Infix Expression

6.001, Spring Semester, 1998, Final Exam Your Name: 2 Question 1 (12 points): Each of the parts below should be treated as independent. For each part,

YOUR NAME PLEASE: *** SOLUTIONS ***

Flowchart & Algorithm

Micro-Operations. execution of a sequence of steps, i.e., cycles

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

The overall datapath for RT, lw,sw beq instrucution

Chapter 2 Instruction Set Architecture

Compiling Code, Procedures and Stacks

CPE300: Digital System Architecture and Design

Branch Instructions. R type: Cond

Chapter 2: Instructions How we talk to the computer

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

INSTRUCTION SET OF 8085

Subprograms, Subroutines, and Functions

Chapter 3 BRANCH, CALL, AND TIME DELAY LOOP

EEL 4744C: Microprocessor Applications. Lecture 7. Part 1. Interrupt. Dr. Tao Li 1

Reading Assignment. Interrupt. Interrupt. Interrupt. EEL 4744C: Microprocessor Applications. Lecture 7. Part 1

Case by Case. Chapter 3

Parallel logic circuits

CSE 140L Spring 2010 Bonus Final

Digital System Design Using Verilog. - Processing Unit Design

16.1. Unit 16. Computer Organization Design of a Simple Processor

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Advanced Computer Architecture

G Programming Languages - Fall 2012

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

BeiHang Short Course, Part 2: Description and Synthesis

The MIPS Processor Datapath

Chapter 20 - Microprogrammed Control (9 th edition)

Bonus Lecture: Fibonacci

LECTURE 16. Functional Programming

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

Module 5 - CPU Design

Problem with Scanning an Infix Expression

COSC 122 Computer Fluency. Computer Organization. Dr. Ramon Lawrence University of British Columbia Okanagan

Three-address code (TAC) TDT4205 Lecture 16

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

361 div.1. Computer Architecture EECS 361 Lecture 7: ALU Design : Division

Programming Fundamentals

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

CS 2210 Programming Project (Part IV)

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Format. 10 multiple choice 8 points each. 1 short answer 20 points. Same basic principals as the midterm

Register Are Two Names For The Same Place

Problem Set 1 Solutions

DEEPIKA KAMBOJ UNIT 2. What is Stack?

We can create PDAs with multiple stacks. At each step we look at the current state, the current input symbol, and the top of each stack.

(2) Explain the addressing mode of OR What do you mean by addressing mode? Explain diff. addressing mode for 8085 with examples.

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -


G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation

COMP 382: Reasoning about algorithms

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

An Introduction to Scheme

COS 126 Written Exam 2 (Spring 2015)

MICROPROGRAMMED CONTROL

William Stallings Computer Organization and Architecture

Chapter 2A Instructions: Language of the Computer

MICROPROCESSOR B.Tech. th ECE

Blog -

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

Mark Redekopp and Gandhi Puvvada, All rights reserved. EE 357 Unit 15. Single-Cycle CPU Datapath and Control

Parsing Scheme (+ (* 2 3) 1) * 1

The Tiny CPU Generator (TCG)

What is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis:

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

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

Multi-cycle Approach. Single cycle CPU. Multi-cycle CPU. Requires state elements to hold intermediate values. one clock cycle or instruction

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

The Itanium Bit Microprocessor Report

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

TDT4255 Computer Design. Lecture 4. Magnus Jahre. TDT4255 Computer Design

8085 INSTRUCTION SET INSTRUCTION DETAILS

Transcription:

Register Machines Connecting evaluators to low level machine code 1

Plan Design a central processing unit (CPU) from: wires logic (networks of AND gates, OR gates, etc) registers control sequencer Our CPU will interpret Scheme as its machine language Today: Iterative algorithms in hardware Recursive algorithms in hardware Then: Scheme in hardware (EC-EVAL) EC-EVAL exposes more details of scheme than M-EVAL 2

The ultimate goal 24 48 430 GCD 46 (define (gcd a b).) Circuit diagram Procedure description 48 30 Ultimate machine 6 48 30 Universal machine 6 3

A universal machine Existence of a universal machine has major implications for what computation means Insight due to Alan Turing (1912-1954) On computable numbers with an application to the Entscheidungsproblem, A.M. Turing, Proc. London Math. Society, 2:42, 1937 Hilbert s Entscheidungsproblem (decision problem) 1900: Is mathematics decidable? That is, is there a definite method guaranteed to produce a correct decision about all assertions in mathematics? Church-Turing thesis: Any procedure that could reasonably be considered to be an effective procedure can be carried out by a universal machine (and thus by any universal machine) 4

Euclid's algorithm to compute GCD (define (gcd a b) (if (= b 0) a (gcd b (remainder a b)))) Given some numbers a and b If b is 0, done (the answer is a) If b is not 0: the new value of a is the old value of b the new value of b is the remainder of a b start again 5

Example register machine: datapaths a b = register rem 0 test operation constant button t wire 6

Example register machine: instructions label operations (controller test-b (test (op =) (reg b) (const 0)) (branch (label gcd-done)) (assign t (op rem) (reg a) (reg b)) (assign a (reg b)) (assign b (reg t)) (goto (label test-b)) gcd-done) 7

Complete register machine condition a b = sequencer program counter rem 0 instructions t 8

Datapath components Button when pressed, value on input wire flows to output Register output the stored value continuously change value when button on input wire is pressed Operation output wire value = some function of input wire values Test an operation output is one bit (true or false) output wire goes to condition register 9

Incrementing a register X 0 Y sum? 0 1 2 1 an op that adds its inputs + What sequence of button presses will result in the register sum containing the value 2? X Y Y? 1 2 3 press sum? X 0 Y 1 Y 2 10

Euclid's algorithm to compute GCD (define (gcd a b) (if (= b 0) a (gcd b (remainder a b)))) Given some numbers a and b If b is 0, done (the answer is a) If b is not 0: the new value of a is the old value of b the new value of b is the remainder of a b start again 11

Datapath for GCD (partial) a X b What sequence of button presses will result in: the register a containing GCD(a,b) the register b containing 0 The operation rem computes the remainder of a b rem Z Y press a b t 9 6? Z 9 6 3 X 6 6 3 Y 6 3 3 Z 6 3 0 X 3 3 0 Y 3 0 0 t 12

Example register machine: instructions (controller test-b (test (op =) (reg b) (const 0)) (branch (label gcd-done)) (assign t (op rem) (reg a) (reg b)) (assign a (reg b)) (assign b (reg t)) (goto (label test-b)) gcd-done) 13

Instructions Controller: generates a sequence of button presses sequencer instructions Sequencer: activates instructions sequentially program counter remembers which one is next Each instruction: commands a button press, OR changes the program counter called a branch instruction 14

Button-press instructions: the sum example X 0 Y sum 1 + (controller (assign sum (const 0)) <X> (assign sum (op +) (reg sum) (const 1)) <Y> (assign sum (op +) (reg sum) (const 1))) 15

Unconditional branch X Y 0 sum 1 sequencer: nextpc <- PC + 1 activate instruction at PC PC <- nextpc start again (controller 0 (assign sum (const 0)) increment 1 (assign sum (op +) (reg sum) (const 1)) 2 (goto (label increment))) + PC nextpc press 0 1 X 1 2 Y 2 3 1 -- 1 2 Y 2 3 1 -- 16

Conditional branch condition a b = sequencer program counter rem 0 insts (controller test-b (test (op =) (reg b) (const 0)) (branch (label gcd-done)) (assign t (op rem) (reg a) (reg b)) (assign a (reg b)) (assign b (reg t)) (goto (label test-b)) gcd-done) t 17

Conditional branch details (test (op =) (reg b) (const 0)) push the button which loads the condition register from this operation's output (branch (label gcd-done)) Overwrite nextpc register with value if condition register is TRUE No effect if condition register is FALSE 18

Datapaths are redundant We can always draw the data path required for an instruction sequence Therefore, we can leave out the data path when describing a register machine 19

Abstract operations Every operation shown so far is abstract: abstract = consists of multiple lower-level operations Lower-level operations might be: AND gates, OR gates, etc (hardware building-blocks) sequences of register machine instructions Example: GCD machine uses (assign t (op rem) (reg a) (reg b)) Rewrite this using lower-level operations 20

Less-abstract GCD machine (controller test-b (test (op =) (reg b) (const 0)) (branch (label gcd-done)) ; (assign t (op rem) (reg a) (reg b)) (assign t (reg a)) rem-loop (test (op <) (reg t) (reg b)) (branch (label rem-done)) (assign t (op -) (reg t) (reg b)) (goto (label rem-loop)) rem-done (assign a (reg b)) (assign b (reg t)) (goto (label test-b)) gcd-done) 21

Importance of register machine abstraction A CPU is a very complicated device We will study only the core of the CPU eval, apply, etc. We will use abstract register-machine operations for all the other instruction sequences and circuits: (test (op self-evaluating?) (reg exp)) remember,(op +) is abstract, (op <) is abstract, etc. no magic in (op self-evaluating?) 22

Review of register machines Registers hold data values Controller specifies sequence of instructions, order of execution controlled by program counter Assign puts value into register Constants Contents of register Result of primitive operation Goto changes value of program counter, and jumps to label Test examines value of a condition, setting a flag Branch resets program counter to new value, if flag is true Data paths are redundant 23

Machines for recursive algorithms GCD, odd?, increment iterative, constant space factorial, EC-EVAL recursive, non-constant space Extend register machines with subroutines and stack Main points Every subroutine has a contract Stacks are THE implementation mechanism for recursive algorithms 24

Part 1: Subroutines Subroutine: a sequence of instructions that starts with a label and ends with an indirect branch can be called from multiple places New register machine instructions (assign continue (label after-call-1)) store the instruction number corresponding to label after-call-1 in register continue this instruction number is called the return point (goto (reg continue)) an indirect branch change the PC to the value stored in register continue 25

Example subroutine: increment set sum to 0, then increment, then increment again dotted line: subroutine blue: call green: label red: indirect jump (controller (assign (reg sum) (const 0)) (assign continue (label after-call-1)) (goto (label increment)) after-call-1 (assign continue (label after-call-2)) (goto (label increment)) after-call-2 (goto (label done)) increment (assign sum (op +) (reg sum) (const 1)) (goto (reg continue)) done) 26

Subroutines have contracts Follow the contract or register machine will fail: registers containing input values and return point registers in which output is produced registers that will be overwritten in addition to the output registers increment (assign sum (op +) (reg sum) (const 1)) (goto (reg continue)) subroutine increment input: sum, continue output: sum writes: none 27

End of part 1 Why subroutines? reuse instructions reuse data path components make instruction sequence more readable just like using helper functions in scheme support recursion Contracts specify inputs, outputs, and registers used by subroutine 28

Part 2: Stacks Stack: a memory device save a register: restore a register: send its value to the stack get a value from the stack When this machine halts, b contains 0: 0 (controller (assign a (const 0)) (assign b (const 5)) (save a) (restore b) ) 5 a b stack 29

Stacks: hold many values, last-in first-out This machine halts with 5 in a and 0 in b (controller 0 (assign a (const 0)) 1 (assign b (const 5)) 2 (save a) 3 (save b) 4 (restore a) 5 (restore b)) contents of stack after step 2 3 4 5 0 5 0 0 empty 5 is the top of stack after step 3 save: put a new value on top of the stack restore: remove the value at top of stack 30

Check your understanding Draw the stack after step 5. What is the top of stack value? Add restores so final state is a: 3, b: 5, c: 8, and stack is empty (controller 0 (assign a (const 8)) 1 (assign b (const 3)) 2 (assign c (const 5)) 3 (save b) 4 (save c) 5 (save a) ) (restore c) (restore b) (restore a) 8 5 3 32

Things to know about stacks stack depth stacks and subroutine contracts tail-call optimization 33

Stack depth depth of the stack = number of values it contains At any point while the machine is executing stack depth = (total # of saves) - (total # of restores) stack depth limits: low: 0 (machine fails if restore when stack empty) high: amount of memory available max stack depth: measures the space required by an algorithm 34

Stacks and subroutine contracts Standard contract: subroutine increment input: output: sum writes: none stack: sum, continue unchanged Rare contract: strange (assign val (op *) (reg val) (const 2)) (restore continue) (goto (reg continue)) input: output: writes: stack: val, return point on top of stack val continue top element removed 35

Optimizing tail calls no work after call except (goto (reg continue)) setup Unoptimized version (assign sum (const 15)) (save continue) (assign continue (label after-call)) (goto (label increment)) after-call (restore continue) (goto (reg continue)) setup Optimized version (assign sum (const 15)) (goto (label increment)) This optimization is important in EC-EVAL Iterative algorithms expressed as recursive procedures would use non-constant space without it 36

End of part 2 stack a LIFO memory device save: put data on top of the stack restore: remove data from top of the stack things to know concept of stack depth expectations and effect on stack is part of the contract tail call optimization 37

Part 3: recursion (define (fact n) (if (= n 1) 1 (* n (fact (- n 1))))) (fact 3) (* 3 (fact 2)) (* 3 (* 2 (fact 1))) (* 3 (* 2 1)) (* 3 2) 6 The stack is the key mechanism for recursion remembers return point of each recursive call remembers intermediate values (eg., n) 38

(controller (assign continue (label halt)) fact r-done b-case halt) (test (op =) (reg n) (const 1)) (branch (label b-case)) (save continue) (save n) (assign n (op -) (reg n) (const 1)) (assign continue (label r-done)) (goto (label fact)) (restore n) (restore continue) (assign val (op *) (reg n) (reg val)) (goto (reg continue)) (assign val (const 1)) (goto (reg continue)) 39

Code: base case (define (fact n) (if (= n 1) 1...)) fact (test (op =) (reg n) (const 1)) (branch (label b-case))... b-case (assign val (const 1)) (goto (reg continue)) fact expects its input in which register? fact expects its return point in which register? fact produces its output in which register? n continue val 40

Code: recursive call (define (fact n)... (fact (- n 1))...) r-done... (assign n (op -) (reg n) (const 1)) (assign continue (label r-done)) (goto (label fact))... At r-done, which register will contain the return value of the recursive call? val 41

Code: after recursive call (define (fact n)... (* n <return-value> )...) (assign val (op *) (reg n) (reg val)) (goto (reg continue)) Problem! Overwrote register n as part of recursive call Also overwrote continue 42

Code: complete recursive case (save continue) (save n) (assign n (op -) (reg n) (const 1)) (assign continue (label r-done)) (goto (label fact)) r-done (restore n) (restore continue) (assign val (op *) (reg n) (reg val)) (goto (reg continue)) Save a register if: value is used after call AND register is not output of subroutine (register written as part of call OR register written by subroutine) AND 43

Check your understanding Write down the contract for subroutine fact input: n, continue output: val writes: none stack: unchanged Writes none? writes n and continue but saves them before writing, restores after 45

Execution trace Contents of registers and stack at each label Top of stack at left label continue n val stack fact halt 3??? empty fact r-done 2??? 3 halt fact r-done 1??? 2 r-done 3 halt b-case r-done 1??? 2 r-done 3 halt r-done r-done 1 1 2 r-done 3 halt r-done r-done 2 2 3 halt halt halt 3 6 empty Contents of stack represents pending operations (* 3 (* 2 (fact 1))) at base case 46

End of part 3 To implement recursion, use a stack stack records pending work and return points max stack depth = space required (for most algorithms) 47

Where we are headed Next time will use register machine idea to implement an evaluator This will allow us to capture high level abstractions of Scheme while connecting to low level machine architecture 48