2076 3

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]

MicroprocessorChapter 4: Assembly Language ProgrammingSimple sequence programs

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

ItemValue / Description
Array length20 (14H) stored at memory location 2200H
Array dataStarts from memory location 2201H onwards
Result storageMemory location 2300H
Data typeUnsigned 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

InstructionOperationPurpose
LDA 2200HA <- [2200H]Load count (14H = 20) into A
MOV C, AC <- AUse C as loop counter
LXI H, 2201HHL <- 2201HPoint HL to first array element
MOV A, MA <- [HL]Load first element as initial max
INX HHL <- HL + 1Move pointer to second element
DCR CC <- C - 1Reduce count by 1 (first element done)
MOV B, MB <- [HL]Load current element into B
CMP BA - B (flags set)Compare current max with current element
JNC SKIPJump if No CarryIf A >= B, skip update
MOV A, BA <- BUpdate max if B is greater
INX HHL <- HL + 1Move to next element
DCR CC <- C - 1Decrement counter
JNZ LOOPJump if C != 0Repeat until all elements checked
STA 2300H[2300H] <- AStore the greatest number
HLTStopEnd of program

Dry Run / Trace (Partial Example)

Assume array: 45H, 78H, 23H, 99H, 12H ...

IterationA (current max)B (new element)CMP resultAction
Initial45H----A = 45H (first element)
145H78HA < B (carry set)A = 78H
278H23HA > B (no carry)A = 78H (no change)
378H99HA < B (carry set)A = 99H
499H12HA > B (no carry)A = 99H (no change)
...99H...A >= allA = 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 CMP to branch when A >= B, since the Carry flag is cleared only when no borrow was needed.

Using CMP followed 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.