Steve Lewis

Calling a function using a pointer - a question about the assmbler from the compiler

Discussion created by Steve Lewis on Sep 18, 2008
Latest reply on Sep 22, 2008 by CompilerGuru
I am using Code warrior for HC12 version 4.7, IDE version 5.9
The target is an MC9S12A256. The program used the BANKED memory model.

I was in the process of debugging an issue in our application, when I scanned through the assembler code and became curious about what exactly was happening at the assembler level.

I have a look up table of pointers to functions. It is stored in a constant storage segment of memory. Hence each entry requires 3 bytes, and is filled in by the compiler with the addresses of the routines that I wish to call. The format is:

typedef void (* __far tMain_Function)(void);


const tMain_Function Run_main_program = (* main_program);
const tMain_Function Run_capture_tacho = (* Tacho_Edge_Seen);
const tMain_Function Run_timer2_isr = (* Opt_Sensor_Timer);

In turn, the function referenced is declared like this:
void __far main_program(void)
/* do something here */

And is called like this:

When I disassmbled the code, it looked like:
001d c600 [1] LDAB #PAGE(Run_main_program)
001f 5b35 [2] STAB /*PPAGE*/53
0021 4bfb0000 [10] CALL [Run_main_program,PCR]

If the call is made from within an interrupt routine, the disassembl;ed code is even longer. E.g.
0000 9635 [3] LDAA 53
0002 36 [2] PSHA
139: Run_capture_tacho();
0003 c600 [1] LDAB #PAGE(Run_capture_tacho)
0005 5b35 [2] STAB /*PPAGE*/53
0007 4bfb0000 [10] CALL [Run_capture_tacho,PCR]
140: }
000b 32 [3] PULA
000c 5a35 [2] STAA 53
The contents of memory location 53 are being saved to stack.

My question is: why is accumulator B being stored to memory location 53 (hex 0x35)? According to the manual, this location is used by the Clocks and reset generator module, PLL divider register REFDV. I have stepped through the code one assembler instruction at the time. The STAB command has no effect because I have the PLL running, the flag PLLSEL is set and according to the manual you cannot change the REFDV register once this flag is set. I then reran this in the debugger and got it to miss the STAB step - everything still worked fine.

I am guessing that this instruction is required by another member of the HC12 family, and that somewhere there is an option or setting I have set/not set which removes it.
My concern is that somewhere this will cause a problem. Also it is in a section of code where I am trying to keep the overall code size down.

Many thanks for any help