csc 3315 subprograms: an example

14
CSC3315 (Spring 2009) 1 CSC 3315 CSC 3315 Subprograms: an Subprograms: an Example Example Hamid Harroud Hamid Harroud School of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn University University http://www.aui.ma/~H.Harroud/csc3315/

Upload: aram

Post on 06-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

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 ; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSC 3315 Subprograms: an Example

CSC3315 (Spring 2009) 1

CSC 3315CSC 3315Subprograms: an Subprograms: an ExampleExample

Hamid HarroudHamid HarroudSchool of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn

UniversityUniversityhttp://www.aui.ma/~H.Harroud/csc3315/

Page 2: CSC 3315 Subprograms: an Example

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)An Example (in Pascal)

Page 3: CSC 3315 Subprograms: an Example

(dynamic link)(static link)

(return address)A 1B 6

F1F1

L4m

F1

F2

Main

P situation just after a call to P is made:

IP = L1P, EP = F2

Page 4: CSC 3315 Subprograms: an Example

(dynamic link)(static link)

(return address)A 2B 7

situation after a call to P in Main is terminated:

IP = L4m, EP = F1

F1

Main

Page 5: CSC 3315 Subprograms: an Example

(dynamic link)(static link)

(return address)A 2B 7

F1F1

L6mB

F1

F2

Main

Q situation just after a call to Q in Main is made:

IP = L1Q, EP = F2

Page 6: CSC 3315 Subprograms: an Example

6)6)(dynamic link)(static link)

(return address)A 2B 7

F1F1

L6mB 11

F2F2

L3QA

F1

F2

F3

Main

Q

R

Situation just after a call to R in Q in Main is made:

IP = L1R, EP = F3

Page 7: CSC 3315 Subprograms: an Example

(dynamic link)(static link)

(return address)A 2B 7

F1F1

L6mB 11

F2F2

L3QA 16

F3F1

L3R

F1

F2

F3

F4

Main

Q

R

P

situation just after a call to P in R in Q in Main is made:

IP = L1P, EP = F4

Page 8: CSC 3315 Subprograms: an Example

(dynamic link)(static link)

(return address)A 3B 8

F1F1

L6mB 11

F2F2

L3QA 16

F1

F2

F3

Main

Q

R

situation after a call to P in R in Q in Main is terminated:

IP = L4R, EP = F3

Page 9: CSC 3315 Subprograms: an Example

(dynamic link)(static link)

(return address)A 3B 8

F1F1

L6mB 11

F1

F2

Main

Q situation after a call to P in Q in Main is terminated:

IP = L3Q, EP = F2

Page 10: CSC 3315 Subprograms: an Example

(10)(10)(dynamic link)(static link)

(return address)A 3B 8

F1F1

L6mB 11

F2F1

L4Q

F1

F2

F3

Main

Q

P

situation just after a call to P in Q in Main is made:

IP = L1P, EP = F3

Page 11: CSC 3315 Subprograms: an Example

(dynamic link)(static link)

(return address)A 4B 9

F1F1

L6mB 11

F1

F2

Main

Q situation after a call to P in Q in Main is terminated:

IP = L4Q, EP = F2

Page 12: CSC 3315 Subprograms: an Example

(dynamic link)(static link)

(return address)A 4B 9

situation after a call to Q in Main is terminated:

IP = L6m, EP = F1

F1

Main

Page 13: CSC 3315 Subprograms: an Example

In case Q in Main is called:Main.Q.B (0, 3)Main.A (1, 3)Main.B hidden

(dynamic link)(static link)

(return address)A 2B 7

F1F1

L6mB

F1

F2

Main

Q

Scope of Variables (Static Scope of Variables (Static link)link)

Page 14: CSC 3315 Subprograms: an Example

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)(lien statique)

(adresse retour)A 2B 7

F1F1

L6mB 11

F2F2

L3QA 16

F3F1

L3R

F1

F2

F3

F4

Main

Q

R

P

Scope of Variables (Static Scope of Variables (Static link)link)