Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
25 views23 pages

Lab 4

The document outlines Lab No. 4 for the course ELE-311L on Microprocessor Systems, focusing on flags, jumps, and loops in assembly language. It details the objectives, pre-task concepts including flag registers, types of jumps (unconditional and conditional), and loop implementations, along with example code snippets for practical understanding. The lab concludes with a task to implement logic gate operations using assembly language and a summary of key learnings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views23 pages

Lab 4

The document outlines Lab No. 4 for the course ELE-311L on Microprocessor Systems, focusing on flags, jumps, and loops in assembly language. It details the objectives, pre-task concepts including flag registers, types of jumps (unconditional and conditional), and loop implementations, along with example code snippets for practical understanding. The lab concludes with a task to implement logic gate operations using assembly language and a summary of key learnings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

University of Engineering and Technology

Peshawar Abbottabad campus

Course no: ELE-311L

Course title: Microprocessor Systems

Instructor: Engr. Malik Adnan Khan

Submitted By: Malaika

Lab: 5

Reg num: 23ABELT0959

Date of submission: 22-04-2025

1
LAB NO.4

Introductions to flags, jumps and loops in assembly

language

1- Objectives
Upon completion of this lab, one will be able to understand.
Flags
Unconditional and conditional jumps
Loops
Use of compare command

2- Pre task
Flag Registers in Assembly Language:

Flag Registers

• Flag Register is a Special Purpose Register that contains the current state of the
Processor/CPU.
• The flag bits become set (1) or reset (0) depending on the value of the result after any
arithmetic and logical operation is applied.
• The Flag Register can be of 16, 32, 64 bits in a CPU but out of all these bits only 9 bits are
useful. The 9 bits Flag values are described below:

2
In this lab we will focus only two basic flags i.e., carry flag and zero flag.

Why do we Study Flag Registers Basically?

There are many reasons to study flag registers in assembly language, few are discussed below:

Theoretically 2 Reasons are there:

(1) It tells us, what controls the operations of the CPU?

For Example: when we give an Interrupt Call (int 21h) to the CPU, then who will handle this
operation.

(2) It tells us, what handles the status of the operations?

For Example: Suppose we are adding 2 binary numbers which are 11111111 and 00000011. when
we add these 2 numbers a carry bit is generated at the end, so the job of flag register is to handle
or store these carry bits.

In Programming, Flag Registers are used for:

(1) For Conditional Jump

(2) For Comparing numbers, which number is smaller, equal or larger.

Types of Flag Registers

(1) Carry Flag (CF)


3
• The Carry Flag is used to store the carry bit after any arithmetic or logical operation is
applied.
• The carry formed after the addition of any two 8-bit values can be either 0 or 1. It’s actually
a single bit. As a result, 1-bit storage be enough to store the carry information.
• In the flags register, the Carry flag is stored in the Left Side bit position.
• The Carry Flag value become set (1) or reset (0) when
• 1: when there is last carry out
• 0: when there is no last carry out

(2) Zero Flag (ZF)

• When the result of any operation comes 0, it will handle by the zero flag.
• The flag value changes to 0 and 1 when
• 1: when result is 0
• 0: when result is other than 0

Jumps in assembly language:

A jump instruction, like "jmp", just switches the CPU to executing a different piece of code. It's
the assembly equivalent of "goto".

4
You say where to jump to using a "jump label", which is just any string name with a colon after
it. (The same exact syntax is used in C/C++)

Assembly jump C++ goto


mov eax,3 int x=3;
jmp lemme_outta_here goto quiddit;
mov eax,999 ; <- not executed! x=999;
lemme_outta_here: quiddit:
ret return x;

Jump Instructions are used for changing the flow of execution of instructions in the
processor. If we want jump to any instruction in between the code, then this can be
achieved by these instructions. There are two types of Jump instructions:

1. Unconditional Jump Instructions


2. Conditional Jump Instructions

1) Unconditional Jump Instructions

These instructions are used to jump on a particular location unconditionally, i.e. there is
no need to satisfy any condition for the jump to take place. There are three types of
procedures used for unconditional jump. They are:

i. NEAR – This procedure targets within the same code segment. (Intra-segment)
ii. SHORT - This procedure also targets within the same code segment, but the
offset is 1 byte long. (Intra-segment)
iii. FAR - In this procedure, the target is outside the segment and the size of the
pointer is double word. (Inter-segment)

5
Syntax: JMP procedure_namememory_location
Example: JMP short target

2) Conditional Jumps

In these types of instructions, the processor must check for the particular condition. If it
is true, then only the jump takes place else the normal flow in the execution of the
statements is maintained.

The ALU operations set flags in the status word (Flag register). The conditional jump
statements tests the flag and jump is the flag is set.

There are following types of conditional jump instructions:

i) JC : Stands for 'Jump if Carry'

It checks whether the carry flag is set or not. If yes, then jump takes place, that is: If CF =
1, then jump.

ii) JNC : Stands for 'Jump if Not Carry'

It checks whether the carry flag is reset or not. If yes, then jump takes place, that is: If CF
= 0, then jump.

iii) JE / JZ : Stands for 'Jump if Equal' or 'Jump if Zero'

It checks whether the zero flag is set or not. If yes, then jump takes place, that is: If ZF =
1, then jump.

iv) JNE / JNZ : Stands for 'Jump if Not Equal' or 'Jump if Not Zero'

It checks whether the zero flag is reset or not. If yes, then jump takes place, that is: If ZF
= 0, then jump.

v) JP / JPE : Stands for 'Jump if Parity' or 'Jump if Even Parity'

It checks whether the Parity flag is set or not. If yes, then jump takes place, that is: If PF
= 1, then jump.

vi) JNP / JPO : Stands for 'Jump if Not Parity' or 'Jump if Odd Parity'

6
It checks whether the Parity flag is reset or not. If yes, then jump takes place, that is: If
PF = 0, then jump.

Loop in assembly language:

A loop is a block of statements that are repeatedly executed until a condition is satisfied.

The assembly language uses JMP instruction to implement loops. However, the processor set can
use the LOOP instruction to implement loops conveniently.

Syntax and explanation

The following code snippet illustrates how a loop is implemented through JMP instruction:

mov AL, 5 ; store the number of iteration in AL

L1:

(loop code)

DEC AL ; decrement AL

JNZ L1

• The number of iterations of the loop is stored in AL.


• At the end of each iteration, the code decrements AL, then takes a conditional jump
if AL is not zero.

The following code snippet illustrates how a loop is implemented through the loop instruction:

mov ECX 5 ; store the number of iterations in ECX register

L1:

(loop code)

loop l1

7
• The loop instruction always extracts the number of iterations from the ECX register.
• The loop instruction decrements the value of ECX and compares it with zero.
• If the value in ECX is equal to zero, the program jumps to the L1 label; otherwise, the
program exits the loop.

3- In lab
1) Program to understand conditional and unconditional jumps:
; multi-segment executable file template.

data segment
; add your data here!
s1 db "The first number is smaller$"
s2 db "The first number is bigger$"

ends

stack segment
dw 128 dup(0)
ends

code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax

; add your code here

mov al,13
mov bl,12

sub al,bl

jc a
jnc b

a:
mov ah, 9
mov dx, offset s1
int 21h
jmp exit

b:
mov ah, 9
mov dx,offset s2
int 21h
8
jmp exit

; wait for any key....


mov ah, 1
int 21h

exit:

mov ax, 4c00h ; exit to operating system.


int 21h
ends

end start ; set entry point and stop the assembler.

2) Second task: Program to understand jump if carry, jump if zero and jump if not carry or zero:

Code:

; multi-segment executable file template.

data segment
; add y our data here!
num1 db ?
num2 db ?
s1 db "The first number is smaller$"
s2 db 0ah,0dh, "The first number is bigger$"
s3 db 0ah,0dh,"both are equal$"

ends

stack segment
dw 128 dup(0)
9
ends

code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax

; add your code here

mov ah,1
int 21h
mov num1,al

mov ah,1
int 21h
mov num2,al

mov al,num1
mov bl,num2

sub al,bl

jz z
jc a
jnc b

a:
mov ah, 9
mov dx, offset s1
int 21h
jmp exit

b:
mov ah, 9
mov dx,offset s2
int 21h
jmp exit

z:
mov ah, 9
mov dx,offset s3
int 21h
jmp exit

; wait for any key....


mov ah, 1
int 21h
10
exit:

mov ax, 4c00h ; exit to operating system.


int 21h
ends

end start ; set entry point and stop the assembler.

Task 3:

Code:
; multi-segment executable file template.

data segment
; add your data here!
pkey db "press any key...$"
ends

stack segment
dw 128 dup(0)
ends

code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
mov dl,"B"
11
mov cx,10

label1:
push cx
mov ah,2
int 21h
inc dl
pop cx
loop label1
exit:

; add your code here

lea dx, pkey


mov ah, 9
int 21h ; output string at ds:dx

; wait for any key....


mov ah, 1
int 21h

mov ax, 4c00h ; exit to operating system.


int 21h
ends

end start ; set entry point and stop the assembler.

12
Task 4:

Code:

; multi-segment executable file template.

data segment
; add your data here!

num1 db ?
ends

stack segment
dw 128 dup(0)
ends

code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax

mov ah,1
int 21h
mov num1,al
mov dl,num1

mov cx,100
label1:
push cx
mov ah,2
int 21h
dec dl
pop cx
loop label1
jmp exit
exit:

; add your code here

mov ah, 9
int 21h ; output string at ds:dx

; wait for any key....


mov ah, 1
int 21h

mov ax, 4c00h ; exit to operating system.


int 21h
13
ends

end start ; set entry point and stop the assembler.

4- Post lab
• Implement all the 7 logic gates operations for user inputs and use Jmp, jc, jnc, jz, jnz, comp by
your own choice and submit them in your lab report.

Code:
data segment
num1 db ?
num2 db ?
ask db 0Ah, 0Dh, "Select Logic Operation:"
db 0AH,0Dh,"a. AND"
db 0Ah, 0Dh,"b.OR"
db 0Ah, 0Dh,"c. NOT of num 1"
db 0Ah, 0Dh,"d. XOR"
db 0Ah, 0Dh,"e. NAND"
db 0Ah, 0Dh,"f. NOR"
db 0Ah, 0Dh,"g. XNOR" "$"

gkey db 0ah, 0dh, "press any key...$", 0Ah, 0Dh


ends

stack segment
dw 128 dup(0)
ends

code segment
start:
; set segment registers:
14
mov ax, data
mov ds, ax
mov es, ax

; input first number


mov ah, 1
int 21h
mov num1, al

; input second number


mov ah, 1
int 21h
mov num2, al

mov ah, 9
mov dx, offset ask
int 21h

mov ah, 1
int 21h

cmp al, 'a'


jz AND_

cmp al, 'b'


jz OR_

cmp al, 'c'


jz NOT_

cmp al, 'd'


jz XOR_

cmp al, 'e'


jz NAND_

cmp al, 'f'


jz NOR_

cmp al, 'g'


jz XNOR_

AND_:
mov al, num1
and al, num2
jmp DISPLAY

OR_:
mov al, num1
or al, num2
15
jmp DISPLAY

NOT_:
mov al, num1
not al
jmp DISPLAY

XOR_:
mov al, num1
xor al, num2
jmp DISPLAY

NAND_:
mov al, num1
and al, num2
not al
jmp DISPLAY

NOR_:
mov al, num1
or al, num2
not al
jmp DISPLAY

XNOR_:
mov al, num1
xor al, num2
not al
jmp DISPLAY

DISPLAY:
mov ah, 2
mov dl, al
int 21h
jmp exit

exit:
lea dx, gkey
mov ah, 9
int 21h

mov ah, 1
int 21h

mov ax, 4c00h


int 21h
ends

end start

16
Conclusion:
In this lab we study about the gate operations flags jumps and loops zero flag is up when it is equal at
both sides and zero flag is down when not equal and the loops run till the condition of the program not
completed forexample its condition is 10 it runs 10 times.

17
18
19
20
21
22
23

You might also like