8085 Programs: Arithmetic, Loops, Arrays, BCD, Multiply/Divide
Open in the Padhaiko app2.7 Various programs in 8085
Assembly language programming on the Intel 8085 involves translating computational algorithms into compact sequences of machine instructions that manipulate CPU registers, status flags, and memory. This unit develops structured 8085 programs across all core syllabus domains: arithmetic and logic, conditional loops, array indexing, BCD conversion, and software-implemented multiplication and division.
2.7.1 Simple programs with arithmetic and logical operations
Because the 8085 is an 8-bit processor, fundamental 8-bit additions and subtractions operate directly between the Accumulator and a register or memory location. Multi-byte (16-bit) operations chain 8-bit instructions using the Carry flag (ADC, SBB) or the dedicated 16-bit register pair addition instruction (DAD).
16-Bit Addition Using DAD vs. Chained ADC
To add two 16-bit numbers stored in memory:
- Method 1 (DAD): Load the first number into
HL(LHLD), the second intoBCorDE, and executeDAD Rp. This runs in only 10 T-states. - Method 2 (ADC): Add the lower bytes using
ADD, and then add the higher bytes along with the intermediate carry usingADC.
Bitwise Masking and Manipulation
Logical instructions are used to inspect, set, or clear individual bit positions within an 8-bit register:
- Masking Bits (AND): To isolate bit D0 (to test if a number is odd or even), execute
ANI 01H. If the result is zero (Z = 1), the number is even. - Setting Bits (OR): To force bit D5 high without modifying other bits, execute
ORI 20H(0010 0000B). - Clearing Bits (AND): To force bit D6 low, execute
ANI BFH(1011 1111B). - Toggling Bits (XOR): To complement bits D1 and D5, execute
XRI 22H(0010 0010B).
2.7.2 Conditions and loops
A loop repeats a sequence of instructions until a terminating condition is met. In the 8085, loops are constructed using counter registers and conditional jump instructions.
Down-Counter Loop Pattern
The standard loop pattern initializes an 8-bit register (such as register C) with the iteration count N, performs the loop body, decrements the counter with DCR C, and branches back with JNZ (Jump if Not Zero) until C reaches zero:
MVI C, 0AH ; Initialize loop counter to 10
LOOP: ... ; Loop body instructions
DCR C ; Decrement counter (modifies Z flag, CY unaffected)
JNZ LOOP ; Loop if counter != 0Time Delay Loops
Because the execution time of each instruction is deterministically known in T-states, software delay loops provide calibrated time pauses in embedded control applications:
- A single register loop (
DCR C+JNZ LOOP) provides delays up to approximately 1.8 milliseconds at 3 MHz. - A nested 16-bit register pair loop (
DCX Bfollowed byMOV A, BandORA Cto test for zero) generates delays extending to hundreds of milliseconds.
2.7.3 Array and table processing
Arrays and sequential data tables in 8085 memory are traversed using Register Indirect Addressing Mode via the HL register pair acting as memory pointer M.
Traversal Conventions
- Load starting address into
HLusingLXI H, addr16. - Access the current element using
MOV A, M,ADD M, orCMP M. - Advance the pointer to the next array element using
INX H(Increment register pair HL). - Decrement the element counter register (
DCR C) and loop withJNZ.
2.7.4 Decimal - BCD conversion
The 8085 represents numbers in pure binary (hexadecimal) or packed BCD (Binary Coded Decimal, two digits 0–9 per byte).
Binary to Packed BCD Conversion
To convert an 8-bit binary number (0 to 99 decimal) into a packed BCD byte:
- Divide the binary number by 10 (decimal
0AH) using repeated subtraction. - The number of successful subtractions forms the tens digit (higher BCD nibble).
- The remaining value forms the units digit (lower BCD nibble).
- Shift the tens digit 4 times to the left into the upper nibble (
RLCfour times) and logical-OR (ORA) it with the units digit to assemble the final packed BCD byte.
2.7.5 Multiplication and division
The 8085 lacks hardware multiplication (MUL) or hardware division (DIV) opcodes. These operations are implemented through software algorithms:
8-Bit Multiplication by Repeated Addition
Multiplication of two 8-bit numbers (Multiplicand * Multiplier) is achieved by adding the Multiplicand to an accumulator repeatedly, using the Multiplier as a loop counter:
- Multiplicand is added N times, where N = Multiplier.
- The 16-bit product is accumulated in a register pair (e.g.,
HLusingDADorDEusingADD/ADC). - Maximum possible product =
FFH * FFH = 255 * 255 = 65,025 = FE01H, requiring 16 bits of storage.
8-Bit Division by Repeated Subtraction
Division (Dividend / Divisor) is performed by repeatedly subtracting the Divisor from the Dividend until the remaining value is smaller than the Divisor:
- The count of subtractions performed equals the Quotient.
- The value remaining in the accumulator equals the Remainder.
Worked Examples
1
Write an 8085 assembly language program to find the largest 8-bit number in an array of 5 data bytes stored in memory starting at address 2050H. Store the largest number at memory location 2060H. Trace the program execution showing register states.
Algorithm and Register Assignment:
HLpair: Array memory pointer, initialized to2050H.- Register
C: Array size counter, initialized to05H. - Register
A(Accumulator): Holds the current maximum candidate. - Loop logic:
- Load the first byte into Accumulator:
MOV A, M. - Decrement counter:
DCR C. - Point to next element:
INX H. - Compare next element with candidate:
CMP M. - If
A >= M(JNC SKIP), candidate remains larger. - If
A < M, update candidate:MOV A, M. - Loop until all elements are checked (
DCR C,JNZ). - Store result:
STA 2060H.
- Load the first byte into Accumulator:
8085 Assembly Program:
START: LXI H, 2050H ; HL points to start of array
MOV C, M ; Load array count into C
INX H ; Point to first data element
MOV A, M ; Assume first element is maximum
DCR C ; Decrement remaining count
LOOP: INX H ; Point to next element
CMP M ; Compare current max in A with [HL]
JNC AHEAD ; If A >= [HL], carry=0 -> skip update
MOV A, M ; If A < [HL], update max with new element
AHEAD: DCR C ; Decrement element counter
JNZ LOOP ; Repeat until all elements compared
STA 2060H ; Store largest number at 2060H
HLT ; Terminate executionRegister State Trace for Array Data [45H, 98H, 12H, 82H, 3AH] (count 05H stored at 2050H):
- Initially:
A = 45H,HL = 2051H,C = 04H(remaining count after the first element). - Iteration 1:
HL = 2052H, element is98H.CMP M(45H - 98H) setsCY = 1. Update:A = 98H. - Iteration 2:
HL = 2053H, element is12H.CMP M(98H - 12H) leavesCY = 0. No update:A = 98H. - Iteration 3:
HL = 2054H, element is82H.CMP M(98H - 82H) leavesCY = 0. No update:A = 98H. - Iteration 4:
HL = 2055H, element is3AH.CMP M(98H - 3AH) leavesCY = 0. No update:A = 98H. The finalDCR CtakesCfrom01Hto00H, soZ = 1,P = 1andAC = 1(no borrow from bit 4). - Final store:
Memory[2060H] = 98H.
The program loads the initial candidate into A, compares each subsequent array byte using CMP M, updates A whenever CY = 1 (A < M), and stores the maximum 98H at 2060H.
2
Write an 8085 assembly language program to multiply two 8-bit unsigned numbers stored at memory locations 2050H and 2051H. Store the 16-bit product at memory locations 2052H (lower byte) and 2053H (higher byte). Trace the execution when multiplicand is 0AH (10 decimal) and multiplier is 0CH (12 decimal).
Multiplication Algorithm Design:
- Multiplicand is at
2050H(0AH). - Multiplier is at
2051H(0CH). - Product accumulator: 16-bit register pair
HL, initialized to0000H. - Multiplicand is placed in register
E(D = 00Hfor 16-bit addition). - In a loop, add
DEtoHLusingDAD Dand decrement the multiplier counter inC. - Repeat until
C = 0. - Store
L(LSB of product) at2052HandH(MSB of product) at2053H.
8085 Assembly Code:
START: LXI H, 0000H ; Clear 16-bit product in HL
LDA 2050H ; Load multiplicand
MOV E, A ; Place multiplicand in E
MVI D, 00H ; Clear D to form 16-bit DE
LDA 2051H ; Load multiplier
MOV C, A ; Multiplier acts as loop counter in C
CPI 00H ; Check if multiplier is zero
JZ FINISH ; If multiplier is 0, product is 0000H
MULT: DAD D ; 16-bit addition: HL <- HL + DE
DCR C ; Decrement multiplier count
JNZ MULT ; Repeat until multiplier reaches 0
FINISH: SHLD 2052H ; Store L at 2052H, H at 2053H
HLT ; Terminate programExecution Trace for 0AH * 0CH (10 * 12):
- Initial:
DE = 000AH,HL = 0000H,C = 0CH = 12. - The loop executes 12 times:
- Iteration 1:
HL = 000AH - Iteration 2:
HL = 0014H - ...
- Iteration 12:
HL = 12 * 000AH = 0078H(120 decimal).
- Iteration 1:
- The last
DCR CtakesCfrom01Hto00H, givingZ = 1,P = 1,AC = 1;DADproduced no carry, soCY = 0. SHLD 2052Hwrites78Hto2052Hand00Hto2053H.
The product of 0AH and 0CH is computed by 12 repeated 16-bit additions of DE to HL, producing HL = 0078H (120 decimal). SHLD 2052H stores LSB 78H at 2052H and MSB 00H at 2053H.
3
Write an 8085 assembly language program to convert an 8-bit binary number stored at memory location 2050H (value between 0 and 99 decimal) into a 2-digit packed BCD number. Store the packed BCD result at memory location 2051H. Trace the program when the binary input is 47H (71 decimal).
Algorithm for Binary to Packed BCD Conversion:
- Load binary number into Accumulator.
- Initialize tens digit counter:
B = 00H. - Compare Accumulator with 10 decimal (
0AH). - If
A < 0AH, branch to pack nibbles. - If
A >= 0AH, subtract0AH(SUI 0AH), increment tens counterB(INR B), and repeat. - When
A < 0AH, Accumulator holds the units digit. Move units digit to registerC. - Move tens counter
Bto Accumulator and rotate left 4 times (RLCfour times) to place it into upper nibble (D7–D4). - Logical-OR Accumulator with units digit in
C:ORA C. - Store packed BCD byte at
2051H.
8085 Assembly Code:
START: LDA 2050H ; Load binary input into Accumulator
MVI B, 00H ; Clear tens counter
LOOP: CPI 0AH ; Compare Accumulator with 10 (0AH)
JC PACK ; If A < 10, division complete
SUI 0AH ; A <- A - 10
INR B ; Tens counter <- Tens counter + 1
JMP LOOP ; Continue division
PACK: MOV C, A ; Save units digit in C
MOV A, B ; Load tens digit into Accumulator
RLC ; Rotate left 4 times
RLC
RLC
RLC ; Upper nibble now contains tens digit
ORA C ; Combine with units digit in lower nibble
STA 2051H ; Store packed BCD result
HLT ; StopExecution Trace for Input 47H (71 Decimal):
- Input:
47H = 71 decimal. - Subtraction loop subtracts
0AH(10) seven times:- After 7 subtractions:
B = 07H, remainder inA = 01H.
- After 7 subtractions:
PACK:C = 01H(units digit).A = 07Hrotated left 4 times ->70H.70H ORA 01H = 71H.
ORA Cleaves71H = 0111 0001B(four 1s), soP = 1, withS = Z = AC = CY = 0.- Stored at
2051H:71H(valid packed BCD for 71 decimal).
The binary number 47H (71 decimal) is divided by 10 by repeated subtraction, yielding tens digit 07H and units remainder 01H. Packing them produces 71H stored at 2051H.
Exercises
Exercises
Write an 8085 assembly language program to add two 16-bit numbers stored at memory locations 2050H-2051H and 2052H-2053H (lower byte first). Store the 16-bit sum at 2054H-2055H and any carry generated at 2056H.
Algorithm:
- Clear register
Cto record any final carry. - Load the lower byte of the first number from
2050HintoA. - Load the lower byte of the second number from
2052HintoB. - Add the lower bytes:
ADD B. - Store lower byte of sum at
2054H. - Load the higher byte of the first number from
2051HintoA. - Load the higher byte of the second number from
2053HintoB. - Add with carry:
ADC B. - Store higher byte of sum at
2055H. - If carry flag is set, increment register
C(JNC SKIP; INR C). - Store carry byte from register
Cat2056H.
8085 Assembly Code:
START: MVI C, 00H ; Clear carry register C
LDA 2050H ; Load lower byte of number 1
LXI H, 2052H ; Point to lower byte of number 2
ADD M ; Add lower bytes: A <- A + [2052H]
STA 2054H ; Store lower byte of sum
LDA 2051H ; Load higher byte of number 1
LXI H, 2053H ; Point to higher byte of number 2
ADC M ; Add higher bytes with carry: A <- A + [2053H] + CY
STA 2055H ; Store higher byte of sum
JNC NOCARRY ; If no final carry, skip
INR C ; Record carry in C
NOCARRY: MOV A, C
STA 2056H ; Store carry at 2056H
HLT ; TerminateThe program adds lower bytes using ADD M and stores the result at 2054H, then adds higher bytes using ADC M and stores at 2055H. Any final overflow carry is stored at 2056H.
Write an 8085 assembly language program to sort an array of 5 unsigned 8-bit numbers stored in memory starting at 3000H in ascending order using the Bubble Sort algorithm.
Bubble Sort Algorithm in 8085:
- Outer loop runs
N - 1 = 4passes. - Inner loop compares adjacent pairs
[HL]and[HL + 1]. - If
[HL] > [HL + 1], swap the two elements in memory using temporary registerD. - Repeat until all passes complete.
8085 Assembly Code:
START: MVI B, 04H ; Outer loop pass counter (N - 1 = 4)
OUTER: LXI H, 3000H ; Initialize array pointer
MVI C, 04H ; Inner loop comparison counter
INNER: MOV A, M ; Load current element into A
INX H ; Point to next adjacent element
CMP M ; Compare [HL - 1] with [HL]
JC NOSWAP ; If [HL - 1] < [HL], already in order
JZ NOSWAP ; If equal, in order
MOV D, M ; Swap: Save [HL] in D
MOV M, A ; Move larger element into [HL]
DCX H ; Point back to previous location
MOV M, D ; Place smaller element into [HL - 1]
INX H ; Restore pointer to next element
NOSWAP: DCR C ; Decrement inner counter
JNZ INNER ; Continue inner pass
DCR B ; Decrement outer pass counter
JNZ OUTER ; Repeat passes until sorted
HLT ; FinishedThe program uses a nested loop structure (outer counter B = 4, inner counter C = 4) with pointer HL comparing adjacent bytes via CMP M. If an out-of-order pair is detected, elements are swapped using register D.
Write an 8085 assembly language program to perform 8-bit division of a dividend stored at 2050H by a divisor stored at 2051H using repeated subtraction. Store the quotient at 2052H and remainder at 2053H.
Algorithm:
- Load dividend from
2050Hinto Accumulator. - Load divisor from
2051Hinto registerB. - Clear register
Cto serve as the quotient counter (C = 00H). - Compare Accumulator with divisor:
CMP B. - If
A < B(JC DONE), division is complete; remaining value inAis the remainder. - If
A >= B, subtract divisor:SUB B. - Increment quotient counter:
INR C. - Jump back to step 4.
- Store quotient from
Cat2052Hand remainder fromAat2053H.
8085 Assembly Code:
START: LDA 2050H ; Load dividend into Accumulator
LXI H, 2051H ; Point to divisor
MOV B, M ; Load divisor into B
MVI C, 00H ; Initialize quotient counter to 0
DIVLOOP: CMP B ; Compare Accumulator with divisor
JC DIVDONE ; If A < B, quotient is complete
SUB B ; A <- A - B
INR C ; Quotient <- Quotient + 1
JMP DIVLOOP ; Repeat subtraction
DIVDONE: STA 2053H ; Store remainder at 2053H
MOV A, C ; Load quotient into Accumulator
STA 2052H ; Store quotient at 2052H
HLT ; TerminateThe program repeatedly subtracts the divisor in B from the dividend in A until A < B, accumulating the quotient in register C (stored at 2052H) and leaving the remainder in A (stored at 2053H).
Write an 8085 assembly language program to transfer a block of 10 data bytes from starting memory address 2000H to destination memory address 3000H. If a data byte is strictly less than 50H, set bit D5 before storing; otherwise, clear bit D6 before storing.
Algorithm and Bit Masking Logic:
- Source pointer:
HL = 2000H. - Destination pointer:
DE = 3000H. - Counter:
C = 0AH(10 elements). - For each byte:
- Load byte into Accumulator:
MOV A, M. - Compare with
50H:CPI 50H. - If
A < 50H(JC SET_D5):- Set bit D5 using
ORI 20H(0010 0000B). - Jump to
STORE.
- Set bit D5 using
- If
A >= 50H:- Clear bit D6 using
ANI BFH(1011 1111B).
- Clear bit D6 using
STORE: Store byte into destination memory usingSTAX D.- Increment pointers
INX H,INX D, decrement counterDCR C, and repeat.
- Load byte into Accumulator:
8085 Assembly Code:
START: LXI H, 2000H ; HL points to source block
LXI D, 3000H ; DE points to destination block
MVI C, 0AH ; Transfer counter = 10 bytes
TLOOP: MOV A, M ; Read byte from source
CPI 50H ; Compare byte with 50H
JC SETD5 ; If A < 50H, carry=1 -> branch
ANI BFH ; Else A >= 50H: clear bit D6 (AND with 10111111B)
JMP STORE
SETD5: ORI 20H ; If A < 50H: set bit D5 (OR with 00100000B)
STORE: STAX D ; Write byte to destination [DE]
INX H ; Increment source pointer
INX D ; Increment destination pointer
DCR C ; Decrement counter
JNZ TLOOP ; Repeat until all 10 bytes transferred
HLT ; TerminateThe program reads each byte via HL, compares with 50H, executes ORI 20H to set D5 if A < 50H, or ANI BFH to clear D6 if A >= 50H, and writes the modified byte to [DE].
Write an 8085 assembly language program to count the number of even numbers in an array of 20 bytes stored in memory starting from address 2500H. Store the final count of even numbers at memory location 2550H.
Even Number Testing Algorithm: In binary, any even integer has its least significant bit D0 = 0; any odd integer has D0 = 1.
- Array pointer:
HL = 2500H. - Loop counter:
C = 14H(20 in decimal). - Even count register:
B = 00H. - In each iteration:
- Load array byte into Accumulator:
MOV A, M. - Test bit D0 using
ANI 01H. - If result is non-zero (
JNZ IS_ODD), the number is odd; do not increment even count. - If result is zero, the number is even; increment counter
B(INR B). - Advance pointer
INX H, decrement loop counterDCR C, and continue untilC = 0. - Store result from
Bat2550H.
- Load array byte into Accumulator:
8085 Assembly Code:
START: LXI H, 2500H ; Initialize array pointer
MVI C, 14H ; Loop counter = 20 decimal (14H)
MVI B, 00H ; Initialize even counter to 0
E_LOOP: MOV A, M ; Load array byte into Accumulator
ANI 01H ; Test bit D0 (AND with 00000001B)
JNZ IS_ODD ; If bit D0 is 1, number is odd -> skip
INR B ; Bit D0 is 0 -> even number, increment B
IS_ODD: INX H ; Advance to next array element
DCR C ; Decrement array counter
JNZ E_LOOP ; Loop until all 20 elements inspected
MOV A, B ; Load even count into Accumulator
STA 2550H ; Store result at 2550H
HLT ; StopThe program tests bit D0 of each byte with ANI 01H. If D0 is 0 (Z = 1), the number is even and register B increments. After 20 elements, the even count is stored at 2550H.