Lab No. 11
Lab No. 11
Objectives
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.
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;
main ENDP
end main
EXIT_:
MOV AX, 4C00H
INT 21H
Call Subroutine
Components of Subroutines
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
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
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.