You are on page 1of 19

Computer Organization and Assembly Language

The Jcc Instructions

Ø

The Jcc (Conditional Jump) instructions
are jumps that may or may not transfer
control depending on the state of the status
flags at the time the instruction is executed.

Ø

Format:

Ø

Action:
If the specified condition cc is
true, it will jump to the address specified in
the operand; otherwise the next instruction
is executed.

Jcc operand

Conditional Jump and Loop Instruction

* Property of STI
Page 1 of 19

Computer Organization and Assembly Language

The Jcc Instructions

Comparing Unsigned Numbers
Mnemonic

“Jump if …”

Condition

JA/JNBE

above/not below nor equal (CF or ZF) = 0

JAE/JNB

above or equal/not below

JB/JNA
JBE/JNA

CF = 0

below/not above nor equal CF = 1
below or equal/not above

Conditional Jump and Loop Instruction

(CF or ZF) = 1

* Property of STI
Page 2 of 19

Computer Organization and Assembly Language

The Jcc Instructions

Comparing Signed Numbers
Mnemonic

“Jump if …”

Condition

JG/JNLE

greater than/not less than
or equal

ZF=0 or
(SF=OF)

JGE/JNL

greater than or equal/not
less than

SF = OF

JL/JNGE

less than/not greater than
or equal

SF ≠ OF

JLE/JNG

less than or equal/not
greater than

ZF=1 or
(SF≠OF)

Conditional Jump and Loop Instruction

* Property of STI
Page 3 of 19

Computer Organization and Assembly Language

The Jcc Instructions
Test Flags
Mnemonic

“Jump if …”

JE/JZ

equal to/zero

ZF = 1

JNE/JNZ

not equal to/not zero

ZF = 0

JC

carry

CF = 1

JNC

not carry

CF = 0

JS

sign

SF = 1

JNS

not sign

SF = 0

JP/JPE

parity/parity even

PF = 1

JNP/JPO

no parity/parity odd

PF = 0

JO

overflow

OF = 1

JNO

no overflow

OF = 0

Conditional Jump and Loop Instruction

Condition

* Property of STI
Page 4 of 19

Computer Organization and Assembly Language

Example – Jcc Instruction

1.

MOV AL, A5H
MOV BL, 73H
CMP AL, BL
JA L1

Conditional Jump and Loop Instruction

* Property of STI
Page 5 of 19

Computer Organization and Assembly Language

Example – Jcc Instruction

2.

MOV AL, A5H
MOV BL, 73H
CMP AL, BL
JG L1

Conditional Jump and Loop Instruction

* Property of STI
Page 6 of 19

Computer Organization and Assembly Language

Example – Jcc Instruction
3.

MOV AL, 00H
MOV CL, 03H
AGAIN: ADD AL, 02H
DEC CL
JNZ AGAIN

Conditional Jump and Loop Instruction

* Property of STI
Page 7 of 19

Computer Organization and Assembly Language

Example – Jcc Instruction

4.

MOV AL, 7FH
MOV BL, 75H
ADD AL, BL
JO NEXT

Conditional Jump and Loop Instruction

* Property of STI
Page 8 of 19

Computer Organization and Assembly Language

Example – Jcc Instruction

5.

MOV AL, 01H
DEC AL
JG L1

Conditional Jump and Loop Instruction

* Property of STI
Page 9 of 19

Computer Organization and Assembly Language

Example – Jcc Instruction

6. Circumventing the limitation of the shortlabel:

L1:

MOV BX, 0002H
MOV CX, 0002H
CMP BX, CX
JE L1
:
:
:
:
:

Conditional Jump and Loop Instruction

* Property of STI
Page 10 of 19

Computer Organization and Assembly Language

The JCXZ Instruction

Ø

The JCXZ (Jump if CX is 0) instruction
transfers control to the target operand if CX
is 0.

Ø

Format:

Ø

Action:
If CX = 0, then jump to the
address specified by the operand.

JCXZ short-label

Conditional Jump and Loop Instruction

* Property of STI
Page 11 of 19

Computer Organization and Assembly Language

Program Tracing Example
Unless otherwise stated, determine the
contents of all the affected general-purpose
registers and the flags after executing the
following programs (assume that all
registers and flags are initially 0):
MOV AL, 23H
MOV CH, 6BH
XOR AL, CH
TEST AL, 77H
JZ L1
AND CH, 33H
L1: XOR CH, AAH
TEST CH, AL
JZ L2
NOT AL
OR CH, AL
L2: NOT CH
What will be AX and CX after executing
this program?

Conditional Jump and Loop Instruction

* Property of STI
Page 12 of 19

Computer Organization and Assembly Language

The LOOP Instruction

Ø

The LOOP instruction decrements CX by 1
and transfers control to the target location if
CX is not 0; otherwise, the instruction
following LOOP is executed.

Ø

Format:

LOOP short-label

Ø

Action:

CX

[CX] - 1

If CX ≠ 0, then jump to the
address specified by the
operand; otherwise execute the
instruction following the LOOP.

Conditional Jump and Loop Instruction

* Property of STI
Page 13 of 19

Computer Organization and Assembly Language

Example - LOOP
Instruction

MOV CX, 0003H
MOV AX, 0000H
L1:

INC AX
LOOP L1
What is the value of AX and CX?

Conditional Jump and Loop Instruction

* Property of STI
Page 14 of 19

Computer Organization and Assembly Language

The LOOPE/LOOPZ
Instruction
Ø

The LOOPE/LOOPZ (Loop while
equal/Loop while zero) instruction
decrements CX by 1 and transfers control
to target location if CX is not zero and if ZF
is 1; otherwise the instruction following
LOOPE/LOOPZ is executed.

Ø

Format: LOOPE short-label
LOOPZ short-label

Ø

Action:

CX

[CX] - 1

If CX ≠ 0 and ZF = 1, then jump
to the address specified by the
operand; otherwise execute the
instruction following the
LOOPE/LOOPZ.

Conditional Jump and Loop Instruction

* Property of STI
Page 15 of 19

Computer Organization and Assembly Language

Example - LOOPE/LOOPZ
Instruction

L1:

MOV CX, 0005H
MOV SI, FFFFH
MOV AL, 01H
INC SI
CMP BETA [SI], AL
LOOPE L1

01H
01H
03H
01H
BETA:

Conditional Jump and Loop Instruction

01H

* Property of STI
Page 16 of 19

Computer Organization and Assembly Language

The LOOPNE/LOOPNZ
Instruction
Ø

The LOOPNE/LOOPNZ (Loop while not
equal/Loop while not zero) instruction
decrements CX by 1 and transfers control
to the target location if CX is not zero and
ZF is 0; otherwise the instruction following
LOOPNE/LOOPNZ is executed.

Ø

Format:

LOOPNE short-label
LOOPNZ short-label

Ø

Action:

CX

[CX] - 1

If CX ≠ 0 and ZF = 0, then jump
to the address specified by the
operand; otherwise execute the
instruction following the
LOOPNE/LOOPNZ.

Conditional Jump and Loop Instruction

* Property of STI
Page 17 of 19

Computer Organization and Assembly Language

Example - LOOPNE/LOOPNZ
Instruction

L1:

MOV CX, 0005H
MOV SI, FFFFH
MOV AL, 01H
INC SI
CMP BETA [SI], AL
LOOPNE L1

04H
07H
01H
05H
BETA:

Conditional Jump and Loop Instruction

03H

* Property of STI
Page 18 of 19

Computer Organization and Assembly Language

Program Tracing Example
Unless otherwise stated, determine the
contents of all the affected general-purpose
registers and the flags after executing the
following programs (assume that all
registers and flags are initially 0):

L1:

MOV AX, 000AH
MOV CX, 0014H
SUB AX, 0001H
LOOP L1
LOOPZ L1
LOOPNZ L1

What will be AX and CX after executing
this program?

Conditional Jump and Loop Instruction

* Property of STI
Page 19 of 19