0% found this document useful (0 votes)
10 views7 pages

Lab Manual 04

Uploaded by

rumanasif3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Lab Manual 04

Uploaded by

rumanasif3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Computer Organization and Assembly Language

Lab Manual (Lab 4)

Course Instructor:
Lab Instructor:

Session: Fall 2024

School of Systems and Technology


UMT Lahore Pakistan
Objectives:
 Addressing Modes
 Loops
 Control Flow Instruction

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.

MOV CL, 10 ; Initialize the counter to 10


L1: ; Start of the loop
; <LOOP-BODY> ; Replace with the operations you want to perform
DEC CL ; Decrement the counter
JMP L2 ; Unconditionally jump to the condition check

L2:
CMP CL, 0 ; Compare the counter with 0
JNE L1 ; If not zero, jump back to L1 to continue the loop

Explanation

1. MOV CL, 10: Initializes the counter (CL) to 10.


2. L1: A label marking the start of the loop.
3. <LOOP-BODY>: Perform desired operations here.
4. DEC CL: Decrement the counter.
5. JMP L2: Unconditionally jumps to the label L2, bypassing the regular flow.
6. L2: Compares the counter with zero (CMP CL, 0).
7. JNE L1: If the counter is not zero, jumps back to L1.

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

JE/JZ Jump Equal or Jump Zero ZF

JNE/JNZ Jump not Equal or Jump Not Zero ZF

JG/JNLE Jump Greater or Jump Not Less/Equal OF, SF, ZF

JGE/JNL Jump Greater/Equal or Jump Not Less OF, SF

JL/JNGE Jump Less or Jump Not Greater/Equal OF, SF

JLE/JNG Jump Less/Equal or Jump Not Greater OF, SF, ZF


The CMP Instruction
The jump condition is often by CMP instruction .it has the form
CMP destination, source
The instruction compares destination and source by computing contents minus source contents. The
result is not stored, but the flags are affected.
CMP AL, BL
JE EQUAL
CMP AL, BH
JE EQUAL
CMP AL, CL
JE EQUAL
NON_EQUAL: ...
EQUAL: ...
Sample Programs

Program 1:

; A program to add ten numbers


[ORG 0x100]
mov bx, Num1 ;point bx to first number
mov cx, 10 ;load count of numbers in cx
l1: add ax, [bx] ;add number to ax

add bx, 2 ;advance bx to next number


sub cx, 1 ;numbers to added reduced
jnz l1 ;if numbers remain, add next

mov [total], ax ;write back sum in memory

mov ax, 0x4c00


int 0x21

Num1: dw 10, 20, 30, 40, 50, 10, 20, 30, 40, 50
total: dw 0

Iteration Table:

BX (Address of Current Num- [BX] (Value at CX AX (Accumulated


Iteration
ber) BX) (Count) Sum)
Start Num1 10 10 0000
1 Num1 10 9 0010 (10)
2 Num1 + 2 20 8 0032 (10 + 20 = 30)
3 Num1 + 4 30 7 0050 (30 + 30 = 60)
4 Num1 + 6 40 6 0090 (60 + 40 = 100)
5 Num1 + 8 50 5 00FA (100 + 50 = 150)
6 Num1 + 10 10 4 0104 (150 + 10 = 160)
7 Num1 + 12 20 3 0118 (160 + 20 = 180)
8 Num1 + 14 30 2 0136 (180 + 30 = 210)
9 Num1 + 16 40 1 015E (210 + 40 = 250)
10 Num1 + 18 50 0 0192 (250 + 50 = 300)

Program 2:
; A program to add ten numbers using register + offset addressing
[ORG 0x100]

mov bx, 0 ;initialize array index to zero


mov cx, 10 ;load count of numbers in cx
mov ax, 0 ;initialize sum to zero

l1: add ax, [Num1+bx] ;add number to ax


add bx, 2 ;advance bx to next number
sub cx, 1 ;numbers to added reduced
jnz l1 ;if numbers remain, add next

mov [total], ax ;write back sum in memory

mov ax, 0x4c00 ;terminate the program


int 0x21

Num1: dw 10, 20, 30, 40, 50, 10, 20, 30, 40, 50
total: dw 0

Iteration Table:

Iteration BX (Offset) [Num1+BX] (Value at Offset) CX (Count) AX (Accumulated Sum)


Start 0 10 10 0000
1 0 10 9 0010 (10)
2 2 20 8 0032 (10 + 20 = 30)
3 4 30 7 0050 (30 + 30 = 60)
4 6 40 6 0090 (60 + 40 = 100)
5 8 50 5 00FA (100 + 50 = 150)
6 10 10 4 0104 (150 + 10 = 160)
7 12 20 3 0118 (160 + 20 = 180)
8 14 30 2 0136 (180 + 30 = 210)
9 16 40 1 015E (210 + 40 = 250)
10 18 50 0 0192 (250 + 50 = 300)

Program 3:

;A program to add ten numbers without a separate counter.


[ORG 0x100]
jmp start
Num1: dw 10, 20, 30, 40, 50, 10, 20, 30, 40, 50
total: dw 0

start:
mov bx, 0 ;initialize array index to zero
mov ax, 0 ;initialize sum to zero

l1: add ax, [Num1+bx] ;add number to ax


add bx, 2 ;advance bx to next number
cmp bx, 20 ;are beyond the last index
jnz l1 ;if not, add next number

mov [total], ax ;write back sum in memory

mov ax, 0x4c00 ;terminate the program


int 0x21

Practice Questions

Question 1

Consider 4 numbers of size byte.


 Add first two numbers if they are equal
 Subtract them if they are not equal.
 Add the other two numbers if first(third) is less than second(fourth)
 or put the value of second(fourth) in first(third) if they are equal.

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.

You might also like