Question

I want to write a small assembly routine which uses a temporary register. When I say temporary register I mean it's not an input or output register in the sense of constraints for an asm block. I could just pick any register and then include it in the clobber list, but I thought it would be nicer for the compiler to be able to choose. What is the correct way to handle this? The only suggestion I've found online is to list it as an output register and then not actually use the output.

Was it helpful?

Solution

You can't use the r (general register) constraint in the clobber list. And an input-only register is assumed to be unmodified by the asm statement. The best solution is to specify the temp as an output register, which gives the compiler the option of discarding the 'result', as well as being able to retire the register.

unsigned long tmp; /* register 'word' type. */

__asm__ ("..." : "=r" (tmp), ... : <inputs> : <clobbered>);

You can now refer to the temp register as %0, in this example. Provided that the tmp variable is never used, the compiler can discard the result and continue to (re)use the register.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top