SourceForge has been redesigned. Learn more.

Preprocessor, inline assembler and SFR.

  • Miha Ulanov

    Miha Ulanov - 2008-01-22

    Let's consider the following code:

    #include <8051.h>
    #define MY_PIN P1_1
    void pin_reset() {
        MY_PIN = 0;

    This compiles into

            clr     _P1_1

    and works fine.

    Now, let's imagine that I have dissatisfied with compiler optimization or something else and want to rewrite my function using assembly. How must I reference MY_PIN from inline assembly?
    If I write this

    void pin_reset() {
        setb MY_PIN

    preprocessor changes it into setb P1_1 and resulting asm doesn't links because of absence of P1_1 name:

            clr     P1_1

    Is there any fine way to refer SFR defines from inline assembly?
    Now I have to write ugly code in my .h's like:

    #define MY_PIN P1_1
    #define MY_ANOTHER_PIN P1_2
    #define _MY_PIN _P1_1
    #define _MY_ANOTHER_PIN _P1_2

    and call underscored version of these macros from assembly, and not underscored from C.

    • sb

      sb - 2008-02-02

      Of course you could look up the actual SFR address of P1.1 and write

      __sbit __at (0x91) MY_PIN; //P1.1

      in your .h's, but this you probably figured out yourself.

      At least you would prevent duplicate definitions of every pin definition, but it might still be considered "ugly" though.

      This seems like a problem since the underscore notation is needed when refering to variables or SFS's in the inline assembly code, and I would also appreciate a more smoth solution to this problem.


Log in to post a comment.