Inline assembly

Thread Starter

tom66

Joined May 9, 2009
2,595
I am trying to write a slow C function in assembly.

To do this I want to embed assembly into my C code. The compiler is MPLAB C30. The manual explains how to do this, so that is great. What is bugging me, is how to avoid clobbering the W regs and memory space the compiler allocates. Say I have code like this, which processes each byte in 6 cycles (addr to be replaced with an address.)

Rich (BB code):
; first code
mov     #5, W0
mov     #31, W1

; subsequent bytes
mov     _TRISB, W2
rlc     addr
bsw.c   W2, W0
mov     W2, _TRISB
nop
mov     W1, _PORTB
As can be clearly seen W0, W1 and W2 would be clobbered by this and the processor would probably get confused or the program wouldn't work properly...

So how do I avoid this?

Any help appreicated!!
 

Thread Starter

tom66

Joined May 9, 2009
2,595
Found a solution, but it's not pretty.

My rather hackish solution to this was to create a dummy function which took three arguments. Those three arguments were discarded, and the dummy function effectively ate W0, W1 and W2, forcing the compiler to find another place for those variables.

The code was then put in a separate assembly file with the name of _asm_processing, and in the C code I called asm_processing (with no underscore.) The MPLAB C30 reference manual explains how to do a lot of this.
 
Top