L12-Computing Factorial

Slides:



Advertisements
Similar presentations
Introduction to Computer Engineering ECE 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin –
Advertisements

Computer Science 210 Computer Organization Recursive Subroutines System Stack Management.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL Subroutine and Interrupt.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 3: Input/output and co-processors dr.ir. A.C. Verschueren.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Intro to Computer Architecture
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
ECE Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Objective At the conclusion of this chapter you will be able to:
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
ECE Lecture 1 1 L7 – A First Program Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
ECE Lecture 1 1 L11-HLL to Assembler Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
ECE Lecture 1 1 L8:Flowcharting a program Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
by Richard P. Paul, 2nd edition, 2000.
M. Mateen Yaqoob The University of Lahore Spring 2014.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
ECE Lecture 1 1 L13a – Integer add Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
Pushing the Return Address To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed. But this.
ECE Lecture 1 1 The Hardware Multiplier Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
ECE291 Computer Engineering II Lecture 8 Josh Potts University of Illinois at Urbana- Champaign.
MicroComputer Engineering ActivationConcept page 1 Problem: !bell!help! !1! 2! !Bal help! !!! Bal bell! 3Jr $31!!! !Jr $31 ! What happend here? What will.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
Computer Science 210 Computer Organization
Section 5: Procedures & Stacks
Chapter 14 Functions.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Run-Time Environments Chapter 7
Prof. Hsien-Hsin Sean Lee
Subroutines and the Stack
Microprocessor and Assembly Language
CS 301 Fall 2002 Control Structures
Application Binary Interface (ABI)
Final Exam Review Department of Electrical and Computer Engineering
Microprocessor and Assembly Language
EECE.3170 Microprocessor Systems Design I
L13b – 32 bit multiply Department of Electrical and
by Richard P. Paul, 2nd edition, 2000.
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Chapter 6 - Procedures and Macros
Computer Science 210 Computer Organization
EECE.3170 Microprocessor Systems Design I
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Method of Classes Chapter 7, page 155 Lecture /4/6.
CSC 143 Recursion.
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Branch instructions Branch : B{<cond>} label
Subroutines and the Stack
Computer Organization and Assembly Language
CSC 497/583 Advanced Topics in Computer Security
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Lecture 3 - Instruction Set - Al
MIPS R3000 Subroutine Calls and Stack
Procedures and Macros.
Lecture 3 - Instruction Set - Al
Presentation transcript:

L12-Computing Factorial ECE 2560 L12-Computing Factorial Department of Electrical and Computer Engineering The Ohio State University ECE 3561 - Lecture 1

HLL to Assembler Pseudo HLL HLL structure Their flow chart HHL code Corresponding Assembler ECE 3561 - Lecture 1

Computing n! What is n! ? This can be done recursively n factorial is n*(n-1)*(n-2)*…(1) n! = n*(n-1)! This can be done recursively Have a subroutine to compute n! In the computation is n=1 then result is 1 If n>1 then the value is n*(n-1)! And to compute (n-1)! the routine called again ECE 3561 - Lecture 1

Pseudocode Factorial – n! function nfact (var n) return z; if (n=1) then z = 1; else z = n * nfact(n-1); end if; end nfact; ECE 3561 - Lecture 1

Features of recursive routines Can have no local data that could be modified in the recursive call Data needs to be on the stack Any register used must be restored before the routine returns. Because there is no local data and no absolute addresses the routine is also “position independent” Position independent – code that can be run in any location in memory. ECE 3561 - Lecture 1

Factorial - recursion Arguments will be passed on the stack. The number n to find n! of is pushed on TOS Result is returned on the stack. The value of n! is returned on the TOS ECE 3561 - Lecture 1

The routine N is passed on the stack so the stack looks like this when entering routine So start by saving the state of the processor ECE 3561 - Lecture 1

The code fact push SR ;save state push R5 push R6 push R7 mov 10(SP),R5 ;get n cmp #1,R5 jeq rtnval dec R5 ;R5=n-1 call fact ;compute n-1! ECE 3561 - Lecture 1

On Recursive call The stack will look like this Now what happens when you start to return with values? ECE 3561 - Lecture 1

Result - on the stack Point size is very small – look at it in word Result is on the stack and at the top of the stack. fact push SR ;save state push R5 push R6 push R7 mov 10(SP),R5 ;get n cmp #1,R5 jjeq rtnval dec R5 ;R5=n-1 call fact ;computer n-1! inc R5 ;R5 now n pop R6 ;n-1 fact to R6 push R6 call shmult ;stack has null then result pop R7 jmp cont rtnval mov #1,R7 cont mov R7,10(SP) ;put value of n! in rtn location pop R7 ;clean up things pop R6 pop R5 pop SR ret Point size is very small – look at it in word ECE 3561 - Lecture 1

Performance of code Code is an extensive use of stack and implementation of good coding practices. Passing arguments on stack Routines have no side effects ECE 3561 - Lecture 1

Extending to 32 bit result Factorial when result limited to 16 bits does not allow for much range for factorial. 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 In a 16-bit value 8! Is the largest that can be computed. Could 32-bits be used? ECE 3561 - Lecture 1

A look at the math The math ECE 3561 - Lecture 1