0

I'm trying to learn HCS12 assembly language but there are no enough examples on the internet. I've tried to write a code but there is no success. I'm stuck. It's not absolutely homework. Can someone write it in HCS12 assembly language with comments? I want code because really I want to read it step by step. By the way, is there any other way more simple to define array?

;The array arr will be located at $1500 and the contents {2, 5, 6, 16, 100, 29, 60}
sum = 0;
for i = 0 : 6
    x = arr[i];
    if( x < 50 )
            sum = sum + x
end

My try:

Entry:

            ;2,5,6,16,100,39,60               
            LDAA #2
            STAA $1500
            LDAA #5
            STAA $1501
            LDAA #6
            STAA $1502
            LDAA #16
            STAA $1503
            LDAA #100
            STAA $1504
            LDAA #39
            STAA $1505
            LDAA #60
            STAA $1506

            CLRA  ; 0 in accumulator A
            CLRB  ; 0 in accumulator B
            ADDB COUNT ; B accumulator has 6

loop:       

            ;LDAA 1, X+ ; 1500 should be x because it should increase up to 0 from 6
                        ; A accumulator has 2 now
            BLO 50; number less than 50
            ;ADDA 


            DECB
            BNE loop
  • _"Can someone write it in HCS12 assembly language with comments?"_ That's not what StackOverflow is for. _"is there any other way more simple to define array?"_. Sure, just place the bytes in the data section of your program and refer to them by the address of the first byte. – Michael Feb 29 '16 at 10:08

1 Answers1

3

Below is one possible way to implement your specific FOR loop.

It's mostly for the HC11 which is source level compatible to the HCS12 so it should also assemble correctly for the HCS12. However, the HCS12 has some extra instructions and addressing modes (e.g., the indexed auto-increment) which can make the code a bit shorter and even more readable. Anyway, I haven't actually tried this but it should be OK.

BTW, your code shows you have some fundamental lack of understanding for certain instructions. For example, BLO 50 does not mean branch if accumulator is below 50. It means check the appropriate CCR (Condition Code Register) flags which should be already set by some previous instruction, and branch to address 50 (obviously, not what you intended) if the value is less than the target. To compare a register to a value or some memory location you must use the CMPx instructions (e.g., CMPA).

;The array arr will be located at $1500 and the contents {2, 5, 6, 16, 100, 29, 60}

                    org       $1500               ;(somewhere in ROM)
arr                 fcb       2,5,6,16,100,29,60  ;as bytes (use dw if words)

                    org       $100                ;wherever your RAM is
;sum = 0;
sum                 rmb       2                   ;16-bit sum

                    org       $8000               ;wherever you ROM is
;for i = 0 : 6
                    clrb                          ;B is your loop counter (i)
                    stb       sum                 ;initialize sum to zero (MSB)
                    stb       sum+1               ;         -//-          (LSB)
ForLoop             cmpb      #6                  ;compare against terminating value
                    bhi       ForEnd              ;if above, exit FOR loop
;    x = arr[i];
                    ldx       #arr                ;register X now points to array
                    abx                           ;add offset to array element (byte size assumed)
                    ldaa      ,x                  ;A is your target variable (x)
;;;;;;;;;;;;;;;;;;; ldaa      b,x                 ;HCS12 only version (for the above two HC11-compatible lines)
                    inx                           ;X points to next value for next iteration
;;;;;;;;;;;;;;;;;;; ldaa      1,x+                ;HCS12 only version (for the above two HC11-compatible lines)
;    if( x < 50 )
                    cmpa      #50
                    bhs       EndIf
;            sum = sum + x
                    adda      sum+1
                    staa      sum+1
                    ldaa      sum
                    adca      #0
                    staa      sum
EndIf
                    incb                          ;(implied i = i + 1 at end of loop)
                    bra       ForLoop
;end
ForEnd

The above assumes your array is constant, so it is placed somewhere in ROM at assembly time. If your array is dynamic, it should be located in RAM, and you would need to use code to load it (similar to how you did). However, for efficiency, a loop is usually used when loading (copying) multiple values from one location to another. This is both more readable and more efficient in terms of needed code memory.

Hope this helps.

Edited: Forgot to initialize SUM to zero.

Edited: Unlike in the HC08, a CLRA in HC11 clears the Carry so the sequence CLRA, ADCA is wrong. Replaced with correct one: LDAA, ADCA #0

tonypdmtr
  • 3,037
  • 2
  • 17
  • 29