Chapter 4 Assembly Language

Table of Contents
4.1 Conditional Branching and Flags
4.2 Variations on Loops
4.3 Modular Programming
4.4 Programming Style
4.5 Memory Addressing
4.6 String Instructions

This chapter describes some important aspects of assembly language.

4.1 Conditional Branching and Flags

4.1.1 The Processor Flags Register

The following diagram shows the location of the various flags in the processor status register.

NT -- Nested Task flag (286+) IF -- Interrupt-Enable Flag AF -- Auxiliary Carry Flag
IOPL -- I/O Privilege Level (286+) TF -- Trap Flag PF -- Parity Flag
OF -- Overflow Flag SF -- Sign Flag CF -- Carry Flag
DF -- Direction Flag ZF -- Zero Flag  

4.1.2 Conditional Jumps

The following table lists the most common jump instructions and the tests they perform:

Instruction Jump Condition Test
JE Jump if Equal ZF=1
JNE Jump if Not Equal ZF=0
JG Jump if Greater (ZF=0) AND (SF=OF)
JGE Jump if Greater or Equal SF=OF
JL Jump if Less SFOF
JLE Jump if Less or Equal (ZF=1) OR (SFOF)

The following conditional branches are similar to the above but involve comparisons which treat the operands as unsigned integers:

Instruction Jump Condition Test
JA Jump if Above (CF=0) AND (ZF=0)
JAE Jump if Above or Equal CF=0
JB Jump if Below CF=1
JBE Jump if Below or Equal (CF=1) OR (ZF=1)

Finally, the branches below specifically test flags:

Instruction Jump Condition Test
JO Jump on Overflow OF=1
JNO Jump on No Overflow OF=0
JC Jump on Carry CF=1
JNC Jump on No Carry CF=0
JS Jump on Sign (Negative) SF=1
JNS Jump on No Sign (Positive) SF=0
JZ Jump if Zero (same as JE) ZF=1
JNZ Jump if Not Zero ZF=0

4.1.3 Meanings of the OF, CF, SF, and ZF Flags

The following table describes the meanings of the four flags used in conditional branching:

OF (Overflow)
1 -- result is outside signed-number range
0 -- otherwise
CF (Carry)

Carry out of (borrow into) high-order bit.

1 -- result is outside unsigned-number range
0 -- otherwise
SF (Sign)

High-order bit of result.

1 -- negative signed number.
0 -- positive signed number.
ZF (Zero)
1 -- result = 0
0 -- otherwise