Thread module can't work in the platform of mc9s12xep100mal

the rtos’s thread module is complex and i can’t make it work,so
i wrote my own createthread function that similar to the rtos ,and this is my code:
typedef void (*Thread_t)();
void CreatThread( Thread_t func, unsigned char *stack )
{
unsigned char *temp = stack;
unsigned char *begin = stack;

temp--;  //SP reg
temp--;
*temp = ( unsigned long )func;  //page reg
temp--;
*temp = ( unsigned long )func >> 8;  //function address
temp--;
*temp = ( unsigned long )func >> 16;  //function address
temp--;
*temp = 0xff;  //Y reg
temp--;
*temp = 0xee;  //Y reg
temp--;
*temp = 0xdd;  //X reg
temp--;
*temp = 0xcc;  //X reg
temp--;
*temp = 0;  //A reg
temp--;
*temp = 0;  //B reg
temp--;
*temp = 0;  //CCR
temp--;  //

*begin = (unsigned short)temp;  //top of the stack that the cpu can capture the return address
begin--;
*begin = (unsigned short)temp >> 8;  //top of the stack that the cpu can capture the return address

}

short temp;
interrupt void overflow(void)
{
DisableInterrupts;
if(ECT_TFLG2_TOF ==1)
{
portSAVE_CONTEXT();
CurCtx = (CurCtx+1)%2;
pxCurrentTCB = &Stacks[CurCtx][498];
portRESTORE_CONTEXT();
temp=ECT_TCNT; //clear the flag of TOF
}
EnableInterrupts;
}

main function:
DisableInterrupts;
CreatThread( TestThread, &Stacks[0][499] );
CreatThread( NewMainTimer, &Stacks[1][499] );
CurCtx = 0;
pxCurrentTCB = &Stacks[CurCtx][498];
TickTimer_Enable();
portRESTORE_CONTEXT();
__asm(“rti”);

it sometimes can work and sometimes doesn’t .
thanks for any help.

Not sure if this helps or not - it is using a very old compiler: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/master/portable/CodeWarrior/HCS12/port.c

i found my thread has some serious problems.i found my variables such A,B did the plus 1 operation,but they would be modified to 0 at sometimes periodic,and i added C,D did the some work,and they all would be modified either.then i put the function in main loop,it has nothing.