Write a simple 8051 assembly language program for finding the smallest and largest number from a given array
; Code for finding the smallest number from internal memory locations
ORG 00H
MOV R0,#50H // Initialize the source memory pointer D: 0050H
MOV R2,#05H // Initialize Iteration counter
MOV B, @R0 /* Use B Register to store smallest value and initialize it to the first value*/
BACK: MOV A,@R0 /* Get the data from source memory pointer and Load into accumulator*/
CJNE A,B,LOOP /* Compare the data if not equal, go to relative address(LOOP)*/
LOOP: JNC LOOP1 // If carry generates, go to relative address LOOP1
MOV B,A // Store smaller value into B-register
INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, go to relative address and repeat the same process until count become zero.*/
SJMP NEXT // Go to NEXT
LOOP1: INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, go to relative address and repeat the same process until count become zero.*/
NEXT: MOV 60H,B /*Store the smallest value into memory location 60H. i.e. D: 0060H*/
END
; Largest number - FROM EXTERNAL MEMORY LOCATIONS
org 00h
mov r3,#6 //length of the array
mov dptr,#4000H //starting address of array i.e. x: 4000h
movx a,@dptr
mov r1,a
nextbyte: inc dptr
movx a,@dptr
clr c //reset borrow flag
mov r2,a //next number in the array
subb a,r1 //other Num-Prev largest no.
jc skip // JNC FOR SMALLEST ELEMENT
mov a,r2 //update larger number in r1
mov r1,a
skip: djnz r3,nextbyte
mov dptr, #4062H //location of the result-4062h. i.e. x: 4062h
mov a,r1 //largest number
movx @dptr,a //store at #4062H
end
Comments
Post a Comment