Subroutines - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Subroutines

Description:

temp = b; return temp; void main() int a,b,c,e,f,c,max; e = e 1; c = greater(e,f); max = max c; b = greater(a,b); 23/3/00. asl5. 2. Subroutines ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 7
Provided by: mpea7
Category:

less

Transcript and Presenter's Notes

Title: Subroutines


1
Subroutines
  • A programming language must provide
  • A way to jump to subroutines
  • return to the instruction after the calling point

int greater(int a, int b) int temp if (a gt
b) temp a else temp b return
temp
void main() int a,b,c,e,f,c,max e e
1 c greater(e,f) max max c ... b
greater(a,b)
2
Subroutines
  • MIPs statements that support procedures
  • Jumpj address
  • jumps to address
  • provides no mechanism for returning
  • Jump and Linkjal subroutine_address
  • jumps to subroutine address
  • saves the address of the instruction after the
    jal in R31
  • Jump Registerjr 31

3
Subroutines
  • Example

4
Subroutines
  • Still have to deal with
  • nested subroutine calls
  • passing parameters
  • reserving storage for local variables
  • A stack is often used to help implement the above

5
Subroutines
  • Stack
  • Last in first out queue
  • stack normally grows towards the bottom of memory
  • On the MIPs R29 is normally used as the system
    stack pointer
  • other stacks can be maintained using other
    address registers
  • PUSH operation performed decrementing
    R29i.e. sub 29, 29,0x4 sw 31, (29)
  • POP operation performed by incrementing
    R29i.e. lw 31, (29) add 29, 29,0x4

6
Subroutines
  • Consider a subroutine A that calls a subroutine B
    that calls subroutine C. C calls no subroutines

A ... jal B ... B sub 29, 29,0x4 sw 31,
(29) ... jal C ...
lw 31, (29) add 29, 29,0x4 jr
31 C ... jr 31
Write a Comment
User Comments (0)
About PowerShow.com