Search code examples
assemblybit-manipulation8051nibble

Elegant way to set SFR nibble


I'd like to move a nibble from Accumulator to upper nibble of P1.

For now, I set the nibble bit by bit

MOV C, ACC.3
MOV P1.7, C
MOV C, ACC.2
MOV P1.6, C
MOV C, ACC.1
MOV P1.5, C
MOV C, ACC.0
MOV P1.4, C

which seems like a poor way to me: it costs 12 instruction cycles and the code is long. I hoped SWAP and XCHD would do the trick, but indirect addressing doesn't seem to work in SFR memory area.

Is there any quicker or shorter (not necessarily both) way to code it? I'd like to leave the lower nibble of P1 untouched.


Solution

  • If you are using the low 4 bits of P1 as input you want them to be set to 1 and that's easily combined into your code.

    swap A
    orl A, #15
    mov P1, A
    

    If you are using them as output, you can use read-modify-write, such as:

    anl A, #15
    swap A
    anl P1, #15
    orl P1, A
    

    Note, this will momentarily zero pins 3-7. If that's not acceptable you will have to calculate the new value in a register.