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.


Structured Sequence Control in Programmig language



Most language provide a set of control statements for expressing the basic control forms of composition, alteration, and iteration.  One important aspect of the statements is that each is a one-in, one-out control statement, meaning that in each statement there is only one entry point to the statement and one exit point from it.  If one of these statements is placed in sequence with some other statements, then the sequence of execution will necessarily proceed from the preceding statement into one-in, one out statement through the statement and out of the following statement.
          The flow of program execution must match the sequence of statements in the program text.  Each one-i,n one-out control statement may include internal branching and looping, but control may leave the statement only through its single exit point.

  (a).  Compound Statements :-> A compound statements is a sequence of statements that may be treated as a single in the construction of larger statements for example :
      begin
      --------------
      -------------- { sequence of statements(1or more)
      --------------
      end.

In C, C++,PERL or Java, it is written simply as { ..... }.
    Within the compound, statements are written in the order in which they are to be executed.  Thus the compound statement inn the basic structure for representing the composition of statements.

#  Conditional statements :-> A conditional statement is one that express alternation of two or more statements, or optional execution of a single statements - where statement means either a single basic statements, a compound statements or another control statement.  The choice of alternative is controlled by a test on some condition, usually written as an expression involving relational and Boolean operations.  The most common forms of conditional statement are the if and case statements -

If statement : The optional execution of a statement is expressed as a single branch if viz
  If condition then statement end if.
whereas a choice between two alternatives uses a two-branch if, namely,
If ( statement condition)
then
      statement
else
     statement
end if.

#  Case Statements :-> If statements are implemented using the usual hardware supported branch and jump instructions.  Case statements are commonly implemented using a jump table to avoid repeated testing of the value of the some variable.
Jump Table :  A Jump table is a vector each of whose components is an unconditional jump instruction.
     If fetch the case no. 1,2,3,4,5,6,7..... and add if the vase add of jump table like
Fiegure :
switch(a)
{
case 1:              If we pass the value of a as 3
----------          A jump table fetch the value as 3 and then add
----------          the offset 3 to the base address of switch
break() ;            i.e jump to let t and then execute the
case 2:              case statement and ultimately will jump
----------          out of the loop.
----------
break() ;
case 3:
----------
----------
break() ;
case 4:
----------
----------
  }
}
 Jump table implementation of case statements.

figure----------


# Iteration Statements :-> Iteration statements provide the basic mechanism for repeated calculations in most programs.  The basic structure of an iteration statement consists of a head and a body.  The head controls the number of times the body will be executed, whereas the body is usually a (compound) statement that provides the action of the statement.
Let us look some typical ones :

1. Simple Repetitions :->
                 The simplest type of iteration statements head specifies that the body is to be executed some fixed number of times.
For example :  perform body k times
This statement causes k to be evaluated and then body of the statement to be executed that many times.

2. Repetition while condition holds :-> 
                   In this form of iteration statement, the expression is re-evaluated each time after the body has been executed and it must have to change the value of variable inside the loop otherwise loop will run infinitely and never terminate.
For example :   While test do body.

3. Repetition while incrementing a counter :->
                    The third alternative form of iteration statement is the statement whose head specifies a variable that serves as a counter or index during the iteration, initial value, final value and increment.
For example :  For I = 1 step 2 until 30 do body.

4. Indefinite repetition :->
                     Where the condition for loop exit are complex and not easily expressible in the usual loop head i.e the loop with no explicit termination
loop
------
exit when condition ;
------
end loop




We can write every iteration with for loops.
->  Simple counter : for ( i =1 ; i<= 10 , i++ ) { body }
-> Infinite loop :- for ( ; ; ) { body }
-> Counter with exit condition : for ( i = 1; i <= 100; && NotEndfile ; i++ ) { body }
Advantage :->   It is easy to debug, understand and verify.
Disadvantage ->
  • Multiple exit points.
  • Do-While do.
  • Exceptional condition.
#  Proper program :- A proper program, is our formal model of a control structure.
  1. Has a single entry arc.
  2. Has a single exit arc.
  3. Has a path from the entry arc to each node and from each node to the exit arc.
A prime program is a proper program that cannot be subdivide into smaller proper programs.
# Function Nodes :-> Represent computation by a program with a single arc coming in and single arc going out and is representing by a square box.

fig

# Decision Nodes :-> Are represented as diamond-shaped boxes with one input arc and two output arcs labeled true and false.

fig--

# Join node :-> Is represented as a point where two arcs flow together to form a single output arc.

fig---

A Composite program is that which is not prime.

Storage Management in Programming Language
Programming Languages Concepts 
Subprogram Sequence Control in Programming language. 
Structured Sequence Control in Programmig language 
Sequence Control with in Statements 
Implicit and Explicit sequence Control 
Difference between C language and C++ language.
INTEGRATED DEVELOPMENT ENVIRONMENT FOR VISUAL BASIC (IDE)
  1. Java threads and Interrupts in Java programming language
  2. Abstract classes and Generic function in Java
  3. Callback functions in Java Programming language
  4. Constructors in programming language
  5. Static components and constants concept in programming language
  6. Data encapsulation in programming language.
  7. Java threads and Interrupts in Java programming language





Implicit and Explicit sequence Control

 

Implicit and Explicit Sequence Control :->

     A sequence control can be either implicit or explicit.
     Implicit means default i.e the logic or control defined exclusively by the language unless modified by some programmer through explicit or external sequence control.  Example GOTO statements in C or break continue.
     Sequence control is nothing but the flow of a program.
     Suppose we write a small program using if-else ladder.  So, the flow is basically if statements (i.e the condition true) or else statements (i.e the condition is false).  Thus there may be two flow of controls depends upon the condition provided by the user and in any program flow control is started from the keyword main.
    Sequence control is primarily categorized in four groups :-
-> Expression ( Precedence rule and parenthesis ).
-> Statements ( Conditional and iterations statements).
-> Declarative Programming ( declaration of function ).
-> Subprogram ( Subprogram Calls ).

# Expression determines how data are manipulated and changed by a program.
# Statements they are nothing but group of expressions.
# Declarative Programming :-> is an execution model that does not depend on statements and causes execution.
# Subprograms they are call to any procedure or co-routine i.e a way of transfer execution. from one statement to other.

# Two type of controls we have :-

1. Sequence Control :->  It control the order of execution of operations.
2. Data Controls :->  It control the transmission of data among subprogram of a program.
  • Structures used in expressions - Precedence rules and parenthesis.
  • Structures used between group of statements - Conditional and iterative statements.
  • Structures used between subprograms - Subprogram calls.

Sequencing with arithmetic expression :->

For a smaller formula such as 
A = B + C - D.
This will need  C-D as 1 instruction and store the result as T1 i.e C - D = T1.
B + T1 = T2 as second & A = T2, third.
Thus it need three instruction and  two temporary variables.
For Example ( a + b )*( c -a )
-> Advantages of notations.
*  No ambiguity and no parenthesis needed.

Prefix Notation :-> Operator come first which is followed by the operands ( a + b ) * ( c - a )
its Prefix Notation = *+ab-ca.

Postfix Notation :-> i.e A operator follows its operands ( a + b ) * ( c - a ) its Postfix notation is ab+ca-*.




Infix Notation
              Only suitable for binary operations and also produces ambiguity in an expression  unless we used parenthesis.
For example 3*4+6 if we doesn't know the precedence rule or also we doesn't know any parenthesis rules.
This expression remains ambiguous.
But prefix and postfix expressions are easier to implement using stacks.

* Hierarchy of operations :->  The operator that may occur in expressions are placed in a hierarchy or precedence order i.e the operation have higher precedence must have to be executed first.

Associativity :->  We can also use associativity rule instead of precedence rules.
For eg we have a + b = c.

Left Associativity  :->  we can solve this expression from left to right i.e '+' is evaluated before '-'.
OR either we can solve this by right to left i.e '-' has to evaluated before '+' i.e Right Associativity.
Precedence Order

*, abs, not -> highest precedence
*, /,  mod, rem
+, -
+, - , &
=, less then equal to, less then, greater then, greater than equal to.


www.CodeNirvana.in

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