Electronic – in dsPIC33 assembler, can I use all of the w registers

assemblerpic

Most of my dsPIC3 application is written in C, but one function is particularly speed sensitive, so I had to write that function in assembler. The function looks roughly like this:

_FunctionName:
lnk #0x0
push w0 .. w15

< code that uses all registers >

pop w15 .. w0

ulnk
return

Is this a bad idea? Or will the use of some registers cause me problems, even if I've pushed them onto the stack?

Best Answer

The C30 compiler expects a subroutine to preserve W8-W15 but it may trash W0-W7. You therefore only need to save/restore W8-W14 if you are going to use them. Otherwise if you can make do with W0-W7 for scratch, then there is no need to save/restore anything.

Note that you never explicitly push/pop W15 since that's the stack pointer. The way to save/restore it is to pop the same amount of stuff off the stack that you push onto it, including the subroutine return address which is generally popped by executing RETURN.

I set the C30 mode to not use stack frames. Stack frames can occasionally be a aid in debugging, but otherwise they are just a waste of space and cycles. There is a command line option to the C30 compiler which prevents it from using stack frames, and therefore doesn't emit the silly link/unlink sequences. I have so far always used that and not had problems.

One other issue with your code above is that you forgot to declare _FunctionName global.