[En-Nut-Discussion] Hardware Register Access Using Volatile Pointer

Nathan Moore nategoose at gmail.com
Tue Apr 26 16:38:57 CEST 2011


You should note that I had an error in the value used in the switch
statement used in the code that I posed, which
I think may have confused you.  The switch was supposed to use the
sizeof the value rather than the actual value
as the branch value, so that different sized values read in from IO
registers can be handled differently.

> #define FAKE_USE(_x) if(_x) { asm volatile (" nop"); }

That forces actual assembly code to be generated, which increases the
program size and decreases the program's speed.
In this case there would likely be a compare, a conditional branch,
and a no-op instruction introduced into the program.

         test _x
         jz  past_nop
         nop
     past_nop:

The method I suggested was intended to not introduce any assembly
code, but only fool the compiler into thinking that we
used the value.  If I had typed the code the way I intended this would
have been more apparent.

    #define FAKE_USE( x )  \
        do { \
             typeof(x) y = (x); \
             switch ( sizeof( y ) ) { \
                   case 1: \
                        asm volatile ("" : : "r" (y) : ); \
                        break;  \
                   case 2: \
                        asm volatile ("" : : "w" (y) : ); \
                        break; \
                 ...
                   default: \
                        assert(("Unexpected size in dummy usage", 0)); \
                 } \
         } while (0);


The sizeof(y) will be a compile time constant, so this shouldn't
result in any actual code generated.



More information about the En-Nut-Discussion mailing list