UMBC CMSC 211

CSEE | 211 | Current | lectures | news | help


Decision-Making in Assembly Language

Decision making is a two step process. First, two numbers are compared and the flags set accordingly. The a conditional jump is make based on those flags.

The comparison is the same for signed and unsigned numbers, but the conditional jumps are different.

Comparing numbers

The cmp instruction has two operands: cmp reg/mem, reg/mem/constant The computer will perform a subtraction of operand2 from operand1 (but not changing the value of either one) and set the O, S, Z, A, P, and C flags (but does not affect the D and I flags). The results of the subtraction are lost. (The carry flag is set on addition and subtraction by the carry out of the left end. Although the carry is ignored for signed arithmetic, it can be quite useful in other situations.

General jumps

Your text does not do more than mention that there are some instructions for jumps based directly on the flags (excluding D and I). The flag is checked and if there is a match for what the instruction specifies, then the computer will jump a distance of up to either -128 or + 127 bytes from the next instruction.

Since we want the assembler to compute things for us, we will give the target as a label. Because of the distance limitation, we will call this a shortlabel. Actually, it could be an address, which is simply a number. Remember that when you define a label for code, it has to have a colon afterwards. Labels of pseudo-instructions such as DB, DW, EQU, PROC, ENDP do not have labels

Jumps do not alter the flags.

There is an unconditional jump, or one that is always preformed. The format is:

jmp shortlabel The jumps based on the flags being set/reset are:

Instruction Description
JZ shortlabel jump if zero
JNZ shortlabel jump if not zero
JS shortlabel jump if sign
JNS shortlabel jump if not sign
JC shortlabel jump if carry
JNC shortlabel jump if not carry
JO shortlabel jump if overflow
JNO shortlabel jump if not overflow
JP shortlabel jump if parity
JNP shortlabel jump if not parity
JPE shortlabel jump if parity even
JPO shortlabel jump if parity odd

Signed Conditional Jumps

To make life easier, there are jumps for both signed and unsigned jumps. Remember: the binary value 10000000 can be -1 or 128, when 01111111 is 127 in either case, but 128 would be large and -1 would be smaller! We want our comparison to give us the correct results.

Instruction SymbolicDescription
JE shortlabel op1 = op2 jump if equal
JNE shortlabel op1 != op2 jump if not equal
JG shortlabel op1 > op2 jump if greater than
JNG shortlabel !(op1 > op2) jump if not greater than
JGE shortlabel op1 >= op2 jump if greater than or equal
JNGE shortlabel !(op1 >= op2) jump if not greater than or equal
JL shortlabel op1 < op2 jump if less than
JNL shortlabel !(op1 < op2) jump if not less than
JLE shortlabel op1 <= op2 jump if less than or equal
JNLE shortlabel !(op1 <= op2) jump if not less than or equal

Suppose we want to do something in C like:

  if (ax < bx)
  {
      X = -1;
  }
  else
  {
      X = 1;
  }
it would look like this:
cmp ax, bx
jl axLess ; go to 'axLess' if ax < bx
mov X, 1
jmp Both ; go to label 'Both'
axLess: mov X, -1
Both:


CSEE | 211 | Current | lectures | news | help