Jan Turoň
Jan Turoň

Reputation: 32922

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.

Upvotes: 0

Views: 370

Answers (2)

Jester
Jester

Reputation: 58802

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.

Upvotes: 1

Kenney
Kenney

Reputation: 9093

I'm not familiar with that CPU but you could do it with 1 AND, 1 SHL and 1 OR operating on bytes.

On Intel 8086+, where al stands for 8 bit-ACC and bl stands for 8bit-P1, it would go like this:

and bl, 0x0f    # clear high nybble
ror al, 8       # or shl 8 if accumulator can be discarded
or  bl, al

Upvotes: 1

Related Questions