SCS3202
Assembly language
programming
Comparison Instructions
CMP minuend, subtrahend
This instruction performs a comparison by subtracting the subtrahend from the
minuend. The result is discarded and only the flags are affected
The minuend can be a register or memory but not an immediate
The subtrahend can be an immediate, register or memory
The flags are modified as follows
SF set if result is negative
ZF two values are equal
PF if the result has an even number of bits
CF, OF, and AF
Example
mov ecx, 10
mov edx, 9
cmp ecx, edx
Jump (control flow) instructions
The jump instructions usually depend on the result of comparison instructions
Hence they tend to come immediately after a comparison
The comparison may be that of
integers – signed/unsigned
float
Strings
Bits in registers
Possible results of comparisons include:
Equal
Not equal
Greater than
Less than
Negative result
Positive result
Parity set
Zero flag set
Etc.
Jump (control flow) instructions
The following is a list of jump instructions
JMP label1 ;
Unconditional jump to label1 – a section of the code must have been
tagged with this label
There is no need for a comparison to precede this instruction
Conditional jump instructions
Instruction Description
JE/JZ Jump Equal or Jump Zero
JNE/JNZ Jump not Equal or Jump Not Zero
JG/JNLE Jump Greater or Jump Not Less/Equal
JGE/JNL Jump Greater/Equal or Jump Not Less
JL/JNGE Jump Less or Jump Not Greater/Equal
JLE/JNG Jump Less/Equal or Jump Not Greater
Jump (control flow) instructions
Conditional jump instructions used with unsigned data i.e. the operands are
considered not to have a sign bit
Instruction Description
JE/JZ Jump Equal or Jump Zero
JNE/JNZ Jump not Equal or Jump Not Zero
JA/JNBE Jump Above or Jump Not Below/Equal
JAE/JNB Jump Above/Equal or Jump Not Below
JB/JNAE Jump Below or Jump Not Above/Equal
JBE/JNA Jump Below/Equal or Jump Not Above
Jump (control flow) instructions
Other jump instructions
Instruction Description
JCXZ Jump if CX is Zero
JC Jump If Carry
JNC Jump If No Carry
JO Jump If Overflow
JNO Jump If No Overflow
JP/JPE Jump Parity or Jump Parity Even
JNP/JPO Jump No Parity or Jump Parity Odd
JS Jump Sign (negative value)
JNS Jump No Sign (positive value)
Looping (control flow) instructions
The jump instructions can be used for implementing looping as shown in the
code below
L1:
;loop body
SUB [i], WORD 1
JNZ L1
However, there are special purpose looping instructions
LOOP label
This instructions assumes that the ECX has been initialised with a positive
integer. ECX should generally not be modified in the body of the loop
The label is at the beginning of the loop and the loop label instruction at the
bottom
The ECX is automatically decremented after each iteration. The looping stops
when the ECX becomes zero
Looping (control flow) instructions
Other looping instructions
The following instructions accept a condition of the ZF as a criteria for exiting the loop
before CX is zero
loope – loop while equal
some values compared and found to be equal within the body of the loop
loopne
Some values compared and found to not to be equal within the body of the loop
loopnz
Some arithmetic instruction that fails to produce a zero within the body of the
loop
loopz
Some arithmetic instruction that results in a zero within the body of the loop
The instruction that affect the ZF should occur immediately before the loop
instruction