Flag Control, Compare and Jump Instructions: Lecture On
Flag Control, Compare and Jump Instructions: Lecture On
ZF
AF
PF
CF
The instructions CLC, STC, and CMC are used to clear, set, and complement
the carry flag.
Compare Instruction
CMP instruction is used to compare two 8-bit or 16-bit numbers. The results
of comparison is reflected by changes in the status flags as a result of subtracting
the source from the destination without saving result of subtraction.
Mnemonic
Meaning
Format
Operation
Flags Affected
CMP
Compare
CMP D,S
Destination
Source
Register
Register
Register
Memory
Memory
Register
Register
Immediate
Memory
Immediate
Accumulator
Immediate
EX: Describe what happens to the status flags as the sequence of instructions
Is executed
MOV AX, 1234H
MOV BX, 0ABCDH
CMP AX, BX
The First two instructions makes
(AX) = 0001001000110100B
(BX) = 1010101111001101B
Jump Instructions
There are two types of jump, unconditional and conditional
In unconditional jump, as the instruction is executed, the jump always takes
Place to change the execution sequence.
Part I
Unconditional Jump
JMP AA
Part II
AA
xxxxxx
Part III
Conditional Jump
Part I
JCC AA
Condition
met?
NO
XXXXXX
Part II
YES
AA xxxxxx
Part III
Unconditional Jump
Mnemonic
JMP
Meaning
Format
Operation
Flags Affected
None
Operand
Short-label
Near label
Far-label
Memptr16
Regptr16
Memptr32
Allowed operand for the jump instruction
Unconditional Jump
Short Jump: JMP short Label1 (8 bit)
Near Jump: JMP near Label
This is the default jump: JMP Label
The displacement (16 bit) is added to the IP of the instruction
following jump instruction.
The displacement can be in the range of 32,768 to 32,768.
The target address can be register indirect, or assigned by the
label.
Register indirect JMP: the target address is the contents of two
memory locations pointed at by the register.
Ex: JMP [SI] will replace the IP with the contents of the memory
locations pointed by DS:SI and SI+1
Example:
MES DB type any letter, number, or punctuation key
DB any F1 to F10 to end the program
DB 0d,0a,0a,$
MOV DX, OFFSET MES
MOV AH,09h
INT 21h ; to output the characters starting from the offset
AGAIN: MOV AH,0h
INT 16h; to check the keyboard
CMP AL,00h
JZ QUIT ;check the value of the input data
MOV AH, 0Eh
INT 10h; echo the character to output
JMP AGAIN
QUIT: INT 20h
Conditional Jump
The folowing flags are affected based on a general comparison instruction
Mnemonic
Description
Flag/Registers
JZ
Jump if ZERO
ZF=1
JE
Jump if EQUAL
ZF=1
JNZ
ZF=0
JNE
ZF=0
Jump if CARRY
CF=1
Jump if NO CARRY
CF=0
Jump if CX=0
CX=1
JC
JNC
JCXZ
JECXZ
Jump if ECX=0
ECX=0
JP
PF=1
JNP
PF=0
Description
Flag/Registers
JAE
CF=0
JNB
CF=0
CF=1
CF=1
JBE
CX=1 or ZF=1
JNA
CF=1 or ZF=1
JNBE
JB
JNAE
Description
Jump if GREATER
op1>op2
Flag/Registers
SF=OF and ZF=0
JNLE
JGE
SF=OF
JNL
SF=OF
SF<>OF
SF<>OF
JLE
ZF=1 or SF<>OF
JNG
ZF=1 or SF<>OF
JL
JNGE
JS
SF=1
JNS
SF=0
JO
Jump if OVERFLOW
OF=1
Jump if NO OVERFLOW
OF=0
JNO
Short Jumps
Conditional Jump is a two byte instruction.
In a jump backward the second byte is the
2s complement of the displacement value.
To calculate the target the second byte is
added to the IP of the instruction right after
the jump.
Ex:
000D ADD AL,[BX]
000F INC BX
0010 DEC CX
0011 JNZ 000D
0013
Example:
Write a program that adds 6 bytes of data and saves the result.
The data should be the following decimal numbers: 52, 21, 51, 31,11, 74
.model small
.stack 100h
.data
Data_in DB 52, 21, 51, 31, 11, 74
Sum DB ?
.code
main proc far
MOV AX, @Data
MOV DS, AX
MOV CX, 06h
MOV BX, offset Data_in
MOV AL, 0