assemblymicrocontroller68hc08

What is the value in the Accumulator?


This is a practice question for an exam on Microcontrollers involving Assembly Language, it asks to find the content in the accumulator after the final line has been executed. I can't understand how the answer is $5C if anyone has any knowledge of Assembly please help me!

     START: LDA   #$11  ; load accumulator

            STA   $2123 ; store accumulator to a memory address

            CLRA

            LDA   #$67     ; load accumulator

            LDHX  #$2120    ; load index register

            SUB   $03, X   ;subtract accumulator using index register

            ADD   #$06      ; add accumulator

Solution

  • START: LDA   #$11  ; load accumulator
     - A=$11
           STA   $2123 ; store accumulator to a memory address
     - memory[$2123] = $11
           CLRA
     - A=$00 (redundant, as next instruction will load A again)
           LDA   #$67     ; load accumulator
     - A=$67
           LDHX  #$2120    ; load index register
     - H:X=$2120
           SUB   $03, X   ;subtract accumulator using index register
     - this is addressing mode "Indexed, 8-Bit Offset" (indexed by H:X)
     - target memory address is calculated as H:X+$03 = $2123
     - finally subtraction is done on A: A=A-memory[$2123] ($67-$11=$56)
           ADD   #$06      ; add accumulator
     - A=A+$06 ($56+$06=$5C)
    

    Pay attention to #$ vs $ (immediate hexadecimal value vs memory address/offset hexadecimal value) and overall to the syntax of your assembler (I'm actually mostly guessing which CPU and assembler exactly you are using, seems like Freescale HCS08, but you should know better and I never did code for similar one), as each assembler can have subtle (different) details how to write certain machine instructions, and every CPU has different instruction set (which instructions are available and how you can resolve certain programming problems with them effectively).

    So you should know the syntax good enough to correctly asses which instruction is produced by particular line, and then you can check the instruction set manual, what exactly that instruction (and its variant) does. It also helps a lot to practice with debugger, actually writing some small tasks and single-stepping in debugger over each instruction, comparing the actual results with your assumptions.