I'm programming an old MCU (68hc11), I'm trying to migrate from C language to assembly code using the 68hc11 instructions.
I want to write a program in assembly that counts the number of POSITIVE, NEGATIVE, and ZERO values that exist inside a given array. DO NOTE that all values inside ARRAY could be all positive or all negative or all zeros,do you get me? So I should define the size of the variables that will store the quantity properly.
NOTE: The end of the array is: ARRAY+QUANTITY-1
Array: contains some random values
QUANTITY: represent the highest number of elements that the ARRAY can hold
I wrote this program in C:
int A[15], pos, neg, nul, i;
[...]
pos = 0;
neg = 0;
nul = 0;
for (i = 0; i < 15; i++) {
if (A[i] > 0) {
pos++;
}
if (A[i] < 0) {
neg++;
}
if (A[i] == 0) {
nul++;
}
}
Now, I want to translate that but in assembly (I GOT STUCK IN HERE, i'm not getting what i want)
RWM EQU $0
ROM EQU $C000
VRESET EQU $FFFE
QUANTITY EQU 800 ;MEANS THE MAXIMUM AMOUNT OF VALUES THAT THE ARRAY WILL CONTAIN
ORG RWM
POSITIVE RMB 2
NEGATIVE RMB 2
ZEROS RMB 2
ORG ROM
START:
CLRA
CLRB
CLR POSITIVE
CLR ZEROS
CLR NEGATIVE
LDY #ARRAY
LOOP
CPY #(ARRAY+QUANTITY-1)
BHI END
LDD 0,Y
INY
BLT NEGATIVE_NUMBER
BEQ ZERO_NUMBER
BGE POSITIVE_NUMBER
NEGATIVE_NUMBER INC NEGATIVE
BRA LOOP
POSITIVE_NUMBER INC POSITIVE
BRA LOOP
ZERO_NUMBER INC ZEROS
BRA LOOP
END BRA END
ARRAY DW 78,554,-44,-4,2547,0,-3,0,1,7,8,
ORG VRESET
DW START
What's wrong with my code?
EDIT:
RWM EQU $0
ROM EQU $C000
VRESET EQU $FFFE
QUANTITY EQU 800 ;MEANS THE MAXIMUM AMOUNT OF VALUES THAT THE ARRAY WILL CONTAIN
ORG RWM
POSITIVE RMB 2
NEGATIVE RMB 2
ZEROS RMB 2
ORG ROM
START:
CLRA
CLRB
CLR POSITIVE
CLR ZEROS
CLR NEGATIVE
LDY #(ARRAY-2)
LOOP
INY
INY
CPY #(ARRAY+2*QUANTITY-1)
BHI END
LDD 0,Y
BLT NEGATIVE_NUMBER
BEQ ZERO_NUMBER
BGE POSITIVE_NUMBER
NEGATIVE_NUMBER INC NEGATIVE
BRA LOOP
POSITIVE_NUMBER INC POSITIVE
BRA LOOP
ZERO_NUMBER INC ZEROS
BRA LOOP
END BRA END
ARRAY DW 78,554,-44,-4,2547,0,-3,0,1,7,8,
ORG VRESET
DW START
I got this output (not sure about it)


I only had a quick look at your program, so my answer may be wrong:
Obviously you are operating with 16 bit numbers. Otherwise you would use
LDAorLDBinstead ofLDD.This would be correct if
QUANTITYis the size of the array in bytes (e.g. 30 in the case ofint A[15]).If
QUANTITYis the number of elements and you are operating with 16-bit numbers, you have to multiply with two:Once again this would work for 8-bit elements. For 16-bit elements you have to add two to each element or to increment
Ytwice:The
INYinstruction will modify the zero flag and therefore influence theBLT,BGEandBEQinstructions. You cannot useINYbetweenLDDand the branch instructions.Not really an error, but the
BGEinstruction will always branch. You may place thePOSITIVE_NUMBERnumber code after theBEQinstruction and remove theBGEinstruction.EDIT
I was thinking about three different ways to do this. The simplest one seems to be: