Docsity

Assembly Language: Procedure Call, Return, Arguments, and Stack - Prof. Bruce Robert Child, Study notes of Computer Science

Toggle sidebar
bg1
1
CS/CoE0447: Computer Organization and Assembly Language University of Pittsburgh
44
Procedures
int len(char *s) {
int l;
for (l=0; *s != ā€˜\0’; s++) l++;
return l;
}
void reverse(char *s, char *r) {
char *p, *t;
int l = len(s);
*(r+l) = ā€˜\0’;
l--;
for (p=s+l t=r; l>=0; l--) { *t++ = *p--; }
}
void main(int) {
char *s = ā€œHello World!ā€;
char r[100];
reverse(s,r);
}
main
reverse
len
reverse(s,r)
len(s) 12
N/A
How can we do this with assembly?
* Need a way to call / return procedures
* Need a way to pass arguments
* Need a way to return a value
CS/CoE0447: Computer Organization and Assembly Language University of Pittsburgh
45
Procedure Call and Return
Procedure call
•Jump to the procedure
•The return goes back to the point immediately after the call
•Need to pass ā€œreturn addressā€ (instruction after call)
•jal Label
$ra = PC+4 # set return address to next PC
PC = PC[31:28] | Label << 2 # jump to procedure
Procedure return
•Need return address (address of instruction after the jal Label)
•Need to jump back to the return point
•jr $ra
PC = $ra # jump back to return address
pf3
pf4
pf5

Partial preview of the text

Download Assembly Language: Procedure Call, Return, Arguments, and Stack - Prof. Bruce Robert Child and more Study notes Computer Science in PDF only on Docsity!

CS/CoE0447: Computer Organization and Assembly Language (^) University of Pittsburgh 44

Procedures

int len(char *s) {

int l;

for (l=0; *s != ā€˜\0’; s++) l++;

return l;

void reverse(char *s, char *r) {

char *p, *t;

int l = len(s);

*(r+l) = ā€˜\0’;

l--;

for (p=s+l t=r; l>=0; l--) { *t++ = *p--; }

void main(int) {

char *s = ā€œHello World!ā€;

char r[100];

reverse(s,r);

main

reverse

len

reverse(s,r)

len(s) (^12)

N/A

How can we do this with assembly?

  • Need a way to call / return procedures
  • Need a way to pass arguments
  • Need a way to return a value

CS/CoE0447: Computer Organization and Assembly Language (^) University of Pittsburgh 45

Procedure Call and Return

 Procedure call

  • Jump to the procedure
  • The return goes back to the point immediately after the call
  • Need to pass ā€œreturn addressā€ (instruction after call)
  • jal Label $ra = PC+4 # set return address to next PC PC = PC[31:28] | Label << 2 # jump to procedure

 Procedure return

  • Need return address (address of instruction after the jal Label )
  • Need to jump back to the return point
  • jr $ra PC = $ra # jump back to return address

CS/CoE0447: Computer Organization and Assembly Language (^) University of Pittsburgh 46

Arguments and Return Value

 Register conventions specified in PRM

  • $a0-$a3: four arguments for passing values to called procedure
  • $v0-$v1: two values returned from called procedure
  • $ra: return address register (set by call, used by return)

 Call chains

  • One procedure calls another, which calls another one
  • E.g., main → reverse → len
  • What happens to $ra??? (e.g., when reverse calls len)

 You must save $ra someplace!

  • Simple approach: A ā€œfreeā€ register (can’t be used by caller)
  • Leaf procedure: Doesn’t make any calls. Doesn’t need to save $ra.

CS/CoE0447: Computer Organization and Assembly Language (^) University of Pittsburgh 47

# procedure reverse($a0,$a1) reverse: move $t7,$ra # save return address jal len # get length of source string blt $v0,$0,rev_exit # exit if empty string add $t0,$a1,$v0 # null terminate target string sb $0,0($t0) # put null into end of string addi $v0,$v0,-1 # decrement length (written /0) add $t0,$a0,$v0 # $t0 holds p (source string) add $t1,$a1,$0 # $t1 holds t (target string) rev_loop: lbu $t2,0($t0) # get char from source string sb $t2,0($t1) # save char to target string addi $t0,$t0,-1 # decrement source string ptr addi $t1,$t1,1 # increment target string ptr addi $v0,$v0,-1 # decrement length slt $t2,$v0,$0 # is l < 0? beq $t2,$0,rev_loop rev_exit: move $ra,$t jr $ra

CS/CoE0447: Computer Organization and Assembly Language (^) University of Pittsburgh 50

More Procedure Call/Return

 Caller: The procedure that calls another one

 Callee: The procedure that is called by the caller

 What if callee wants to use registers?

  • Caller is also using registers!!!
  • If callee wants to use same registers, it must save them
  • Consider what happened with $ra in a call chain

 Register usage conventions specified by PRM

  • $t0-$t9: Temp. registers; if caller wants them, must save before call
  • $s0-$s7: Saved registers; saved by callee prior to using them

CS/CoE0447: Computer Organization and Assembly Language (^) University of Pittsburgh 51

Where to save?

 Need memory space to hold saved (ā€œspilledā€) registers

  • Caller spills $t0-$t9 that be must saved to memory
  • Callee spills $s0-$s7 to memory, when these regs are used
  • Other registers (e.g., $v0, $v1 might also need to be saved)
  • Non-leaf caller saves $ra when making another call

 Each procedure needs locations to save registers

 In general, call-chain depth (number of called procs) is

unknown, so we need to support undetermined length

 Suggestion: Use a stack. Add ā€œstack elementā€ onto stack for

each call. The ā€œstack elementā€ has the locations.

CS/CoE0447: Computer Organization and Assembly Language (^) University of Pittsburgh 52

Program Stack

 Program stack: Memory locations used by running program

  • Has space for saved registers
  • Has space for local variables, when can’t all fit in registers E.g., local arrays are allocated on the stack
  • Has space for return address

 Each procedure allocates space for these items

  • So-called ā€œactivation frameā€ (a.k.a., ā€œactivation recordā€)
  • Purpose of locations in activation frame are known

 Prologue (entry point into the procedure): Allocates an

activation frame on the stack

 Epilogue (exit point from procedure): De-allocates the

activation frame, does actual return

CS/CoE0447: Computer Organization and Assembly Language (^) University of Pittsburgh 53

Stack and frame pointers

 Stack pointer ($sp)

  • Keeps the address to the top of

the stack

  • $29 is reserved for this purpose
  • Stack grows from high address to

low

  • Typical stack operations are

push/pop

 Procedure frame

  • Contains saved registers and local

variables

  • ā€œActivation recordā€  Frame pointer ($fp)
  • Points to the first word of a frame
  • Offers a stable reference pointer
  • $30 is reserved for this
  • Some compilers don’t use $fp

CS/CoE0447: Computer Organization and Assembly Language (^) University of Pittsburgh 56

Example: Factorial

fact(3) returns 6

fact(3-1) * 3 returns 2 * 3

fact(2-1) * 2 returns 1 * 2

fact(1) * 1 returns 1 * 1

call factorial again, when not at end of recursion (f==1)

on each call, we need to pass a new argument to next one

on return, we do the actual computation and pass value back

need the return address & possibly temporary storage

set up a stack to make space

See factorial.asm