Hello Mike,
Firstly, you will need to gain an understanding of the various addressing modes available with HCS08 MCUs. This is described within every datasheet and/or reference manual. You will need to understand the difference between immediate addressing, direct addressing, extended addressing, and indexed addressing modes with zero offset, 8-bit offset or 16-bit offset.
Examination of the various assembly instruction details will reveal that some instructions are not capable of handling all the above addressing modes. Instructions like CLR and INC fall in this category, along with many others. In fact, the assembly snippet that you give should not assemble without error, because the variable TEST requires use of extended addressing mode, which the instruction does not support.
To eliminate the assembly errors, you might use code similar to the compiled C example. This code, which uses indexed addressing with zero offset, will work for a variable at any RAM location. To clear the memory location would also require a similar construct.
ldhx #TEST ; H:X contains variable address
clr ,x
If you were to explicitly define the variable TEST within zero page RAM (this would require the use of a #pragma), it is possible that the compiler may use the direct addressing instructions, but this cannot be guaranteed for all compilers. You would need to check the actual compiler output.
As another example, the following C function
void incr_mem( byte *addr) // Increment byte value at specific address
{
*addr++;
}
could be represented by the following assembly code
; On entry, H:X = address of location to be incremented
INCR_MEM:
inc ,x
rts
Keep in mind that the code generated by a C compiler will, in most cases, will require greater code size and more execution cycles, than well written assembly code, maybe by a factor of 2:1, or more. This is the penalty for using a portable, higher level language, and why it can be necessary to use non-portable inline assembly code within time critical C functions.
Optimised assembly code for the HCS08 should attempt to use as few dedicated variables as possible, and make use of the stack for storage of intermediate values. A similar situation exists for C, where the number of global and static variables should be minimized, in favour of local, stack based variables, where feasible.
I assume that what you actually mean by the term "register" is simply a labelled memory location, rather than referring to an internal register within the CPU. A labeled memory location in assembly is equivalent to a global variable in C.
Regards,
Mac