140 likes | 264 Views
CSC 3315 Subprograms: an Example. Hamid Harroud School of Science and Engineering, Akhawayn University http://www.aui.ma/~H.Harroud/csc3315/. program Main; var A, B: integer; procedure P ; begin {P} {L1P} A := A + 1; {L2P} B := B + 1; {L3P} end; procedure Q ;
E N D
CSC 3315Subprograms: an Example HamidHarroud School of Science and Engineering, Akhawayn University http://www.aui.ma/~H.Harroud/csc3315/
program Main; var A, B: integer; procedure P; begin {P} {L1P} A := A + 1; {L2P} B := B + 1; {L3P} end; procedure Q; var B: integer; procedure R; var A: integer; begin {R} {L1R} A := 16; {L2R}P; {L3R} write(A, B); {L4R} end; {continued} begin {Q} {L1Q} B := 11; {L2Q}R; {L3Q}P; {L4Q} write(A, B); {L5Q} end; begin {Main} {L1m} A := 1; {L2m} B := 6; {L3m}P; {L4m} write(A, B); {L5m}Q; {L6m} write(A, B); {L7m} end. An Example (in Pascal)
(dynamic link) F1 (static link) (return address) Main A 1 B 6 F2 F1 F1 P L4m situation just after a call to P is made: IP = L1P, EP = F2
(dynamic link) F1 (static link) (return address) Main A 2 B 7 situation after a call to P in Main is terminated: IP = L4m, EP = F1
(dynamic link) F1 (static link) (return address) Main A 2 B 7 F2 F1 F1 Q L6m situation just after a call to Q in Main is made: IP = L1Q, EP = F2 B
6) (dynamic link) F1 (static link) (return address) Main A 2 B 7 F2 F1 F1 Q L6m Situation just after a call to R in Q in Main is made: IP = L1R, EP = F3 B 11 F3 F2 F2 R L3Q A
(dynamic link) F1 (static link) (return address) Main A 2 B 7 F2 F1 F1 Q L6m situation just after a call to P in R in Q in Main is made: IP = L1P, EP = F4 B 11 F3 F2 F2 R L3Q A 16 F4 F3 F1 P L3R
(dynamic link) F1 (static link) (return address) Main A 3 B 8 F2 F1 F1 Q L6m situation after a call to P in R in Q in Main is terminated: IP = L4R, EP = F3 B 11 F3 F2 F2 R L3Q A 16
(dynamic link) F1 (static link) (return address) Main A 3 B 8 F2 F1 F1 Q L6m situation after a call to P in Q in Main is terminated: IP = L3Q, EP = F2 B 11
(10) (dynamic link) F1 (static link) (return address) Main A 3 B 8 F2 F1 F1 Q L6m situation just after a call to P in Q in Main is made: IP = L1P, EP = F3 B 11 F3 F2 F1 P L4Q
(dynamic link) F1 (static link) (return address) Main A 4 B 9 F2 F1 F1 Q L6m situation after a call to P in Q in Main is terminated: IP = L4Q, EP = F2 B 11
(dynamic link) F1 (static link) (return address) Main A 4 B 9 situation after a call to Q in Main is terminated: IP = L6m, EP = F1
Scope of Variables (Static link) In case Q in Main is called: Main.Q.B (0, 3) Main.A (1, 3) Main.B hidden (dynamic link) F1 (static link) (return address) Main A 2 B 7 F2 F1 F1 Q L6m B
Scope of Variables (Static link) In case P in R in Q in Main is called: Main.Q.R.A hidden Main.Q.B hidden Main.A (1, 3) Main.B (1, 4) (lien dynamique) F1 (lien statique) (adresse retour) Main A 2 B 7 F2 F1 F1 Q L6m B 11 F3 F2 F2 R L3Q A 16 F4 F3 F1 P L3R