Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

BACS1024 Introduction to Computer Systems

Assembly Language Program - Part V


1. Comparison Instruction
2. Conditional Processing

1. Conditional Instructions

1) CMP instruction
 Objective: To compare 2 numeric data.
 Function: Perform implied subtraction of a source from a destination.
 Flag affected: AF, CF, OF, PF, SF, ZF.
 Comparison for unsigned and signed operands.

Unsigned operands Signed operands


CMP result
ZF CF SF / OF / ZF
Destination < source 0 1 SF <> OF
Destination > source 0 0 SF = OF
Destination = source 1 0 ZF = 1

 Format: CMP destination, source


 E.g.:
MOV AX,5
CMP AX,10
 E.g.:
MOV AX,1000
MOV CX,1000
CMP AX,CX
 E.g.:
CMP VAR,"3"

2. Conditional Processing

1) A logic structure can be implemented using a combination of comparisons and


jumps.

2) There are 2 key steps involved:


a) An operation (CMP) modifies the CPU status flags.
b) A conditional jump instruction tests the flags and causes a branch to a new
address.

3) a conditional jump instruction branches to a destination label when a status flag


condition is true. Otherwise, the instruction immediately following the conditional
jump is executed.

4) Format: Jnnn destination

5) E.g.:
MOV AX,5
CMP AX,5
JE L1

1
BACS1024 Introduction to Computer Systems

JNE L2

6) Types of conditional jump instructions:

a) Jnnn based on flag values

Mnemonic Description Flags / Registers


JZ Jump if zero ZF = 1
JNZ Jump if not zero ZF = 0
JC Jump if carry CF = 1
JNC Jump of not carry CF = 0
JO Jump if overflow OF = 1
JNO Jump if not overflow OF = 0
JS Jump is signed SF = 1
JNS Jump if not signed SF = 0
SP Jump if parity (even) PF = 1
JNP Jump if not parity (odd) PF = 0

b) Jnnn based on operand value / CX value

Mnemonic Description
JE Jump if equal
JNE Jump of not equal
JCXZ Jump if CX = 0

c) Jnnn based on comparison of unsigned operands

Mnemonic Description
JA Jump if above
JNBE Jump if not below or equal
JAE Jump if above or equal
JNB Jump if not below
JB Jump if below
JNAE Jump if not above or equal
JBE Jump if below or equal
JNA Jump is not above

d) Jnnn based on comparison of signed operands

Mnemonic Description
JG Jump if greater
JNLE Jump if not less or equal
JGE Jump if greater or equal
JNL Jump if not less than
JL Jump if less than
JNGE Jump if not greater or equal
JLE Jump if less than or equal
JNG Jump is not greater

You might also like