0% found this document useful (0 votes)
12 views13 pages

Lab 3

This lab focuses on understanding flags, jumps, and loops in assembly language, emphasizing the importance of flag registers in controlling CPU operations. It covers unconditional and conditional jump instructions, as well as loop implementation using JMP and LOOP instructions. The lab includes practical tasks to apply these concepts through programming exercises.
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)
12 views13 pages

Lab 3

This lab focuses on understanding flags, jumps, and loops in assembly language, emphasizing the importance of flag registers in controlling CPU operations. It covers unconditional and conditional jump instructions, as well as loop implementation using JMP and LOOP instructions. The lab includes practical tasks to apply these concepts through programming exercises.
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/ 13

LAB NO.

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.

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)


2
• 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".

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++)

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)

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.

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'

5
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

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

You might also like