Shop OBEX P1 Docs P2 Docs Learn Events
Applying mask to long — Parallax Forums

Applying mask to long

escherescher Posts: 138
edited 2017-06-05 23:01 in Propeller 1
This is a conceptual question but I'm having an issue applying a solution in ASM:

If you have let's say a WORD:

1010101010101010

And you want to set some bits in a LONG, via a mask such as:

00000000001111111111111111000000

So that the LONG reads:

00000000001010101010101010000000

Without affecting any bits not specified by the mask, and appropriately toggling those IN the mask from 1-> 0 or 0 -> 1 as necessary.

I've bashed out a bunch of different attempted solutions but they've all had caveats or corner cases or been simply bad.

Do I HAVE to perform an OR operation to set the high bits, and an ANDN to set the low bits, separately?

Thank you!

Comments

  • jmgjmg Posts: 15,144
    escher wrote: »
    Do I HAVE to perform an OR operation to set the high bits, and an ANDN to set the low bits, separately?
    Pretty much.
    It is a two operation process to define a subset, but if you want to avoid an interim illegal value, (eg all low or all hi on a port) you can use XOR-AND-XOR

  • jmg wrote: »
    escher wrote: »
    Do I HAVE to perform an OR operation to set the high bits, and an ANDN to set the low bits, separately?
    Pretty much.
    It is a two operation process to define a subset, but if you want to avoid an interim illegal value, (eg all low or all hi on a port) you can use XOR-AND-XOR

    Can you elaborate on XOR-AND-XOR?
  • When working with a memory location it might not matter to have interim values during multiple operations. Things are different when acting on ina/outa. There interim values affecting other pins might be problematic.

    Usually inside a Pasm-Cog you 'filter/mask' outa anyways by using dira to explicit declare pins as output, so interim values are not really a problem.

    Not sure about XOR-AND-XOR either.

    Enjoy!

    Mike


  • jmgjmg Posts: 15,144
    escher wrote: »
    Can you elaborate on XOR-AND-XOR?
    Taking your example, here is XOR-AND-XOR
    applied to 
    AAAA000000111110000111111100XXXX  Pins
    ----------1010101010101010------  XOR with NewValue
    AAAA000000010100101101010100XXXX  Tmp = Pins XOR NewValue
    00000000001111111111111111000000  AND  to remove out of scope bits
    00000000000101001011010101000000  These are bits that need to be toggled
    AAAA000000101010101010101000XXXX  Pins = Pins XOR Tmp
    

    If the destination is just some internal RAM that does not need atomic changes, the OR ANDN is ok.

Sign In or Register to comment.