MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail:

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
MIPS Function Continued
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
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.
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.
Computer Organization. This module surveys the physical resources of a computer system. –Basic components CPUMemoryBus I/O devices –CPU structure Registers.
CS 536 Spring Code generation I Lecture 20.
MIPS Programming Arrays Writing Procedures: Calling Convention.
Intro to Computer Architecture
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
First Programming Assignment For MIPS R3000 Processor Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
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.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
MIPS Calling Convention. Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
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.
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.
CS 312 Computer Architecture & Organization
Computer Architecture & Operations I
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.
Computer Science 210 Computer Organization
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
CS 286 Computer Organization and Architecture
© Craig Zilles (adapted from slides by Howard Huang)
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Functions and Procedures
Assembly Programming using MIPS R3000 CPU
CSCI206 - Computer Organization & Programming
Instructions - Type and Format
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Procedures.
Program and memory layout
CS 286 Computer Organization and Architecture
CS 286 Computer Architecture & Organization
Systems Architecture I
CS 286 Computer Organization and Architecture
Assembly Programming using MIPS R3000 CPU
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
CS 286 Computer Architecture & Organization
MIPS function continued
MIPS R3000 Subroutine Calls and Stack
Presentation transcript:

MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki CS 312 Computer Organization and Architecture Subcall/001

CS 312 Computer Organization and Architecture Subcall/002 Subroutine Calls in MIPS R3000 This PPT presentations describe how to write an assembly program for MIPS R3000 processor that uses subroutine calls The concept of “Stack Frame” in MIPS R3000 processor Implementation of recursive subroutine calls in MIPS R3000 assembly program

CS 312 Computer Organization and Architecture Subcall/003 Subroutine Calls in MIPS R3000 main: jr $31 High Address Low Address the first address in your memory the last address in your memory In this presentation, it is assumed that the low address appears at the top Each program executes from the top to the bottom

CS 312 Computer Organization and Architecture Subcall/004 Subroutine Calls in MIPS R3000 main: jr $31 xyz: jal xyz jr $ra      Go back to the instruction right after the “jar” instruction In MIPS R3000 assembly language: (a) Calling a subroutine (b) Returning to a calling routine “jal” instruction “jr $ra” instruction Jump to “xyz”

CS 312 Computer Organization and Architecture Subcall/005 Subroutine Calls in MIPS R3000 main: jr $31 jal xyz           xyz: jr $ra “jr $ra” instruction makes sure that the program execution flow goes back to “the right return point”

xyz: jr $ra main: jr $31 jal xyz           CS 312 Computer Organization and Architecture Subcall/006 Subroutine Calls in MIPS R3000 li $s0, 10 LOOP: sub $s0, $s0, 1 jne $s0, $zero, LOOP li $s0, 200 Processor We have one processor add $s0, $s0, 5      slt $s0, $t3, $zero       a danger in using subroutine calls in MIPS assembly programming Destroying register contents during a subroutine call S0 register as a loop counter

CS 312 Computer Organization and Architecture Subcall/007 Subroutine Calls in MIPS R3000 main: jr $31 jal xyz      xyz: jr $ra jal xyz xyz: jr $ra jal xyz xyz: jr $ra jal xyz    “recursive subroutine call” Why are recursive subroutine calls problematic in using processor registers? li $s0, 10 add $s0,... li $s0, 10 add $s0,... li $s0, 10 add $s0,... It’s guaranteed that the same registers will be used in every recursive call... What should we do this to solve this problem?

CS 312 Computer Organization and Architecture Subcall/008 Concept of stack (1) main: jr $31 jal xyz           li $s0, 10 LOOP: sub $s0, $s0, 1 jne $s0, $zero, LOOP xyz: jr $ra li $s0, 200 add $s0, $s0, 5      slt $s0, $t3, $zero      xyz: Stack Area reserve my stack space save all registers I modify restore all registers I modified release my stack space $s0

CS 312 Computer Organization and Architecture Subcall/009 main: jr $31 jal xyz           “recursive subroutine call” xyz: jr $ra jal xyz li $s0, 10 add $s0,... $s0 save $s0 restore $s0 $s0    xyz: jr $ra jal xyz li $s0, 10 add $s0,... save $s0 restore $s0 xyz: jr $ra jal xyz li $s0, 10 add $s0,... save $s0 restore $s0 Stack Each called instance of a subroutine reserves its stack space and saves the register it modifies Concept of stack (2)

CS 312 Computer Organization and Architecture main: jr $31 xyz: jr $ra jal xyz xyz: jr $ra Stack jal xyz (next instruction) $ra  Load the address of the next instruction to “$ra” register  Jump (set PC register) to the first instruction in the called subroutine  “jr $ra” jumps to the address in $ra register next inst. $ra xyz: jr $ra jal xyz Subroutine Calls in MIPS R3000 Subcall/010 Resume to “main”    jal xyz

xyz: jr $ra CS 312 Computer Organization and Architecture Subroutine Calls in MIPS R3000 main: jr $31 xyz: save $s0 restore $s0 jr $ra jal xyz xyz: jr $ra Stack jal xyz (next instruction) $ra next inst. $ra save $s0 jal xyz save $ra $s0 $ra save $ra $s0 $ra next inst. $ra restore $ra $s0 $ra restore $ra Subcall/011 save $s0 restore $s0 save $ra

CS 312 Computer Organization and Architecture Subcall/014 main: jr $31 Stack jal xyz xyz: jr $ra Stack Frame for “main”Stack Frame for “xyz(1)” xyz: jr $ra Stack Frame for “xyz(2)” xyz: jr $ra Stack Frame for “xyz(3)” Subroutine Calls in MIPS R3000 jal xyz    jal xyz

CS 312 Computer Organization and Architecture Subcall/015 Subroutine Calls in MIPS R bytes High Address Low Address 8 registers Stack Frame $fp $sp = $sp-32 $sp $fp $fp = $sp+28 $sp

CS 312 Computer Organization and Architecture Subcall/018 Example (3)   xyz: # create stack frame subu$sp, $sp, 32 # save ($fp, $ra) registers sw$ra, 8($sp) sw$fp, 4($sp) # set up the new frame pointer addu$fp, $sp, 28 # save other GPR’s sw$s0, 28($sp) Set up the stack frame and save the registers

CS 312 Computer Organization and Architecture Subcall/019 Example (4) xyz:   jr$ra # restore registers lw$s0, 28($sp) # restore SP, FP, $ra for the caller lw$ra, 8($sp) lw$fp, 4($sp) # restore the caller's stack pointer addu$sp, $sp, 32 Restore the registers and release the stack frame