8086/8088 Program Flow Control-II
Objectives
      To learn the basic of various flow control Instructions of 8086.
      Use these flow control instructions to perform simple tasks
Flow control, or branching, instructions for jumps, loops and conditional jumps allow us to
control the execution of program statements and to build high-level programming structures such
as if-else statements, case statements, and loops in assembler programs.
Loop Instruction
Loop instruction jumps to the specified label until cx is equal to zero. So programmer must place
the counts/iterations in register cx before using loop instruction.
Loop structure usually consists of a condition statement and repetition structure.
There are three types of loops
1. For loop
2. While loop
3. Do while loop
Any task that must be performed repeatedly will use loop instruction. Advantage of using loop
instruction is that it will save your time and effort as well as the memory of the system in
performing a certain repeated task.
Example
;32-bit addition
.model small
;.data
.code
main PROC
      ;mov ax,@data;
      ;mov ds,ax;
 mov ax, 0
mov cx, 10
LOOPSTART:
  inc ax;
  loop LOOPSTART;
       mov     ax, 4C00h;
       int     21h;
main ENDP
end main
EXIT_:
      MOV AX, 4C00H
      INT 21H
Call Subroutine
A program is made up of instructions which implement the solution to a problem. In a given
program, it is often needed to perform a sub-task many times on different data values. So,we
split the program into smaller units which solve a particular part of the problem. These task
specific sub-units are termed as functions, procedures or subroutines. In assembly language, we
use the word subroutine for all subprograms to distinguish between functions used in other
programming languages and those used in assembly languages. The block of instructions that
constitute a subroutine can be included at every point in the main program when that task is
needed. However, this would result in unnecessary waste of memory space. Rather, only one
copy of the instructions that constitute the subroutine is placed in memory and can be accessed
repeatedly.
Components of Subroutines
An assembly language routine has:
1. An entry point: The location of the first instruction in the routine.
2. Parameters: The list of registers or memory locations that contain the parameters for the
routine.
3. Return values: The list of registers or memory locations to save the results.
4. Working storage: The registers or memory location required by the routine to perform its
task.
Calling Subroutines
There are two ideas behind a subroutine:
1. You should be able to call the subroutine from anywhere.
2. Once the subroutine is complete, it should return to the place that called the subroutine.
There are special instructions for transferring control to subroutines and restoring control to the
main program. The instruction that transfers the control is usually termed as call, jump or branch
to subroutine. The calling program is called Caller and the subroutine called is known as Callee.
The instruction that transfer control back to the caller is known as Return.
Example
;Subroutine with arguments Example
.model small
.data
    val1 dw 2000h
    val2 dw 3000h
    avg dw ?
.stack 256
.code
main proc
    mov ax,@data
    mov ds,ax
    mov cx,val1
    mov dx,val2
         push cx
  push dx
  call takeavg
  add sp,4
  mov avg,ax
  mov ah,4ch
  int 21h
main endp
; Average Subroutine
takeavg proc
  mov bp,sp
  mov ax,[bp+4]
   mov bx,[bp+2]
   add ax,bx
   sar ax, 1
   ret
takeavg endp
end main
Tasks
Task 1 initialize an array with 10 elements and calculate its average using loop instruction. Make
a separate subroutine that must be called from main function and display the result on screen.