Inline assembly

Discussion in 'Programmer's Corner' started by tom66, Jul 13, 2010.

  1. tom66

    Thread Starter Senior Member

    May 9, 2009
    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.)

    Code ( (Unknown Language)):
    2. ; first code
    3. mov     #5, W0
    4. mov     #31, W1
    6. ; subsequent bytes
    7. mov     _TRISB, W2
    8. rlc     addr
    9. bsw.c   W2, W0
    10. mov     W2, _TRISB
    11. nop
    12. 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!!
  2. tom66

    Thread Starter Senior Member

    May 9, 2009
    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.