简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM