Search code examples
assembly68hc11

68hc11 assembly (first steps) - sorting


I just fell in love with this particular microcontroller, 68hc11 has an amazing architecture.

I'm not an expert but i want to improve, assembly is kinda hard but i want to program this microcontroller.

This assembly code will execute from $100, will allocate a 200-byte array at $800, and will initialize that array with the values 200, 199, … 1. (descending order).

Vreset              equ       $FFFE
RAM                 equ       $800
ROM                 equ       $100

ARRAY_SIZE          equ       200

                    org       RAM

array               rmb       ARRAY_SIZE

                    org       ROM

Start               ldx       #array
                    ldaa      #ARRAY_SIZE
Loop                staa      ,x
                    inx
                    deca
                    bne       Loop

                    bra       *

                    org       Vreset
                    dw        Start

I want to get the two highest values from a given array.. i mean, i want to create an array, give 10 values (stored inside an array) and finally obtain the two highest values:

Example:

the array may contain these values:

5 7 9 96 57 58 1 5 6 9

I would like to obtain this output:

96 58

Can help me to do this? I'm kinda lost :/


Solution

  • The 68HC11 is a classic MCU architecture that was (and possibly still is, to some extent) taught in many universities.

    Officially, it is end-of-life. But, due to momentum, many people are still actively using it either by IP equivalents loaded in FPGAs, or clones from companies such as Tekmos.

    To create the array, use code similar to what you showed. My example uses constant array in ROM.

    As to your question finding the two highest values, there are many possible solutions. Here's just one to get you started:

    Vreset              equ       $FFFE
    ROM                 equ       $100
    
                        org       ROM
    
    array               fcb       5,7,9,96,57,58,1,5,6,9
    ;ARRAY_SIZE         equ       *-array
    
    Start               ldx       #array              ;X -> array
                        ldaa      ,x                  ;let A keep the 1st maximum (assume 1st element)
                        clrb                          ;let B keep the 2nd maximum (assume zero)
    Loop                inx                           ;X -> next array element
                        cpx       #array+::array      ;(ASM11 idiom, ::array = 10 i.e., number of elements)
    ;                   cpx       #array+ARRAY_SIZE   ;(alternative for most assemblers)
                        bhs       Done                ;if past the array end, done
    
                        cmpa      ,x                  ;compare with current array element
                        bhi       DoB                 ;if already 1st maximum, skip
                        tab                           ;update 2nd maximum with previous 1st
                        ldaa      ,x                  ;else A = new maximum
                        bra       Cont
    ;                   bra       Loop                ;(optimization of previous line)
    
    DoB                 cmpb      ,x
                        bhi       Cont                ;if already 2nd maximum, skip
    ;                   bhi       Loop                ;(optimization of previous line)
                        ldab      ,x                  ;else B = new maximum <= A
    
    Cont                bra       Loop                ;repeat for all array elements
    
    Done                bra       *                   ;A = 1st maximum, B = 2nd maximum <= A
    
                        org       Vreset
                        dw        Start