int add 3 int a int b int
int add 3 (int a, int b, int c) { return a + b + c; } int sum = 0; main () { sum += add 3 (1, 2, 3); sum += 10; sum += add 3 (10, 20, 30); }
int add 3 (int a, int b, int c) { return a + b + c; } int sum = 0; main () { sum += add 3 (1, 2, 3); sum += 10; sum += add 3 (10, 20, 30); } main () add 3 (1, 2, 3) sum += 10 add 3 (10, 20, 30) sum +=
int add 3 (int a, int b, int c) { return a + b + c; } int sum = 0; main () { sum += add 3 (1, 2, 3); sum += 10; sum += add 3 (10, 20, 30); } 1. Call from anywhere
int add 3 (int a, int b, int c) { 1. Call from anywhere return a + b + c; } int sum = 0; main () { sum += add 3 (1, 2, 3); sum += 10; sum += add 3 (10, 20, 30); } 2. Pass Values to Parameters – can be different
int add 3 (int a, int b, int c) { 1. Call from anywhere return a + b + c; 2. Pass Values to Params Can be different } int sum = 0; main () { sum += add 3 (1, 2, 3); sum += 10; sum += add 3 (10, 20, 30); } 3. Subroutine returns a value
int add 3 (int a, int b, int c) { 1. Call from anywhere return a + b + c; 2. Pass Values to Params Can be different } 3. Sub returns a value int sum = 0; main () { sum += add 3 (1, 2, 3); sum += 10; sum += add 3 (10, 20, 30); } 4. Execution Resumes After the Call
int add 3 (int a, int b, int c) { int tmp = a + b + c ; 1. Call from anywhere return tmp; 2. Pass Values to Params Can be different } 3. Sub returns a value int sum = 0; 4. Subroutine “returns” to where it was called main () from { sum += add 3 (1, 2, 3); sum += 10; sum += add 3 (10, 20, 30); } 5. Can have local variables
e e l l Ca int add 3 (int a, int b, int c) { 1. Call from anywhere return a + b + c; 2. Pass Values to Params Can be different } 3. Sub returns a value int sum = 0; 4. Subroutine “returns” r e l l to where it was called a C main () from e t i { 5. Local variables s l l a C sum += add 3 (1, 2, 3); sum += 10; e t i s l l a sum += add 3 (10, C 20, 30); }
• Calling Conventions – Call from anywhere – Return to where it was called from
boo () { coo (); } boo: br coo boo_1: … coo () { return; } coo: br boo_1
boo () { coo (); } coo () { return; } boo: br coo boo_1: br coo boo_2: … coo: br ? ? ?
boo () { coo (); } coo () { return; } boo: call boo_1: call boo_2: … coo: ret coo
• call coo – r 31 = PC + 4 – PC = coo • ret – PC = r 31 • r 31 = ra boo: call boo_1: call boo_2: … coo: ret coo
boo () { coo (); } coo () { doo (); return; } doo () { return; } boo: call boo_1: ret coo: call doo coo_1: ret doo: ret
• Stack – R 27 • PUSH • POP • TOP 0 x 1000000. text. data. bss heap 0 x 17 fff 80 Stack:
PUSH (value) • ADDI SP, -4 • STW R 8, 0(SP) 00 x. FFEC 0 x. FFF 0 0 x. FFF 4 0 x. FFF 8 0 x. FFFC
Value = POP () • LDW R 8, 0(SP) • ADDI SP, +4 00 x. FFEC 0 x. FFF 0 0 x. FFF 4 0 x. FFF 8 0 x. FFFC 0 x 12345678 0 x 11223344 0 x 55667788 0 xaabbccdd 0 xff 001122
• LDW R 8, 8(SP) TOP(n) • LDW R 8, -8(SP) 00 x. FFEC 0 x. FFF 0 0 x. FFF 4 0 x. FFF 8 0 x. FFFC 0 x 12345678 0 x 11223344 0 x 55667788 0 xaabbccdd 0 xff 001122
boo () { coo (); return; } coo () { doo (); return; } doo () { return; } boo: addi sp, -4 stw ra, 0(sp) call coo boo_1: ldw ra, 0(sp) addi sp, +4 ret coo: addi sp, -4 stw ra, 0(sp) call doo coo_1: ldw ra, 0(sp) addi sp, +4 ret doo: ret
- Slides: 19