# 8051 Program to Find the Smallest Number in An Array Stored in Internal Memory

## Problem

Write an assembly language program for microcontroller 8051 to find smallest number from an array of 10 numbers.

Assume array of the ten bytes is stored in internal memory of 8051 microcontroller from memory location 50H and Store smallest number in memory location 60H.

## Algorithm

Step 1: Initialize byte counter and memory pointer to read numbers from array.

Step2: Read number from the array.

Step3: Increment memory pointer to read next number.

Step 4: Decrement byte counter.

Step 5: Compare two numbers.

Step 6: If number < next number, then go to step 8.

Step 7: Replace number with next number which is smallest.

Step 8: Increment memory pointer to read next number in the array.

Step 9: Decrement byte counter by 1.

Step 10: If byte counter is not equal to zero then go to step 5.

Step 11: Store result.

Step 12: Stop.

## Program

CLR PSW.3 ; Clear 3rd bit of PSW.

CLR PSW.4 ; Clear 4th bit of PSW .

; Above steps are used to select register bank 0.

MOV R1, 0AH ; Initialize byte counter.

MOV R0, #50H ; Load 5oh into register R0 to use it as memory pointer.

DEC R1 ; Decrement byte counter by 1

MOV 60H, @R0 ; Store the number in memory location 60h.

REPEAT: INC R0 ; Increment memory pointer by 1.

MOV A, @R0 ; Get the second/next number in accumulator

CJNE A, 60H, SKIP ; If number ≠ next number then go to SKIP

AJMP NEXT ; else go to NEXT

SKIP: JNC NEXT ; If next number < the previous number then go to NEXT.

MOV 60H, A ; Or replace the next number with previous one