0% found this document useful (0 votes)
11 views8 pages

Lab 6

The document discusses program control instructions in x86 assembly language, focusing on unconditional and conditional jumps that alter the flow of execution. It explains the syntax and usage of various jump instructions, including how they interact with the FLAGS register and the conditions they test. Additionally, it includes examples and exercises for implementing branching and control structures in assembly language.

Uploaded by

noor4601283
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)
11 views8 pages

Lab 6

The document discusses program control instructions in x86 assembly language, focusing on unconditional and conditional jumps that alter the flow of execution. It explains the syntax and usage of various jump instructions, including how they interact with the FLAGS register and the conditions they test. Additionally, it includes examples and exercises for implementing branching and control structures in assembly language.

Uploaded by

noor4601283
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/ 8

Computer Architecture & Organization

Department of Computer Science &


Information Technology
Lab # 06
Program Control Instructions (Implementing Branching in x86
Assembly Language)
Conditional execution in assembly language is accomplished by several looping and
branching instructions. These instructions can change the flow of control in a program.
Program control execution is observed in two scenarios.

• Un Conditional Jump
• Conditional jump

Unconditional Branches (Jumps)


An unconditional branch (jump) instruction transfers control to a specified label in the
program without testing any condition. This is similar to goto in a HLL. Transfer of control
may be forward, to execute a new set of instructions or backward, to re-execute the same steps.
The 80x86 jump instruction has the following format:

Syntax: Jmp label

Here a program is presented that uses jump instruction to loop forever through the program
and calculates the 1+2+3+……+n sum .

.code
Main proc
Mov ax,0
Mov bx,0
Forever: Inc bx
Add ax,bx
Jmp forever
Main endp
Conditional Branches
Unlike an unconditional branch, a conditional branch tests a condition before transfer of
control.This is performed by a set of jump instructions j<condition> depending upon the condition.
The conditional instructions transfer the control by breaking the sequential flow and they do it by
changing the offset value in IP.
This conditional branching is used to implement if structures, other selection structures, and loop
structures in 80x86 assembly language. Most conditions considered by the conditional jump
instructions are settings of flags in the FLAGS register. For example, jz lable1 means to transfer
control to the instruction to label1, if the zero flag ZF is set to 1. Conditional branch instructions
Computer Architecture & Organization
Department of Computer Science &
Information Technology

don not modify flags; they just react to previously set flag values. Most common way to set flags
for conditional branches is to use compare instruction that has the following format:

Syntax: cmp operand1, operand2

Flags are set the same as for the subtraction operation operand1–operand2. Operands, however, are
not changed. Following are the conditional jump instructions used on unsigned data used for logical
operations:

Instruction Description Flags tested

JE/JZ Jump Equal or Jump Zero ZF

JNE/JNZ Jump not Equal or Jump Not Zero ZF

JA/JNBE Jump Above or Jump Not CF, ZF


Below/Equal

JAE/JNB Jump Above/Equal or Jump Not CF


Below

JB/JNAE Jump Below or Jump Not CF


Above/Equal

JBE/JNA Jump Below/Equal or Jump Not AF, CF


Above

The following conditional jump instructions have special uses and check the value of flags
Instruction Description Flags tested

JXCZ Jump if CX is Zero None

JC Jump If Carry CF

JNC Jump If No Carry CF

JO Jump If Overflow OF

JNO Jump If No Overflow OF


Computer Architecture & Organization
Department of Computer Science &
Information Technology

JP/JPE Jump Parity or Jump Parity Even PF

JNP/JPO Jump No Parity or Jump Parity PF


Odd

JS Jump Sign (negative value) SF

JNS Jump No Sign (positive value) SF

Example-1

Un Conditional Conditional Both Un-conditional & Conditional

Mov dl,1 Mov dl,1 Mov dl,1

Add dl,30h Add dl,30h Add dl,30h Endpara:


Start: Start:
Start: Mov ah,2 Mov ah,2 Mov ah,2 Mov ah,4ch

Int 21h Int 21h Int 21h Int 21h

Inc dl Inc dl Inc dl


Jmp start Cmp dl,35h
Cmp dl,5
Jne Start
Jne start

Jmp endpara

Implementation of if Structure
Let's implement the following pseudo-code in x86 assembly language.

if (total 100)
then add value to total;
end if;
Assuming total and value are in memory and count in CX, the assembly code is shown below:
cmp total, 100
jae addValue
addValue: mov bx, value
add total, bx
Computer Architecture & Organization
Department of Computer Science &
Information Technology

EXERCISE:

Q1 write an assembly language program that repeatedly (forever) calculates 1*2*3*…*n.

Q2 Assume for each part of this exercise that the AX contains 00 4F and the doubleword
referenced by value contains FF38. Determine whether each of the following conditional branch
instructions causes a transfer of control to label dest.

i. cmp ax,value
jb dest

Your answer: YES NO

Reason

ii. cmp ax,value


ja dest

Your answer: YES NO


Computer Architecture & Organization
Department of Computer Science &
Information Technology

Reason

iii. cmp ax,04fh


je dest

Your answer: YES NO

Reason

iv. add ax,200


jne dest

Your answer: YES NO

Reason


Q3 Write down the code to find number input by user is EVEN or
ODD Save “Enter number “ @ Location loc1

Save “Number is EVEN”@ Location loc2

Save “Number is ODD” @ Location loc3
Hint:
Save the input to another register(Say
bl) Mov 2 to Al
Divide bl to al
Computer Architecture & Organization
Department of Computer Science &
Information Technology

Q4 Write down the code to find Largest number using @least one conditional & unconditional
jump instructions. Input two numbers from user; Output should look like as:

• Enter number 1:
• Enter number 2:
Largest number is :
(save above strings @ locations msg1,msg2 and msg3of DS)

Q5 Repeat the Q4 to find largest number by using @least one conditional & unconditional jump
instructions. Input two numbers from user ; Output should look like as:

• Enter number 1:
• Enter number 2:
Largest number is :

36
Computer Architecture & Organization Department of Software Engineering

(save above strings @ locations msg1,msg2 and msg3of DS)



Save numbers (input by user) @ locations num1 and num2 of DS
Computer Architecture & Organization Department of Computer Science &
Information Technology

You might also like