This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

"using 0" ==> weird bugs involving R0

we just figured out that placing "using 0" after an interrupt handler definition is a bad idea. At least when using optimize 9,size, "using 0" tells the compiler not to bother pushing/popping R0. Then it happily goes around writing to R0, doesn't pop it, and all sorts of interesting (bad) things happen. Take away "using 0," and the compiler starts pushing/popping R0, and life gets boring (good) again.

  • "Using" is essentially manual assignment of the four register banks (first 32 bytes of data memory) in the 8051. When you specify "using", you're telling the compiler that you've guaranteed that register bank to this particular routine, which implies you've told it not to use bank 0 everywhere else. You've reserved bank 0 for this interrupt routine and any other routines you, the programmer, have determined do not conflict with that ISR.

    This is a particularly difficult thing to do with bank 0, since it's the default bank used by all the code if you don't specify another one. Rather than scatter "using 1" after every single other function in the code, it's probably easier to assign bank 1, 2, or 3 to your ISR and leave 0 for the main code.

    I find "using" useful only for small projects, where you don't need much data space and performance of a couple of ISRs is critical, so that they deserve dedicated data/register space.

  • Spencer;
    The manual warns against "using 0". This gives the compiler/linker permission to trash some of the registers without saving. Since Reg Bank 0 is the default system Reg Bank, never attempt to re-assign.
    Also, just a note. Never skip a reg bank assignment. That is, don't assign "using 1" and "using 3". You just moved the stack pointer default to the last position of reg bank 3 and lost 8 precious bytes at the location of reg bank 2.
    Bradford