Featured

    Featured Posts

Subprogram Sequence Control in Programming language.

Simple Subprogram Call Return :-

         A program is composed of a single main program, which during execution may call various subprograms, variables procedures, which in turn may each call other sub-sub-programs and so forth to any depth.  When a subprogram is start execution, the execution of its parent (calling) program stops temporarily and will start its execution after completion of its subprogram from the same point it stops execution.  This means a subprogram will work like a copy statement.  Instead of pasting the statement we pass the execution to other place
     Some assumptions are made to get to some more general instructions
  1. Subpropram cannot be recursive :- A subprogram cannot call itself because if a subprogram is recursive it will lead to a non-ending substitution.
  2. Explicit call statements are required :- For the copy rule to apply, each point of call of a subprogram must be explicitly indicated in the program to be translated.
  3. Subprogram must execute completely at each call :- Implicit in the copy rule is the assumption that each subprogram is executed from its beginning to its logical end each time it is called.
  4. Immediate transfer of control at point of call :- An explicit call statement in a program indicates that control is to transfer directly to the subprogram at that point, and thus copying the body into the calling program has the same effect.
  5. Single execution sequence :- Execution proceeds in a single sequence from calling program to called program and back to calling program.


#  Simple Call-return Subprogram :- 
              It is a mechanism for transfer of control between programs and subprograms.
Mechanism :
   Simple subprogram calls ordinarily arise in two forms :
   The function call for subprograms that return values directly and the procedure or subroutine call for subprograms that operate only through side effects on shared data.
Implementation :-  To understand the implementation of the simple call-return control structure it is important to build a more complete model of what it means to say that a program is being executed.
For subprogram we need more :-
  1. There is a distinction between a subprogram definition and a subprogram activation.  The definition is what we see in the written program, which is translated into a template.  An activation is created each time a subprogram is called using the template created from  the definitions.
  2. An activation is implemented as two parts : Code segment containing the executable code constants and an activation record containing local data, parameters, and various other data items.
  3. The code segment is invariant during execution.  It is created by the translator and stored statically in memory.  During execution, it is used but never modified.  Every activation of the sub program uses the same code segment.
  4. The activation record is created a new each time the sub program is called, and it is destroyed when the sub program returns.
#  Current Instruction Pointer :- It is a pointer that refers to the current instruction ( CIP )
#  Current Environment Pointer :- Because all activation of the same sub program use the same code segment, it is not enough to know the current instruction pointer ( CIP ).  So CEP is introduced i.e every sub program now has different environment i.e a pointer to an activation record is commonly known as environment pointer.
    With the CIP and CEP pointers, it now becomes easy to understand how a program is executed, when we change an environment i.e call a function.  We have to store the values of the last CIP and CEP so that they become useful when a subprogram returns to its calling statement i.e for transferring the control back to the calling program.
    When we start executing a program an CIP is assigned to the first instruction of the program and an environment pointer is assigned.
    The old values of the CIP and CEP must be saved somewhere by the subprogram call instruction before new values are assigned.
    Or we can include an activation record of the subprogram being called and has two variables ( ip, cp ) and the return instruction fetch the old values of ( ip, cp ).
    Now we want to improve the model.
    The simpler implementation is to allocate storage for a single activation record of each subprogram rather then creating the activation record every time we call a statement.

2. Stack Based Implementation :->
             The simplest run-time storage management technique to handle this activation record structure is the stack.  Free storage at the start of execution is set up as a sequential block in memory.  As storage is allocated, it is taken from sequential locations in this stack block beginning at one end.  Storage must be freed in the reverse order of allocation so that a block of storage being freed is always at the top of the stack.
             A single stack pointer is all that is needed to control storage management.  The stock pointer always points to the top of the stack, the next available word of free storage in the stack block.  All storage in use lies in the stack below the location pointed to by the stack pointer.  All free storage lies above the pointer.  Wherever compaction is required it is done by moving the top by down some position.
            The activation record contain all the variable items of information associated with a given subprogram activation.




# Recursive Subprogram :-
                    Recursion is the primary control mechanism for repeating sequences of statements.  Recursion in the form of recursive subprogram calls, is one of the most important sequence-control structure in programming.
Specification :- The only difference between a recursive call and an ordinary call is that the recursive call creates a second activation of the subprogram during the lifetime of the first activation.  If the second the second activation leads to another recursive call, then three activation's may exist simultaneously, and so on.  In general, if the execution of the program results in chain of a first call of subprogram.  A followed by K recursive calls that occur before any return is made, then K+1 activation's of A will exist at the point just before return from the Kth recursive call.
         The only new element introduced by recursion is the multiple activation's of the same subprogram that all exist simultaneously at the same point during execution.
Implementation :- Because of the possibility of multiple activation's, we need both the CIP and CEP pointers.  At the time of each subprogram call, a new activation record is created, which is subsequently destroyed upon return.
       From the CEP pointers, we reach the top of the stack.  So we need a control stack thus we are having different value of CP and the all points to the top of the central stack.


www.CodeNirvana.in

www.posthatke.com. Powered by Blogger.
Copyright © www.posthatke.com | Blogger Templates | Designed By posthatke.com