Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

68hc11 - Positive, negative and zero (assembly)

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:

ERROR

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)

inage

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What's wrong with my code?

I only had a quick look at your program, so my answer may be wrong:

LDD 0,Y

Obviously you are operating with 16 bit numbers. Otherwise you would use LDA or LDB instead of LDD.

CPY #(ARRAY+QUANTITY-1)

This would be correct if QUANTITY is the size of the array in bytes (e.g. 30 in the case of int A[15]).

If QUANTITY is the number of elements and you are operating with 16-bit numbers, you have to multiply with two:

CPY #(ARRAY+2*QUANTITY-1)
INY

Once again this would work for 8-bit elements. For 16-bit elements you have to add two to each element or to increment Y twice:

INY
INY
INY
BLT NEGATIVE_NUMBER

The INY instruction will modify the zero flag and therefore influence the BLT, BGE and BEQ instructions. You cannot use INY between LDD and the branch instructions.

BLT ...
BEQ ...
BGE ...

Not really an error, but the BGE instruction will always branch. You may place the POSITIVE_NUMBER number code after the BEQ instruction and remove the BGE instruction.

EDIT

I'm not understanding this part: INY followed by Bxx

I was thinking about three different ways to do this. The simplest one seems to be:

    LDY #(ARRAY-2)  ; Because we'll do INY twice right now
LOOP
    INY
    INY
    CPY #(ARRAY+2*QUANTITY-1)
    BHI END
    LDD 0,Y
    BLT NEGATIVE_NUMBER
    ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...