Saturday, April 30, 2016

8051 Program – average of a set of numbers

;average of a set of numbers
; assume the sum of numbers is within 8bit range
; 4000H - 4004h = numbers
; 4005H = average
ORG 0H
DATA_ADDR EQU 4000H
NUM_1 EQU 10H
NUM_2 EQU 20H
NUM_3 EQU 30H
NUM_4 EQU 40H
NUM_5 EQU 50H
COUNT EQU 5
RESULT_ADDR EQU 4005H
MAIN:
MOV A, #NUM_1 ; fill memory with data
MOV DPTR,#DATA_ADDR
MOVX @DPTR, A
INC DPTR
MOV A, #NUM_2
MOVX @DPTR, A
INC DPTR
MOV A, #NUM_3
MOVX @DPTR, A
INC DPTR
MOV A, #NUM_4
MOVX @DPTR, A
INC DPTR
MOV A, #NUM_5
MOVX @DPTR, A
LCALL AVERAGE
SJMP MAIN
AVERAGE:
MOV DPTR,#DATA_ADDR ;Load addr into dptr register
CLR A ;clear a register
MOV B,A ;move data from a to b
MOV R1,#COUNT ;move 05 to r1 register
up: MOVX A,@DPTR ;move data from external memory location to a
ADD A,B ;add a and b
MOV B,A ;move the result to b
DEC R1 ;decrement r1
INC DPTR ;increment dptr
CJNE R1,#0,up ;compare r1 with 0, if not equal, jump to label up
MOV A,B ;move data from b to a
MOV B,#COUNT ;move 05h to b
DIV AB ;divide a by b
MOV DPTR,#RESULT_ADDR ; Load addr into dptr register
MOVX @DPTR,A ;move data from a to external memory location
MOV A,B ;move data from b to a
INC DPTR ;increment dptr
MOVX @DPTR,A ;move data from a to external memory location
RET
END



Related topics:
8051 Program - sum of a set of numbers   |   8051 Program - sum of set of numbers in external ram   |   8051 Program - arithmetic operation 8bit   |   8051 Program - addition 8bit   |   8051 Program - subtraction 8bit   |   8051 Program - move block of data external memory   |   8051 Program - ram to ram   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment