I am trying to write a subroutine that will find an average of positive numbers in an array. I am stuck on adding the numbers. The operations only seem to work with A and B registers. In this case I am not sure if I am executing addition properly. This is the best way that I came up with. Also in my branching, when I ran the program, it counted negative numbers as well, I am not sure where the problem is. (HCS12, WINIDE32)
p.s. SWI we supposed to use for reasons to be explained to us later on...
rest of the program:
Rich (BB code):
* Subroutine: AvgPositive
* Scope: count average of positive numbers using stack
* Input parameters: Index reg X points to the array, Index reg Y holds size of array
* Output parameters: result is returned to reg A
* Stack is used therefore no registers are modified
AVGPOSITIVE
LDX #ARRAY
CLRB
LDY #SIZE_ARRAY ;counter to end of subroutine
LOOP3
BRCLR 0,X,80,LOOP4 ;branch if contents are positive
LOOP4
LDAB 0,X
ABA
INX
DBNE Y,LOOP3
RTS
SWI
rest of the program:
Rich (BB code):
PROGRAM_ADD EQU $0800
ARRAY_ADD EQU $0900
STACK_ADD EQU $2000
SIZE_ARRAY EQU $4
ORG ARRAY_ADD
COUNT_POS DS 1
AVG_POS DS 1
ARRAY DB $23,$24,$86,$26
ORG PROGRAM_ADD
MAIN1
LDS #STACK_ADD
LDAB #SIZE_ARRAY ;counter
PART1
LDX #ARRAY
BSR COUNTPOSITIVE
PART2
LDX #ARRAy
BSR AVGPOSITIVE