[Solved] Positive, negative and zero (assembly)


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
    ...

9

solved Positive, negative and zero (assembly)