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

swap equivelant.

Hi,

Is there an equivelan function or keyword to "swap"? Is there anyway to get the high or lower nibbles of a variable or swap them in C? any help would be appreciated.

-=N

Parents
  • I don't think you have direct access to the 8051 swap instruction. It's not one of the intrinsic functions (though it might make a useful addition, perhaps).

    This code will do the job:

    U8 lo = n & 0x0f;
    U8 hi = n >> 4;
    U8 swap = (n << 4) | (n >> 4);

    But you'll have to check the output from the compiler to see if they're clever enough to generate SWAP instructions for that.

    If not, and it's a key part of your project, you could just write your own little assembly function.

    PUBLIC  _swap
    _swap:  MOV A, R7
            SWAP
            MOV R7, A
            RET
    

    Marshalling the argument into R7 and putting it back when you return from the call are probably going to make this more expensive than the (n & 0x0f) operation, at least. Might be a little better than the shifts, if those shifts are taken literally.

    Try an experiment, and let us know what the code looks like.

Reply
  • I don't think you have direct access to the 8051 swap instruction. It's not one of the intrinsic functions (though it might make a useful addition, perhaps).

    This code will do the job:

    U8 lo = n & 0x0f;
    U8 hi = n >> 4;
    U8 swap = (n << 4) | (n >> 4);

    But you'll have to check the output from the compiler to see if they're clever enough to generate SWAP instructions for that.

    If not, and it's a key part of your project, you could just write your own little assembly function.

    PUBLIC  _swap
    _swap:  MOV A, R7
            SWAP
            MOV R7, A
            RET
    

    Marshalling the argument into R7 and putting it back when you return from the call are probably going to make this more expensive than the (n & 0x0f) operation, at least. Might be a little better than the shifts, if those shifts are taken literally.

    Try an experiment, and let us know what the code looks like.

Children