An Elegant Weapon for a More Civilized Age

Similar documents
EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

CPSC W Term 2 Problem Set #3 - Solution

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CIT Week13 Lecture

Machine-level Programming (3)

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

You may work with a partner on this quiz; both of you must submit your answers.

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

CPS104 Recitation: Assembly Programming

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

THEORY OF COMPILATION

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

Register Allocation, iii. Bringing in functions & using spilling & coalescing

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Instruction Set Architecture

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

COMP 210 Example Question Exam 2 (Solutions at the bottom)

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

Process Layout and Function Calls

Assignment 11: functions, calling conventions, and the stack

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

Intel assembly language using gcc

Machine-Level Programming III: Procedures

CSC 2400: Computing Systems. X86 Assembly: Function Calls

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Compiler Construction D7011E

Procedure-Calling Conventions October 30

CS241 Computer Organization Spring 2015 IA

Sungkyunkwan University

Machine Representa/on of Programs: Control Flow cont d. Previous lecture. Do- While loop. While- Do loop CS Instructors: Sanjeev Se(a

x86 assembly CS449 Fall 2017

CS213. Machine-Level Programming III: Procedures

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Process Layout, Function Calls, and the Heap

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

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Sungkyunkwan University

Instruction Set Architecture

Instruction Set Architecture

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CS241 Computer Organization Spring Loops & Arrays

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

CMSC 313 Lecture 12 [draft] How C functions pass parameters

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Lab 10: Introduction to x86 Assembly

Instruction Set Architecture

CS642: Computer Security

CISC 360 Instruction Set Architecture

X86 Assembly -Procedure II:1

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

CS61 Section Solutions 3

Machine Level Programming II: Arithmetic &Control

Code Generation. Lecture 30

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

Computer Science 104:! Y86 & Single Cycle Processor Design!

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

System Programming and Computer Architecture (Fall 2009)

Lecture 10: Recursion vs Iteration

Instructor: Alvin R. Lebeck

The Hardware/Software Interface CSE351 Spring 2013

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

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Control flow. Condition codes Conditional and unconditional jumps Loops Switch statements

W4118: PC Hardware and x86. Junfeng Yang

Systems I. Machine-Level Programming V: Procedures

Machine Programming 2: Control flow

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS61, Fall 2012 Midterm Review Section

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Assembly Language Programming - III

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

Machine-Level Programming II: Control Flow

X86 Stack Calling Function POV

Machine Programming 3: Procedures

Computer Science 104:! Y86 & Single Cycle Processor Design!

Machine-Level Programming II: Control and Arithmetic

Assembly I: Basic Operations. Jo, Heeseung

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

Transcription:

An Elegant Weapon for a More Civilized Age

Solving an Easy Problem What are the input types? What is the output type? Give example input/output pairs Which input represents the domain of the recursion, ie, which input becomes smaller? How is problem size defined? What function is used to produce smaller problem instances? What functions can construct the output type? What is the output value when the problem is smallest?

Solving an Easy Problem (contd) How can a problem instance be reduced to one or more smaller problem instances? What function creates the output value? Is your case analysis correct and complete? If an input can be of more than one type, eg, sometimes an atom, sometimes a pair, then you will need to provide a case for each type

Solving a Hard Problem Identify one (or more) sub problems that would make the hard problem into an easy problem if solved Give example input/output pairs for helper functions which would solve the sub problems Define the helper functions and test your solutions If any of the sub problems are hard themselves then identify additional helper functions which would permit you to solve them

Debugging Imperative Programs An imperative program is understood by the programmer as a process which transforms the state of an abstract machine The state of the abstract machine is comprised of the values of variables and the contents of the stack and heap By observing how the values of variables change over time, the programmer verifies that the process is defined correctly

Debugging Functional Programs A functional program is understood by the programmer as the definition of the solution to a problem A functional programmer fixes errors by reformulating this definition using new terms These terms are the solutions of sub problems each of which can be independently verified by testing A functional program is debugged by rewriting it using simpler and simpler pieces until each piece is demonstrably correct

Compiling Function Calls in C A function's local environment consists of the values bound to its parameters and local variables When a function is called, the local environment of the calling function is pushed onto the call stack The saved local environment is termed an activation record A return statement pops the call stack and restores the local environment

Recursion is Expensive! Repeatedly saving and restoring the contexts associated with function calls requires time The saved contexts cause the call stack to grow

n! Two Ways int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } int fact(int n, int acc) { if (n == 0) return acc; else return fact(n-1, acc*n); }

n! Two Ways int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } context disgarded context saved context used int fact(int n, int acc) { if (n == 0) return acc; else return fact(n-1, acc*n); }

n! Two Ways int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } context disgarded context saved context used int fact(int n, int acc) { if (n == 0) return acc; else return fact(n-1, acc*n); } context disgarded context saved

Sisyphus

Tail Call Optimization A good compiler* will recognize the pointlessness of the push-pop sequence and compile the tail call as a jump This saves the expense of saving and restoring the local environment The call stack does not grow Tail recursion is as efficient as iteration! *gcc optimizes tail calls when you use -O3 or higher

Compiler Object Code gcc -O1 gcc -O4 fact: fact: push pushl %ebp push pushl %ebp movl %esp, %ebp movl %esp, %ebp $8, %esp movl 8(%ebp), %eax subtraction subl movl 8(%ebp), %ecx movl 12(%ebp), %edx movl 12(%ebp), %edx p2align 4,,15 movl %ecx, %eax L8: test testl %edx, %edx test testl %edx, %edx je L1 je L9 leal -1(%edx), %eax multiplication imull %edx, %eax movl %eax, 4(%esp) subtraction decl %edx movl %ecx, %eax jump jmp L8 multiplication imull %edx, %eax L9: movl %eax, (%esp) pop popl %ebp function call call fact ret loop body L1: movl %ebp, %esp function body %ebp pop popl ret

Fibonacci Numbers Three Ways int fib(int n) { if (n < 2) return n; else return fib(n-1) + fib(n-2); }

Fibonacci Numbers Three Ways int fib(int n) { if (n < 2) return n; else return fib(n-1) + fib(n-2); } O(2n) space and time complexity!

Fibonacci Numbers Tree Ways int fib(int n) { int temp; int acc0 = 0, acc1 = 1; while (n > 0) { temp = acc0; acc0 = acc1; acc1 += temp; n--; } return acc0; }

Fibonacci Numbers Three Ways int fib(int n, int acc0, int acc1) { if (n == 0)return acc0; else return fib(n-1, acc1, acc0+acc1); }

Fibonacci Numbers Three Ways int fib(int n, int acc0, int acc1) { if (n == 0)return acc0; else return fib(n-1, acc1, acc0+acc1); }

Tail Positions Tail positions are shown in red: (if pred val1 val2 ) (cond (pred1 val1 ) (else valn )) (or pred1 pred2 predn-1 predn ) (and pred1 pred2 predn-1 predn ) (predn-1 valn-1 ) These positions within special forms in tail positions are also tail positions!

Identify the Tail Positions (and a b (if x y z) c) (if a (if x y z) (if u v w)) (or a (and a b)) (if a (or b c d) e) (cond (a (if a b c)) (x y) (else z)) (if (if a b c) x y) (cond (x y) ((if a b c) d) (else (or u v)))

Identify the Tail Positions (and a b (if x y z) c) (if a (if x y z) (if u v w)) (or a (and a b)) (if a (or b c d) e) (cond (a (if a b c)) (x y) (else z)) (if (if a b c) x y) (cond (x y) ((if a b c) d) (else (or u v)))

let, let* and letrec special-forms collateral sequential recursive (let ((var1 val1 ) (let* ((var1 val1 ) (letrec ((var1 val1 ) (var2 val2 ) (var2 val2 ) (var2 val2 ) (varn valn )) (varn valn )) (varn valn )) body) body) body) scope of var1

let is just lambda! (let ((var1 val1 ) (var2 val2 ) (varn valn )) body) ((lambda (var1 var2 varn ) body) (val1 val2 valn ))

Example (let ((x 2) (y 3)) (+ x y)) ((lambda (x y) (+ x y)) 2 3)

let* is just nested let's! (let* ((var1 val1 ) (var2 val2 ) (varn valn )) body) (let ((var1 val1 )) (let ((var2 val2 )) (let ((varn valn )) body) ))

Example (let* ((x 2)(y 3)) (+ x y)) (let ((x 2))(let ((y 3)) (+ x y))) ((lambda (x) ((lambda (y) (+ x y)) 3) 2)

Also Tail Positions! collateral sequential recursive (let ((var1 val1 ) (let* ((var1 val1 ) (letrec ((var1 val1 ) (var2 val2 ) (var2 val2 ) (var2 val2 ) (varn valn )) (varn valn )) (varn valn )) body) body) body)

Lisp