user3316768
user3316768

Reputation: 31

I am trying to reverse an array in assembly using indexed loops

So I'm working on an assignment for a M68HC11 microprocessor class. I don't even know if I am doing what the objective is asking. I think I am on the right track, and I thought that my code would at least achieve the same functionality, if not methodology, of the task. This is the objective:

" A 16 byte array has been previously stored in consecutive memory beginning at location $A0. Write a compact 68HC11 assembly language program using an indexed loop which will store these bytes in consecutive memory locations in reverse order beginning in location $C0.

If the array is X, then the array has previously been stored as follows:

Memory location Contents
    $A0          x1
    $A1          x2
    $A2          x3
        etc

After the program has run, the array will also be stored as follows:

Memory location Contents
    $C0          x16
    $C1          x15
    $C2          x14
        etc

Locate your program at $E000."

And here is my code:

*HW2 PROGRAM

        ORG $E000 ;Beginning of Program

COUNTER EQU #16 ;Initialize Counter with number of bytes
COUNTERTWO  EQU #0

FIRST   EQU #$A0 ; Location of first array

SECOND  EQU #$C0 ; Location of second array

        LDX FIRST; Set X index register to First array
        LDY SECOND; SET Y INDEX register to second array


DO      LDAA COUNTER, X
        STAA COUNTERTWO, Y


        INC COUNTERTWO; increment counter for array2
        DEC COUNTER ; decrement COUNTER for array1

        BNE DO ; loop back to do



SPIN    BRA SPIN ;Spin loop to end program

        ORG $FFFE

        FCB $E0
        FCB $00

Can anyone point me in the right direction? Thank you

Upvotes: 1

Views: 303

Answers (2)

tonypdmtr
tonypdmtr

Reputation: 3225

ARRAY1              equ       $A0
ARRAY2              equ       $C0
LENGTH              equ       16
RESET               equ       $FFFE
ROM                 equ       $E000

                    org       ROM

Start               ldx       #ARRAY1             ;X -> start of source array
                    ldy       #ARRAY2+LENGTH-1    ;Y -> end of destination array
                    ldab      #LENGTH             ;B = number of bytes to copy
Loop                ldaa      ,x                  ;A = source byte
                    staa      ,y                  ;write to destination
                    inx                           ;bump up source pointer
                    dey                           ;bump down destination pointer
                    decb                          ;one less byte to copy
                    bne       Loop                ;repeat until done

                    bra       *                   ;halt

                    org       RESET
                    dw        Start

Upvotes: 1

user3185968
user3185968

Reputation:

I'd recommend writing a higher-level description of your program before you start:

extern char X[16];
extern char Y[16];

void func(void) __attribute__((noreturn))
{
   int counter = ??;                    //what should counter be?
   for (int countertwo = 0; counter; counter--, countertwo++)
   {
       Y[countertwo] = X[counter];
   }
   l1:
   goto l1;
}

In your assembly version, you've initialised countertwo to zero, which suggests 0-indexed arrays. Why then do you initialise counter to 16 instead of 15?

Upvotes: 1

Related Questions