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)
See Question&Answers more detail:
os