> >HW4 -- Due 6/4/98 > >1) Consider the following program written in a ALGOL-like language: > >procedure BLAH; > integer GLOBAL; > integer array LIST[1:2]; > procedure FOO (PARAM); > integer PARAM; > begin > PARAM := 3; > GLOBAL := GLOBAL + 1; > PARAM := 5 > end; > begin > LIST[1] := 3; > LIST[2] := 1; > GLOBAL := 1; > FOO (LIST(GLOBAL)) > end; > >Hand execute the program under the following assumptions, and compare >the resulting values in the array LIST in BLAH after the return from >FOO. > >a) Parameters are passed by value. Comment LIST[1] LIST[2] GLOBAL PARAM --------------------------------------------------------------------- Execute Main 3 1 1 x Call of FOO 3 Foo, Line 1 3 Foo, Line 2 2 Foo, Line 3 5 Foo Returns x >b) Parameters are passed by reference. Comment LIST[1] LIST[2] GLOBAL PARAM --------------------------------------------------------------------- Execute Main 3 1 1 x Call of FOO Ref to LIST[1] Foo, Line 1 3 Foo, Line 2 2 Foo, Line 3 5 Foo Returns x >b) Parameters are passed by name. Comment LIST[1] LIST[2] GLOBAL PARAM --------------------------------------------------------------------- Execute Main 3 1 1 x Call of FOO Ref to LIST[1] Foo, Line 1 3 Foo, Line 2 2 Ref to LIST[2] Foo, Line 3 5 Foo Returns x >b) Parameters are passed by value-result. Comment LIST[1] LIST[2] GLOBAL PARAM --------------------------------------------------------------------- Execute Main 3 1 1 x Call of FOO 3 Foo, Line 1 3 Foo, Line 2 2 Foo, Line 3 5 Foo Returns 5 x >2) FORTRAN allows a subprogram to have multiple entry points. Why is >this sometimes a valuable capability ? - Allows one subprogram to have multiple uses; executing only the portion that is needed (note that this possibly saves coding time as well as required memory to load entire program). - Allows subsequent calls to a subprogram to ignore an already executed "initialization" portion >3) Show the stack with all activation record instances, including >static and dynamic chains, when execution reaches position 1 in the >following skeletal program: > > >procedure BLAH; > procedure A; > procedure B; > begin {B} > ... <------------ 1 > end; {B} > procedure C; > begin {C} > ... > B; > ... > end; {C} > begin {A} > ... > C; > ... > end; {A} > begin {BLAH} > ... > A; > ... > end; {BLAH} Static Chain ------------ +-----------------------+ | | | B |-----+ | | | +-----------------------+ | | | | | C |--+ | | | | | +-----------------------+ | | | | | | | |<-+ | | | | | A |<----+ | | | |-----+ | | | +-----------------------+ | | | | | BLAH |<----+ | | +-----------------------+ Dynamic Chain ------------- +-----------------------+ | | | B |--+ | | | +-----------------------+ | | | | | |<-+ | | | C |--+ | | | +-----------------------+ | | | | | |<-+ | | | A |--+ | | | +-----------------------+ | | | | | BLAH |<-+ | | +-----------------------+ >4) For the skeletal program in Problem 3, show the display that would >be active at position 1, along with the activation records on the >stack. Display ------- +-----------------------+ | | | B |<-------------------------+ | | | +-----------------------+ | | | | | C | | | | | +-----------------------+ | | | | | A |<---------------------+ | | | | | +-----------------------+ | | | | | | | BLAH |<-----------------+ | | | | | | | +-----------------------+ | | | +-+-+-+-+-+-+ Display: | 0 | 1 | 2 | +-+-+-+-+-+-+ >5) Show the stack with all activation record instances, including >static and dynamic chains, when execution reaches position 1 in the >following skeletal program: > > >procedure BLAH; > procedure C; forward; > procedure A (FLAG : boolean); > procedure B; > begin {B} > ... > A (false) > end; {B} > begin {A} > if FLAG > then B > else C > ... > end; {A} > procedure C; > procedure D; > begin {D} > ... <--------------- 1 > end; {D} > begin {C} > ... > D; > ... > end; {C} > begin {BLAH} > ... > A(true); > ... > end; {BLAH} > >The calling sequence for this program for execution to reach D is > > BLAH calls A > A calls B > B calls A > A calls C > C calls D > >Show the local variables as part of the activation. Static Chain ------------ +-----------------------+ | | | D |--+ | | | +-----------------------+ | | | | | C |<-+ | | | |------+ | | | +-----------------------+ | | | | | | | | | | | A |----+ | | | | | +-----------------------+ | | | | | | | | | | | | | | | B |--+ | | | | | | | +-----------------------+ | | | | | | | | | |<-+ | | | | | | | A |--+ | | | | | | | +-----------------------+ | | | | | | | | | BLAH |<-+-+-+ | | +-----------------------+ Dynamic Chain ------------- +-----------------------+ | | | D |--+ | | | +-----------------------+ | | | | | C |<-+ | | | |--+ | | | +-----------------------+ | | | | | |<-+ | | | A |--+ | | | | FLAG: false | | +-----------------------+ | | | | | |<-+ | | | B |--+ | | | +-----------------------+ | | | | | |<-+ | | | A |--+ | | | | FLAG: true | | +-----------------------+ | | | | | BLAH |<-+ | | +-----------------------+