I'm trying to save the context of the registers and store the stack pointer to a C variable from Assembly, it works but it throws many errors and crashes when it gets to the return statement.
Here is the code:
_saveContext:
PUSH SR
PUSH W0
MOV #32, W0
MOV W0, SR
PUSH W1
PUSH.D W2
PUSH.D W4
PUSH.D W6
PUSH.D W8
PUSH.D W10
PUSH.D W12
PUSH W14
PUSH RCOUNT
PUSH TBLPAG
PUSH ACCAL
PUSH ACCAH
PUSH ACCAU
PUSH ACCBL
PUSH ACCBH
PUSH ACCBU
PUSH DCOUNT
PUSH DOSTARTL
PUSH DOSTARTH
PUSH DOENDL
PUSH DOENDH
PUSH CORCON
PUSH PSVPAG
MOV W15, W0
MOV W0, _stackPointer //Save to C Var
RETURN
When simulating the code in MPLAB it works, the C variable gets assigned the value of the stack pointer but when it gets to RETURN it causes multiple errors:
"CORE-E0004: Trap due to unimplemented FLASH memory access, occurred from instruction at 0xXXXXXX" errors
I think it might have something to do with the way I am accessing the stackpointer (W15).
Anyone have any advice? Thanks
Well I found a work around, I will post it here maybe it will help someone else.
The solution was adding the code into my C file directly and getting rid of the assembly file completely. Instead of calling the assembly subroutine I just added a block of assembly into C:
asm volatile("PUSH SR \n"
"PUSH.D W0 \n"
"PUSH.D W2 \n"
"PUSH.D W4 \n"
"PUSH.D W6 \n"
"PUSH.D W8 \n"
"PUSH.D W10 \n"
"PUSH.D W12 \n"
"PUSH W14 \n"
"PUSH RCOUNT \n"
"PUSH TBLPAG \n"
"PUSH ACCAL \n"
"PUSH ACCAH \n"
"PUSH ACCAU \n"
"PUSH ACCBL \n"
"PUSH ACCBH \n"
"PUSH ACCBU \n"
"PUSH DCOUNT \n"
"PUSH DOSTARTL \n"
"PUSH DOSTARTH \n"
"PUSH DOENDL \n"
"PUSH DOENDH \n"
"PUSH CORCON \n"
"PUSH PSVPAG \n"
"MOV W15, _stackPointer \n");
This solved all the problems I was having