Chapter 6: Arithmetic, Logic and Shift Instructions
Introduction
Learning Outcomes
Compute CF, ZF, SF, OF, PF and AF from the result of an operation; explain the difference between CF and OF.
Use addition, subtraction, comparison, logic, multiplication, division, single-operand, shift and rotate instructions with valid operands; trace how shifts affect CF.
Know the implicit registers of MUL, IMUL, DIV, IDIV (AL, AX, DX:AX) and the divide error.
Trace the array addition, BCD adjustment and temperature conversion programs step by step.
The FLAGS Register
Arithmetic and Logic Instructions
- The results of arithmetic and logic instructions affect the CPU's status flags.
- The flags live in the 16-bit FLAGS register; each bit is a flag that is either 0 or 1.
- The 8086 uses 9 of these 16 bits: 6 status flags (describe the result) and 3 control flags (steer the CPU's behavior).
- Conditional jumps (
JZ,JC,JO…) make their decisions from these flags.
Dashed bits are unused on the 8086.
The FLAGS Register
The Flags at a Glance
| Bit | Flag | Kind | Set to 1 if… | Example / note |
|---|---|---|---|---|
| 0 | CF · Carry | status | unsigned overflow: carry on addition, borrow on subtraction | 255 + 1 → AL = 00h, CF = 1; 7 − 9 → CF = 1; STC, CLC, CMC |
| 2 | PF · Parity | status | the low byte of the result has an even number of 1 bits | 96h = 1001 0110b → PF = 1; even for word results, only the low byte |
| 4 | AF · Auxiliary | status | carry/borrow from bit 3 into bit 4 | 08h + 08h → AF = 1; used only by AAA, AAS, DAA, DAS |
| 6 | ZF · Zero | status | result is zero | SUB AL, AL → ZF = 1; after CMP AX, BX, ZF = 1 means AX = BX |
| 7 | SF · Sign | status | the most significant bit (MSB) of the result is 1 | copy of the MSB: 96h → SF = 1 |
| 8 | TF · Trap | control | INT 1 after every instruction | single-step debugging |
| 9 | IF · Interrupt | control | INTR interrupts are accepted | STI → 1, CLI → 0; does not affect NMI |
| 10 | DF · Direction | control | string instructions run backward | 0: SI/DI increment, 1: decrement; CLD / STD |
| 11 | OF · Overflow | status | signed overflow: the result does not fit the signed range | 100 + 50 → AL = 96h (−106), OF = 1 |
The FLAGS Register
CF or OF?
The CPU adds bits and nothing more; the interpretation is the programmer's. CF says whether the result is right as unsigned, OF whether it is right as signed.
| AL + operand | unsigned view | signed view | AL | CF | OF |
|---|---|---|---|---|---|
FFh + 01h | 255 + 1 = 256 | -1 + 1 = 0 | 00h | 1 | 0 |
64h + 32h | 100 + 50 = 150 | 100 + 50 = 150 | 96h | 0 | 1 |
C8h + 64h | 200 + 100 = 300 | -56 + 100 = 44 | 2Ch | 1 | 0 |
80h + 80h | 128 + 128 = 256 | -128 + -128 = -256 | 00h | 1 | 1 |
05h + 0Ah | 5 + 10 = 15 | 5 + 10 = 15 | 0Fh | 0 | 0 |
The FLAGS Register · Step through the code
Code Trace: CF, OF and the Other Flags
AL = 9Ch: unsigned 156, signed −100. The flags start at 0; MOV does not change them.
156 + 112 = 268 > 255 → AL = 0Ch, CF = 1. Signed −100 + 112 = 12 is correct → OF = 0. 0Ch has two 1s → PF = 1.
AL = 50h (80). The flags still reflect the ADD.
80 + 80 = 160 is correct unsigned (CF = 0), but two positives gave a negative sum (A0h = −96) → OF = 1, SF = 1.
A0h + A0h: unsigned 320 (CF = 1), signed −96 + (−96) = −192 (OF = 1). AL = 40h: both views are wrong.
AL = FFh. CF = 1 is still there.
INC AL: FFh + 1 = 00h → ZF = 1, AF = 1; but INC leaves CF alone: CF = 1 is left over from the previous addition.
CMC complements CF: CF = 0. The other flags do not change.
MOV AL, 9Ch ; 156 or -100 ADD AL, 70h ; + 112 MOV AL, 50h ; 80 ADD AL, 50h ; + 80 ADD AL, AL ; A0h + A0h MOV AL, 0FFh INC AL ; CF untouched CMC ; flip CF RET
The FLAGS Register
Flags After ADD, Step by Step
MOV AL, 64h (100) and ADD AL, 32h (50). Bits are added from right to left.
The result is 96h = 1001 0110b. Unsigned it is 150 (right); signed it is −106 (wrong!).
CF: did a carry come out of the leftmost bit? No → CF = 0 (150 ≤ 255).
OF: two positive numbers gave a negative sum (the carries into and out of bit 7 differ) → OF = 1.
SF = bit 7 of the result = 1. The result is not zero: ZF = 0.
PF: 1001 0110b has four 1s → even → PF = 1. AF: the low nibbles give 4 + 2 = 6, no carry → AF = 0.
Result: AL = 96h; CF = 0, ZF = 0, SF = 1, OF = 1, PF = 1, AF = 0.
The FLAGS Register
Demo: Flag Calculator
Pick two values and an instruction to see the result and the flags. Values may be decimal (-5), hex (3Fh) or binary (1010b).
Instruction Groups
Three Groups of Instructions
ADD, SUB, CMP, AND, TEST, OR, XOR
dest, src
MUL, IMUL, DIV, IDIV
one operand + implicit AL / AX / DX
INC, DEC, NOT, NEG
the operand is both source and destination
Instruction Groups
What the Instructions Do
| Group 1 | What it does |
|---|---|
ADD | adds two values, writes the result to dest |
SUB | subtracts src from dest, writes the result to dest |
CMP | subtracts but does not store the result; flags only |
AND | bitwise AND |
TEST | bitwise AND; result not stored |
OR | bitwise OR |
XOR | bitwise exclusive OR |
| Group 2 | What it does |
|---|---|
MUL | unsigned multiply |
IMUL | signed multiply |
DIV | unsigned divide (quotient + remainder) |
IDIV | signed divide |
| Group 3 | What it does |
|---|---|
INC | adds 1 |
DEC | subtracts 1 |
NOT | inverts every bit |
NEG | changes the sign (two's complement) |
Group 1: Two Operands
ADD, SUB, CMP, AND, TEST, OR, XOR: Operands
| Form | Example |
|---|---|
| REG, memory | ADD AX, [BX] |
| memory, REG | SUB var1, CL |
| REG, REG | XOR AX, AX |
| memory, immediate | AND byte ptr [SI], 0Fh |
| REG, immediate | CMP AL, 'A' |
- REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
- memory:
[BX],[BX+SI+7], a variable … - immediate:
5,-24,3Fh,10001101b…
ADD [BX], [SI] (two memory), ADD AL, BX (size mismatch), ADD 5, AL (immediate dest), ADD DS, AX (segment register).Group 1: Two Operands
Rules and Affected Flags
- After a two-operand instruction the result is always stored in the first operand (dest).
CMPandTESTonly affect the flags; they store no result.ADD/SUB: adds the second operand to / subtracts it from the first.CMP: subtracts the second operand from the first without storing the result.AND/OR/XOR: operate on corresponding bits of the two operands;TESTis like AND but only affects the flags.
| Instruction | CF | ZF | SF | OF | PF | AF |
|---|---|---|---|---|---|---|
ADD, SUB, CMP | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
AND, OR, XOR, TEST | 0 | ✓ | ✓ | 0 | ✓ | ? |
✓: set by the result · 0: always cleared · ?: undefined. Logic instructions always clear CF and OF.
Group 1: Two Operands · Step through the code
Code Trace: ADD, SUB, CMP
AL = F0h. MOV does not change the flags (all 0 at the start).
F0h + 20h = 110h; AL keeps 10h; the carry out → CF = 1. Signed: −16 + 32 = 16 is correct → OF = 0.
BL = 7. The flags still reflect the ADD.
7 − 9 = −2 = FEh. A borrow occurred → CF = 1; MSB = 1 → SF = 1. 7 − 9 borrows in the low nibble → AF = 1.
CMP AL, 10h: 10h − 10h = 0 → ZF = 1. AL is unchanged: that is how equality is tested.
FEh − 0: ZF = 0, SF = 1, CF = 0. Unsigned FEh > 0, signed −2 < 0.
FEh + 2 = 100h → BL = 00h, CF = 1, ZF = 1. Signed: −2 + 2 = 0 is correct → OF = 0.
mov al, 0F0h ; 240 add al, 20h ; 240 + 32 = 272 mov bl, 7 sub bl, 9 ; 7 - 9 cmp al, 10h ; AL - 10h cmp bl, 0 add bl, 2 ; FEh + 2
Group 1: Two Operands · Step through the code
32-Bit Addition with ADC
The first 32-bit number is loaded into DX:AX: low word FFFFh…
…high word 0001h → DX:AX = 0001FFFFh (131071).
The second number is CX:BX = 00020001h (131073).
FFFFh + 0001h = 10000h → AX = 0000h, CF = 1: a carry for the high word.
ADC = ADD + CF: 0001h + 0002h + 1 = 0004h. Result DX:AX = 00040000h (262144) ✓
; DX:AX = 0001FFFFh, CX:BX = 00020001h mov ax, 0FFFFh mov dx, 0001h mov bx, 0001h mov cx, 0002h add ax, bx ; low words adc dx, cx ; high + CF
SBB (subtract with borrow) does multi-word subtraction the same way: SUB first, then SBB.
Group 1: Two Operands · Step through the code
32-Bit Subtraction with SBB
The first number is DX:AX = 00040000h (262144): high word in DX, low word in AX.
The subtrahend is CX:BX = 00010001h (65537).
0000h − 0001h: the low word is too small, so a borrow occurs → AX = FFFFh, CF = 1.
SBB = SUB − CF: 0004h − 0001h − 1 = 0002h. Result DX:AX = 0002FFFFh (196607) ✓
; DX:AX = 00040000h, CX:BX = 00010001h mov ax, 0000h mov dx, 0004h mov bx, 0001h mov cx, 0001h sub ax, bx ; low words sbb dx, cx ; high - CF
Rule: SUB on the lowest word, SBB on every word after it. Instructions in between must not disturb CF (MOV, INC, LOOP preserve it).
Logic Operations
Logic Operation Rules
| A | B | A AND B |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 0 |
1 if both are 1.
| A | B | A OR B |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 0 | 0 |
1 if at least one is 1.
| A | B | A XOR B |
|---|---|---|
| 1 | 1 | 0 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 0 | 0 |
1 if they differ.
| A | NOT A |
|---|---|
| 1 | 0 |
| 0 | 1 |
Inverts. One operand.
AND AL, BL is really 8 independent ANDs.Logic Operations
Masking: AND, OR, XOR, TEST
AND clears bits: bit 5, which is 0 in the mask, is cleared and the rest are kept → lowercase becomes uppercase.
OR sets bits: bit 5, which is 1 in the mask, becomes 1 → uppercase becomes lowercase.
XOR flips bits: bit 5, which is 1 in the mask, changes → the letter switches case.
TEST tests bits: the result 01h ≠ 0 → ZF = 0, so bit 0 = 1 and the number is odd. BL is unchanged.
Logic Operations
Common Idioms
| Code | Effect | Why? |
|---|---|---|
XOR AX, AX | AX = 0, ZF = 1, CF = 0 | shorter than MOV AX, 0 (2 bytes); also clears flags |
AND AL, 0Fh | '7' (37h) → 7 | turns an ASCII digit into a number (high nibble cleared) |
OR AL, 30h | 7 → '7' (37h) | turns a number into an ASCII digit |
AND AL, 0DFh | 'a' → 'A' | clears bit 5: uppercase |
OR AL, 20h | 'A' → 'a' | sets bit 5: lowercase |
TEST AL, 1 | odd if ZF = 0 | tests bit 0 without changing AL |
TEST AL, 80h | negative if ZF = 0 | tests the sign bit |
OR AX, AX | AX unchanged | sets ZF/SF: is AX zero, is it negative? |
Logic Operations · Step through the code
Code Trace: AND, OR, XOR, TEST
AL = 'a' = 61h = 0110 0001b.
61h AND DFh = 41h = 'A'. 41h has two 1s → PF = 1. CF = OF = 0.
41h OR 20h = 61h = 'a'. Three 1s → PF = 0.
61h XOR 20h = 41h = 'A' (bit 5 flipped).
BL = '7' = 37h (an ASCII digit).
37h AND 0Fh = 07h: now the number 7.
07h AND 01h = 01h ≠ 0 → ZF = 0: the number is odd. BL is unchanged.
XOR with itself → 00h, ZF = 1, PF = 1 (zero 1s, even).
mov al, 'a' ; 61h and al, 0DFh ; uppercase or al, 20h ; lowercase xor al, 20h ; flip case mov bl, '7' ; 37h and bl, 0Fh ; number 7 test bl, 1 ; odd? xor bl, bl ; BL = 0
Group 2: Multiply and Divide
MUL and IMUL
| Instruction | Byte operand | Word operand |
|---|---|---|
MUL (unsigned) | AX = AL × operand | (DX AX) = AX × operand |
IMUL (signed) | AX = AL × operand | (DX AX) = AX × operand |
Operand: REG or memory (MUL BL, IMUL word ptr [SI]). The 8086 has no immediate operand here: MUL 5 is invalid.
Group 2: Multiply and Divide
DIV and IDIV
| Instruction | Byte operand | Word operand |
|---|---|---|
DIV (unsigned) | AL = AX / operand AH = remainder (modulus) | AX = (DX AX) / operand DX = remainder (modulus) |
IDIV (signed) | AL = AX / operand AH = remainder | AX = (DX AX) / operand DX = remainder |
MUL / IMUL set only CF and OF meaningfully: if the result exceeds the operand size (the upper half is needed), CF = OF = 1. The rest are undefined.
After DIV / IDIV all flags are undefined.
If the divisor is 0, or the quotient does not fit in its destination (AL / AX), the CPU raises INT 0 (divide error).
IDIV truncates the quotient toward zero; the remainder has the sign of the dividend.
Group 2: Multiply and Divide
Multiply and Divide Step by Step
MOV AL, 200, MOV BL, 3. 200 = C8h, which is −56 signed.
MUL BL: 200 × 3 = 600 = 0258h → AX. The result did not fit in AL (AH ≠ 0) → CF = OF = 1.
Same bits with IMUL BL: −56 × 3 = −168 = FF58h. A different result! −168 does not fit in a byte → CF = OF = 1.
AL = FEh (−2), BL = 5: IMUL BL → −10 = FFF6h. AH is just the sign extension of AL → CF = OF = 0: the result fit in AL.
DIV BL: AX = 23, BL = 5 → quotient AL = 4, remainder AH = 3. New AX = 0304h.
IDIV BL: −23 / 5 → quotient AL = FCh (−4), remainder AH = FDh (−3). Check: −4 × 5 + (−3) = −23.
AX = 1000h (4096), DIV BL (2): the quotient 2048 does not fit in AL → divide error, INT 0. A word division (DX:AX / BX) was needed.
DX:AX = FFFFFFFFh, DIV BX (BX = 1): the divisor is not zero, but the quotient FFFFFFFFh does not fit in AX → INT 0 again. Rule: in word division, if DX ≥ divisor the quotient cannot fit (in byte division, AH ≥ divisor).
Group 2: Multiply and Divide
Preparing to Divide: CBW, CWD
- Division needs a dividend twice as wide as the operand: AX for byte division, DX:AX for word division.
- Before unsigned division, clear the upper half:
MOV AH, 0orXOR DX, DX. - Before signed division, sign-extend:
CBW(AL → AX),CWD(AX → DX:AX). - Forgetting DX is a classic bug: an old value left in DX enlarges the dividend and can cause a divide error.
; unsigned: 1000 / 7 mov ax, 1000 xor dx, dx ; DX:AX = 1000 mov bx, 7 div bx ; AX = 142, DX = 6 ; signed: -1000 / 7 mov ax, -1000 cwd ; DX = FFFFh idiv bx ; AX = -142, DX = -6
Group 2: Multiply and Divide · Step through the code
Code Trace: DIV, CWD and IDIV
AX = 2000 = 07D0h. In word division the dividend is DX:AX; what DX holds matters too.
XOR DX, DX: the upper half is zero for unsigned division → DX:AX = 2000.
Divisor BX = 9.
DIV BX: 2000 / 9 → AX = 222 (00DEh), DX = 2. The flags are undefined.
AX = −2000 = F830h. DX still holds the previous remainder (2): DX:AX is now a wrong dividend.
CWD: the sign bit of AX is 1 → DX = FFFFh. DX:AX = FFFFF830h, −2000 in 32 bits.
IDIV BX: −2000 / 9 → AX = FF22h (−222), DX = FFFEh (−2). The remainder has the sign of the dividend.
AX = 2000, but DX = FFFEh was not cleared: DX:AX = FFFE07D0h, about 4.29 billion.
DIV BX: the quotient does not fit in 16 bits (DX ≥ divisor) → the CPU raises INT 0 and the program stops. Fix: XOR DX, DX before dividing.
MOV AX, 2000 XOR DX, DX ; DX:AX = 2000 MOV BX, 9 DIV BX ; unsigned div MOV AX, -2000 CWD ; sign-extend IDIV BX ; signed div MOV AX, 2000 ; DX not cleared! DIV BX RET
Signed Numbers · Step through the code
Signed Overflow: JO and CBW
AL = BL = A4h: −92 (256 − 92 = 164 = A4h).
A4h + A4h = 148h → AL = 48h = +72. Two negatives gave a positive sum → OF = 1 (CF = 1 too, but it does not matter in the signed view).
OF = 1, so JO jumps to the widen label. The true result −184 is outside −128 … 127.
AL = A4h, CBW: MSB = 1 → AH = FFh, AX = FFA4h (−92 in 16 bits).
The first number is saved in DX; the second (BL) is moved into AL and extended too: AX = FFA4h.
FFA4h + FFA4h = 1FF48h → AX = FF48h = −184 ✓. The result fits in 16 bits: OF = 0.
mov al, -92 ; A4h mov bl, -92 add al, bl ; 8-bit add jo widen ; if OF = 1 jmp done widen: mov al, -92 ; first number cbw ; AL -> AX mov dx, ax mov al, bl ; second number cbw add ax, dx ; 16-bit add done: ret
JO (jump if overflow) jumps if OF = 1, JNO if OF = 0. If a signed byte sum can overflow, first widen the operands to words with CBW.
Signed Numbers · Step through the code
Signed Byte Sum with CBW
SI points to the start of the array, CX = 4 elements, BX = 0 (sum cleared).
AL = 64h (+100). MSB = 0 → CBW AH = 00h, AX = 0064h.
BX = BX + AX = 0064h (100).
AL = E2h (−30). MSB = 1 → CBW AH = FFh, AX = FFE2h.
BX = BX + AX = 0046h (70).
AL = 5Ah (+90). MSB = 0 → CBW AH = 00h, AX = 005Ah.
BX = 00A0h (160). Adding bytes, 70 + 90 = 160 > 127 → AL = A0h (−96), OF = 1.
AL = F9h (−7). MSB = 1 → CBW AH = FFh, AX = FFF9h.
BX = BX + AX = 0099h (153).
The loop is done; total = 0099h = 153 ✓ is written to memory. With MOV AH, 0 instead of CBW, −30 = E2h would be added as 226.
org 100h jmp start arr db 100, -30, 90, -7 total dw 0 start: lea si, arr mov cx, 4 xor bx, bx ; BX = 16-bit sum again: mov al, [si] cbw ; AL -> AX (signed) add bx, ax inc si loop again mov total, bx ; 153 = 0099h ret
Signed Numbers · Step through the code
32-Bit Sum with CWD and ADC
SI = arr, CX = 3 words. The (32-bit) total in memory is 0000:0000.
AX = 7530h (30000). Positive → CWD gives DX = 0000h.
Low word: 0000h + 7530h = 7530h, CF = 0.
High word: 0000h + 0000h + CF(0) = 0000h. Total 00007530h.
AX = 61A8h (25000), DX = 0000h.
7530h + 61A8h = D6D8h (55000). Negative in a signed 16-bit view (OF = 1), but no problem since the total is kept in 32 bits.
The high word stays 0000h: total 0000D6D8h = 55000.
AX = F060h (−4000). MSB = 1 → CWD gives DX = FFFFh: DX:AX = FFFFF060h (−4000 in 32 bits).
D6D8h + F060h = 1C738h → low word C738h, CF = 1.
FFFFh + 0000h + CF(1) = 10000h → high word 0000h. Total 0000C738h = 51000 ✓
org 100h jmp start arr dw 30000, 25000, -4000 total dw 0, 0 ; 32-bit total start: lea si, arr mov cx, 3 again: mov ax, [si] cwd ; AX -> DX:AX add [total], ax ; low word adc [total+2], dx ; high + CF add si, 2 loop again ret
Signed word array: each element is widened to 32 bits with CWD; the low word is added with ADD, the high word with ADC.
Group 2: Multiply and Divide
Demo: Multiply / Divide Calculator
Pick an instruction, a size and values to see which registers receive the result. For division the dividend is AX for bytes and DX:AX for words.
Group 2: Multiply and Divide · Step by step
32 × 32-Bit Multiplication: Partial Products
The 8086 multiplies at most 16 × 16 bits. 32-bit numbers are split into words: A × B = A1·B1·232 + (A1·B0 + A0·B1)·216 + A0·B0. Four MULs; the results are added by word position with ADD/ADC; the result is 64 bits.
A = 9ABCDEF0h and B = EDCBFEDCh are split into 16-bit halves: A = A1:A0, B = B1:B0.
A0 × B0 is not shifted: it lands in the two lowest words of the result (z1:z0).
A1 × B0 and A0 × B1 are both multiplied by 216: they go into columns z2:z1.
A1 × B1 is multiplied by 232: it goes into columns z3:z2.
Column sums: z1 = 257D1h → write 57D1h, carry 2; z2 = 22835h → write 2835h, carry 2. Each ADD gives at most one carry: hence the ADC chain in the code.
Result: 8FBC 2835 57D1 B640h. Check (decimal): 2596069104 × 3989569244 = 10357197452617037376 ✓
Group 2: Multiply and Divide · Step through the code
32 × 32-Bit Multiplication: Code Trace
AX = A0 = DEF0h, MUL [y]: A0 × B0 = DEF0h × FEDCh = DDF1:B640h (DX:AX). DX ≠ 0, so MUL sets CF = 1.
The first partial product is written to words z0 (bits 0–15) and z1 (bits 16–31).
A1 × B0 = 9ABCh × FEDCh = 9A0B:8190h. Shifted 16 bits left: it is added into z1 and z2.
z1 + AX = DDF1h + 8190h → z1 = 5F81h, CF = 1.
z2 = 0 + DX + CF = 9A0Ch. z2 was 0 before, so no new carry arises here.
A0 × B1 = DEF0h × EDCBh = CF14:F850h. This too is added at the z1–z2 position.
z1 = 5F81h + F850h → 57D1h, CF = 1.
z2 = 9A0Ch + CF14h + CF → 6921h, CF = 1: the carry must go into z3!
ADC [z+6], 0: adds only the carry → z3 = 0001h.
A1 × B1 = 9ABCh × EDCBh = 8FBA:BF14h, shifted 32 bits left: z2–z3.
z2 = 6921h + BF14h → 2835h, CF = 1.
z3 = 0001h + 8FBAh + CF = 8FBCh. Result z3:z2:z1:z0 = 8FBC 2835 57D1 B640h ✓
org 100h mov ax, [x] ; A0 mul [y] ; A0*B0 mov [z], ax mov [z+2], dx mov ax, [x+2] ; A1 mul [y] ; A1*B0 add [z+2], ax adc [z+4], dx mov ax, [x] ; A0 mul [y+2] ; A0*B1 add [z+2], ax adc [z+4], dx adc [z+6], 0 mov ax, [x+2] ; A1 mul [y+2] ; A1*B1 add [z+4], ax adc [z+6], dx ret x dw 0DEF0h, 9ABCh ; A = 9ABCDEF0h y dw 0FEDCh, 0EDCBh ; B = EDCBFEDCh z dw 4 dup(0) ; 64-bit result
Group 3: One Operand
INC, DEC, NOT, NEG
- Operands: REG (AX, BX, … SP) or memory (
[BX],[BX+SI+7], a variable). No immediates. INCandDECaffect only ZF, SF, OF, PF, AF; they leave CF alone.NOTaffects no flags at all! It inverts every bit of the operand (ones' complement).NEGaffects CF, ZF, SF, OF, PF, AF. It negates the operand (two's complement): invert every bit, then add 1. E.g. 5 → −5, −2 → 2.
| Instruction | CF | ZF | SF | OF | PF | AF |
|---|---|---|---|---|---|---|
INC, DEC | — | ✓ | ✓ | ✓ | ✓ | ✓ |
NOT | — | — | — | — | — | — |
NEG | ✓ (1 if operand ≠ 0) | ✓ | ✓ | ✓ | ✓ | ✓ |
—: unchanged. Because INC/DEC preserve CF, a loop can bump its counter while doing multi-byte addition with ADC.
Group 3: One Operand
NEG: Two's Complement Step by Step
x = 5 = 0000 0101b.
NOT: every bit is inverted → 1111 1010b = FAh (−6). Ones' complement.
+1: FBh = 1111 1011b = −5. NEG does both steps in one instruction; CF = 1 (x ≠ 0).
x = FEh (−2): NOT → 01h, +1 → 02h. The negative of a negative is positive.
Special case: x = 80h (−128). NOT → 7Fh, +1 → 80h: +128 does not fit in a byte → OF = 1.
x = 0: the result is 0, CF = 0 (the only case where NEG leaves CF = 0), ZF = 1.
Group 3: One Operand · Step through the code
Code Trace: INC, DEC, NOT, NEG
AL = FFh (255 / −1).
FFh + 1 = 00h, ZF = 1. ADD would have set CF = 1; INC leaves CF alone → CF stays 0.
00h − 1 = FFh, SF = 1. There was a borrow, but CF still did not change.
BL = 7Fh = +127, the largest positive byte.
7Fh + 1 = 80h = −128: signed overflow → OF = 1, SF = 1.
NOT: 80h → 7Fh. The flags did not change at all (SF still shows 1).
NEG: −127 = 81h. CF = 1 (operand ≠ 0), SF = 1, OF = 0.
mov al, 0FFh inc al ; FFh + 1 dec al ; 00h - 1 mov bl, 7Fh ; +127 inc bl ; +128 ? not bl neg bl
Shift and Rotate
Shifts: SHL/SAL, SHR, SAR
SHL/SAL: shift left, 0 enters from the right, the bit shifted out goes to CF. Each step is × 2.SHR: logical shift right, 0 enters from the left. Unsigned ÷ 2.SAR: arithmetic shift right, the sign bit is kept. Signed ÷ 2 (rounds toward −∞).1 or CL: shl ax, 1, shr al, cl. An immediate count such as shl al, 4 is valid from the 80186 on. OF is defined only when the count is 1.sar 1 → FCh = −4; idiv 2 → −3.Shift and Rotate
Rotates: ROL, ROR, RCL, RCR
ROL: rotate left; b7 goes to both b0 and CF.ROR: rotate right; b0 goes to both b7 and CF.RCL: rotate left through CF (a 9-bit ring).RCR: rotate right through CF.shl ax, 1 + rcl dx, 1 → DX:AX × 2.Shift and Rotate
Shift and Rotate Explorer
Each button applies the instruction as op al, 1. Press SHL repeatedly and the number doubles at each step; SAR keeps the sign bit.
Shift and Rotate · Step through the code
Code Trace: Multiplying by Shifting (13 × 5)
The multiplicand BL = 13 = 0Dh (0000 1101b).
The multiplier DL = 5 = 0101b: as in long multiplication on paper, its bits are examined one by one.
4 passes for a 4-bit multiplier.
shr dl, 1: the bit shifted out is 1 → CF = 1.
CF = 1 → no jump; BL is added.
result = 0 + 0Dh = 0Dh (13 × 1).
The multiplicand moves one place left: BL = 1Ah (26).
loop: CX = 0003h.
The bit shifted out is 0 → CF = 0.
CF = 0 → jumps to skip.
BL = 34h (52).
loop: CX = 0002h.
The bit shifted out is 1 → CF = 1.
CF = 1 → BL is added.
result = 0Dh + 34h = 41h = 65 (13 × 4 added).
BL = 68h (104).
loop: CX = 0001h.
The bit shifted out is 0 → CF = 0.
CF = 0 → skip.
BL = D0h.
loop: CX = 0000h.
Result: 13 × 5 = 65 = 41h. This is how CPUs without MUL multiply.
org 100h mov bl, mcand ; 13 mov dl, mplier ; 5 = 0101b mov cx, 4 ; 4 bits check: shr dl, 1 ; low bit → CF jnc skip add result, bl ; add if bit is 1 skip: shl bl, 1 ; mcand × 2 loop check ret mcand db 13 mplier db 5 result db 0
Shift and Rotate
Fast Multiplication and Division by Shifting
; AX ← AX × 3 = AX×2 + AX mov bx, ax ; save shl ax, 1 ; AX × 2 add ax, bx ; ×2 + ×1 ; unsigned ÷ 8 and remainder mov dx, ax and dx, 0007h ; rem = low 3 bits mov cl, 3 shr ax, cl ; quotient ; signed ÷ 4 mov cl, 2 sar ax, cl ; −7 → −2
- On the 8086
multakes 70–133 anddiv80–162 clocks; ashl reg, 1takes just 2. - Multiplying/dividing by powers of 2 becomes a shift; other constants become shifts plus additions.
- Remainder of division by 2n: the low n bits, via
and. - Signed division uses SAR; for a negative dividend SAR rounds toward −∞ while IDIV rounds toward zero: for −7, SAR 2 → −2, IDIV 4 → −1.
Shift and Rotate · Step through the code
37 × 10 and 370 ÷ 16 by Shifting
org 100h mov ax, 37 shl ax, 1 ; AX × 2 mov bx, ax ; save shl ax, 1 ; AX × 4 shl ax, 1 ; AX × 8 add ax, bx ; ×8 + ×2 mov bx, ax and bx, 000Fh ; rem: low 4 bits mov cl, 4 shr ax, cl ; quotient: ÷ 16 ret
AX = 37 = 0025h = 0000 0000 0010 0101b.
SHL AX, 1: bits move one place left, 0 enters from the right → AX = 74 (004Ah). 2 clocks; MUL takes more than 70.
BX = 74: ×2 is saved.
SHL → AX = 148 (×4).
SHL → AX = 296 (×8).
ADD AX, BX: 296 + 74 = 370 = 0172h = 37 × 10.
BX = 370: a copy for the division.
AND BX, 000Fh: low 4 bits = 370 mod 16 = 2.
CL = 4: on the 8086 a shift count above 1 goes in CL.
SHR AX, CL: 4 bits right = ÷ 16 → AX = 23 (0017h). 23 × 16 + 2 = 370.
RET: the program ends.
Example: Adding Arrays
Adding Arrays
org 100h jmp start vec1 db 1, 2, 5, 6 vec2 db 3, 5, 6, 1 vec3 db ?, ?, ?, ? start: lea si, vec1 lea bx, vec2 lea di, vec3 mov cx, 4 sum: mov al, [si] add al, [bx] mov [di], al inc si inc bx inc di loop sum ret
vec3[i] = vec1[i] + vec2[i], i = 0…3.JMP startjumps over the data definitions; otherwise the CPU would take the data bytes for instructions.- SI, BX, DI are pointers to the three arrays;
LEAloads the address (offset). [SI]: the memory byte SI points to (indirect addressing).LOOP sum: decrements CX and goes back tosumwhile CX ≠ 0 → 4 passes.- Expected result: vec3 = 4, 7, 11, 7.
Example: Adding Arrays · Step through the code
Code Trace: Adding Arrays
JMP start: the data (0102h–010Dh) is skipped; execution goes to 010Eh.
SI = address of vec1 = 0102h.
BX = 0106h (vec2), DI = 010Ah (vec3).
CX = 4 passes.
Pass 1: AL ← [SI] = [0102h] = 1.
AL ← AL + [BX] = 1 + 3 = 4 (04h).
[DI] ← AL: vec3[0] = 4.
All three pointers advance one byte (these are byte arrays; for word arrays, add 2).
LOOP: CX = 3 ≠ 0 → back to sum.
Pass 2: AL ← [SI] = [0103h] = 2.
AL ← AL + [BX] = 2 + 5 = 7 (07h).
[DI] ← AL: vec3[1] = 7.
All three pointers advance one byte (these are byte arrays; for word arrays, add 2).
LOOP: CX = 2 ≠ 0 → back to sum.
Pass 3 (summary): 5 + 6 = 11 → vec3[2]. LOOP: CX = 1 ≠ 0 → back to sum.
Pass 4 (summary): 6 + 1 = 7 → vec3[3]. LOOP: CX = 0 → the loop ends.
RET: vec3 = 4, 7, 11, 7 (04h 07h 0Bh 07h).
org 100h jmp start vec1 db 1, 2, 5, 6 vec2 db 3, 5, 6, 1 vec3 db ?, ?, ?, ? start: lea si, vec1 lea bx, vec2 lea di, vec3 mov cx, 4 sum: mov al, [si] add al, [bx] mov [di], al inc si inc bx inc di loop sum ret
Example: Adding Arrays
Program and Data in Memory
| Address | Bytes | Meaning |
|---|---|---|
0100 | EB 0C | JMP 010Eh+0Ch: 0102h + 0Ch |
0102 | 01 02 05 06 | vec1data |
0106 | 03 05 06 01 | vec2data |
010A | 04 07 0B 07 | vec3after the run |
010E | BE 02 01 | MOV SI, 0102hLEA → MOV |
0111 | BB 06 01 | MOV BX, 0106h |
0114 | BF 0A 01 | MOV DI, 010Ah |
| Address | Bytes | Meaning |
|---|---|---|
0117 | B9 04 00 | MOV CX, 0004hlittle-endian |
011A | 8A 04 | MOV AL, [SI]sum: |
011C | 02 07 | ADD AL, [BX] |
011E | 88 05 | MOV [DI], AL |
0120 | 46 / 43 / 47 | INC SI / BX / DI1 byte each |
0123 | E2 F5 | LOOP 011AhF5h = −11 |
0125 | C3 | RET |
emu8086 memory window (CS = DS = 0700h). Code and data share one segment; the assembler encoded LEA as the shorter MOV reg, imm.
BCD Arithmetic
Binary Coded Decimal (BCD)
- BCD (Binary Coded Decimal): each decimal digit is kept in its own 4-bit group (0000–1001).
- Unpacked BCD: one digit per byte. Packed BCD: two digits per byte.
- The CPU adds in binary; special instructions adjust the result to BCD.
| Representation | The number 14 | Byte(s) |
|---|---|---|
| Binary | 0000 1110b | 0Eh |
| Unpacked BCD | 0000 0001 · 0000 0100 | 01h 04h (AH, AL) |
| Packed BCD | 0001 0100b | 14h |
| ASCII | '1' '4' | 31h 34h |
BCD Arithmetic
BCD Adjust Instructions
| Instruction | When? | Effect |
|---|---|---|
AAA | after unpacked addition | adjust AL to BCD, carry into AH |
AAS | after unpacked subtraction | adjust AL, borrow from AH |
AAM | after unpacked multiplication | AH = AL / 10, AL = AL mod 10 |
AAD | before unpacked division | AL = AH × 10 + AL, AH = 0 |
DAA / DAS | after packed addition / subtraction | adjust the two digits in AL |
DAS → 45h (83 − 38 = 45).BCD Arithmetic · Step through the code
Code Trace: 9 + 5 with AAA
AH = 09h: the first digit, 9.
AL = 05h: the second digit, 5. AX = 0905h.
09h + 05h = 0Eh (14): correct in binary but not BCD (E > 9). No nibble carry → AF = 0.
AH = 0: the tens digit is cleared. AX = 000Eh.
AAA: the low nibble of AL is E > 9 → AL + 6 = 14h, AH + 1, AF = CF = 1; the high nibble of AL is cleared → AX = 0104h → “14”.
30h is added to each digit: AX = 3134h = '1','4'.
BL = '4' is saved, AL = '1'.
AH = 0Eh (teletype).
'1' is printed.
AL = '4'.
'4' is printed. The screen shows 14.
RET.
; first number '9': mov ah, 09h ; second number '5': mov al, 05h ; al = al + ah = 09h + 05h = 0eh add al, ah ; clear tens byte of bcd xor ah, ah ; adjust result to bcd form, ah = 1, al = 4 -> '14' aaa ; --- print the result (extra) --- or ax, 3030h ; to ASCII mov bl, al ; save '4' mov al, ah ; '1' mov ah, 0Eh int 10h mov al, bl int 10h ret
BCD Arithmetic · Step through the code
Code Trace: 5 − 8 with AAS
AL = 05h. emu8086 starts the program with AX = 0000h.
BL = 08h.
5 − 8 = −3 = FDh. Borrow: CF = 1; the low nibble borrows too: AF = 1.
AAS: AF = 1 → AL − 6 = F7h, AH − 1 = FFh, CF = AF = 1; the high nibble is cleared → AX = FF07h.
AL = 37h = '7'.
AH = 0Eh.
'7' is printed.
Waits for a key.
The result is “7, borrow 1”, as in 15 − 8 = 7. The true difference is −3; CF = 1 says the result is negative and 7 is the tens' complement of 3.
; make 5 - 8, al = 0fdh (not in bcd form) mov al, 05h mov bl, 08h sub al, bl ; convert to bcd, al = 7 ; and 1 is borrowed from ah, ; like calculating 15 - 8: aas ; --- print --- or al, 30h mov ah, 0Eh int 10h mov ah, 0 int 16h ret
BCD Arithmetic
AAM and AAD
mov al, 7 mov bl, 8 mul bl ; AX = 0038h (56) aam ; AH = 05h, AL = 06h
Divides AL by 10: quotient to AH, remainder to AL. 56 → “5”, “6”.
mov ax, 0504h ; '54' unpacked aad ; AX = 0036h (54) mov bl, 6 div bl ; AL = 09h, AH = 00h
AL = AH × 10 + AL, AH = 0: the two digits become a binary number, then a normal DIV.
DAA: MOV AL,38h / ADD AL,45h → AL = 7Dh, DAA → AL = 83h (38 + 45 = 83).BCD Arithmetic · Step through the code
Code Trace: AAM, AAD and DAA
Two single-digit numbers: AL = 6, BL = 9.
MUL BL: AX = 6 × 9 = 54 = 0036h. A binary result; to print it, split it into digits.
AAM: AL / 10 → AH = 5 (quotient), AL = 4 (remainder). AX = 0504h: unpacked “54”.
AX = 0702h: unpacked BCD “72” (AH = 7, AL = 2).
AAD: AL = 7 × 10 + 2 = 72 = 48h, AH = 0. Converted to binary before the division.
DIV BL: 72 / 8 → AL = 9, AH = 0 (remainder). Without AAD, 0702h = 1794 would be divided.
AL = 19h: packed BCD “19” (two digits in one byte).
19h + 28h = 41h: a binary addition. 9 + 8 = 17 overflowed the low nibble → AF = 1; 41h is wrong as BCD.
DAA: AF = 1 → AL + 06h = 47h. The result is “47” = 19 + 28 ✓
MOV AL, 6 MOV BL, 9 MUL BL ; AX = 6 × 9 AAM ; split digits MOV AX, 0702h ; '72' unpacked AAD ; to binary MOV BL, 8 DIV BL MOV AL, 19h ; packed 19 ADD AL, 28h ; + 28 DAA ; adjust BCD RET
Example: Temperature Conversion · Step through the code
Code Trace: °C → °F
CL = 15 (°C) = 0Fh.
AL = 9.
IMUL CL: AX = 9 × 15 = 135 = 0087h. 135 does not fit in a signed byte (>127) → CF = OF = 1; but the full result is in AX.
CL = 5 (the divisor).
IDIV CL: AX / 5 = 135 / 5 → AL = 1Bh (27), AH = 0 (remainder). The flags are undefined.
27 + 32 = 59 = 3Bh → AX = 003Bh. 15 °C = 59 °F ✓
RET. Final state in emu8086: AX = 003Bh, CX = 0005h.
org 100h start: ;celsius to fahrenheit according to f = c * 9 / 5 + 32 mov cl, 15 mov al, 9 imul cl mov cl, 5 idiv cl add al, 32 ret
f = c × 9 / 5 + 32. Multiply first, then divide: in integer division 9/5 would be 1 and the result would be wrong.
Example: Temperature Conversion · Step through the code
Code Trace: °F → °C
CL = 59 (°F) = 3Bh.
59 − 32 = 27 = 1Bh.
AL = 5.
AX = 5 × 27 = 135 = 0087h; CF = OF = 1 (it did not fit in AL).
CL = 9.
135 / 9 = 15 = 0Fh → AL, remainder AH = 0. 59 °F = 15 °C ✓
RET.
org 100h start: ;fahrenheit to celsius according to: c = (f - 32) * 5 / 9 mov cl, 59 sub cl, 32 mov al, 5 imul cl mov cl, 9 idiv cl ret
Example: Temperature Conversion
Why IMUL / IDIV? Limits
c = −10: IMUL → AX = FFA6h (−90), IDIV → AL = EEh (−18), +32 → 0Eh = 14 °F ✓
With MUL/DIV, F6h would be taken as 246 and the result would be meaningless.
IDIV drops the fraction (toward zero): c = 1 → 9/5 = 1 → 33 °F (really 33.8). The remainder (AH) can be checked for rounding.
The result is in the signed byte AL: c ∈ [−71, 53]. At c = 54, AL = 81h (OF = 1, wrong); at c = 72 the quotient does not fit in AL → divide error.
f − 32 must fit in a byte: f ∈ [−96, 127]. For a wider range use word operations (AX, CWD, IDIV BX).
Lab · 8086 instructions step by step
Lab: Arithmetic and Logic
Summary
Summary: Instructions and Their Flags
| Instruction | CF | ZF | SF | OF | PF | AF |
|---|---|---|---|---|---|---|
ADD SUB CMP ADC SBB | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
NEG | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
INC DEC | — | ✓ | ✓ | ✓ | ✓ | ✓ |
AND OR XOR TEST | 0 | ✓ | ✓ | 0 | ✓ | ? |
NOT | — | — | — | — | — | — |
MUL IMUL | ✓ | ? | ? | ✓ | ? | ? |
DIV IDIV | ? | ? | ? | ? | ? | ? |
SHL SHR SAR | ✓ | ✓ | ✓ | ✓¹ | ✓ | ? |
ROL ROR RCL RCR | ✓ | — | — | ✓¹ | — | — |
AAA AAS | ✓ | ? | ? | ? | ? | ✓ |
MOV LEA LOOP | — | — | — | — | — | — |
✓: set by result · 0: cleared · —: unchanged · ?: undefined · ¹: defined only for a count of 1.
Summary
Summary
CF unsigned, OF signed overflow; ZF zero; SF the MSB; PF an even number of 1s in the low byte; AF the nibble carry.
Result in the first operand; CMP/TEST flags only. Logic instructions set CF = OF = 0.
Implicit AL/AX/DX. Byte: AX = AL × op, AL = AX / op (AH remainder). Word: DX:AX.
INC/DEC preserve CF; NOT changes no flags; NEG is two's complement.
Count 1 or CL. SHL ×2, SHR unsigned ÷2, SAR signed ÷2 (rounds toward −∞); rotates lose no bits, RCL/RCR form a 9 (17)-bit ring with CF.
AAA/AAS after addition/subtraction, AAM after multiplication, AAD before division.
Prepare AH/DX before dividing; use IMUL/IDIV for signed data; detect overflow with the flags.
Summary
Exercises
- What are AL, CF, OF, SF, ZF, PF after
MOV AL, 0C0h/ADD AL, 0C0h? - After
CMP AL, BL, which flag tells unsigned “AL < BL”, and which flags tell signed “AL < BL”? - With AX = 0FFFFh and BX = 0FFFFh, what are DX, AX, CF after
MUL BX? - What happens on
DIV BLwith AX = 0100h and BL = 0? - Change the array addition program for word (DW) arrays.
- Write a program that counts the 1 bits in a byte using SHL/TEST and a loop. Compare with PF.
Hints / answers
- C0h + C0h = 180h → AL = 80h; CF = 1, OF = 0 (−64 + −64 = −128 fits), SF = 1, ZF = 0, PF = 0 (a single 1 bit).
- Unsigned: CF = 1. Signed: SF ≠ OF.
- 65535² = FFFE0001h → DX = FFFEh, AX = 0001h, CF = OF = 1.
- Division by zero → divide error (INT 0).
vec1 dw …,mov ax,[si],add ax,[bx],mov [di],ax; the pointers advance withadd si,2.- CX = 8 passes; each pass tests a bit with TEST AL,80h, INCs a counter if it is 1, then SHL AL,1. PF only tells whether the number of 1s is even or odd.
Summary
Exercises (2)
- What happens on
DIV BXwith DX = 0005h, AX = 0000h, BX = 0005h? - What are AL and OF after
MOV AL, 70h/ADD AL, 30h? How do you get the correct sum in 16 bits? - Compute 00012345h − 0000ABCDh with SUB/SBB. What is CF after SUB?
- In a 32 × 32-bit multiplication, which words of the result do the four partial products add into?
- With DX = 0000h, AX = 0FFFh, CF = 0, what is DX:AX after
SHL AX, 1andRCL DX, 1?
Hints / answers
- DX ≥ BX: the dividend 50000h = 327680 gives the quotient 10000h = 65536, which does not fit in AX → divide error (INT 0), even though the divisor is not zero.
- AL = A0h, OF = 1 (112 + 48 = 160 > 127). Widen both numbers separately with
CBWand add in 16 bits: 00A0h = 160. - 2345h − ABCDh = 7778h, CF = 1 (borrow); 0001h − 0000h − 1 = 0000h → 00007778h (30584).
- A0·B0 → z1:z0; A1·B0 and A0·B1 → z2:z1; A1·B1 → z3:z2. Carries move up a word with ADC.
- AX = 1FFEh, the bit shifted out is 0 → CF = 0; RCL shifts a 0 into DX → DX = 0000h. DX:AX = 00001FFEh (0FFFh × 2).
Summary
References
- Intel, The 8086 Family User's Manual, 1979 — Instruction Set chapter.
- B. B. Brey, The Intel Microprocessors, 8th ed., Pearson, 2009 — Chapter 5: arithmetic, logic, shift and rotate instructions.
- emu8086 help: the “8086 instructions” and “Arithmetic and logic instructions” pages.
- K. R. Irvine, Assembly Language for x86 Processors, 7th ed., Pearson, 2014 — Chapter 7: shift and rotate instructions.
- A. S. Tanenbaum, Modern Operating Systems, 3rd ed., Pearson, 2008.
- M. Bodur, Mikroişlemcilere Giriş: Assembler ile Yazılım ve Arayüz, EMO, 2014 (in Turkish).
- Ş. Ç. Öztürk, O. Ergin, Intel 8086 ile Mikroişlemci Programlamaya Giriş, 2009 (in Turkish).