Lab 6
Lab 6
• Un Conditional Jump
• Conditional jump
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:
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:
The following conditional jump instructions have special uses and check the value of flags
Instruction Description Flags tested
JC Jump If Carry CF
JO Jump If Overflow OF
Example-1
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:
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
Reason
Reason
Reason
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