Subroutines - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Subroutines

Description:

ret. restore. Save. To allocate a new set of registers. Changes the register mapping. Restore ... ret. restore. Stack. Space on Stack. Caller's registers. Local ... – PowerPoint PPT presentation

Number of Views:146
Avg rating:3.0/5.0
Slides: 13
Provided by: mah138
Category:
Tags: ret | subroutines

less

Transcript and Presenter's Notes

Title: Subroutines


1
Subroutines
2
Subroutines
  • Repeat same code with different parameters
  • Provide for separate compilation
  • Provide for reuse

3
Skeleton
  • Subroutine name
  • save sp,
    frame_size, sp
  • lt body of codegt
  • ret
  • restore

4
  • Save
  • To allocate a new set of registers
  • Changes the register mapping
  • Restore
  • Restore old register windows
  • Changes back to previous mapping

5
registers
  • i0-i5 -- incoming parameters
  • i6 -- frame pointer
  • i7 -- address of call instruction
  • l0-l7 -- all yours
  • o0 -- all yours, but also used to pass a
    parameter to and/or return a value from a
    subroutine
  • o1-o5 -- all yours, but also used to pass
    parameters to a subroutine
  • o6 -- stack pointer
  • o7 -- address of call instruction
  • g0 -- always zero
  • g1-g7 -- all yours

6
Pass parameters to subroutinesreturn value
  • before save after save
  • i7 i0
  • l7 l0
  • o7 call ? ? i7 call
  • o6 old sp ? ? i6 new fp
  • o5 ? ? i5
  • .
  • .
  • .
  • o0 ? ? i0
  • l7 l0
  • o7 o0

7
Example
  • int sum( int a, int b )
  • return ( a b )
  • c sum( 1, 2 )
  • mov 1,o0
  • mov 2,o1
  • call a
  • nop
  • -------------------------------------
  • a
  • save sp,-96,sp
  • mov i0,l0 o6
  • mov i1,l1
  • add l0,l1,l2
  • mov l2,i0
  • ret
  • restore

8
Stack
  • Space on Stack
  • Callers registers
  • Local registers for the called subroutine

9
Call by reference
  • void prt_sum( int x, int n )
  • register int i, sum 0
  • for( i 0 i lt n i )
  • sum sum xi
  • printf("d\n",sum)

10
Arrays
  • .global main
  • main
  • save sp, -96, sp
  • clr l0
  • clr l1
  • mov i0, l2
  • loop
  • cmp l0, i1
  • bge done
  • nop
  • ld l2, o0
  • add l1, o0, l1
  • add l2, 4, l2
  • inc l0
  • ba loop
  • nop
  • done
  • set fmt, o0
  • mov l1, o0

11
Leaf Subroutine
  • Not call other subroutines
  • Not execute save or restore
  • Uses calling subroutines register sets
  • But only output and global calling registers
  • Return address is stored in o78 not i78
  • Correct return instruction is retl

12
  • .align 4
  • void main() .global main
  • void swap() .type main,function
  • .proc 020
  • main
  • !PROLOGUE 0
  • int a,b save sp, -120, sp
  • !PROLOGUE 1
  • a 5 mov 5, o0
  • st o0, fp-20
  • b 44 mov 44, o0
  • st o0, fp-24
  • swap(a,b) add fp, -20, o0
  • call swap, 0
  • add fp, -24, o1 ! scheduled in delay slot
  • ret
  • restore
  • .align 4
  • void swap(x,y) .global swap
Write a Comment
User Comments (0)
About PowerShow.com