8086 Programs: Arithmetic, Strings, I/O, Number Display
Open in the Padhaiko app3.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.
- Arithmetic & Logical Operations:
ADD,SUB,MUL(unsignedAX * src -> DX:AX),DIV(unsignedDX:AX / src -> AXquotient,DXremainder),AND(bit clear/mask),OR(bit set),XOR(bit toggle/clear register viaXOR AX, AX). - String Output: Place
'\$'(24H) at the end of a string in.DATA, load its offset intoDXviaLEA DX, MSG, and callINT 21HwithAH = 09H.
.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 MAIN3.13.2 Conditions and loops
Decision making uses CMP op1, op2 (computes op1 - op2 and sets flags without altering op1) followed by conditional jumps:
- Unsigned Comparisons:
JA/JNBE(above),JAE/JNB,JB/JNAE/JC(below),JBE/JNA,JE/JZ(equal),JNE/JNZ(not equal). - Signed Comparisons:
JG/JNLE(greater),JGE/JNL,JL/JNGE(less),JLE/JNG. - Counted Loops: The
LOOP labelinstruction automatically decrementsCXby 1 and jumps tolabelifCX != 0. Always guard againstCX = 0(JCXZ) before entering aLOOP, becauseLOOPdecrementsCXfirst (0000H - 1 = FFFFH).
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:
- Index-Loop Pattern: Point
SItoOFFSET ARRAY, setCXto element count, process[SI], advanceINC SI(for bytes) orADD SI, 2(for words), andLOOP. - String Primitives:
MOVSB/MOVSWcopies[DS:SI]to[ES:DI];LODSBloads[DS:SI]intoAL;STOSBstoresALto[ES:DI];CMPSBcompares[DS:SI]with[ES:DI];SCASBscans[ES:DI]forAL.
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:
| Instruction | Operand Format | Purpose & Conversion Rule |
|---|---|---|
AAA | Unpacked 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. |
AAS | Unpacked BCD (after SUB) | Adjusts AL after subtraction of two unpacked BCD/ASCII digits (-6 from AL, -1 from AH on borrow). |
AAM | Unpacked BCD (after MUL) | Converts binary product in AL (0–99) into two unpacked BCD digits: AH = AL / 10, AL = AL % 10. |
AAD | Unpacked 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. |
DAA | Packed BCD (after ADD) | Adjusts AL containing two packed BCD nibbles (00–99) after binary ADD/ADC. |
DAS | Packed 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:
Algorithms for Decimal, Binary, and Hexadecimal Display
- Decimal Display (
DIV 10+ Stack Reversal): Repeatedly divide the 16-bit unsigned number inAXby10(XOR DX, DXthenDIV BXwithBX = 10). Each remainder inDL(0–9) is the next least-significant digit, so pushDXonto the stack and increment a digit counterCX. OnceAX = 0, pop each digitCXtimes, add30H('0'), and print withINT 21H AH = 02H. - Binary Display (
SHL+ Carry Print): LoadCX = 16. On each iteration, shiftBXleft by 1 bit (SHL BX, 1) so the current MSB moves into Carry FlagCF. SetDL = '0'(30H), executeADC DL, 0(turns'0'into'1'ifCF = 1), and printDLviaINT 21H AH = 02H. - Hexadecimal Display (
ROL4 bits + Nibble-to-ASCII): LoadCX = 4(four hex nibbles in 16 bits). RotateBXleft by 4 bits (PUSH CX,MOV CL, 4,ROL BX, CL,POP CX) so the top nibble enters bits3–0ofBL. CopyBLtoDL, maskAND DL, 0FH. IfDL <= 9, add30H('0'–'9'); otherwise add37H('A'–'F', since10 + 37H = 41H = 'A'), and print viaINT 21H AH = 02H.
1
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 = 1andCX = NUM(5). - If
CX <= 1, factorial is1. Otherwise loopMUL CX(AX = AX * CX) whileLOOPdecrementsCX: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),LOOPends withCX = 0.
Complete 8086 Program:
.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 MAINTracing DIV 10 Stack Pushes for AX = 120 (0078H):
- Pass 1:
120 / 10->AX = 12,DX = 0-> push0,CX = 1. - Pass 2:
12 / 10->AX = 1,DX = 2-> push2,CX = 2. - Pass 3:
1 / 10->AX = 0,DX = 1-> push1,CX = 3. - Popping
3times prints'1','2','0'-> displays120.
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.
2
(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:
- Read string via
INT 21H AH = 0AHintoBUF(MAX=50, ACT=?, TXT 50 DUP(?)). - Clear the screen via
INT 10H AH = 06H(AL=0, BH=07H, CX=0000H, DX=184FH) and home cursor viaAH = 02H(DX=0000H). - Load
CL = ACT,CH = 0,SI = OFFSET TXT. LoopCXtimes: print[SI]viaAH = 02H, then print0DH(CR) and0AH(LF), andINC SI. - Print the length (
ACT) by dividingAL = ACTby10(MOV BL, 10,XOR AH, AH,DIV BL):ALhas tens digit,AHhas ones digit; add30Hto both and display.
Complete 8086 Program:
.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 MAINFor "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.
3
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:
.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 MAINTracing BX = 012CH (0000 0001 0010 1100B):
DISP_BINshifts out all 16 bits from MSB to LSB:0000000100101100.DISP_HEXrotatesBXleft 4 bits at a time:- Rotation 1:
BX = 12C0H-> nibble0H->'0'(30H) - Rotation 2:
BX = 2C01H-> nibble1H->'1'(31H) - Rotation 3:
BX = C012H-> nibble2H->'2'(32H) - Rotation 4:
BX = 012CH-> nibbleCH(12 > 9) ->12 + 37H = 43H('C').
- Rotation 1:
DISP_BIN prints 0000000100101100 and DISP_HEX prints 012C.
Exercises
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:
.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 MAINTrace Result:
TOTAL = 10 + 20 + 30 + 40 + 50 = 150(0096H).- After
DIV COUNT(150 / 5):AX = 30(001EH),DX = 0,AVG = 001EH.
TOTAL = 150 (0096H) and AVG = 30 (001EH), with AX = 001EH.
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:
.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 MAINTrace of AL Across Elements:
- Start:
AL = 14H. - Compare
14Hvs7AH:14H < 7AH->AL = 7AH. - Compare
7AHvs3CH:7AH >= 3CH->AL = 7AH. - Compare
7AHvs92H:7AH < 92H->AL = 92H. - Compare
92Hvs51H:92H >= 51H->AL = 92H.
MAX_VAL = 92H, with AL updating 14H -> 7AH -> 92H.
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:
.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 MAINCounting 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).
VOWELS = 05H (5 vowels: I, O, O, E, O).
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
ALisFH(15 > 9).AAAadds6toAL(6FH + 06H = 75H), zeroes the upper nibble ofAL(AL = 05H), incrementsAH(AH = 01H), and setsAF = 1,CF = 1. - Result:
AX = 0105H(unpacked BCD15),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,DAAadds06H:7FH + 06H = 85H. - Upper nibble
8H <= 9andCF = 0, so no further+60Hadjustment occurs. - Result:
AL = 85H(packed BCD85),CF = 0.
(a) AX = 0105H, CF = 1 (unpacked BCD 15); (b) AL = 85H, CF = 0 (packed BCD 85).
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.
DISP_HEX outputs '2' (32H), 'A' (41H), '9' (39H), 'F' (46H) -> 2A9F, restoring BX = 2A9FH.