Chapter 6: Arithmetic, Logic and Shift Instructions

Microprocessors
FLAGS registerCF · ZF · SF · OF · PF · AFInstruction groupsADD · SUB · CMPAND · OR · XOR · TESTMUL · IMUL · DIV · IDIVINC · DEC · NOT · NEGSHL · SHR · SAR · ROL · RCRArray additionBCD: AAA · AASTemperature conversion
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Flags

Compute CF, ZF, SF, OF, PF and AF from the result of an operation; explain the difference between CF and OF.

Instructions

Use addition, subtraction, comparison, logic, multiplication, division, single-operand, shift and rotate instructions with valid operands; trace how shifts affect CF.

Multiply / divide

Know the implicit registers of MUL, IMUL, DIV, IDIV (AL, AX, DX:AX) and the divide error.

Practice

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.
1514131211OF10DF9IF8TF7SF6ZF54AF32PF10CFcontrol flags: DF, IF, TFstatus flags: OF, SF, ZF, AF, PF, CF

Dashed bits are unused on the 8086.

The FLAGS Register

The Flags at a Glance

BitFlagKindSet to 1 if…Example / note
0CF · Carrystatusunsigned overflow: carry on addition, borrow on subtraction255 + 1 → AL = 00h, CF = 1; 7 − 9 → CF = 1; STC, CLC, CMC
2PF · Paritystatusthe low byte of the result has an even number of 1 bits96h = 1001 0110b → PF = 1; even for word results, only the low byte
4AF · Auxiliarystatuscarry/borrow from bit 3 into bit 408h + 08h → AF = 1; used only by AAA, AAS, DAA, DAS
6ZF · Zerostatusresult is zeroSUB AL, AL → ZF = 1; after CMP AX, BX, ZF = 1 means AX = BX
7SF · Signstatusthe most significant bit (MSB) of the result is 1copy of the MSB: 96h → SF = 1
8TF · TrapcontrolINT 1 after every instructionsingle-step debugging
9IF · InterruptcontrolINTR interrupts are acceptedSTI → 1, CLI → 0; does not affect NMI
10DF · Directioncontrolstring instructions run backward0: SI/DI increment, 1: decrement; CLD / STD
11OF · Overflowstatussigned overflow: the result does not fit the signed range100 + 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 + operandunsigned viewsigned viewALCFOF
FFh + 01h255 + 1 = 256-1 + 1 = 000h10
64h + 32h100 + 50 = 150100 + 50 = 15096h01
C8h + 64h200 + 100 = 300-56 + 100 = 442Ch10
80h + 80h128 + 128 = 256-128 + -128 = -25600h11
05h + 0Ah5 + 10 = 155 + 10 = 150Fh00
OF rule: if two numbers of the same sign add up to a result of the other sign, OF = 1. Adding two numbers of different signs never sets OF.

The FLAGS Register · Step through the code

Code Trace: CF, OF and the Other Flags

AL17060514131201009Chunsigned: 156signed: -100CF0OF0SF0ZF0AF0PF0

AL = 9Ch: unsigned 156, signed −100. The flags start at 0; MOV does not change them.

AL07060504131201000Chunsigned: 12signed: 12CF1OF0SF0ZF0AF0PF1

156 + 112 = 268 > 255 → AL = 0Ch, CF = 1. Signed −100 + 112 = 12 is correct → OF = 0. 0Ch has two 1s → PF = 1.

AL071605140302010050hunsigned: 80signed: 80CF1OF0SF0ZF0AF0PF1

AL = 50h (80). The flags still reflect the ADD.

AL1706150403020100A0hunsigned: 160signed: -96CF0OF1SF1ZF0AF0PF1

80 + 80 = 160 is correct unsigned (CF = 0), but two positives gave a negative sum (A0h = −96) → OF = 1, SF = 1.

AL071605040302010040hunsigned: 64signed: 64CF1OF1SF0ZF0AF0PF0

A0h + A0h: unsigned 320 (CF = 1), signed −96 + (−96) = −192 (OF = 1). AL = 40h: both views are wrong.

AL1716151413121110FFhunsigned: 255signed: -1CF1OF1SF0ZF0AF0PF0

AL = FFh. CF = 1 is still there.

AL070605040302010000hunsigned: 0signed: 0CF1OF0SF0ZF1AF1PF1

INC AL: FFh + 1 = 00h → ZF = 1, AF = 1; but INC leaves CF alone: CF = 1 is left over from the previous addition.

AL070605040302010000hunsigned: 0signed: 0CF0OF0SF0ZF1AF1PF1

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

A = 64h0716150403120100B = 32h00110010

MOV AL, 64h (100) and ADD AL, 32h (50). Bits are added from right to left.

A = 64h0716150403120100B = 32h00110010AL = 96h10010110

The result is 96h = 1001 0110b. Unsigned it is 150 (right); signed it is −106 (wrong!).

A = 64h0716150403120100B = 32h00110010AL = 96h10010110carry out of bit 7: 0→ CF = 0

CF: did a carry come out of the leftmost bit? No → CF = 0 (150 ≤ 255).

A = 64h0716150403120100B = 32h00110010AL = 96h10010110carry into bit 7 is 1, out is 0→ OF = 1

OF: two positive numbers gave a negative sum (the carries into and out of bit 7 differ) → OF = 1.

A = 64h0716150403120100B = 32h00110010AL = 96h10010110MSB = 1 → SF = 1result ≠ 0 → ZF = 0

SF = bit 7 of the result = 1. The result is not zero: ZF = 0.

A = 64h0716150403120100B = 32h00110010AL = 96h10010110four 1s (even) → PF = 1nibbles 4 + 2: no carry → AF = 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

Group 1 · two operands

ADD, SUB, CMP, AND, TEST, OR, XOR

dest, src

Group 2 · multiply and divide

MUL, IMUL, DIV, IDIV

one operand + implicit AL / AX / DX

Group 3 · one operand

INC, DEC, NOT, NEG

the operand is both source and destination

Increment (INC) and decrement (DEC) are in group 3. The groups are defined by operand form.
dest ← dest ∘ srcADD AX, BXAX ← AL × operandMUL BLoperand ← f(operand)INC CX

Instruction Groups

What the Instructions Do

Group 1What it does
ADDadds two values, writes the result to dest
SUBsubtracts src from dest, writes the result to dest
CMPsubtracts but does not store the result; flags only
ANDbitwise AND
TESTbitwise AND; result not stored
ORbitwise OR
XORbitwise exclusive OR
Group 2What it does
MULunsigned multiply
IMULsigned multiply
DIVunsigned divide (quotient + remainder)
IDIVsigned divide
Group 3What it does
INCadds 1
DECsubtracts 1
NOTinverts every bit
NEGchanges the sign (two's complement)

Group 1: Two Operands

ADD, SUB, CMP, AND, TEST, OR, XOR: Operands

FormExample
REG, memoryADD AX, [BX]
memory, REGSUB var1, CL
REG, REGXOR AX, AX
memory, immediateAND byte ptr [SI], 0Fh
REG, immediateCMP 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 …
Invalid: 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).
  • CMP and TEST only 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; TEST is like AND but only affects the flags.
InstructionCFZFSFOFPFAF
ADD, SUB, CMP✓✓✓✓✓✓
AND, OR, XOR, TEST0✓✓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

AND
ABA AND B
111
100
010
000

1 if both are 1.

OR
ABA OR B
111
101
011
000

1 if at least one is 1.

XOR
ABA XOR B
110
101
011
000

1 if they differ.

NOT
ANOT A
10
01

Inverts. One operand.

These rules apply to each of the 8 or 16 bits separately: AND AL, BL is really 8 independent ANDs.

Logic Operations

Masking: AND, OR, XOR, TEST

AL='a' 61h0716150403020110mask DFh11011111ANDAL='A' 41h01000001

AND clears bits: bit 5, which is 0 in the mask, is cleared and the rest are kept → lowercase becomes uppercase.

AL='A' 41h0716050403020110mask 20h00100000ORAL='a' 61h01100001

OR sets bits: bit 5, which is 1 in the mask, becomes 1 → uppercase becomes lowercase.

AL='a' 61h0716150403020110mask 20h00100000XORAL='A' 41h01000001

XOR flips bits: bit 5, which is 1 in the mask, changes → the letter switches case.

BL='7' 37h0706151403121110mask 01h00000001TEST(not stored)00000001

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

CodeEffectWhy?
XOR AX, AXAX = 0, ZF = 1, CF = 0shorter than MOV AX, 0 (2 bytes); also clears flags
AND AL, 0Fh'7' (37h) → 7turns an ASCII digit into a number (high nibble cleared)
OR AL, 30h7 → '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, 1odd if ZF = 0tests bit 0 without changing AL
TEST AL, 80hnegative if ZF = 0tests the sign bit
OR AX, AXAX unchangedsets 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

byte operand (8-bit)AL×operandAX (16-bit)MUL / IMULword operand (16-bit)AX×operandDXAX32-bit result DX:AX (DX high)
InstructionByte operandWord 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

InstructionByte operandWord 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
DXAXdividend (32-bit)÷operanddivisor (16-bit)AXquotientDXremainderbyte division:AX ÷ operand → AL, AH
Flags

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.

Divide error

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

ALC8hBL03h

MOV AL, 200, MOV BL, 3. 200 = C8h, which is −56 signed.

AX0258hCF1OF1

MUL BL: 200 × 3 = 600 = 0258h → AX. The result did not fit in AL (AH ≠ 0) → CF = OF = 1.

AXFF58hCF1OF1

Same bits with IMUL BL: −56 × 3 = −168 = FF58h. A different result! −168 does not fit in a byte → CF = OF = 1.

AXFFF6hCF0OF0

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.

AX0017hBL05hAL04hAH03h

DIV BL: AX = 23, BL = 5 → quotient AL = 4, remainder AH = 3. New AX = 0304h.

AXFFE9hBL05hALFChAHFDh

IDIV BL: −23 / 5 → quotient AL = FCh (−4), remainder AH = FDh (−3). Check: −4 × 5 + (−3) = −23.

AX1000hBL02hAL?

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.

DXFFFFhAXFFFFhBX0001hAX?

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, 0 or XOR 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.
AL11111001F9h = -7CBW: bit 7 is copied into AHAX1111111111111001FFF9h = -7
; 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

DX0000hAX07D0hBX0000hDX:AX = 000007D0hunsigned 2000 · signed 2000

AX = 2000 = 07D0h. In word division the dividend is DX:AX; what DX holds matters too.

DX0000hAX07D0hBX0000hDX:AX = 000007D0hunsigned 2000 · signed 2000

XOR DX, DX: the upper half is zero for unsigned division → DX:AX = 2000.

DX0000hAX07D0hBX0009hDX:AX = 000007D0hunsigned 2000 · signed 2000

Divisor BX = 9.

DX0002hAX00DEhBX0009hDX:AX = 000200DEhunsigned 131294 · signed 131294quotient AX = 222, remainder DX = 2

DIV BX: 2000 / 9 → AX = 222 (00DEh), DX = 2. The flags are undefined.

DX0002hAXF830hBX0009hDX:AX = 0002F830hunsigned 194608 · signed 194608

AX = −2000 = F830h. DX still holds the previous remainder (2): DX:AX is now a wrong dividend.

DXFFFFhAXF830hBX0009hDX:AX = FFFFF830hunsigned 4294965296 · signed -2000

CWD: the sign bit of AX is 1 → DX = FFFFh. DX:AX = FFFFF830h, −2000 in 32 bits.

DXFFFEhAXFF22hBX0009hDX:AX = FFFEFF22hunsigned 4294901538 · signed -65758quotient AX = -222, remainder DX = -2

IDIV BX: −2000 / 9 → AX = FF22h (−222), DX = FFFEh (−2). The remainder has the sign of the dividend.

DXFFFEhAX07D0hBX0009hDX:AX = FFFE07D0hunsigned 4294838224 · signed -129072

AX = 2000, but DX = FFFEh was not cleared: DX:AX = FFFE07D0h, about 4.29 billion.

DXFFFEhAX07D0hBX0009hDX:AX = FFFE07D0hunsigned 4294838224 · signed -129072quotient doesn't fit in AX → INT 0

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.

z3 · bit 48–63z2 · bit 32–47z1 · bit 16–31z0 · bit 0–15A =9ABC (A1)DEF0 (A0)× B =EDCB (B1)FEDC (B0)

A = 9ABCDEF0h and B = EDCBFEDCh are split into 16-bit halves: A = A1:A0, B = B1:B0.

z3 · bit 48–63z2 · bit 32–47z1 · bit 16–31z0 · bit 0–15A =9ABC (A1)DEF0 (A0)× B =EDCB (B1)FEDC (B0)A0 × B01. MULDDF1B640

A0 × B0 is not shifted: it lands in the two lowest words of the result (z1:z0).

z3 · bit 48–63z2 · bit 32–47z1 · bit 16–31z0 · bit 0–15A =9ABC (A1)DEF0 (A0)× B =EDCB (B1)FEDC (B0)A0 × B01. MULDDF1B640A1 × B0MUL 2 · shifted 16 bits9A0B8190A0 × B1MUL 3 · shifted 16 bitsCF14F850

A1 × B0 and A0 × B1 are both multiplied by 216: they go into columns z2:z1.

z3 · bit 48–63z2 · bit 32–47z1 · bit 16–31z0 · bit 0–15A =9ABC (A1)DEF0 (A0)× B =EDCB (B1)FEDC (B0)A0 × B01. MULDDF1B640A1 × B0MUL 2 · shifted 16 bits9A0B8190A0 × B1MUL 3 · shifted 16 bitsCF14F850A1 × B1MUL 4 · shifted 32 bits8FBABF14

A1 × B1 is multiplied by 232: it goes into columns z3:z2.

z3 · bit 48–63z2 · bit 32–47z1 · bit 16–31z0 · bit 0–15A =9ABC (A1)DEF0 (A0)× B =EDCB (B1)FEDC (B0)A0 × B01. MULDDF1B640A1 × B0MUL 2 · shifted 16 bits9A0B8190A0 × B1MUL 3 · shifted 16 bitsCF14F850A1 × B1MUL 4 · shifted 32 bits8FBABF14+2 carry+2 carrycolumn carries

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.

z3 · bit 48–63z2 · bit 32–47z1 · bit 16–31z0 · bit 0–15A =9ABC (A1)DEF0 (A0)× B =EDCB (B1)FEDC (B0)A0 × B01. MULDDF1B640A1 × B0MUL 2 · shifted 16 bits9A0B8190A0 × B1MUL 3 · shifted 16 bitsCF14F850A1 × B1MUL 4 · shifted 32 bits8FBABF14+2 carry+2 carrycolumn carriesA × B =8FBC283557D1B640

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.
  • INC and DEC affect only ZF, SF, OF, PF, AF; they leave CF alone.
  • NOT affects no flags at all! It inverts every bit of the operand (ones' complement).
  • NEG affects 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.
InstructionCFZFSFOFPFAF
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 = 05h0706050403120110

x = 5 = 0000 0101b.

x = 05h0706050403120110NOT x11111010FAh

NOT: every bit is inverted → 1111 1010b = FAh (−6). Ones' complement.

x = 05h0706050403120110NOT x11111010FAh+ 1 → NEG11111011FBh

+1: FBh = 1111 1011b = −5. NEG does both steps in one instruction; CF = 1 (x ≠ 0).

x = FEh1716151413121100NOT x0000000101h+ 1 → NEG0000001002h

x = FEh (−2): NOT → 01h, +1 → 02h. The negative of a negative is positive.

x = 80h1706050403020100NOT x011111117Fh+ 1 → NEG1000000080h

Special case: x = 80h (−128). NOT → 7Fh, +1 → 80h: +128 does not fit in a byte → OF = 1.

x = 00h0706050403020100NOT x11111111FFh+ 1 → NEG0000000000h

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

b7b6b5b4b3b2b1b0CF0
SHL/SAL: shift left, 0 enters from the right, the bit shifted out goes to CF. Each step is × 2.
b7b6b5b4b3b2b1b0CF0
SHR: logical shift right, 0 enters from the left. Unsigned ÷ 2.
b7b6b5b4b3b2b1b0CFsign bit kept
SAR: arithmetic shift right, the sign bit is kept. Signed ÷ 2 (rounds toward −∞).
Shift countOn the 8086 only 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 and IDIV round differently: −7 (F9h) sar 1 → FCh = −4; idiv 2 → −3.

Shift and Rotate

Rotates: ROL, ROR, RCL, RCR

b7b6b5b4b3b2b1b0CF
ROL: rotate left; b7 goes to both b0 and CF.
b7b6b5b4b3b2b1b0CF
ROR: rotate right; b0 goes to both b7 and CF.
b7b6b5b4b3b2b1b0CF
RCL: rotate left through CF (a 9-bit ring).
b7b6b5b4b3b2b1b0CF
RCR: rotate right through CF.
No bit is lost in a rotate. Only CF and (when the count is 1) OF change; SF, ZF, PF are not affected. RCL/RCR are used for multi-word shifts: 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 mul takes 70–133 and div 80–162 clocks; a shl reg, 1 takes 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.
Example: AX = 25 → BX = 25, AX = 50 → 50 + 25 = 75.

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 = 0025h = 37015000000080701001010BX = 0000h = 00000000000000000

AX = 37 = 0025h = 0000 0000 0010 0101b.

AX = 004Ah = 74015000000080710010100BX = 0000h = 0000000000000000037 → 74

SHL AX, 1: bits move one place left, 0 enters from the right → AX = 74 (004Ah). 2 clocks; MUL takes more than 70.

AX = 004Ah = 74015000000080710010100BX = 004Ah = 740000000001001010

BX = 74: ×2 is saved.

AX = 0094h = 148015000000081700101000BX = 004Ah = 74000000000100101074 → 148

SHL → AX = 148 (×4).

AX = 0128h = 296015000000180701010000BX = 004Ah = 740000000001001010148 → 296

SHL → AX = 296 (×8).

AX = 0172h = 370015000000180711100100BX = 004Ah = 740000000001001010296 → 370

ADD AX, BX: 296 + 74 = 370 = 0172h = 37 × 10.

AX = 0172h = 370015000000180711100100BX = 0172h = 3700000000101110010

BX = 370: a copy for the division.

AX = 0172h = 370015000000180711100100BX = 0002h = 20000000000000010

AND BX, 000Fh: low 4 bits = 370 mod 16 = 2.

AX = 0172h = 370015000000180711100100BX = 0002h = 20000000000000010

CL = 4: on the 8086 a shift count above 1 goes in CL.

AX = 0017h = 23015000000080700101110BX = 0002h = 20000000000000010370 → 23

SHR AX, CL: 4 bits right = ÷ 16 → AX = 23 (0017h). 23 × 16 + 2 = 370.

AX = 0017h = 23015000000080700101110BX = 0002h = 20000000000000010

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 start jumps over the data definitions; otherwise the CPU would take the data bytes for instructions.
  • SI, BX, DI are pointers to the three arrays; LEA loads the address (offset).
  • [SI]: the memory byte SI points to (indirect addressing).
  • LOOP sum: decrements CX and goes back to sum while CX ≠ 0 → 4 passes.
  • Expected result: vec3 = 4, 7, 11, 7.

Example: Adding Arrays · Step through the code

Code Trace: Adding Arrays

vec1 (0102h)1256vec2 (0106h)3561vec3 (010Ah)0000

JMP start: the data (0102h–010Dh) is skipped; execution goes to 010Eh.

vec1 (0102h)1256▲SIvec2 (0106h)3561vec3 (010Ah)0000

SI = address of vec1 = 0102h.

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)0000▲DI

BX = 0106h (vec2), DI = 010Ah (vec3).

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)0000▲DI

CX = 4 passes.

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)0000▲DI

Pass 1: AL ← [SI] = [0102h] = 1.

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)0000▲DI

AL ← AL + [BX] = 1 + 3 = 4 (04h).

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)4000▲DI

[DI] ← AL: vec3[0] = 4.

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)4000▲DI

All three pointers advance one byte (these are byte arrays; for word arrays, add 2).

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)4000▲DI

LOOP: CX = 3 ≠ 0 → back to sum.

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)4000▲DI

Pass 2: AL ← [SI] = [0103h] = 2.

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)4000▲DI

AL ← AL + [BX] = 2 + 5 = 7 (07h).

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)4700▲DI

[DI] ← AL: vec3[1] = 7.

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)4700▲DI

All three pointers advance one byte (these are byte arrays; for word arrays, add 2).

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)4700▲DI

LOOP: CX = 2 ≠ 0 → back to sum.

vec1 (0102h)1256▲SIvec2 (0106h)3561▲BXvec3 (010Ah)47110▲DI

Pass 3 (summary): 5 + 6 = 11 → vec3[2]. LOOP: CX = 1 ≠ 0 → back to sum.

vec1 (0102h)1256vec2 (0106h)3561▲SIvec3 (010Ah)47117▲BX

Pass 4 (summary): 6 + 1 = 7 → vec3[3]. LOOP: CX = 0 → the loop ends.

vec1 (0102h)1256vec2 (0106h)3561▲SIvec3 (010Ah)47117▲BX

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

AddressBytesMeaning
0100EB 0CJMP 010Eh
+0Ch: 0102h + 0Ch
010201 02 05 06vec1
data
010603 05 06 01vec2
data
010A04 07 0B 07vec3
after the run
010EBE 02 01MOV SI, 0102h
LEA → MOV
0111BB 06 01MOV BX, 0106h
0114BF 0A 01MOV DI, 010Ah
AddressBytesMeaning
0117B9 04 00MOV CX, 0004h
little-endian
011A8A 04MOV AL, [SI]
sum:
011C02 07ADD AL, [BX]
011E88 05MOV [DI], AL
012046 / 43 / 47INC SI / BX / DI
1 byte each
0123E2 F5LOOP 011Ah
F5h = −11
0125C3RET

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.
RepresentationThe number 14Byte(s)
Binary0000 1110b0Eh
Unpacked BCD0000 0001 · 0000 010001h 04h (AH, AL)
Packed BCD0001 0100b14h
ASCII'1' '4'31h 34h

BCD Arithmetic

BCD Adjust Instructions

InstructionWhen?Effect
AAAafter unpacked additionadjust AL to BCD, carry into AH
AASafter unpacked subtractionadjust AL, borrow from AH
AAMafter unpacked multiplicationAH = AL / 10, AL = AL mod 10
AADbefore unpacked divisionAL = AH × 10 + AL, AH = 0
DAA / DASafter packed addition / subtractionadjust the two digits in AL
The adjust instructions work only on AL (and AH), so BCD addition/subtraction is done byte by byte. Example: 83h − 38h = 4Bh → 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

AAM · after multiplication
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”.

AAD · before division
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.

For packed BCD use 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

AH00hAL06hBL09hAF0CF0AX = 0006h (binary 6)AL nibbles: 0 | 6

Two single-digit numbers: AL = 6, BL = 9.

AH00hAL36hBL09hAF?CF0AX = 0036h (binary 54)AL nibbles: 3 | 6

MUL BL: AX = 6 × 9 = 54 = 0036h. A binary result; to print it, split it into digits.

AH05hAL04hBL09hAF?CF?AX = 0504h (binary 1284)AL nibbles: 0 | 4

AAM: AL / 10 → AH = 5 (quotient), AL = 4 (remainder). AX = 0504h: unpacked “54”.

AH07hAL02hBL09hAF?CF?AX = 0702h (binary 1794)AL nibbles: 0 | 2

AX = 0702h: unpacked BCD “72” (AH = 7, AL = 2).

AH00hAL48hBL09hAF?CF?AX = 0048h (binary 72)AL nibbles: 4 | 8

AAD: AL = 7 × 10 + 2 = 72 = 48h, AH = 0. Converted to binary before the division.

AH00hAL09hBL08hAF?CF?AX = 0009h (binary 9)AL nibbles: 0 | 9

DIV BL: 72 / 8 → AL = 9, AH = 0 (remainder). Without AAD, 0702h = 1794 would be divided.

AH00hAL19hBL08hAF?CF?AX = 0019h (binary 25)AL nibbles: 1 | 9

AL = 19h: packed BCD “19” (two digits in one byte).

AH00hAL41hBL08hAF1CF0AX = 0041h (binary 65)AL nibbles: 4 | 1

19h + 28h = 41h: a binary addition. 9 + 8 = 17 overflowed the low nibble → AF = 1; 41h is wrong as BCD.

AH00hAL47hBL08hAF1CF0AX = 0047h (binary 71)AL nibbles: 4 | 7

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

Negative temperatures

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.

Integer division

IDIV drops the fraction (toward zero): c = 1 → 9/5 = 1 → 33 °F (really 33.8). The remainder (AH) can be checked for rounding.

Valid range for °C → °F

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.

Valid range for °F → °C

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

InstructionCFZFSFOFPFAF
ADD SUB CMP ADC SBB✓✓✓✓✓✓
NEG✓✓✓✓✓✓
INC DEC—✓✓✓✓✓
AND OR XOR TEST0✓✓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

Flags

CF unsigned, OF signed overflow; ZF zero; SF the MSB; PF an even number of 1s in the low byte; AF the nibble carry.

Group 1

Result in the first operand; CMP/TEST flags only. Logic instructions set CF = OF = 0.

Group 2

Implicit AL/AX/DX. Byte: AX = AL × op, AL = AX / op (AH remainder). Word: DX:AX.

Group 3

INC/DEC preserve CF; NOT changes no flags; NEG is two's complement.

Shifts and rotates

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.

BCD

AAA/AAS after addition/subtraction, AAM after multiplication, AAD before division.

Pitfalls

Prepare AH/DX before dividing; use IMUL/IDIV for signed data; detect overflow with the flags.

Summary

Exercises

  1. What are AL, CF, OF, SF, ZF, PF after MOV AL, 0C0h / ADD AL, 0C0h?
  2. After CMP AL, BL, which flag tells unsigned “AL < BL”, and which flags tell signed “AL < BL”?
  3. With AX = 0FFFFh and BX = 0FFFFh, what are DX, AX, CF after MUL BX?
  4. What happens on DIV BL with AX = 0100h and BL = 0?
  5. Change the array addition program for word (DW) arrays.
  6. Write a program that counts the 1 bits in a byte using SHL/TEST and a loop. Compare with PF.
Hints / answers
  1. C0h + C0h = 180h → AL = 80h; CF = 1, OF = 0 (−64 + −64 = −128 fits), SF = 1, ZF = 0, PF = 0 (a single 1 bit).
  2. Unsigned: CF = 1. Signed: SF ≠ OF.
  3. 65535² = FFFE0001h → DX = FFFEh, AX = 0001h, CF = OF = 1.
  4. Division by zero → divide error (INT 0).
  5. vec1 dw …, mov ax,[si], add ax,[bx], mov [di],ax; the pointers advance with add si,2.
  6. 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)

  1. What happens on DIV BX with DX = 0005h, AX = 0000h, BX = 0005h?
  2. What are AL and OF after MOV AL, 70h / ADD AL, 30h? How do you get the correct sum in 16 bits?
  3. Compute 00012345h − 0000ABCDh with SUB/SBB. What is CF after SUB?
  4. In a 32 × 32-bit multiplication, which words of the result do the four partial products add into?
  5. With DX = 0000h, AX = 0FFFh, CF = 0, what is DX:AX after SHL AX, 1 and RCL DX, 1?
Hints / answers
  1. 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.
  2. AL = A0h, OF = 1 (112 + 48 = 160 > 127). Widen both numbers separately with CBW and add in 16 bits: 00A0h = 160.
  3. 2345h − ABCDh = 7778h, CF = 1 (borrow); 0001h − 0000h − 1 = 0000h → 00007778h (30584).
  4. A0·B0 → z1:z0; A1·B0 and A0·B1 → z2:z1; A1·B1 → z3:z2. Carries move up a word with ADC.
  5. 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).

The End

Chapter 6: Arithmetic, Logic and Shift Instructions
Previous: Chapter 5.2: Interrupt Functions · Next: Chapter 7: Flow Control
1 / 1 Sercan KÜLCÜ. All rights reserved.