8085 Program to Find Largest and Smallest in an Array
Write an 8085 assembly program that:
-
Reads a block of N 8-bit numbers from memory (
2500H
= count,2501H…
= data) -
Finds the largest and smallest values in the array
-
Stores the results as:
3000H
= largest value3001H
= smallest value
Assume N is a small positive integer (e.g., ≤ 16). This is a classic array scan using comparison and branch logic.
⚡ TL;DR — Final Working Code
Section titled “⚡ TL;DR — Final Working Code” ; Interface constants ARRAY_ADDR EQU 2500H MAX_OUTPUT EQU 3000H MIN_OUTPUT EQU 3001H
; Step 1: Load array count and first element LXI H, ARRAY_ADDR MOV C, M ; C ← count (N) INX H MOV A, M ; A ← first value (max) MOV B, M ; B ← also min INX H DCR C ; Already processed one
; Step 2: Loop through arrayLOOP: MOV D, M ; D ← current element CMP D JNC SKIP_MAX MOV A, D ; Update maxSKIP_MAX: MOV E, D MOV D, B CMP E JC SKIP_MIN MOV B, E ; Update minSKIP_MIN: INX H DCR C JNZ LOOP
; Step 3: Store results LXI H, MAX_OUTPUT MOV M, A ; Store max INX H MOV M, B ; Store min HLT
🧱 Step 1: Define the Interface
Section titled “🧱 Step 1: Define the Interface”Before any code, define how input and output are communicated:
Address | Meaning |
---|---|
2500H | Number of elements (N) |
2501H… | Array of N elements |
3000H | Output: largest value |
3001H | Output: smallest value |
Why this matters: A clear interface makes testing easy and ensures correct data flow. It’s the contract your program must adhere to.
How to test it:
In Sim8085, set 2500H = 04H
, and memory 2501H…2504H
to known values (e.g., 30H, 10H, 50H, 20H
). Then inspect memory after steps complete to verify setup.
🧱 Step 2: Initialize Registers and Setup the Scan
Section titled “🧱 Step 2: Initialize Registers and Setup the Scan”Before scanning the array, we need to load the data and prepare storage for the current largest and smallest values.
🧠 What We’re Doing
Section titled “🧠 What We’re Doing”-
Load the count (N) from
2500H
into registerC
-
Use the first data element (at
2501H
) to initialize:A
as the current maximumB
as the current minimum
-
Set
HL
to point to the next array element (start of the loop)
🧾 Code (Initialization)
Section titled “🧾 Code (Initialization)”LXI H, 2500H ; HL → countMOV C, M ; C ← number of elements (N)INX H ; HL → first elementMOV A, M ; A ← first value (initial max)MOV B, M ; B ← also set as initial minINX H ; HL → next element for loopDCR C ; We've already processed oneHLT
🧪 Manual Test
Section titled “🧪 Manual Test”Set memory:
2500H = 04H2501H = 30H2502H = 10H2503H = 50H2504H = 20H
Expected after halt:
A
= 30H (max)B
= 30H (min)C
= 03H (remaining elements)HL
= 2502H (next value to compare)
This prepares us to scan through the rest of the array in Step 3.
🧱 Step 3: Loop Through the Array and Compare
Section titled “🧱 Step 3: Loop Through the Array and Compare”Now that we’ve initialized the maximum (A
) and minimum (B
) values using the first element, we loop through the remaining elements and compare each one with both A
and B
.
🧠 What We’re Doing
Section titled “🧠 What We’re Doing”-
At each step:
- Compare the current element (
M
) with the value inA
(max)- If it’s larger, update
A
- If it’s larger, update
- Then compare the same element with
B
(min)- If it’s smaller, update
B
- If it’s smaller, update
- Compare the current element (
-
Decrement
C
each time — the loop runsN−1
times -
Use conditional jumps (
JC
,JNC
) to decide when to update
🧾 Code (Compare and Loop)
Section titled “🧾 Code (Compare and Loop)”LOOP: MOV D, M ; D ← current element CMP D ; Compare A (max) with D JNC SKIP_MAX ; If A ≥ D, skip updating max MOV A, D ; A ← new max
SKIP_MAX: MOV E, D ; E ← same current element MOV D, B ; D ← current min CMP E ; Compare B (min) with current element JC SKIP_MIN ; If B < E, skip updating min MOV B, E ; B ← new min
SKIP_MIN: INX H ; HL → next element DCR C JNZ LOOPHLT
🧪 Manual Test (Continued)
Section titled “🧪 Manual Test (Continued)”With:
2500H = 04H2501H = 30H2502H = 10H2503H = 50H2504H = 20H
After execution:
A
should contain50H
(largest)B
should contain10H
(smallest)
The loop completes the core logic. Next, we’ll store the results into memory.
🧱 Step 4: Store the Results in Memory
Section titled “🧱 Step 4: Store the Results in Memory”After scanning the entire array:
- Register
A
holds the largest value - Register
B
holds the smallest value
We now store these results at their designated memory addresses to make them observable and usable outside the program.
📦 Output Mapping
Section titled “📦 Output Mapping”Address | Meaning |
---|---|
3000H | Largest value |
3001H | Smallest value |
🧾 Code (Store Phase)
Section titled “🧾 Code (Store Phase)”LXI H, 3000H ; HL → output locationMOV M, A ; Store largest at 3000HINX H ; HL → 3001HMOV M, B ; Store smallest at 3001HHLT
This completes the program’s contract: ✅ Read array → ✅ Compare each element → ✅ Write final results
🧪 Manual Test
Section titled “🧪 Manual Test”For input:
2500H = 04H2501H = 30H2502H = 10H2503H = 50H2504H = 20H
Expected output after execution:
3000H = 50H ; Largest3001H = 10H ; Smallest
📚 Summary
Section titled “📚 Summary”This program walks through an array in memory and identifies both the largest and smallest 8-bit values using straightforward comparison and conditional logic.
Key concepts covered:
- Initializing state from the first element
- Comparing elements using
CMP
,JNC
, andJC
- Using registers (
A
,B
) as running max/min - Writing outputs clearly and predictably to memory
The program uses no subroutines or extra memory, everything is done using registers and direct memory access, making it perfect for learning array processing in 8085 assembly.