Help How do procedure calls work Process Image












- Slides: 12
Help! How do procedure calls work?
Process Image Stack v ^ Heap Data Text Reserved for OS Stack: Stores procedure call info Heap: Stores ‘new’ed-up data Data: Stores. data variables Text: Stores code: . text
Storage for a Procedure: On the Stack! Procedure Storage A method or procedure requires storage for: Arguments: count, item Local variables: price, total, object Return address: where to jump to when procedure finishes Saved registers: since calling routine does not want a change in its register values: $s registers Procedure = Method calc_total(int count, int item) { Object *object = db. read (item); int price = object->get_price(); int total = price*count; object->print(count); }
Each Procedure has an Activation Record on the Stack Procedure = method Each method needs storage for arguments, return address, local variables. Activation records are pushed and popped on a stack. calc_total(int count, int item) { Object *object = db. read (item); int price = object->get_price(); int total = price*count; main() { object->print(count); cout << “Enter item number”; return total; get_price() cin >> item_nr; cout << “Enter number of items”; } cin >> nr_items; calc_total() int total = calc_total(nr_items, item_nr); calc_total() } main() print() { … } print() calc_total() main() push(calc_total) push(get_price) pop() push(print)
Activation record for calc_total() Activation Record Parm: count Parm: item Local variable: object Local variable: price Local variable: total Saved register: $s 0 Saved register: $s 1 Return Address Optional: Previous stack pointer Optional: Previous frame pointer Calc_total Procedure calc_total(int count, int item) { Object *object = db. read (item); int price = object->get_price(); int total = price*count; object->print(count); return total; }
Activation record for calc_total() Activation Record Parm: count Parm: item Local variable: object reference Local variable: price Local variable: total Saved register: $s 0 Saved register: $s 1 Return Address $sp (before) Push: $sp = $sp – 32 Then: count = 28($sp) item = 24($sp) object address = 20($sp) price = 16($sp) total = 12($sp) $s 0 = 8($sp) $s 1 = 4($sp) Return address = 0($sp) $sp (after) 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Assumes each variable is one word large.
Calling sequnce for calc_total() Parm: count -1 Parm: item -5 Local variable: object reference $sp (before) -2 -3 -6 -7 -4 -8 # Calling sequence: # lw $t 1, count # sw $t 1, -4($sp) # lw $t 1, item # sw $t 1, -8($sp) # jal calc_total Local variable: price Local variable: total Saved register: $s 0 Saved register: $s 1 Return Address Activation Record Assumes each variable is one word large: 4 bytes.
Header Comments for calc_total # Register Conventions: # $s 0=total # $s 1=object # $t 1=price # $t 2=item # $t 3=count Stack Documentation: Parm: count = 28($sp) Parm: item = 24($sp) object = 20($sp) Price = 16($sp) total = 12($sp) Register Convention Saved register $s 0 = 8($sp) Saved register $s 1: 4($sp) Return Address: 0($sp)
Within calc_total Start of calc_total: addi $sp, -32 sw $ra, 0($sp) sw $s 1, 4($sp) sw $s 0, 8($sp) lw $s 0, 0 lw $t 2, 24($sp) lw $t 3, 28($sp) …. Parm: count # push(32) # return addr # save reg $s 1 # save reg $s 0 # $s 0=total = 0 # $t 2=item # $t 3=count $sp (before) Parm: item Local variable: object reference Local variable: price Local variable: total Saved register: $s 0 Saved register: $s 1 Return Address calc_total stack $sp (after)
Within calc_total End of calc_total ……. move $v 0, $s 0 lw $ra, 0($sp) lw $s 1, 4($sp) lw $s 0, 8($sp) addi $sp, 32 jr $ra Parm: count # return total # return addr # save reg $s 1 # save reg $s 0 # pop(32) # return $sp (after) Parm: item Local variable: object reference Local variable: price Local variable: total Saved register: $s 0 Saved register: $s 1 Return Address calc_total stack $sp (before)
Alternative Calling Sequence for calc_total() Local variable: object reference Pass parameters in Argument registers: $a 0 -$a 4 Local variable: price # Calling sequence: # lw $a 0, count # lw $a 1, item # jal calc_total Local variable: total Saved register: $s 0 Saved register: $s 1 Return Address In this case stack is 6 x 4 = 24 bytes long, instead of 32. Activation Record Assumes each variable is one word large.
Summary We have learned: What is stored on a stack and why How to call a procedure: 2 methods How to allocate and use stack memory How to return from a procedure