Download Vector Pascal - Mathematical & Computer Sciences

Transcript
5.2. PROCEDURE AND FUNCTION MECHANISM
stack before a call
to foo
struct zot
base addr
of zot
0
x
4
y
69
stack
pntr
8
0
x
4
y
8
z
z
stack on entry to bar
stack
pntr
0
x
-12
4
y
-8
stack on entry to foo
stack
pntr
8
z
locals
of foo
0
d link
ret add
frame base
n
register
frame base
register
d link
n+4
ret add
n+8
x
8
n+12
y
12
n+16
16
z
Figure 5.1: Stacks and records
type vec1 = array[1..10] of integer;
scalar = integer;
function sum(var v:vec1);scalar;
function total( i:scalar):scalar;
begin
total:=if i<1 then 0 else v[i]+total(i-1);
end
total(length(v))
Total recurses on i, but each invocation accesses the same copy of v.
Can we use the d-link to access v?
No
Consider the following: