Sunteți pe pagina 1din 30

Compare Instructions

Jump instructions
Prepared by:
Group 4

(cmp)
Instruction
The Compare
compare (cmp) instruction is
identical to the sub instruction
with
one crucial semantic difference does
not retain the difference it computers
(does not stores the results); it just sets
the condition code bits in the flag
register.
The syntax for the cmp instruction
similar to that sub instruction
--------------------------------------------------------------- cmp ( LeftOperand, RightOperand );
----------------------------------------------------------

The instruction computes LeftOperand


RightOperand. Specific forms are:
--------------------------------------------------------------- cmp( reg, reg );
//must be
the same size
cmp( reg, mem );
cmp( mem, reg );
cmp( reg, immediate data );
cmp( mem, immediate data );
--------------------------------------------------------------The 8086 sets the flags in an appropriate way
so that it can be read as compare
LeftOperand to RightOperand

-------------cmp( ax, bx );
--------------------------------------------------------------This
instruction
performs
the
computation
AX BX and sets the
flags depending upon the result of the
computation.
ZF zero flag is set if and only if AX = BX.
The only time where AX BX produces
zero 0 result.
CF carry flag is set after a compare
operation if subtracting BX from AX
requires a borrow. (this only occurs when

SF Sign flag is set to 1 if the result is


negative. At first glance, you might
think that this flag
would be set if AX is
less than BX. but this isnt always the
case.
if AX = 7FFF and BX = -1 (FFFF),
subtracting AX from BX produces 8000h,
which is negative (and so the sign flag
will be set)
therefore, for signed comparisons
anyway, the sign flag doesnt contain the
proper status.

For signed operands, considering


AX = FFFF and BX = 1. where AX is
greater than BX, subtracting AX from
BX their difference is FFFE which is
still NEGATIVE.
(the sign flag and the overflow flag,
taken together can be used for
comparing two signed values)

OF Overflow flag is set after the comparing


operation if the difference of AX and BX
produced an overflow or underflow.
As mentioned above signed flag and
overflow flag are both used when
performing signed values
comparisons
The cmp instruction also affects the Parity
and Auxiliary Carry flags, but you rarely
test these two flags after a compare
operation .

Compare String Instruction (CMPS)


compares the contents of one memory
location
(addressed
by
ES:DI).
Depending on the direction flag,
CMPS also increments and decrements
the SI and DI registers, by
1 for byte,
2 for word &;
4 for double word

CMPS can successively compare


strings of any length

CMPS provides an alphanumeric


comparison, that is, a comparison according
to ASCII values.
the operation is not suited to algebraic
comparisons, which consists of signed
numeric values.
Consider the comparison of 2 string
containing JEAN and JOAN. A comparison
from left to right. ONE BYTE AT A TIME:
J:J
//equal
E : O //unequal (e is low)
A : A//equal
N : N //equal

A comparison of the entire 4-bytes


ends with a comparison of N in JEAN
and N in JOAN (equal). now since the
two names are not identical, the
operation should end as soon as it
makes a comparison between two
different characters.

The CMPXCHG (compare and exhange)


instruction is available only in 80486 and later
processors. It supports the following syntax:
-------------------------------------------------------------------cmpxchg reg, reg
cmpxchg mem, reg
------------------------------------------------------------------- This operand must be at the same size:
8 bits / 16 bit / 32 bits

This intruction also uses the accumulator


register.
It automatically chooses AL, AX, EAX to match
the size of the operands

This instruction compares AL, AX, or EAX


with the first operand and sets the zero
flag.
if they are equal. If so, then cmpxchg, copies
the second operand into the first operand.
If they are not equal, then cmpxchg, copies
the first operand into the accumulator the
following algorith describes the following
operation:

------------------------------------------------------------------cmpxchg operand1, operand2


-------------------------------------------------------------------

if ( { AL/AX,EAX } = operand1 ) then


zero = 1 ; Set the Zero Flag
operand1 = operand2
else
zero = 0 ;Clear the Zero Flag
{ AL / AX / EAX } = operand1
endif

NOTE: the cmpxhg instruction only affects


the 8086 Zero flag. You cannot test other
flags after cmpxchg as you could do in just
CMP instruction.

Control Flow Program


The instruction discussed thus far execute
sequentially; that is, CPU executes each
instruction in the sequence that appears
in the program.
To write real programs requires several
control structures, NOT just the sequence.
8086 program control instruction belong
to three groups:
i. Unconditional Transfers
ii. Conditional Transfers
iii. Subroutine Call and Return Instructions

Jump (jmp) Instructions


A
commonly
used
instruction
for
transferring
control
is
jump
(jmp)
instruction.
A jump is unconditional, because the
operation transfer control under all
circumstances.
A jump also flushes the pre-fetch the
instruction queue.
Thus a program with many jump operations
may lose some processing speed.

A JMP operation within the same


segment may be short or near (or even
far if the destination is a procedure
with the far attribute)
A Jump operation to a label within
-128 to +127 bytes is Short Jump.
The assembler generates one byte for
the operation (EB)
A Jump that exceeds -128 to +127
bytes become a Near Jump. (within
32kbytes). for which the assembler
generates different machine code (E9)

The jump (jmp) instructions:


Unconditional Jump Instruction
Conditional Jump Instruction
The jump (jmp) instruction
Unconditionally transfers control to
another point in the program. forms of
this instruction:
Direct jumps
2 indirect jumps

Direct Jump instruction specifies the


target
address
using
a
labeled
statement.
Direct jump is completely equivalent to
goto statement is high level language.
Heres an example:
--------------------------------------------------------------- <<statements>>
jmp Laterinpgm
..
Laterinpgm: <<statements>>
---------------------------------------------------------------

The second form of the jmp instruction is a


register Indirect Jump Instruction.
--------------------------------------------------------------jmp label
jmp( reg32 );
jmp( mem32 );
--------------------------------------------------------------This instruction transfer control to the
instruction, whose address is specified (32bit general purpose register).

The Conditional jumps test one or more


CPU flags to see if they match some
paticular pattern...
If the flag setting MATCH the condition,

the conditional jump instruction


TRANSFER control to the target location,
if the match FAILS, the CPU IGNORES
the conditional jump and execution
continues
the instruction the
conditional jump

Some conditional jump instructions


simply test the setting of the sign, carry,
overflow and zero flags.
for example: we could test the zero flag
after an instruction to check if the result
was zero
Most of the time, however, we will
probably execute a conditional jump
(jmp) after a compare (cmp) instruction.
The cmp instruction sets the flag so that
you can test for a less than, greater than,
equality and so on.

The conditional jump instructions take the


following FORM:
--------------------------------------------------------------jcc label;
--------------------------------------------------------------The cc in jcc indicates that you must substitute
some character sequences that specifies the
type of condition to test. For example, js
stands for jump is the sign flag is set.

A typical js instruction is:


--------------------------------------------------------------js ValueIsNegative;
--------------------------------------------------------------In this example, the js instruction transfers
control to the ValueIsNegative label if the
sign flag is currently set;
control falls through to the next instruction
following the js instruction if the sign flag is
CLEAR

jcc, Instructions That Tests Flags


INSTRUCTION

DESCRIPTION

CONDITION

jc

Jump if carry

Carry = 1

jnc

Jump if no carry

Carry = 0

jz

Jump if zero

Zero = 1

jnz

Jump if not zero

Zero = 0

js

Jump if sign

Sign = 1

jns

Jump if not sign

Sign = 0

jo

Jump if overflow

Overflow = 1

jno

Jump if no overflow

Overflow = 0

jp

Jump if parity

Parity = 1

jpe

Jump if parity even

Parity = 1

jnp

Jump if no parity

Parity = 0

jpo

Jump if parity odd

Parity = 0

jcc, Instructions That For Unsigned


Comparison
INSTRUCTION

DESCRIPTION

CONDITION

ja

Jump if above (>)

Carry = 0, Zero
=0

jnbe

Jump if not below / equal


(not <=)

Carry = 0, Zero
=0

jae

Jump if above or equal (>=)

Carry = 0

jnb

Jump if not below (not <)

Carry = 0

jb

Jump if below (<)

Carry = 1

jnae

Jump if not above / equal


(not >=)

Carry = 1

jbe

Jump if below or equal (<=)

Carry = 1

jna

Jump if not above (not >)

Carry = 1

je

Jump if equal (=)

Zero = 1

jne

Jump if not equal (|)

Zero = 0

jcc, Instructions That For Signed


Comparison
INSTRUCTION

DESCRIPTION

CONDITION

jg

Jump if greater (>)

Sign =
Overflow , Zero
=0

jnle

Jump if not less than / equal


(not <=)

Sign =
Overflow , Zero
=0

jge

Jump if greater than or equal


(>=)

Sign =
Overflow

jnl

Jump if not less than (not <)

Sign =
Overflow

jl

Jump if less than (<)

Sign <>
Overflow

jnge

Jump if not greater than /


equal (not >=)

Sign <>
Overflow

jle

Jump if less than or equal


(<=)

Sign <>
Overflow , Zero
=1

Distinguishing the purpose of conditional


jumps should clarify their use. The type of
data (unsigned or signed) on which you
are performing comparisons or arithmetic
can determine which instruction to use.
RECALL:
UNSIGNED (logical data) treats all bits as data bits;
SIGNED (arithmetic data) treats the left most bit as
a sign, where 0 is positive and 1 is negative.

The 8086 conditional jump instruction


gives you the ability to split program flow
into one of two path depending on some
condition.

In this example, assume that the CX contains


11000110 and the DX contains 00010110.
--------------------------------------------------------------cmp CX,DX
-------------------------------------------------------------- If we treat the data as unsigned, the CX value is
larger;
But if we treat the data as signed, however, the
CX value is smaller.

The use here of CMP is valid, and you have


to select the appropriate conditional jump
instruction such as JB (jump below) for
unsigned or JL (jump low) for signed data

The JCXZ (the jump if CX is zero)


instruction branches to the target
address if CX contains zero. Although
you can use it anytime you need to see
if CX contains zero.
You would normally use it before a loop
instruction.
the loop instruction can repeat a sequence
of operation in CX times !

If CX equals zero, loop will repeat the


operation 65,536 times, you can use
JXCZ to skip over such a loop when CX is
zero

The conditional jump instruction only


test the
8086 flags.

They do NOT affect any of


them.

The JCXZ, jump if CX is zero, also does


not affect any flags.

S-ar putea să vă placă și