Lab Manual 04
Lab Manual 04
Course Instructor:
Lab Instructor:
Loops
Loops in assembly allow repeating a sequence of instructions until a certain condition is met. Al-
though assembly doesn't have a dedicated loop keyword like higher-level languages, loops are typi-
cally created using branch instructions and counter registers like CX.
Use of Counter Registers: Registers like CX are used to count iterations (e.g., loop with
CX).
Decrement and Check: After each iteration, the counter is decremented and checked if it
reaches zero.
Repeat Instructions: Commonly used to process arrays, strings, or repeated operations.
Efficiency: Loops allow repetitive tasks to be handled efficiently with minimal code.
Control Flow Instructions manage the sequence of execution in a program. They allow skipping,
repeating, or branching to different parts of the code, based on conditions or program logic.
Unconditional Jump
The JMP instruction can be used for implementing loops.
L2:
CMP CL, 0 ; Compare the counter with 0
JNE L1 ; If not zero, jump back to L1 to continue the loop
Explanation
This uses an unconditional jump (JMP) to separate the decrement operation from the condition
check, making the logic explicitly unconditional in one part.
Conditional Jump
If the condition for the jump is the true ,the next instruction to be executed is the one at destination
label ,which may be precede or follow the jump itself.
JNZ is an example of conditional jump instruction
JXXX Destination label
For example, the following code snippet can be used for executing the loop-body 10 times.
MOV CL, 10
L1:
<LOOP-BODY>
DEC CL
JNZ L1
The processor instruction set, however, includes a group of loop instructions for implementing
iteration. The basic LOOP instruction has the following syntax −
LOOP label
Where, label is the target label that identifies the target instruction as in the jump instructions. The
LOOP instruction assumes that the CL register contains the loop count. When the loop instruction
is executed, the CL register is decremented and the control jumps to the target label, until the CL
register value, i.e., the counter reaches the value zero.
Flags
Instruction Description
tested
Program 1:
Num1: dw 10, 20, 30, 40, 50, 10, 20, 30, 40, 50
total: dw 0
Iteration Table:
Program 2:
; A program to add ten numbers using register + offset addressing
[ORG 0x100]
Num1: dw 10, 20, 30, 40, 50, 10, 20, 30, 40, 50
total: dw 0
Iteration Table:
Program 3:
start:
mov bx, 0 ;initialize array index to zero
mov ax, 0 ;initialize sum to zero
Practice Questions
Question 1
Question 2
Handling Two Number Sets with Comparison and Jump. Given two pairs of byte-sized
numbers, compare the first pair:
If the first number is greater than the second, swap their values.
Otherwise, multiply the first number by 2.
Compare the second pair:
If the first number is smaller than the second, move the sum of both into a register.
Otherwise, move the larger number into that register.