Saturday, April 30, 2016

8051 Program – search an element in an array

; search an element in an array
; find the first match
; array: 4000H - 4009H
; result: 4010H (element index)
ORG 0H
ARRAY_ADDR EQU 4000H
RESULT_ADDR EQU 4010H
ELEMENT_COUNT EQU 10
DATA_TO_FIND EQU 0AH
MAIN:
MOV DPTR,#ARRAY_ADDR ; fill memory
MOV A, #01H
MOVX @DPTR,A
INC DPTR
MOV A, #09H
MOVX @DPTR,A
INC DPTR
MOV A, #06H
MOVX @DPTR,A
INC DPTR
MOV A, #05H
MOVX @DPTR,A
INC DPTR
MOV A, #03H
MOVX @DPTR,A
INC DPTR
MOV A, #07H
MOVX @DPTR,A
INC DPTR
MOV A, #0AH
MOVX @DPTR,A
INC DPTR
MOV A, #0BH
MOVX @DPTR,A
INC DPTR
MOV A, #07FH
MOVX @DPTR,A
INC DPTR
MOV A, #04H
MOVX @DPTR,A
LCALL FIND
SJMP MAIN
FIND:
MOV DPTR,#ARRAY_ADDR ;load dptr with:ARRAY_ADDR
MOV R7,#ELEMENT_COUNT ;load r1 with:ELEMENT_COUNT
MOV R6, #0
loop:
MOVX A,@DPTR ;move data from memory location to a
CJNE A,#DATA_TO_FIND,loop1
;element found store index
MOV DPTR,#RESULT_ADDR ;load dptr with:RESULT_ADDR
MOV A,R6 ;laod a with array index
MOVX @DPTR,A ;move data from a to external memory location
SJMP return
loop1: INC DPTR ;increment dptr
INC R6 ;next index
DJNZ R7, loop
;element not found
MOV DPTR,#RESULT_ADDR
MOV A,#0FFH
MOVX @DPTR,A
return:
RET
END



Related topics:
8051 Program - search a byte in array and count match   |   8051 Program - square of a given number 8bit   |   8051 Program - square root of a given number 8bit   |   8051 Program - square and cube operation   |   8051 Program - count number of ones in a byte   |   8051 Program - count 0 and 1 in a byte   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment