assembly68hc11

Swapping positions (HC11)


I'm just playing with my MC 68HC11; in C i can perform a simple byte swap by doing something like this:

swapped = ((num>>24)&0xff) | // move byte 3 to byte 0
                    ((num<<8)&0xff0000) | // move byte 1 to byte 2
                    ((num>>8)&0xff00) | // move byte 2 to byte 1
                    ((num<<24)&0xff000000); // byte 0 to byte 3

But now i want to achieve something a little harder using assembly code:

I created an ARRAY and added some values (using little endian logic). I want to read that ARRAY and swap all the values into big endian logic and store them inside "BIGENDIAN". I was thinking something like this:

RWM     EQU $0
ROM     EQU     $C000
RESET       EQU     $FFFE

        ORG     RWM
BIGENDIAN   RMB  16 

        ORG     ROM
Main:       


END     BRA END

ARRAY   DW  $0124,$FEEB,$0011,$0070,$ABEF,$074B,$8004,$8080

        ORG RESET
        DW  Main

I tried but it did not work properly.


Solution

  • DW creates 16-bit words. (Your C example is for 32-bit words.)

    For 16-bit one possibility is this:

    RAM                 equ       $0
    ROM                 equ       $C000
    Vreset              equ       $FFFE
    
                        org       RAM
    
    BIGENDIAN           rmb       16
    
                        org       ROM
    
    ARRAY               dw        $0124,$FEEB,$0011,$0070,$ABEF,$074B,$8004,$8080
    
    Start               ldx       #ARRAY              ;X -> source
                        ldy       #BIGENDIAN          ;Y -> destination
    
    Loop                ldd       ,x                  ;A = MSB, B = LSB (big endian view)
                        staa      1,y
                        stab      ,y
    
    ; one alternative for above two instructions
    ;                   pshb
    ;                   tab
    ;                   pula
    ;                   std       ,y
    
                        ldab      #2                  ;B = word size
                        abx                           ;X -> next word
                        aby                           ;Y -> next word
    
                        cpx       #ARRAY+::ARRAY
                        blo       Loop
    
    Done                bra       *
    
                        org       Vreset
                        dw        Start
    

    If you meant 32-bit words, so two 16-bit should be considered one word, I'll revise.