Saturday, April 30, 2016

8051 Program – largest element in an array

; largest element in an array
; array: 4000H - 4009H
; result: 4010H
ORG 0H
ARRAY_ADDR EQU 4000H
RESULT_ADDR EQU 4010H
ELEMENT_COUNT EQU 10
TEMP EQU 40H
MAIN:
MOV DPTR,#ARRAY_ADDR ; fill memory
MOV A, #05H
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 LARGE
SJMP MAIN
LARGE:
MOV DPTR,#ARRAY_ADDR
MOV TEMP,#00
MOV R5,#ELEMENT_COUNT
loop2: MOVX A,@DPTR
CJNE A,TEMP,loop1
loop3: INC DPTR
DJNZ R5,loop2
MOV A,TEMP
MOVX @DPTR,A
RET
loop1: JC loop3
MOV TEMP,A
SJMP loop3
RET
END

; largest element in a given array
; external memory
ORG 0H
START:
MOV R0,#10H ;count
MOV DPTR,#9000H
LOOP: CLR C ;Carry=0
MOVX A,@DPTR
MOV R2,A ;R2=first no
INC DPTR
MOVX A,@DPTR ;Acc=second no
MOV R3,A
SUBB A,R3 ;Compare
JNC SKIP
XCH A,R2 ;Exchange
SKIP: MOVX @DPTR,A;Big no -> mem
DEC 82H ;DPL=DPL-1
MOV A,R2
MOVX @DPTR,A;Small no -> mem
INC DPTR
DEC R0
CJNE R0,#01H,LOOP
INC DPTR
MOVX A,@DPTR ;A=largest no
SJMP $
END

; largest element in a given string
; external memory
ORG 9000H
MOV R3,#05H
MOV DPTR,#8000H
MOVX A,@DPTR
MOV R1,A
NEXTBYTE:INC DPTR
MOVX A,@DPTR
CLR C
MOV R2,A
SUBB A,R1
JC SKIP
MOV A,R2
MOV R1,A
SKIP:DJNZ R3,NEXTBYTE
MOV DPL,#62H
MOV A,R1
MOVX @DPTR,A
SJMP $
ORG 8000H
DB 04,05,06,07,08,02
END
;Result: 8062 -> 08



Related topics:
8051 Program - smallest element in an array   |   8051 Program - factorial of a given number   |   8051 Program - fibonacci series   |   8051 Program - lcm of two numbers   |   8051 Program - gcf of two numbers   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment