Presentation is loading. Please wait.

Presentation is loading. Please wait.

Help! How do procedure calls work?

Similar presentations


Presentation on theme: "Help! How do procedure calls work?"— Presentation transcript:

1 Help! How do procedure calls work?

2 Process Image Stack: Stores procedure call info
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

3 Storage for a Procedure
Procedure Storage 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); } 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 Object is now a pointer; used in calls; should be discussed in audio.

4 Each Procedure has an Activation Record
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; object->print(count); return total; } print() { … } main() { cout << “Enter item number”; cin >> item_nr; cout << “Enter number of items”; cin >> nr_items; int total = calc_total(nr_items, item_nr); } get_price() print() In this code, main() executes first. An activation record is allocated to calc_total() calc_total() calc_total() main() main() main() main() push(calc_total) push(get_price) pop() push(print)

5 Activation record for calc_total()
Calc_total Procedure Parm: count Parm: item Local variable: object Local variable: price Local variable: total Saved register: $s0 Saved register: $s1 Return Address Optional: Previous stack pointer Optional: Previous frame pointer 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; }

6 Activation record for calc_total()
$sp (before) Parm: count Parm: item Local variable: object reference Local variable: price Local variable: total Saved register: $s0 Saved register: $s1 Return Address Push: $sp = $sp – 32 Then: count = 28($sp) item = 24($sp) object address = 20($sp) price = 16($sp) total = 12($sp) $s0 = 8($sp) $s1 = 4($sp) Return address = 0($sp) 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 $sp (after) Assumes each variable is one word large.

7 Calling sequnce for calc_total()
$sp (before) Parm: count Parm: item Local variable: object reference Local variable: price Local variable: total Saved register: $s0 Saved register: $s1 Return Address -1 -2 -3 -4 -5 -6 -7 -8 # Calling sequence: # lw $t1, count # sw $t1,-4($sp) # lw $t1,item # sw $t1,-8($sp) # jal calc_total Activation Record Assumes each variable is one word large.

8 Header Comments for calc_total
Local v Header Comments for calc_total # Register Conventions: # $s0=total # $s1=object # $t1=price # $t2=item # $t3=count Stack Documentation: Parm: count = 28($sp) Parm: item = 24($sp) object = 20($sp) Price = 16($sp) total = 12($sp) Saved register $s0 = 8($sp) Saved register $s1: 4($sp) Return Address: 0($sp) Register Convention

9 Within calc_total Start of calc_total calc_total stack
Local v Within calc_total Start of calc_total $sp (before) calc_total: addi $sp,$sp,-32 # push(32) sw $ra,0($sp) # return addr sw $s1,4($sp) # save reg $s1 sw $s0,8($sp) # save reg $s0 lw $s0,0 # $s0=total = 0 lw $t2,24($sp) # $t2=item lw $t3,28($sp) # $t3=count …. Parm: count Parm: item Local variable: object reference Local variable: price Local variable: total Saved register: $s0 Saved register: $s1 Return Address Removed references to price and object; should be changed in audio $sp (after) calc_total stack

10 Within calc_total End of calc_total calc_total stack
Local v Within calc_total End of calc_total $sp (after) Parm: count Parm: item Local variable: object reference Local variable: price Local variable: total Saved register: $s0 Saved register: $s1 Return Address ……. move $v0,$s0 # return total lw $ra,0($sp) # return addr lw $s1,4($sp) # save reg $s1 lw $s0,8($sp) # save reg $s0 addi $sp,$sp,32 # pop(32) jr $ra # return $sp (before) calc_total stack

11 Alternative Calling Sequence for calc_total()
Local variable: object reference Local variable: price Local variable: total Saved register: $s0 Saved register: $s1 Return Address Pass parameters in Argument registers: $a0-$a4 # Calling sequence: # lw $a0, count # lw $a1,item # jal calc_total In this case stack is 6 x 4 = 24 bytes long, instead of 32. Activation Record Assumes each variable is one word large.

12 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


Download ppt "Help! How do procedure calls work?"

Similar presentations


Ads by Google