CSC167 ยท 2075 ยท 10 marks
Write an assembly language program to find the smallest number in an array using 8 bit microprocessor, (assume appropriate array data and address where minimum array size of 15 should be considered.)
Write an assembly language program to find the smallest number in an array using 8 bit microprocessor, (assume appropriate array data and address where minimum array size of 15 should be considered.)[10]
Quick answer
After the fourteenth comparison the counter in C reaches zero, the loop ends, and STA 2300H stores the accumulator.
Assembly Language Program to Find the Smallest Number in an Array (8-bit Microprocessor - 8085)
Problem Understanding
- We need to find the smallest (minimum) number in an array of 8-bit data.
- Array size: minimum 15 elements
- We assume:
- Memory location 2200H stores the length of the array (count = 15 = 0FH)
- Memory locations 2201H onwards store the array elements
- Result (smallest number) is stored at memory location 2300H
Assumed Array Data
| Memory Address | Data (Hex) |
|---|---|
| 2200H | 0FH (length = 15) |
| 2201H | 45H |
| 2202H | 12H |
| 2203H | 78H |
| 2204H | 03H |
| 2205H | 56H |
| 2206H | 89H |
| 2207H | 21H |
| 2208H | 0AH |
| 2209H | 34H |
| 220AH | 67H |
| 220BH | 09H |
| 220CH | 55H |
| 220DH | 1FH |
| 220EH | 72H |
| 220FH | 2BH |
Expected smallest value = 03H (at address 2204H)
Algorithm / Logic
Step 1: Load the count (length) of array from memory 2200H into register C
Step 2: Point HL register pair to the first element (2201H)
Step 3: Load the first element into Accumulator as the assumed minimum
Step 4: Decrement counter C by 1 (first element already loaded)
Step 5: Advance HL pointer to next element
Step 6: Compare current element (M) with Accumulator (current minimum)
- If current element >= Accumulator, skip (no update)
- If current element < Accumulator, update Accumulator with current element
Step 7: Decrement counter C, advance pointer
Step 8: Repeat Steps 6-7 until counter C = 0
Step 9: Store the result (Accumulator) at 2300H
Step 10: HLT
Assembly Language Program (8085 ALP)
; ============================================================
; Program : Find Smallest Number in an Array
; Array : Starts at 2201H, Length stored at 2200H
; Result : Stored at 2300H
; Array Size: 15 elements (0FH)
; ============================================================
LDA 2200H ; Load the count (length = 0FH) into Accumulator
MOV C, A ; Move count into register C (loop counter)
LXI H, 2201H ; Point HL pair to the first element of array
MOV A, M ; Load first element into Accumulator
; Assume first element as the current minimum
DCR C ; Decrement counter (first element already read)
INX H ; Advance HL pointer to next element
LOOP: MOV B, M ; Load next array element into register B
CMP B ; Compare Accumulator (current min) with B
; CMP performs A - B and sets flags
; If A <= B, Carry flag = 0, so A is still smaller
; If A > B, Carry flag = 1, so B is the new minimum
JC UPDATE ; Jump to UPDATE if Carry = 1 (i.e., A > B, B is smaller)
JZ SKIP ; Jump to SKIP if Zero = 1 (i.e., A = B, no update needed)
JMP SKIP ; Jump to SKIP if A < B (A is already smaller)
UPDATE: MOV A, B ; Update Accumulator with the new smaller value (B)
SKIP: INX H ; Advance HL pointer to next memory location
DCR C ; Decrement loop counter
JNZ LOOP ; If counter not zero, repeat loop
STA 2300H ; Store the smallest number into memory location 2300H
HLT ; Stop the program
Step-by-Step Instruction Explanation
| Instruction | Operation | Purpose |
|---|---|---|
LDA 2200H | A <- M[2200H] | Load array length (15 = 0FH) |
MOV C, A | C <- A | Set loop counter to 15 |
LXI H, 2201H | HL <- 2201H | Point to first array element |
MOV A, M | A <- M[HL] | Load first element as assumed minimum |
DCR C | C <- C - 1 | Reduce count (first element consumed) |
INX H | HL <- HL + 1 | Move to next element |
MOV B, M | B <- M[HL] | Load current element into B |
CMP B | A - B (flags set) | Compare current min with current element |
JC UPDATE | Jump if Carry=1 | If A > B, update minimum |
MOV A, B | A <- B | B is new minimum |
INX H | HL <- HL + 1 | Advance to next element |
DCR C | C <- C - 1 | Decrement counter |
JNZ LOOP | Jump if C != 0 | Repeat until all elements checked |
STA 2300H | M[2300H] <- A | Store final minimum |
HLT | -- | End program |
Dry Run (Partial Trace)
| Iteration | A (Current Min) | B (New Element) | CMP Result | Action |
|---|---|---|---|---|
| 1 | 45H | 12H | A > B | A <- 12H (new minimum) |
| 2 | 12H | 78H | A < B | No change |
| 3 | 12H | 03H | A > B | A <- 03H (new minimum) |
| 4 | 03H | 56H | A < B | No change |
| 5 | 03H | 89H | A < B | No change |
| 6 | 03H | 21H | A < B | No change |
| 7 | 03H | 0AH | A < B | No change |
| 8 | 03H | 34H | A < B | No change |
| 9 | 03H | 67H | A < B | No change |
| 10 | 03H | 09H | A < B | No change |
| 11 | 03H | 55H | A < B | No change |
| 12 | 03H | 1FH | A < B | No change |
| 13 | 03H | 72H | A < B | No change |
| 14 | 03H | 2BH | A < B | No change |
Result: memory location 2300H holds 03H, the smallest number in the array, which matches the expected value identified with the assumed data.
Conclusion
The program treats the first element as the assumed minimum and then walks the array once with the HL pair, comparing each element against the accumulator and replacing it whenever a smaller value is found. Since every element is examined exactly once, the routine needs (n - 1) comparisons for n elements, which is 14 comparisons for the 15-element array assumed here, and it works unchanged for any array length placed in 2200H.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.