Lab 3
Lab 3
3
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:
1
In this lab we will focus only two basic flags i.e., carry flag and zero flag.
There are many reasons to study flag registers in assembly language, few are discussed below:
For Example: when we give an Interrupt Call (int 21h) to the CPU, then who will handle this
operation.
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.
• 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
A jump instruction, like "jmp", just switches the CPU to executing a different piece of code. It's
the assembly equivalent of "goto".
3
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++)
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:
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)
4
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.
It checks whether the carry flag is set or not. If yes, then jump takes place, that is: If CF =
1, then jump.
It checks whether the carry flag is reset or not. If yes, then jump takes place, that is: If CF
= 0, then jump.
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.
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'
5
It checks whether the Parity flag is reset or not. If yes, then jump takes place, that is: If
PF = 0, then jump.
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.
The following code snippet illustrates how a loop is implemented through JMP instruction:
L1:
(loop code)
DEC AL ; decrement AL
JNZ L1
The following code snippet illustrates how a loop is implemented through the loop instruction:
L1:
(loop code)
loop l1
6
• 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:
2) Second task: Program to understand jump if carry, jump if zero and jump if not carry or zero:
7
8
9
3) Task 3: program to understand jump if zero for user input:
10
4) Program to understand loop:
11
12
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.
13