PadhaikoAll subjects

8086 Programs: Arithmetic, Strings, I/O, Number Display

Microprocessors (ENEX 201)

Open in the Padhaiko app

3.13 Various programs in 8086

Writing complete 8086 assembly programs combines segment setup (.MODEL SMALL, .STACK 100H, .DATA, .CODE), addressing modes, loop/branch control, and DOS/BIOS interrupt services (INT 21H, INT 10H).

3.13.1 Simple programs for arithmetic, logical, string and input/output

A basic .EXE program initializes DS with @DATA, performs register/memory operations, and exits cleanly using INT 21H with AH = 4CH.

8086 Program: 16-Bit Multiplication and Bit Masking8086 ASM
.MODEL SMALL
.STACK 100H
.DATA
    NUM1 DW 0120H       ; 288 decimal
    NUM2 DW 0010H       ; 16 decimal
    PROD DD ?           ; 32-bit storage for DX:AX
    MASK_VAL DW 00FFH
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV AX, NUM1        ; AX = 0120H
    MUL NUM2            ; DX:AX = 0120H * 0010H = 0000:1200H
    MOV WORD PTR PROD, AX
    MOV WORD PTR PROD+2, DX

    AND AX, MASK_VAL    ; Keep only lower 8 bits of result (00H)
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

3.13.2 Conditions and loops

Decision making uses CMP op1, op2 (computes op1 - op2 and sets flags without altering op1) followed by conditional jumps:

3.13.3 Array and string processing

Arrays and strings are processed using index/base pointers (SI, DI, BX) or dedicated 8086 string instructions paired with CLD (DF = 0, forward scan) and REP prefixes:

3.13.4 ASCII and decimal numbers operation

Keyboard input (INT 21H AH = 01H/0AH) delivers ASCII digits '0'–'9' (30H–39H), and screen output requires ASCII digits. Converting between binary/BCD values and ASCII uses simple offsets (30H) or dedicated 8086 BCD adjustment instructions:

8086 ASCII and BCD Adjustment Instructions
InstructionOperand FormatPurpose & Conversion Rule
AAAUnpacked BCD (after ADD)Adjusts AL to valid unpacked BCD (0–9); if (AL & 0FH) > 9 or AF = 1, adds 6 to AL, increments AH, sets AF = CF = 1, clears upper nibble of AL.
AASUnpacked BCD (after SUB)Adjusts AL after subtraction of two unpacked BCD/ASCII digits (-6 from AL, -1 from AH on borrow).
AAMUnpacked BCD (after MUL)Converts binary product in AL (0–99) into two unpacked BCD digits: AH = AL / 10, AL = AL % 10.
AADUnpacked BCD (before DIV)Converts two unpacked BCD digits in AH (tens) and AL (ones) into binary in AL = AH * 10 + AL, AH = 0 before DIV.
DAAPacked BCD (after ADD)Adjusts AL containing two packed BCD nibbles (00–99) after binary ADD/ADC.
DASPacked BCD (after SUB)Adjusts AL containing two packed BCD nibbles after binary SUB/SBB.

3.13.5 Displaying numbers in decimal, binary and hexadecimal formats

To print a 16-bit binary integer stored in AX or BX on the DOS console, extract its digits in the target radix and convert each digit to ASCII:

Definition

Algorithms for Decimal, Binary, and Hexadecimal Display

  1. Decimal Display (DIV 10 + Stack Reversal): Repeatedly divide the 16-bit unsigned number in AX by 10 (XOR DX, DX then DIV BX with BX = 10). Each remainder in DL (0–9) is the next least-significant digit, so push DX onto the stack and increment a digit counter CX. Once AX = 0, pop each digit CX times, add 30H ('0'), and print with INT 21H AH = 02H.
  2. Binary Display (SHL + Carry Print): Load CX = 16. On each iteration, shift BX left by 1 bit (SHL BX, 1) so the current MSB moves into Carry Flag CF. Set DL = '0' (30H), execute ADC DL, 0 (turns '0' into '1' if CF = 1), and print DL via INT 21H AH = 02H.
  3. Hexadecimal Display (ROL 4 bits + Nibble-to-ASCII): Load CX = 4 (four hex nibbles in 16 bits). Rotate BX left by 4 bits (PUSH CX, MOV CL, 4, ROL BX, CL, POP CX) so the top nibble enters bits 3–0 of BL. Copy BL to DL, mask AND DL, 0FH. If DL <= 9, add 30H ('0'–'9'); otherwise add 37H ('A'–'F', since 10 + 37H = 41H = 'A'), and print via INT 21H AH = 02H.
Example 1

1 (6 marks)

Write an 8086 assembly program to compute the factorial of N = 5 stored in memory variable NUM, and display the result in decimal on the screen. Trace AX, CX, and the stack digits during the DIV 10 loop.

Factorial Loop Design:

  • Initialize AX = 1 and CX = NUM (5).
  • If CX <= 1, factorial is 1. Otherwise loop MUL CX (AX = AX * CX) while LOOP decrements CX:
    • CX = 5: AX = 1 * 5 = 5 (0005H)
    • CX = 4: AX = 5 * 4 = 20 (0014H)
    • CX = 3: AX = 20 * 3 = 60 (003CH)
    • CX = 2: AX = 60 * 2 = 120 (0078H)
    • CX = 1: AX = 120 * 1 = 120 (0078H), LOOP ends with CX = 0.
Registers After Factorial Loop (N = 5)
AX0078H
CX0000H
DX0000H

Complete 8086 Program:

8086 Factorial with Decimal Display8086 ASM
.MODEL SMALL
.STACK 100H
.DATA
    NUM DB 05H
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV CL, NUM         ; Load N = 5
    MOV CH, 00H         ; CX = 0005H
    MOV AX, 0001H       ; Result accumulator = 1
    JCXZ PRINT_RES      ; 0! = 1

FACT_LOOP:
    MUL CX              ; DX:AX = AX * CX
    LOOP FACT_LOOP      ; Decrement CX; repeat until CX = 0

PRINT_RES:
    MOV BX, 10          ; Divisor = 10 for decimal conversion
    XOR CX, CX          ; Digit counter = 0

DIV_LOOP:
    XOR DX, DX          ; Clear DX before 16-bit DIV
    DIV BX              ; AX = quotient, DX = remainder (0..9)
    PUSH DX             ; Save digit on stack
    INC CX              ; Count pushed digits
    CMP AX, 0000H
    JNZ DIV_LOOP

DISP_LOOP:
    POP DX              ; Pop digits in most-significant-first order
    ADD DL, 30H         ; Convert 0..9 to ASCII '0'..'9'
    MOV AH, 02H
    INT 21H
    LOOP DISP_LOOP

    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

Tracing DIV 10 Stack Pushes for AX = 120 (0078H):

  • Pass 1: 120 / 10 -> AX = 12, DX = 0 -> push 0, CX = 1.
  • Pass 2: 12 / 10 -> AX = 1, DX = 2 -> push 2, CX = 2.
  • Pass 3: 1 / 10 -> AX = 0, DX = 1 -> push 1, CX = 3.
  • Popping 3 times prints '1', '2', '0' -> displays 120.
Answer:

The factorial loop produces AX = 0078H (120), and the DIV 10 + stack reversal routine pushes 0, 2, 1 and pops '1', '2', '0' to display 120.

Example 2

2 (6 marks)

(Institute of Engineering Exam Style) Write an 8086 assembly program that accepts a string from the user, clears the screen, displays each character on a new line, and displays the total length of the string (assume length < 100). Trace key registers for input "8086".

Program Logic:

  1. Read string via INT 21H AH = 0AH into BUF (MAX=50, ACT=?, TXT 50 DUP(?)).
  2. Clear the screen via INT 10H AH = 06H (AL=0, BH=07H, CX=0000H, DX=184FH) and home cursor via AH = 02H (DX=0000H).
  3. Load CL = ACT, CH = 0, SI = OFFSET TXT. Loop CX times: print [SI] via AH = 02H, then print 0DH (CR) and 0AH (LF), and INC SI.
  4. Print the length (ACT) by dividing AL = ACT by 10 (MOV BL, 10, XOR AH, AH, DIV BL): AL has tens digit, AH has ones digit; add 30H to both and display.

Complete 8086 Program:

String Per-Line Display and Total Length8086 ASM
.MODEL SMALL
.STACK 100H
.DATA
    PROMPT_MSG DB 'Enter string: $'
    LEN_MSG    DB 'Length: $'
    BUF        DB 50
    ACT        DB ?
    TXT        DB 50 DUP(?)
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    LEA DX, PROMPT_MSG
    MOV AH, 09H
    INT 21H

    LEA DX, BUF
    MOV AH, 0AH
    INT 21H

    ; Clear screen and reset cursor to (0,0)
    MOV AX, 0600H
    MOV BH, 07H
    MOV CX, 0000H
    MOV DX, 184FH
    INT 10H

    MOV AH, 02H
    MOV BH, 00H
    MOV DX, 0000H
    INT 10H

    ; Print each character on a new line
    MOV CL, ACT
    MOV CH, 00H
    JCXZ SHOW_LEN
    LEA SI, TXT

CHAR_LOOP:
    MOV DL, [SI]
    MOV AH, 02H
    INT 21H
    MOV DL, 0DH         ; Carriage Return
    INT 21H
    MOV DL, 0AH         ; Line Feed
    INT 21H
    INC SI
    LOOP CHAR_LOOP

SHOW_LEN:
    LEA DX, LEN_MSG
    MOV AH, 09H
    INT 21H

    MOV AL, ACT         ; Divide length (0..49) by 10
    XOR AH, AH
    MOV BL, 10
    DIV BL              ; AL = tens, AH = ones
    MOV BX, AX          ; Save digits in BL (tens) and BH (ones)

    MOV DL, BL
    ADD DL, 30H
    MOV AH, 02H
    INT 21H

    MOV DL, BH
    ADD DL, 30H
    MOV AH, 02H
    INT 21H

    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN
Registers at SHOW_LEN for Input '8086' (ACT = 4)
AL00H
AH04H
BL00H
BH04H
CL00H
[ACT]04H
[TXT]38H
Answer:

For "8086", ACT = 04H; the loop prints '8', '0', '8', '6' on consecutive lines, and DIV 10 yields BL = 00H ('0') and BH = 04H ('4'), displaying Length: 04.

Example 3

3 (5 marks)

Write two reusable 8086 procedures DISP_BIN and DISP_HEX that display the 16-bit value in BX in binary and hexadecimal formats, and trace the output characters produced when BX = 012CH.

DISP_BIN and DISP_HEX Procedures:

Binary and Hexadecimal Display Procedures8086 ASM
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
    MOV BX, 012CH
    CALL DISP_BIN
    MOV DL, ' '
    MOV AH, 02H
    INT 21H
    MOV BX, 012CH       ; Reload BX since DISP_BIN rotated/shifted it
    CALL DISP_HEX

    MOV AH, 4CH
    INT 21H
MAIN ENDP

DISP_BIN PROC NEAR
    MOV CX, 16
BIN_NEXT:
    SHL BX, 1           ; Shift MSB into Carry Flag (CF)
    MOV DL, '0'
    ADC DL, 0           ; DL = '0' + CF ('0' or '1')
    MOV AH, 02H
    INT 21H
    LOOP BIN_NEXT
    RET
DISP_BIN ENDP

DISP_HEX PROC NEAR
    MOV CX, 4           ; 4 hex nibbles in 16 bits
HEX_NEXT:
    PUSH CX
    MOV CL, 4
    ROL BX, CL          ; Bring top nibble into bits 3..0 of BL
    POP CX
    MOV DL, BL
    AND DL, 0FH         ; Isolate lowest nibble (0..15)
    CMP DL, 9
    JBE IS_DIGIT
    ADD DL, 07H         ; Offset for 'A'..'F' (0AH + 37H = 41H)
IS_DIGIT:
    ADD DL, 30H         ; Convert to ASCII
    MOV AH, 02H
    INT 21H
    LOOP HEX_NEXT
    RET
DISP_HEX ENDP
END MAIN

Tracing BX = 012CH (0000 0001 0010 1100B):

  • DISP_BIN shifts out all 16 bits from MSB to LSB: 0000000100101100.
  • DISP_HEX rotates BX left 4 bits at a time:
    • Rotation 1: BX = 12C0H -> nibble 0H -> '0' (30H)
    • Rotation 2: BX = 2C01H -> nibble 1H -> '1' (31H)
    • Rotation 3: BX = C012H -> nibble 2H -> '2' (32H)
    • Rotation 4: BX = 012CH -> nibble CH (12 > 9) -> 12 + 37H = 43H ('C').
DISP_HEX Nibble Rotation Trace for 012CH
BX012CH
DL43H
CX0000H
Answer:

DISP_BIN prints 0000000100101100 and DISP_HEX prints 012C.

Exercises

Question 1

Write an 8086 assembly program to compute the sum and average of an array of five 16-bit unsigned numbers ARR DW 10, 20, 30, 40, 50 and store them in TOTAL and AVG. Give the final values of AX, TOTAL, and AVG.

8086 Array Sum and Average Program:

Sum and Average of Five 16-Bit Numbers8086 ASM
.MODEL SMALL
.STACK 100H
.DATA
    ARR   DW 10, 20, 30, 40, 50
    COUNT DW 5
    TOTAL DW ?
    AVG   DW ?
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    LEA SI, ARR
    MOV CX, COUNT
    XOR AX, AX          ; Sum = 0
SUM_LOOP:
    ADD AX, [SI]        ; Add current word element
    ADD SI, 2           ; Advance pointer by 2 bytes (DW)
    LOOP SUM_LOOP

    MOV TOTAL, AX       ; Store sum = 150 (0096H)
    XOR DX, DX          ; Clear DX for 16-bit unsigned DIV
    DIV COUNT           ; AX = 150 / 5 = 30 (001EH)
    MOV AVG, AX

    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

Trace Result:

  • TOTAL = 10 + 20 + 30 + 40 + 50 = 150 (0096H).
  • After DIV COUNT (150 / 5): AX = 30 (001EH), DX = 0, AVG = 001EH.
Final Registers and Memory
AX001EH
DX0000H
CX0000H
[TOTAL]0096H
[AVG]001EH
Answer:

TOTAL = 150 (0096H) and AVG = 30 (001EH), with AX = 001EH.

Question 2

Write an 8086 assembly program to find the largest 8-bit unsigned number in the array LIST DB 14H, 7AH, 3CH, 92H, 51H and store it in MAX_VAL. Trace AL through each comparison.

8086 Largest-in-Array Program:

Find Largest Byte in Array8086 ASM
.MODEL SMALL
.STACK 100H
.DATA
    LIST    DB 14H, 7AH, 3CH, 92H, 51H
    MAX_VAL DB ?
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    LEA SI, LIST
    MOV AL, [SI]        ; Assume first element (14H) is max
    MOV CX, 4           ; Remaining 4 elements to compare
NEXT_EL:
    INC SI
    CMP AL, [SI]        ; Compare current max AL with [SI]
    JAE SKIP            ; Keep AL if AL >= [SI] (unsigned)
    MOV AL, [SI]        ; Update AL with larger element
SKIP:
    LOOP NEXT_EL

    MOV MAX_VAL, AL
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

Trace of AL Across Elements:

  • Start: AL = 14H.
  • Compare 14H vs 7AH: 14H < 7AH -> AL = 7AH.
  • Compare 7AH vs 3CH: 7AH >= 3CH -> AL = 7AH.
  • Compare 7AH vs 92H: 7AH < 92H -> AL = 92H.
  • Compare 92H vs 51H: 92H >= 51H -> AL = 92H.
Final State
AL92H
CX0000H
[MAX_VAL]92H
Answer:

MAX_VAL = 92H, with AL updating 14H -> 7AH -> 92H.

Question 3

Write an 8086 assembly program to count the number of uppercase vowels (A, E, I, O, U) in the '\$'-terminated string STR DB 'MICROPROCESSOR\$' and store the count in VOWELS. What value is stored in VOWELS?

Vowel-Counting Program:

Count Vowels in a String8086 ASM
.MODEL SMALL
.STACK 100H
.DATA
    STR_TXT DB 'MICROPROCESSOR$'
    VOWELS  DB 0
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    LEA SI, STR_TXT
    XOR BL, BL          ; BL = vowel counter = 0
SCAN:
    MOV AL, [SI]
    CMP AL, '$'         ; End of string?
    JE  DONE
    CMP AL, 'A'
    JE  IS_VOWEL
    CMP AL, 'E'
    JE  IS_VOWEL
    CMP AL, 'I'
    JE  IS_VOWEL
    CMP AL, 'O'
    JE  IS_VOWEL
    CMP AL, 'U'
    JNE NEXT_CH
IS_VOWEL:
    INC BL
NEXT_CH:
    INC SI
    JMP SCAN
DONE:
    MOV VOWELS, BL
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

Counting Vowels in "MICROPROCESSOR": Scanning letter by letter: M, I (1), C, R, O (2), P, R, O (3), C, E (4), S, S, O (5), R. Total vowels = 5 (05H).

Final Counter State
BL05H
[VOWELS]05H
Answer:

VOWELS = 05H (5 vowels: I, O, O, E, O).

Question 4

Determine the contents of AX and CF after each sequence: (a) XOR AH, AH / MOV AL, '9' / ADD AL, '6' / AAA (b) MOV AL, 48H / ADD AL, 37H / DAA

Part (a): AAA After Adding ASCII '9' (39H) and '6' (36H):

  • AL = 39H + 36H = 6FH (AF = 0, CF = 0).
  • Lower nibble of AL is FH (15 > 9). AAA adds 6 to AL (6FH + 06H = 75H), zeroes the upper nibble of AL (AL = 05H), increments AH (AH = 01H), and sets AF = 1, CF = 1.
  • Result: AX = 0105H (unpacked BCD 15), CF = 1.

Part (b): DAA After Adding Packed BCD 48H + 37H:

  • Binary sum: 48H + 37H = 7FH (AF = 0, CF = 0).
  • Since lower nibble FH > 9, DAA adds 06H: 7FH + 06H = 85H.
  • Upper nibble 8H <= 9 and CF = 0, so no further +60H adjustment occurs.
  • Result: AL = 85H (packed BCD 85), CF = 0.
Final State for (a)
AX0105H
AF1
CF1
Final State for (b)
AL85H
AF1
CF0
Answer:

(a) AX = 0105H, CF = 1 (unpacked BCD 15); (b) AL = 85H, CF = 0 (packed BCD 85).

Question 5

Trace the DISP_HEX procedure (from Example 3) step by step when BX = 2A9FH, showing BX after each ROL BX, 4, the masked nibble DL, and the final ASCII character printed for all 4 nibbles.

4-Iteration DISP_HEX Trace for BX = 2A9FH:

  • Iteration 1 (CX = 4): ROL BX, 4 -> BX = A9F2H. DL = 02H (<= 9) -> + 30H = 32H ('2').
  • Iteration 2 (CX = 3): ROL BX, 4 -> BX = 9F2AH. DL = 0AH (10 > 9) -> + 37H = 41H ('A').
  • Iteration 3 (CX = 2): ROL BX, 4 -> BX = F2A9H. DL = 09H (<= 9) -> + 30H = 39H ('9').
  • Iteration 4 (CX = 1): ROL BX, 4 -> BX = 2A9FH. DL = 0FH (15 > 9) -> + 37H = 46H ('F').

Final Register State: After 4 rotations of 4 bits (16 bits total), BX returns to its original value 2A9FH, and the console displays 2A9F.

State After DISP_HEX Completes
BX2A9FH
DL46H
CX0000H
Answer:

DISP_HEX outputs '2' (32H), 'A' (41H), '9' (39H), 'F' (46H) -> 2A9F, restoring BX = 2A9FH.

Past exam questions for Microprocessors

← Interrupt Services: INT 21H and INT 10HMemory Hierarchy & I/O/Memory Interfacing with 8085 →