CSC167 ยท 2076 ยท 10 marks
Write an assembly language program to find the greatest number in an array in using 8 bit microprocessor. (Assume appropriate array data and address where minimum array size of 20 should be considered.)
Write an assembly language program to find the greatest number in an array in using 8 bit microprocessor. (Assume appropriate array data and address where minimum array size of 20 should be considered.)[10]
Quick answer
Find the greatest (maximum) number in an array of 8-bit numbers using the 8085 (8-bit) microprocessor. The array has a minimum size of 20 elements.
Assembly Language Program to Find the Greatest Number in an Array (8085 Microprocessor)
Problem Statement
Assumptions
| Item | Value / Description |
|---|---|
| Array length | 20 (14H) stored at memory location 2200H |
| Array data | Starts from memory location 2201H onwards |
| Result storage | Memory location 2300H |
| Data type | Unsigned 8-bit numbers |
Sample Array Data (20 elements at 2201H onwards)
2200H --> 14H ; count = 20
2201H --> 45H
2202H --> 78H
2203H --> 23H
2204H --> 99H ; greatest
2205H --> 12H
... (remaining 15 elements)
2214H --> 67H
Algorithm / Logic
Step 1: Load the count (length) of array into register C
Step 2: Load the first element of array into Accumulator (A)
This is our initial "maximum" value
Step 3: Decrement counter C by 1 (since first element already loaded)
Step 4: Point HL register pair to the next element (2202H)
Step 5: LOOP:
Load next element from memory into register B
Compare A with B (A - B)
If A >= B, skip (A is still the maximum)
If A < B, update A = B (new maximum found)
Increment HL to point to next element
Decrement counter C
If C != 0, repeat LOOP
Step 6: Store the result (maximum value) into 2300H
Step 7: HLT
Assembly Language Program (8085)
LDA 2200H ; Load array length (20 = 14H) into Accumulator
MOV C, A ; Move length/count into register C (counter)
LXI H, 2201H ; Point HL to the start of array (first element)
MOV A, M ; Load first element into Accumulator
; Assume first element as the greatest initially
INX H ; Increment HL to point to second element
DCR C ; Decrement counter (one element already loaded)
LOOP: MOV B, M ; Load next array element into register B
CMP B ; Compare Accumulator (current max) with B
; Internally performs A - B and sets flags
JNC SKIP ; Jump to SKIP if A >= B (No Carry means A >= B)
; i.e., current max is still greater or equal
MOV A, B ; If A < B, update Accumulator with new maximum
SKIP: INX H ; Increment HL to point to next memory location
DCR C ; Decrement counter register C by 1
JNZ LOOP ; If counter != 0, jump back to LOOP
STA 2300H ; Store the greatest number into memory location 2300H
HLT ; Stop the program
Step-by-Step Explanation of Each Instruction
| Instruction | Operation | Purpose |
|---|---|---|
LDA 2200H | A <- [2200H] | Load count (14H = 20) into A |
MOV C, A | C <- A | Use C as loop counter |
LXI H, 2201H | HL <- 2201H | Point HL to first array element |
MOV A, M | A <- [HL] | Load first element as initial max |
INX H | HL <- HL + 1 | Move pointer to second element |
DCR C | C <- C - 1 | Reduce count by 1 (first element done) |
MOV B, M | B <- [HL] | Load current element into B |
CMP B | A - B (flags set) | Compare current max with current element |
JNC SKIP | Jump if No Carry | If A >= B, skip update |
MOV A, B | A <- B | Update max if B is greater |
INX H | HL <- HL + 1 | Move to next element |
DCR C | C <- C - 1 | Decrement counter |
JNZ LOOP | Jump if C != 0 | Repeat until all elements checked |
STA 2300H | [2300H] <- A | Store the greatest number |
HLT | Stop | End of program |
Dry Run / Trace (Partial Example)
Assume array: 45H, 78H, 23H, 99H, 12H ...
| Iteration | A (current max) | B (new element) | CMP result | Action |
|---|---|---|---|---|
| Initial | 45H | -- | -- | A = 45H (first element) |
| 1 | 45H | 78H | A < B (carry set) | A = 78H |
| 2 | 78H | 23H | A > B (no carry) | A = 78H (no change) |
| 3 | 78H | 99H | A < B (carry set) | A = 99H |
| 4 | 99H | 12H | A > B (no carry) | A = 99H (no change) |
| ... | 99H | ... | A >= all | A = 99H |
Result stored at 2300H = 99H (Greatest number)
Key Notes
CMP instruction in 8085 subtracts the operand from Accumulator without storing the result, but sets the Carry Flag (CY) and Zero Flag (ZF) according to the result: if A = B, CY = 0 and ZF = 1; if A > B, CY = 0 and ZF = 0; if A < B, CY = 1 and ZF = 0.
JNC (Jump if No Carry) is used right after
CMPto branch when A >= B, since the Carry flag is cleared only when no borrow was needed.
Using
CMPfollowed by a conditional jump lets the program compare values without altering the Accumulator, which is essential here because the running maximum stored in A must be preserved across every loop iteration until it is actually replaced.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.