2075 3

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]

MicroprocessorChapter 4: Assembly Language ProgrammingSimple sequence programs

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 AddressData (Hex)
2200H0FH (length = 15)
2201H45H
2202H12H
2203H78H
2204H03H
2205H56H
2206H89H
2207H21H
2208H0AH
2209H34H
220AH67H
220BH09H
220CH55H
220DH1FH
220EH72H
220FH2BH

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

InstructionOperationPurpose
LDA 2200HA <- M[2200H]Load array length (15 = 0FH)
MOV C, AC <- ASet loop counter to 15
LXI H, 2201HHL <- 2201HPoint to first array element
MOV A, MA <- M[HL]Load first element as assumed minimum
DCR CC <- C - 1Reduce count (first element consumed)
INX HHL <- HL + 1Move to next element
MOV B, MB <- M[HL]Load current element into B
CMP BA - B (flags set)Compare current min with current element
JC UPDATEJump if Carry=1If A > B, update minimum
MOV A, BA <- BB is new minimum
INX HHL <- HL + 1Advance to next element
DCR CC <- C - 1Decrement counter
JNZ LOOPJump if C != 0Repeat until all elements checked
STA 2300HM[2300H] <- AStore final minimum
HLT--End program

Dry Run (Partial Trace)

IterationA (Current Min)B (New Element)CMP ResultAction
145H12HA > BA <- 12H (new minimum)
212H78HA < BNo change
312H03HA > BA <- 03H (new minimum)
403H56HA < BNo change
503H89HA < BNo change
603H21HA < BNo change
703H0AHA < BNo change
803H34HA < BNo change
903H67HA < BNo change
1003H09HA < BNo change
1103H55HA < BNo change
1203H1FHA < BNo change
1303H72HA < BNo change
1403H2BHA < BNo 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.