Flow Control and Conditional Jump Instructions
Flow Control and Conditional Jump Instructions
Statement Purpose:
Objective of this lab is to introduce students the philosophy and ways to control transfer based on condition or unconditionally at various points in programs. They will make familiar with condition as well unconditional JUMP instructions based on any type of condition whatsoever.
Activity Outcomes:
This lab teaches you the following topics:
- Students will know that transferring control based on conditions is very important to write real world applications as we have to do transfer our direction based on some decisions in real life.
- They will know that they can transfer control on various types of decision g. something is greater or smaller than other etc.
- They will know how processor make decisions and how to give him a program based on
Instructor Note:
The circuits in the CPU can perform simple decision-making based on the current state of the processor. For the 8086/8088 processors, the processor state is implemented as nine bits, called flags, in the Flags register. Each decision made by the 8086/8088 CPU is based on the values of these flags.
The flags are classified as either status flags or control flags. There are 6 status flags: Carry flag (CF), Parity flag (PF), Auxiliary carry flag (AF), Zero flag (ZF), Sign flag (SF), and Overflow flag (OF). There are 3 control flags: Trap flag (TF), Interrupt flag (IF), and Direction flag (DF).
15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
OF | DF | IF | TF | SF | ZF | AF | PF | CF |
The Flags register: Bits 8, 9, and 10 are the Control flags.
The Status flags reflect the result of some instructions executed by the processor. For example, when a subtraction operation results in a zero, the Zero flag (ZF) is set to 1 (true). The Control flags enable or disable certain operations of the processor. For example, if the Interrupt flag (IF) is cleared to 0, inputs from the keyboard are ignored by the processor.
Most 8086/8088 instructions can be classified into three categories:
- Instructions that modify one or more Status (Status flags modifying instructions).
- Instructions that modify one or more Control (Control flags modifying instructions).
- Instructions that do not modify any
Introduction
FLOW CONTROL INSTRUCTIONS
- The JMP (Jump), CALL, RET, and IRET instructions transfer control unconditionally to another part of the program.
- The conditional jump instructions, except JCXZ and JECXZ, transfer control to another part of the program each depending on one or more Status flag These instructions are of the form Jcondition, where condition is represented by one, two, or three letters.
- JCXZ and JECXZ transfer control to another part of the program if CX = 0 and ECX = 0
- The LOOP instruction decrements CX and transfer control to the beginning of its loop if CX ¹ If CX = 0 before the LOOP instruction, it is decremented to -1 at the end of the first iteration of the loop. This -1 is treated as the unsigned number 65535, thus the loop will iterate 65536 times.
- The conditional loop instructions LOOPE and LOOPZ, which are equivalent, decrement CX and transfer control to the beginning of their loops if CX > 0 and ZF = 1. If CX £ 0 before the loop, the loop is executed once.
- The conditional loop instructions LOOPNE and LOOPNZ, which are equivalent, decrement CX and transfer control to the beginning of their loops if CX > 0 and ZF = 0. If CX £ 0 before the loop, the loop is executed once.
THE JMP INSTRUCTION (Unconditional Jump) The JMP instruction, whose syntax is:
JMP target
unconditionally transfers control to the target location. There are two major categories of JMP instructions:
- Intrasegment jump: A jump to a statement in the same code
- Intersegment or far jump: A jump to a statement in a different code
Intrasegment jumps simply change the value in the IP register. Intersegment jumps change both CS and IP.
Direct and Indirect jumps
A jump can either be direct or indirect. In a direct jump the address of the target is obtained from the instruction itself, i.e., the operand of the JMP instruction is a label. Example:
JMP L2
In an indirect jump the address of the target is obtained from a 16-bit or a 32-bit variable or a general-purpose register referenced by the JMP instruction.
CONDITIONAL JUMP INSTRUCTIONS
Conditional jumps are of the general form:
Jcondition StatementLabel
where condition is one, two, or three letters the StatementLabel must in the current code segment and should be within -128 to +127 bytes from the conditional jump instruction.
Activities:
Activity 1:
Jump based on Equality or Zero Flag: Take two numbers from the user and display whether number 2 is equal or not to number 1.
Solution:
Note: Replace JZ with JE and there will be no change in the output as both works similarly.
TITLE Lab Flow Control and Conditional Jump Instructions
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
msg1 BYTE “2nd Number is equal to 1st Number “,0 msg2 BYTE “2nd Number is not equal to 1st Number “,0 msg01 BYTE “Enter 1st Number”,0
msg02 BYTE “Enter 2nd Number”,0
main PROC
mov edx, OFFSET msg01 call writestring
call crlf call readint
mov ebx, eax
mov edx, OFFSET msg02 call writestring
call crlf call readint
cmp eax, ebx jz EQUAL
mov edx, OFFSET msg2 call writestring
call crlf jmp LAST EQUAL:
mov edx, OFFSET msg1 call writestring
call crlf LAST:
exit
main ENDP END main
Activity 2:
Jump based for Signed Number: Take two numbers from the user and display whether number 2 is greater or not from number 1.
Solution:
Note: Replace JG with JNBE and there will be no change in the output as both works similarly. TITLE Lab Flow Control and Conditional Jump Instructions
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
msg1 BYTE “2nd Number is greater than 1st Number “,0 msg2 BYTE “2nd Number is less than 1st Number “,0 msg01 BYTE “Enter 1st Number”,0
msg02 BYTE “Enter 2nd Number”,0
main PROC
mov edx, OFFSET msg01 call writestring
call crlf call readint
mov ebx, eax
mov edx, OFFSET msg02 call writestring
call crlf call readint
cmp eax, ebx jg Greater
mov edx, OFFSET msg2 call writestring
call crlf jmp LAST Greater:
mov edx, OFFSET msg1 call writestring
call crlf LAST:
exit
main ENDP END main
Activity 3:
Jump based on Unsigned Value: Take two numbers from the user and display whether number 2 is greater or not from number 1.
Solution:
Note: Replace JA with JNBE and there will be no change in the output as both works similarly. TITLE Lab Flow Control and Conditional Jump Instructions
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
msg1 BYTE “2nd Number is greater than 1st Number “,0 msg2 BYTE “2nd Number is less than 1st Number “,0 msg01 BYTE “Enter 1st Number”,0
msg02 BYTE “Enter 2nd Number”,0
main PROC
mov edx, OFFSET msg01 call writestring
call crlf call readint
mov ebx, eax
mov edx, OFFSET msg02 call writestring
call crlf call readint
cmp eax, ebx ja Greater
mov edx, OFFSET msg2 call writestring
call crlf
jmp LAST Greater:
mov edx, OFFSET msg1 call writestring
call crlf LAST:
exit
main ENDP END main
Home Activities:
Activity 1:
Take 3 numbers will be entered by the user. Tell which number is the greatest. Write its Code.
Solution:
TITLE Lab Flow Control and Conditional Jump Instructions
; Author: Ashfaq Hussain Farooqi include irvine32.inc
.data
.code main proc
num1 dword ? num2 dword ? num3 dword ?
res1 byte “Number 1 is greatest”,0 res2 byte “Number 2 is greatest”,0 res3 byte “Number 3 is greatest”,0
call readint mov num1,eax
call readint mov num2,eax call readint mov num3,eax
mov eax,num1 cmpeax,num2 jg con2
jmp num22 con2: cmpeax,num3 jg result1 num22:
mov eax,num2 cmpeax,num1 jg con3
jmp result3 con3: cmpeax,num3 jg result2 jmp result3
result1: ;output call crlf
mov edx,offset res1 call writestring
jmp endd
result2: ;output
call crlf
mov edx,offset res2 call writestring
jmp endd
result3: ;output
call crlf
mov edx,offset res3 call writestring
jmp endd
exit
main endp end main
endd: call crlf
Activity 2:
Take a number entered by the user. Tell whether the number is:
• + EVEN
- -EVEN
• +ODD
-
-ODD
Write its Code. Solution:
TITLE Lab Flow Control and Conditional Jump Instructions
; Author: Ashfaq Hussain Farooqi
INCLUDE Irvine32.inc
.data
msg BYTE “The number is + even”,0 msg1 BYTE “The number is + odd”,0 msg2 BYTE “The number is – even”,0 msg3 BYTE “The number is – odd”,0 var DWORD ?
.code
main PROC mov eax, 0
call readint test ax, 8000h jz Positive test ax, 0001h jnz odd
mov edx, offset msg2 call writestring
call crlf jmp last
odd:
mov edx, offset msg3 call writestring
jmp last
positive:
test ax, 0001h jz even1
mov edx, offset msg1 call writestring
jmp last even1:
mov edx, offset msg call writestring
last:
call crlf
exit main ENDP END main
Assignment:
Problem: Find the greater value of the same indexes of two arrays (array1 and array2). Subtract the greater value from the smaller value. Store that value in the same index of array three as shown in the following table.
Array1 | 5 | 10 | 15 | 20 | 25 |
Array2 | 10 | 5 | 10 | 25 | 10 |
Array3 (Output) | 5 | 5 | 5 | 5 | 15 |