Download presentation
Presentation is loading. Please wait.
Published byἈπολλωνία Παπαντωνίου Modified over 6 years ago
1
int add3 (int a, int b, int c) { return a + b + c; } int sum = 0; main () sum += add3 (1, 2, 3); sum += 10; sum += add3 (10, 20, 30);
2
int add3 (int a, int b, int c) { return a + b + c; } int sum = 0; main () sum += add3 (1, 2, 3); sum += 10; sum += add3 (10, 20, 30); main () add3 (1, 2, 3) sum += sum += 10 add3 (10, 20, 30) sum +=
3
1. Call from anywhere int add3 (int a, int b, int c) {
return a + b + c; } int sum = 0; main () sum += add3 (1, 2, 3); sum += 10; sum += add3 (10, 20, 30); 1. Call from anywhere
4
2. Pass Values to Parameters – can be different
int add3 (int a, int b, int c) { return a + b + c; } int sum = 0; main () sum += add3 (1, 2, 3); sum += 10; sum += add3 (10, 20, 30); 2. Pass Values to Parameters – can be different 1. Call from anywhere
5
3. Subroutine returns a value
int add3 (int a, int b, int c) { return a + b + c; } int sum = 0; main () sum += add3 (1, 2, 3); sum += 10; sum += add3 (10, 20, 30); 3. Subroutine returns a value Call from anywhere Pass Values to Params Can be different
6
4. Execution Resumes After the Call
int add3 (int a, int b, int c) { return a + b + c; } int sum = 0; main () sum += add3 (1, 2, 3); sum += 10; sum += add3 (10, 20, 30); 4. Execution Resumes After the Call Call from anywhere Pass Values to Params Can be different Sub returns a value
7
5. Can have local variables
int add3 (int a, int b, int c) { int tmp = a + b + c ; return tmp; } int sum = 0; main () { sum += add3 (1, 2, 3); sum += 10; sum += add3 (10, 20, 30); 5. Can have local variables Call from anywhere Pass Values to Params Can be different Sub returns a value Subroutine “returns” to where it was called from
8
Callee Caller Call site
int add3 (int a, int b, int c) { return a + b + c; } int sum = 0; main () sum += add3 (1, 2, 3); sum += 10; sum += add3 (10, 20, 30); Call from anywhere Pass Values to Params Can be different Sub returns a value Subroutine “returns” to where it was called from Local variables Call site
9
Calling Conventions Call from anywhere
Return to where it was called from
10
boo () { coo (); } coo () return;
br coo boo_1: … coo: br boo_1
11
boo () { coo (); } coo () return;
br coo boo_1: boo_2: … coo: br ?????
12
boo () { coo (); } coo () return;
call coo boo_1: boo_2: … coo: ret
13
call coo ret r31 = ra boo: call coo boo_1: boo_2: … coo: ret
r31 = PC + 4 PC = coo ret PC = r31 r31 = ra boo: call coo boo_1: boo_2: … coo: ret
14
boo: call coo boo_1: ret coo: call doo coo_1: ret doo:
boo () { coo (); } coo () doo (); return; doo () boo: call coo boo_1: ret coo: call doo coo_1: ret doo:
15
.text 0x1000000 Stack PUSH POP .data TOP .bss heap R27 0x17fff80
.data .bss heap 0x17fff80 Stack:
16
PUSH (value) ADDI SP, SP, -4 STW R8, 0(SP) 00xFFEC 0xFFF0 0xFFF4
0xFFFC
17
Value = POP () LDW R8, 0(SP) ADDI SP, SP, +4 00xFFEC 0x12345678 0xFFF0
0xaabbccdd 0xFFFC 0xff001122
18
TOP(n) LDW R8, 8(SP) LDW R8, -8(SP) 00xFFEC 0x12345678 0xFFF0
0xaabbccdd 0xFFFC 0xff001122
19
boo () { coo (); return; } coo () doo (); doo ()
addi sp, sp, -4 stw ra, 0(sp) call coo boo_1: ldw ra, 0(sp) addi sp, sp, +4 ret coo: call doo coo_1: doo:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.