PadhaikoAll subjects

8085 Programs: Arithmetic, Loops, Arrays, BCD, Multiply/Divide

Microprocessors (ENEX 201)

Open in the Padhaiko app

2.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:

Bitwise Masking and Manipulation

Logical instructions are used to inspect, set, or clear individual bit positions within an 8-bit register:


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:

Text
        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 != 0

Time 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:


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

  1. Load starting address into HL using LXI H, addr16.
  2. Access the current element using MOV A, M, ADD M, or CMP M.
  3. Advance the pointer to the next array element using INX H (Increment register pair HL).
  4. Decrement the element counter register (DCR C) and loop with JNZ.

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:

  1. Divide the binary number by 10 (decimal 0AH) using repeated subtraction.
  2. The number of successful subtractions forms the tens digit (higher BCD nibble).
  3. The remaining value forms the units digit (lower BCD nibble).
  4. Shift the tens digit 4 times to the left into the upper nibble (RLC four 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:

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:


Worked Examples

Example 1

1 (8 marks)

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:

  • HL pair: Array memory pointer, initialized to 2050H.
  • Register C: Array size counter, initialized to 05H.
  • Register A (Accumulator): Holds the current maximum candidate.
  • Loop logic:
    1. Load the first byte into Accumulator: MOV A, M.
    2. Decrement counter: DCR C.
    3. Point to next element: INX H.
    4. Compare next element with candidate: CMP M.
    5. If A >= M (JNC SKIP), candidate remains larger.
    6. If A < M, update candidate: MOV A, M.
    7. Loop until all elements are checked (DCR C, JNZ).
    8. Store result: STA 2060H.

8085 Assembly Program:

Find Maximum in Array8085 ASM
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 execution

Register 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 is 98H. CMP M (45H - 98H) sets CY = 1. Update: A = 98H.
  • Iteration 2: HL = 2053H, element is 12H. CMP M (98H - 12H) leaves CY = 0. No update: A = 98H.
  • Iteration 3: HL = 2054H, element is 82H. CMP M (98H - 82H) leaves CY = 0. No update: A = 98H.
  • Iteration 4: HL = 2055H, element is 3AH. CMP M (98H - 3AH) leaves CY = 0. No update: A = 98H. The final DCR C takes C from 01H to 00H, so Z = 1, P = 1 and AC = 1 (no borrow from bit 4).
  • Final store: Memory[2060H] = 98H.
Registers After Finding Maximum
A98H
C00H
H20H
L55H
S0
Z1
AC1
P1
CY0
[2060H]98H
Answer:

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.

Example 2

2 (8 marks)

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 to 0000H.
  • Multiplicand is placed in register E (D = 00H for 16-bit addition).
  • In a loop, add DE to HL using DAD D and decrement the multiplier counter in C.
  • Repeat until C = 0.
  • Store L (LSB of product) at 2052H and H (MSB of product) at 2053H.

8085 Assembly Code:

8-Bit Multiplication via DAD8085 ASM
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 program

Execution 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).
  • The last DCR C takes C from 01H to 00H, giving Z = 1, P = 1, AC = 1; DAD produced no carry, so CY = 0.
  • SHLD 2052H writes 78H to 2052H and 00H to 2053H.
Registers After Multiplication
A0CH
D00H
E0AH
H00H
L78H
C00H
S0
Z1
AC1
P1
CY0
[2052H]78H
[2053H]00H
Answer:

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.

Example 3

3 (6 marks)

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:

  1. Load binary number into Accumulator.
  2. Initialize tens digit counter: B = 00H.
  3. Compare Accumulator with 10 decimal (0AH).
  4. If A < 0AH, branch to pack nibbles.
  5. If A >= 0AH, subtract 0AH (SUI 0AH), increment tens counter B (INR B), and repeat.
  6. When A < 0AH, Accumulator holds the units digit. Move units digit to register C.
  7. Move tens counter B to Accumulator and rotate left 4 times (RLC four times) to place it into upper nibble (D7–D4).
  8. Logical-OR Accumulator with units digit in C: ORA C.
  9. Store packed BCD byte at 2051H.

8085 Assembly Code:

Binary to Packed BCD8085 ASM
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             ; Stop

Execution Trace for Input 47H (71 Decimal):

  • Input: 47H = 71 decimal.
  • Subtraction loop subtracts 0AH (10) seven times:
    • After 7 subtractions: B = 07H, remainder in A = 01H.
  • PACK:
    • C = 01H (units digit).
    • A = 07H rotated left 4 times -> 70H.
    • 70H ORA 01H = 71H.
  • ORA C leaves 71H = 0111 0001B (four 1s), so P = 1, with S = Z = AC = CY = 0.
  • Stored at 2051H: 71H (valid packed BCD for 71 decimal).
Registers After BCD Conversion
A71H
B07H
C01H
S0
Z0
AC0
P1
CY0
[2051H]71H
Answer:

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

Question 1

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:

  1. Clear register C to record any final carry.
  2. Load the lower byte of the first number from 2050H into A.
  3. Load the lower byte of the second number from 2052H into B.
  4. Add the lower bytes: ADD B.
  5. Store lower byte of sum at 2054H.
  6. Load the higher byte of the first number from 2051H into A.
  7. Load the higher byte of the second number from 2053H into B.
  8. Add with carry: ADC B.
  9. Store higher byte of sum at 2055H.
  10. If carry flag is set, increment register C (JNC SKIP; INR C).
  11. Store carry byte from register C at 2056H.

8085 Assembly Code:

16-Bit Addition with Carry8085 ASM
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             ; Terminate
Answer:

The 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.

Question 2

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 = 4 passes.
  • Inner loop compares adjacent pairs [HL] and [HL + 1].
  • If [HL] > [HL + 1], swap the two elements in memory using temporary register D.
  • Repeat until all passes complete.

8085 Assembly Code:

Bubble Sort Ascending8085 ASM
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             ; Finished
Answer:

The 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.

Question 3

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:

  1. Load dividend from 2050H into Accumulator.
  2. Load divisor from 2051H into register B.
  3. Clear register C to serve as the quotient counter (C = 00H).
  4. Compare Accumulator with divisor: CMP B.
  5. If A < B (JC DONE), division is complete; remaining value in A is the remainder.
  6. If A >= B, subtract divisor: SUB B.
  7. Increment quotient counter: INR C.
  8. Jump back to step 4.
  9. Store quotient from C at 2052H and remainder from A at 2053H.

8085 Assembly Code:

8-Bit Division by Repeated Subtraction8085 ASM
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             ; Terminate
Answer:

The 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).

Question 4

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:
    1. Load byte into Accumulator: MOV A, M.
    2. Compare with 50H: CPI 50H.
    3. If A < 50H (JC SET_D5):
      • Set bit D5 using ORI 20H (0010 0000B).
      • Jump to STORE.
    4. If A >= 50H:
      • Clear bit D6 using ANI BFH (1011 1111B).
    5. STORE: Store byte into destination memory using STAX D.
    6. Increment pointers INX H, INX D, decrement counter DCR C, and repeat.

8085 Assembly Code:

Conditional Bit Manipulation Transfer8085 ASM
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             ; Terminate
Answer:

The 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].

Question 5

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:
    1. Load array byte into Accumulator: MOV A, M.
    2. Test bit D0 using ANI 01H.
    3. If result is non-zero (JNZ IS_ODD), the number is odd; do not increment even count.
    4. If result is zero, the number is even; increment counter B (INR B).
    5. Advance pointer INX H, decrement loop counter DCR C, and continue until C = 0.
    6. Store result from B at 2550H.

8085 Assembly Code:

Count Even Numbers in Array8085 ASM
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             ; Stop
Answer:

The 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.

Past exam questions for Microprocessors

← 8085 Instruction SetInstruction, Machine & T-Cycles; Timing Diagrams →