Title: Stackbased Linkage Convention
1Stack-based Linkage Convention
- Recitation 10/31 11/2
- Panickos Neophytou
2Topics
- Saving registers on the stack.
- The Stack-based Linkage Convention.
- The prolog and epilog of the called subroutine.
- The call and return of the caller.
- Nested subroutine calls and the chain of
activation. - Example program converting user input to upper
case.
3Pushing the Return Address
- Subroutine
- return address in ra when the jr
- save the value of ra and then to restore it when
needed
4Chain of SubroutineCalls
- main is called by the OS
- main calls subA
- subA calls subB
- subB calls subC
- jr ra after popping from the stack.
5Register Problem
- main calls subA and subA calls subB.
- subA can't save any values in s0?s7 (because it
is not allowed to alter them). - any values it saves in t0?t9 might be changed
by subB (because subB is allowed to alter them). - gt That means subA cant use any registers
- SOLUTION Let subA use s0-s7
- store values on stack before using them
- Restore values before calling jr.
6Pushing and Popping Registers
- Rule if a subroutine is expected to alter any of
the "S" registers - must first push their values onto the stack
- must pop these values from the stack back into
the registers they came from before jr.
7Stack-based Linkage Convention
- Subroutine Call (done by the caller)
- Push onto the stack any registers t0-t9 that
contain values that must be saved. The subroutine
might change these registers. - Put argument values into a0-a3.
- Call the subroutine using jal.
- Subroutine Prolog (done by the subroutine at its
beginning) - If this subroutine might call other subroutines,
push ra onto the stack. - Push onto the stack any registers s0-s7 that
this subroutine might alter.
8Stack-based Linkage Convention
- Subroutine Body
- The subroutine may alter any "T" or "A" register,
or any "S" register that it saved in the prolog. - If the subroutine calls another subroutine, then
it does so by following these rules. - Subroutine Epilog (done by the subroutine just
before it returns to the caller) - Put returned values in v0-v1
- Pop from the stack (in reverse order) any
registers s0-s7 that were pushed in the prolog.
- If it was pushed in the prolog , pop the return
address from the stack into ra. - Return to the caller using jr ra.
- Regaining Control from a subroutine (done by the
caller) - Pop from the stack (in reverse order) any
registers t0-t9 that were previously pushed.
9Diagrams
10Example Program
- Read in lines of text from the user
- Lower case characters from each line are
converted to upper case - Quit with Q
11Complete Program Design
12main
.text .globl main main
?????? what goes here?
la a0,mainPr prompt the user
li v0,4 service 4
syscall jal doLines process
lines of input ??????
what goes here?
jr ra return to OS
.data mainPr .ascii "Type each line of text
followed by ENTER.\n" .asciiz "Type Q at
the start of a line to finish.\n"
13doLines
a0 -- address of the prompt text ra
-- return address no return values
.text .globl doLines doLines
sub sp,sp,4 push the return
address sw ra,(sp) loop
get a line la
a0,________ argument address of buffer
li a1,____ argument
length of buffer jal _______
get line from user la
a0,line if "Q" jal
testEnd return to caller beqz
v0,endloop
convert to capitals la
a0,________ argument address of buffer
la a1,____ argument length
of buffer jal _______
convert la a0,outline
print out the result li v0,4
syscall b loop
continue with next line endloop
lw ra,(sp) pop
return address add sp,sp,4
jr ra return to
caller .data outline .ascii ""
pad so output lines
line up with input line
.space 132 input buffer
14getLine
a0 -- address of input buffer a1 --
length of buffer no return values
.text .globl getLine getLine
move t0,a0 save buffer address
la a0,prompt
prompt the user li v0,4
service 4 syscall move
a0,t0 restore buffer address
li v0,8 service 8
syscall read in a line to the
buffer jr ra
return to caller .data prompt
.asciiz "gt"
15 convert
a0 -- address of input buffer a1 --
length of input buffer register use s0
-- pointer into character buffer no return
values .text .globl
convert convert sub sp,sp,4
push the return address sw
ra,(sp) What should be done
HERE?? for (
pbuffer p!0 p ) move s0,a0
pbuffer cloop lbu a0,(s0)
get a char from the string beqz
a0,endC exit if null byte
argument a0 char to
convert jal conChar
convert character sb v0,(s0)
put converted char into string
addu s0,s0,1 p
b cloop endC
And what should be done HERE?? lw
ra,(sp) pop return address
add sp,sp,4 jr ra
return to caller
16 convert
a0 -- address of input buffer a1 --
length of input buffer register use s0
-- pointer into character buffer no return
values .text .globl
convert convert sub sp,sp,4
push the return address sw
ra,(sp) sub sp,sp,4
push s0 sw s0,(sp)
for ( pbuffer p!0 p
) move s0,a0
pbuffer cloop lbu a0,(s0) get
a char from the string beqz a0,endC
exit if null byte
argument a0 char to convert
jal conChar convert character
sb v0,(s0) put converted
char into string addu
s0,s0,1 p b cloop
endC lw s0,(sp) pop s0
add sp,sp,4 lw
ra,(sp) pop return address
add sp,sp,4 jr ra
return to caller
17 conChar
a0 -- character v0 -- converted
character .text .globl
convert conChar move v0,a0
assume no change
is ch in 'a' .. 'z' / li
t0,'a' ch lt 'a' ? blt
a0,t0,outc li t0,'z'
'z' lt ch ? blt t0,a0,outc
sub v0,a0,32
convert to upper case outc jr ra
return to caller