8051 Program to Find the Largest Number in An Array

Problem

Write an assembly language program for microcontroller 8051 to find largest 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 largest 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 largest.

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.

Flowchart

Flowchart for 8051 Program to Find the Largest Number in An Array Stored in Internal Memory

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: JC NEXT ; If next number > the previous number then go to NEXT.

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

NEXT: DJNZ R1, REPEAT ; Decrement byte counter by 1, if byte counter≠ 0 then go to REPEAT.

STOP: AJMP STOP ; Stop

Recent posts

Leave a Comment

Your email address will not be published. Required fields are marked *