Procedures 2a MIPS code examples Making Use of a Stack.

Slides:



Advertisements
Similar presentations
Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
Advertisements

The University of Adelaide, School of Computer Science
Recursion in MIPS Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Leaf and Non-Leaf Procedures A leaf procedure is one that.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
The University of Adelaide, School of Computer Science
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
CS 536 Spring Code generation I Lecture 20.
Intro to Computer Architecture
Procedures I Fall 2005 C functions main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */ int mult (int mcand,
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Subroutines, parameters and the stack Bryan Duggan.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Architecture & Operations I
Function Calls in MIPS To call a function: jal func
Computer Science 210 Computer Organization
MIPS Assembly Language Programming
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
CSCI206 - Computer Organization & Programming
MIPS Procedures.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Functions.
MIPS functions.
MIPS Procedures.
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS Functions.
Program and memory layout
Systems Architecture I
Program and memory layout
Computer Architecture
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
MIPS Functions.
MIPS function continued
MIPS Functions.
Presentation transcript:

Procedures 2a MIPS code examples Making Use of a Stack

Outline Assumptions for example Implementation: sprod/prod/add2 Example: factorial

Assumptions for Example We suppose the following: –x1,y1,x2,y2 stored in memory starting at location X –Procedures: »add2 - adds together $4, $5 puts result in $6 »prod - multiplies $4, $5 puts result in $6 »sprod – (x1 * y1) + (x2 * y2), puts result in $8 –$29 is the stack pointer (address of ‘top’ of stack) –The stack in MIPS grows downwards »so that subtracting 4 from $29 makes room for a push »and adding 4 to $29 adjusts for a pop

Strategy for example: –First examine code for simple procedures: »Prod »Add2 –Then examine code for more complex procedure: »sprod

Prod prod :addi $29,$29,-4#step 1 of push sw $31,0($29)#step 2 of push $31 mult $6,$4,$5 lw $31,0($29)#step 1 of pop: # restore return address addi $29,$29,4 #step 2 of pop: # adjust top of stack jr $31 #return

add2 add2 :addi $29,$29,-4#step 1 of push sw $31,0($29)#step 2 of push $31 add $6,$4,$5 lw $31,0($29)#step 1 of pop: # restore return address addi $29,$29,4 #adjust top of stack jr $31 #return

Implementation of sprod sprod :lw $4,X($0)#x1 into $4 li $5, 4 lw $5,X($5)#y1 into $5 addi $29,$29,-4#step 1 for push sw $31,0($29)#step 2 for push $31 #return address now saved, #to be popped at end sprod jal prod#call prod, result in $6 add $20,$0,$6#copy result into $20 li $4, 8 lw $4,X($4)#x2 into $4 li $5, 12 lw $5,X($5)#y2 into $5 jal prod#call prod, result in $6 # continued …

sprod continued add $4,$0,$20#first result into $4 add $5,$0,$6#second result into $5 jal add2#puts result in $6 lw $31,0($29)#step 1 of pop: # restores return address addi $29,$29,4#step 2 of pop: # adjust ‘top’ of stack jr $31#return to caller of sprod

Example Factorial n! = n  (n-1) ...  2  1 4! = 4  3  2  1 = 24 4! = 4  (3  2  1) = 4  3! n! = n  (n-1)! (unless n=1, in which case n!=1) Function to implement factorial: fact(n) { if n==1 then return 1 elsereturn n*fact(n-1); }

Factorial in MIPS (Assumptions) The argument (n) is in $4 $8 is already initialised to 1 –$8 will hold the result We will use the pretend instructions – push – pop – print »for sake of simplicity.

Nearly a MIPS representation MAIN :li $4,4#the argument n = 4 li $11,1# set $11 to have the value 1 li $8,1# initialize $8 to have the value 1 jal fact#call “fact” procedure print $8 #no such instruction! exit #no such instruction - halt altogether fact:beq $4,$11,RETURN#if n==1 goto RETURN push($31) #save on stack push($4) #save argument addi $4,$4,-1#n becomes n-1 jal fact#recursive call to fact RETURN: $4 = pop() #no such instruction! $31 = pop() mult $8,$8,$4#$8 = $8*$4 jr $31#return

Summary jal –jump and link »saves return address in $31 jr $31 –jumps to return address in register $31 Stack pointer used to store nested return addresses ($29)

Next Time How to handle arguments to procedures How to write programs for the MIPS simulator SPIM Answering your questions about the Coursework.