Chapter 6: Arithmetic, Logic and Shift Instructions

Chapter 6: Arithmetic, Logic and Shift Instructions · Study Questions
FlagsADD · SUB · SBBAND · OR · XOR · TESTMUL · IMULDIV · IDIV · CWDINC · NEG · NOT · CBWShifts and rotatesArray sumBCD: DAA · AAATemperature conversion
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · The Flag Register

Question 1: CF or OF?

MOV AL, 0C8h and then ADD AL, 64h are executed. What are AL, CF and OF afterwards?

D: Read as unsigned, 200 + 100 = 300 > 255: a carry goes out, 300 − 256 = 44 = 2Ch stays in AL, CF = 1. Read as signed, −56 + 100 = 44 is correct: the sum of two numbers with different signs cannot overflow, OF = 0. AL is 8 bits; 12Ch does not fit there.

Multiple Choice · The Flag Register

Question 2: The Largest Positive Byte

MOV AL, 7Fh and ADD AL, 1 are executed. What are the flags afterwards?

D: 7Fh + 1 = 80h. Unsigned, 127 + 1 = 128 fits in a byte: CF = 0. Signed, +127 + 1 = +128 does not fit and the result appears as −128: the sum of two positives came out negative, OF = 1. The most significant bit of the result is 1: SF = 1. Also, the lower nibble F + 1 produces a carry: AF = 1.

Output Question · Group 1: Two-Operand Instructions

Question 3: High and Low Byte

(a) AX starts at 0000h. What is AX after each line? (b) After MOV AX, 0x00F0 and ADD AH, 0x0010, what is AX? If the second instruction were ADD AH, 0x001E?

MOV AH, 09H
MOV AL, 05H
ADD AL, AH
Show answer

(a) AX: 0900h → 0905h → 090Eh. Writing to AH changes the upper byte of AX and writing to AL the lower byte; 09h + 05h = 0Eh. 0Eh = 0000 1110b has three 1s: PF = 0.

(b) AH = 00h + 10h → AX = 10F0h; AL (F0h) is unchanged. Even though the immediate is written as 0x0010, the destination is 8 bits, so it is used as a byte. With 001Eh, AX = 1EF0h.

Output Question · Group 1: Two-Operand Instructions

Question 4: A Negative Result in Subtraction

After the code runs, what are AX and BX? What are CF, ZF, SF, OF, AF and PF? (AX = BX = 0000h at the start.)

MOV AL, 05H
MOV BL, 08H
SUB AL, BL
Show answer

AX = 00FDh, BX = 0008h. 05h − 08h = −3, which is FDh in 8-bit two's complement; AH is unchanged.

CFZFSFOFAFPF
101010

CF = 1: a larger number was subtracted from a smaller one, so a borrow occurred. SF = 1: bit 7 = 1. OF = 0: signed 5 − 8 = −3 is in range and correct. AF = 1: borrow in the lower nibble for 5 − 8. PF = 0: FDh = 1111 1101b has seven 1s.

Multiple Choice · Group 1: Two-Operand Instructions

Question 5: A Valid Instruction

var1 is a byte variable (DB). Which is a valid 8086 instruction?

C: The SUB memory, register form is valid and the sizes (byte, byte) match. Both operands cannot be memory (A); AL is 8 bits and BX is 16 (B); the destination cannot be a constant, since the result is written to the first operand (D).

Output Question · Group 1: Two-Operand Instructions

Question 6: 32 Bits with SUB and SBB

00012345h − 0000ABCDh is computed as DX:AX − CX:BX. What are AX and CF after the SUB? What is DX:AX in the end?

MOV AX, 2345h
MOV DX, 0001h
MOV BX, 0ABCDh
MOV CX, 0000h
SUB AX, BX
SBB DX, CX
Show answer

SUB AX, BX: 2345h − ABCDh needs a borrow → AX = 7778h, CF = 1.

SBB DX, CX: 0001h − 0000h − CF(1) = 0000h.

DX:AX = 00007778h = 30584 (74565 − 43981). SUB is used on the lowest word and SBB on every higher word; the borrow is carried through CF.

Multiple Choice · Logical Operations

Question 7: Converting to Uppercase

AL holds a lowercase letter (e.g. 'k' = 6Bh). Which instruction converts it to uppercase?

B: Lowercase and uppercase letters differ only in bit 5 ('k' = 6Bh, 'K' = 4Bh). The mask DFh = 1101 1111b clears bit 5 and keeps the other bits. OR AL, 20h sets bit 5 and makes the letter lowercase; XOR AL, 0FFh flips all bits; AND AL, 20h clears everything except bit 5.

Step-by-Step Trace · Logical Operations

Question 8: AND, OR, XOR, TEST

What is AL after each instruction? What are ZF and SF after TEST?

AL6ChAL (binary)0716150413120100CF0ZF0SF0OF0PF0

AL = 6Ch = 0110 1100b.

AL0ChAL (binary)0706050413120100CF0ZF0SF0OF0PF1

AND 0Fh clears the upper nibble (masking): AL = 0Ch. Logic instructions set CF = OF = 0.

AL3ChAL (binary)0706151413120100CF0ZF0SF0OF0PF1

OR 30h sets bits 4 and 5: AL = 3Ch = 0011 1100b.

ALC3hAL (binary)1716050403021110CF0ZF0SF1OF0PF1

XOR 0FFh flips all bits: AL = C3h = 1100 0011b. Bit 7 = 1 → SF = 1.

ALC3hAL (binary)1716050403021110CF0ZF0SF1OF0PF0

TEST performs AND but does not store the result: C3h AND 80h = 80h ≠ 0 → ZF = 0, SF = 1; AL stays C3h. 80h has a single 1: PF = 0.

ORG 100h
MOV AL, 6Ch
AND AL, 0Fh
OR  AL, 30h
XOR AL, 0FFh
TEST AL, 80h
RET

Multiple Choice · Logical Operations

Question 9: TEST versus AND

TEST AL, 80h versus AND AL, 80h: what is the difference?

A: TEST does a bitwise AND like AND but discards the result; it relates to AND as CMP relates to SUB. Both set CF = OF = 0 and set ZF, SF and PF from the result. TEST AL, 80h tests the sign bit without changing AL.

Output Question · Group 2: Multiplication and Division

Question 10: MUL, DIV and Decimal 10

After the code runs, what are AX and CX? What if the last line were ADD AL, 10h?

MOV CL, 4H
MOV AL, 3H
MUL CL
MOV CL, 2H
DIV CL
ADD AL, 10
Show answer

AX = 0010h, CX = 0002h.

  • MUL CL: AX ← AL × CL = 3 × 4 = 12 = 000Ch.
  • DIV CL: 000Ch ÷ 2 → AL = 06h (quotient), AH = 00h (remainder).
  • ADD AL, 10: 10 is decimal (0Ah): 06h + 0Ah = 10h.

10h would give 06h + 10h → AX = 0016h. The suffix determines the base of the number.

Output Question · Group 2: Multiplication and Division

Question 11: Quotient and Remainder

After the code runs, what is AX? What do AH and AL show? What would happen if BL = 0?

MOV AX, 100
MOV BL, 7
DIV BL
Show answer

AX = 020Eh. 100 (decimal) = 0064h; in byte division the dividend is AX.

100 ÷ 7 = 14, remainder 2 → AL = 0Eh (quotient), AH = 02h (remainder).

If BL = 0 the processor generates the divide error interrupt (INT 0). The same happens when the quotient does not fit in AL: AX = 0400h, BL = 2 → 200h. After DIV all flags are undefined.

Multiple Choice · Group 2: Multiplication and Division

Question 12: The Upper Half of the Product

In a 16-bit multiplication, in which register are the upper 16 bits of the product stored? What is its value in this code?

MOV AX, 1234h
MOV BX, 0100h
MUL BX

B: MUL BX: DX:AX ← AX × BX = 1234h × 0100h = 0012 3400h → DX = 0012h (upper), AX = 3400h (lower). Because the upper half is not zero, CF = OF = 1. In an 8-bit MUL the result is only in AX (AH:AL).

Multiple Choice · Group 2: Multiplication and Division

Question 13: The Result of IMUL

With AL = FEh and BL = 05h, IMUL BL is executed. What is AX afterwards?

C: IMUL is signed: FEh = −2, so the result is −2 × 5 = −10 = FFF6h. AH is merely the sign extension of AL, because the result fits in AL: CF = OF = 0. With the same bits, MUL BL would give unsigned 254 × 5 = 1270 = 04F6h.

Multiple Choice · Group 2: Multiplication and Division

Question 14: Divide Error

Which values make DIV BL cause a divide error (INT 0)?

C: 0400h ÷ 2 = 200h; the quotient does not fit in AL (at most FFh) → INT 0; the error occurs even though the divisor is not zero. Rule: in byte division, if AH ≥ the divisor the quotient does not fit (04h ≥ 2). The others give FFh, 0Eh remainder 2 and 80h; all fit in AL.

Step-by-Step Trace · Group 2: Multiplication and Division

Question 15: CWD and IDIV

−1000 ÷ 7 and 1000 ÷ 7 are computed with word division. What are AX and DX after each division? What would happen without CWD?

DX0000hAXFC18hBX0000hCX0000h

AX = −1000 = FC18h (65536 − 1000 = 64536).

DXFFFFhAXFC18hBX0000hCX0000h

CWD: the sign bit of AX is 1 → DX = FFFFh. DX:AX = FFFFFC18h, which is −1000 in 32 bits.

DXFFFFhAXFC18hBX0007hCX0000h

BX = 7 (the divisor).

DXFFFAhAXFF72hBX0007hCX0000h

IDIV BX: quotient AX = FF72h (−142), remainder DX = FFFAh (−6). Check: −142 × 7 − 6 = −1000. The remainder has the same sign as the dividend.

DXFFFAhAXFF72hBX0007hCXFF72h

The quotient is saved in CX.

DX0000hAX03E8hBX0007hCXFF72h

For unsigned division DX is cleared: DX:AX = 0000 03E8h = 1000.

DX0006hAX008EhBX0007hCXFF72h

DIV BX: AX = 008Eh (142), DX = 0006h. If CWD were forgotten, DX would stay 0 in the first division and a wrong result such as 0000FC18h = 64536 ÷ 7 = 9219 (2403h) would come out.

ORG 100h
MOV AX, -1000
CWD
MOV BX, 7
IDIV BX
MOV CX, AX
MOV AX, 1000
XOR DX, DX
DIV BX
RET

Multiple Choice · Group 3: One-Operand Instructions

Question 16: Preserving CF

With AL = FFh and CF = 0, after which of the following does CF still remain 0?

A: INC and DEC do not touch CF: although FFh + 1 = 00h (ZF = 1), CF stays 0. ADD AL, 1 gives the same result but sets CF = 1 because a carry comes out. With NEG, CF = 1 if the operand is not zero; in ADD AL, 80h, 255 + 128 = 383 does not fit in a byte, so CF = 1. This property of INC and DEC lets you preserve CF while incrementing a counter in an ADC chain.

Step-by-Step Trace · Group 3: One-Operand Instructions

Question 17: NEG, NOT and CBW

Initially all registers and flags are 0. What are AX, BL, CF and ZF when the code has run?

AX0019AHALBL0000000000CF0ZF0SF0OF0AL binary: 00011001

AL = 25 = 19h = 0001 1001b. MOV does not affect the flags.

AX00E7AHALBL0000000000CF1ZF0SF1OF0AL binary: 11100111

NEG: AL = 0 − 25 = E7h (256 − 25 = 231). Since the operand is not zero, CF = 1; MSB = 1 → SF = 1.

AX00E7AHALBLE711100111CF1ZF0SF1OF0AL binary: 11100111

BL ← AL = E7h. MOV copies; the flags do not change.

AX00E7AHALBL1800011000CF1ZF0SF1OF0AL binary: 11100111

NOT: all bits are flipped: 1110 0111 → 0001 1000 = 18h = 24. The one's complement is 1 less than the two's complement. NOT does not affect the flags.

AXFFE7AHALBL1800011000CF1ZF0SF1OF0AL binary: 11100111

CBW: the MSB of AL (1) is copied into all bits of AH → AX = FFE7h = −25 (16 bits).

AXFF00AHALBL1800011000CF1ZF1SF0OF0AL binary: 00000000

E7h + 19h = 100h: the carry that does not fit in 8 bits goes to CF → AL = 00h, CF = 1, ZF = 1. Read as signed, −25 + 25 = 0 is correct: OF = 0. AH is unchanged: AX = FF00h.

MOV AL, 25
NEG AL        ; two's complement
MOV BL, AL
NOT BL        ; one's complement
CBW           ; AL -> AX
ADD AL, 25

Step-by-Step Trace · Shifts and Rotates

Question 18: SHL, RCR, SAR

What are AL and CF after each instruction?

AL96h1b70b60b51b40b31b21b10b00CF

AL = 96h = 1001 0110b.

AL2Ch0b70b61b50b41b31b20b10b01CF

SHL: bits move left, 0 enters from the right, b7 (1) goes to CF → AL = 2Ch, CF = 1.

AL96h1b70b60b51b40b31b21b10b00CF

RCR: together with CF a 9-bit ring rotates right; the old CF (1) enters b7, b0 (0) goes out to CF → AL = 96h, CF = 0.

ALCBh1b71b60b50b41b30b21b11b00CF

SAR: the sign bit (1) is copied → 1100 1011b = CBh, CF = 0. Signed division: −106 ÷ 2 = −53.

ORG 100h
MOV AL, 96h
SHL AL, 1
RCR AL, 1
SAR AL, 1
ret

Spot the Bug · Shifts and Rotates

Question 19: Multiplication by Shifting: Hidden Assumptions

What is result when the program ends? Which algorithm is applied? Which hidden assumptions does the code rely on?

jmp start
mcand:  db 13   ; multiplicand
mplier: db 5    ; multiplier
result: db 0
start:  mov cl, 4
        mov bl, [mcand+si]
        mov dl, [mplier+si]
checkbit: shr dl, 1
        jnc skip
        add [result+si], bl
skip:   shl bl, 1
        loop checkbit
        ret
Show answer

result = 41h (13 × 5 = 65).

The bits of the multiplier are shifted one by one into CF with SHR; if the bit is 1, the current multiplicand is added to the result, and the multiplicand is doubled with SHL in every round: 5 = 0101b → 13 × 1 + 13 × 4 = 65.

  • SI = 0 is assumed. If SI = 1, BL = 5 (multiplier) and DL = 0 (result) would be read, no addition would happen, and result would stay 0.
  • CH = 0 is assumed: LOOP uses CX but only CL has been loaded. If CH = 1 the loop runs 260 times (the result is still 41h, but for nothing). When emu8086 loads a .COM program it puts the program size in CX; mov cx, 4 should be written.

Multiple Choice · Shifts and Rotates

Question 20: SAR versus IDIV

With AL = F7h (−9), sar al, 1 is executed. What is AL afterwards?

C: SAR shifts right while preserving the sign bit: 1111 0111b → 1111 1011b = FBh = −5; the bit shifted out (1) goes to CF. SAR rounds toward −∞ (−4.5 → −5), whereas IDIV truncates toward zero (−9 ÷ 2 = −4, FCh). 7Bh is the result of SHR and EEh the result of SHL.

Output Question · Example: Array Sum

Question 21: Summing a Word Array

What is vec3 when the program ends? Why do the pointers increase by 2 each time? In which sum is CF = 1, and why is that element wrong?

org 100h
jmp start
vec1 dw 1000, 2000, 40000
vec2 dw 3000, 500, 30000
vec3 dw 3 dup(0)
start:
lea si, vec1
lea bx, vec2
lea di, vec3
mov cx, 3
sum:
mov ax, [si]
add ax, [bx]
mov [di], ax
add si, 2
add bx, 2
add di, 2
loop sum
ret
Show answer

vec3 = 4000, 2500, 4464 (0FA0h, 09C4h, 1170h).

The elements are DW, that is 2 bytes; for a byte array INC would have been enough. In the third round 40000 + 30000 = 70000 > 65535: a carry comes out, CF = 1, and 4464 = 70000 − 65536 remains in AX.

The correct sum needs 17 bits: the carry must be added to a separate word with ADC.

Step-by-Step Trace · BCD Arithmetic

Question 22: Packed BCD with DAA

38 + 45 and then + 29 are computed in packed BCD. What are AL and CF after each DAA?

AL38hAL: two BCD digits (nibbles)0706151413020100CF0AF0

AL = 38h: "38" in packed BCD (upper nibble 3, lower nibble 8).

AL7DhAL: two BCD digits (nibbles)0716151413120110CF0AF0

38h + 45h = 7Dh (binary addition). Lower nibble D > 9: not BCD.

AL83hAL: two BCD digits (nibbles)1706050403021110CF0AF1

DAA: lower nibble greater than 9 → AL + 06h = 83h, AF = 1. The result is "83" = 38 + 45 ✓

ALAChAL: two BCD digits (nibbles)1706150413120100CF0AF0

83h + 29h = ACh. Upper nibble A > 9, lower nibble C > 9.

AL12hAL: two BCD digits (nibbles)0706051403021100CF1AF1

DAA: AL + 66h = 112h → AL = 12h, CF = 1. 83 + 29 = 112: "12", and the hundreds digit is in CF.

ORG 100h
MOV AL, 38h
ADD AL, 45h
DAA
ADD AL, 29h
DAA
RET

Coding Question · BCD Arithmetic

Question 23: Adding Two ASCII Digits

AL and BL each hold an ASCII digit (e.g. '7' and '8'). Write code using AAA that puts the sum into AH (tens) and AL (ones) as two ASCII digits. What is AX for '7' + '8'?

Show answer
MOV AH, 0       ; tens digit = 0
ADD AL, BL      ; 37h + 38h = 6Fh
AAA             ; AH = 01h, AL = 05h
OR  AX, 3030h   ; convert to ASCII

'7' + '8' → AX = 3135h = '1', '5'. AAA looks only at the lower nibble of AL and at AF: because the lower nibble F of 6Fh is > 9, it adds 6, increments AH by 1 and clears the upper nibble of AL. So there is no need to subtract the 30h values first. For '9' + '9' = 72h the lower nibble is 2, but since 9 + 9 produces a carry out of the lower nibble, AF = 1; AAA corrects it again → 3138h ('18').

Output Question · Example: Temperature Conversion

Question 24: Limit of the Temperature Conversion

The program computes f = c × 9 / 5 + 32. What is AL for c = 40 and c = 60? What is OF at c = 60, and why is the result suspect?

org 100h
mov cl, 60     ; c
mov al, 9
imul cl
mov cl, 5
idiv cl
add al, 32
ret
Show answer
cIMULIDIVALOF
40AX = 0168h (360)48h (72)68h = 1040
60AX = 021Ch (540)6Ch (108)8Ch1

108 + 32 = 140 does not fit in a signed byte (at most 127): ADD sets OF = 1. If AL = 8Ch is read as unsigned, 140 °F is correct, but since the program works signed with IMUL/IDIV, 8Ch means −116. The valid range is c ∈ [−71, 53]; word operations are needed for a wider range.

The End

Chapter 6: Arithmetic, Logic and Shift Instructions: Study Questions
Lecture slides: Chapter 6: Arithmetic, Logic and Shift Instructions · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.