Difference between revisions of "Setting Clearing and Copying bits of data"

From BeebWiki
Jump to: navigation, search
(Created page with "Category:6502 AND xx will clear the bits in A that are also clear in xx, for example: A xx after AND xx %abcdefgh %01010101 %0b0d0f0h ORA...")
 
(Added swapping data.)
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
 
[[Category:6502]]
 
[[Category:6502]]
 +
===Clearing bits===
 
AND xx will clear the bits in A that are also clear in xx, for example:
 
AND xx will clear the bits in A that are also clear in xx, for example:
  
Line 5: Line 6:
 
   %abcdefgh    %01010101    %0b0d0f0h
 
   %abcdefgh    %01010101    %0b0d0f0h
  
 +
===Setting bits===
 
ORA xx will set the bits in A that are also set in xx, for example:
 
ORA xx will set the bits in A that are also set in xx, for example:
  
Line 10: Line 12:
 
   %abcdefgh    %01010101    %a1c1e1g1
 
   %abcdefgh    %01010101    %a1c1e1g1
  
 +
===Toggling bits===
 
EOR xx will toggle the bits in A that are set in xx, for example:
 
EOR xx will toggle the bits in A that are set in xx, for example:
  
Line 15: Line 18:
 
   %abcdefgh    %01010101    %aBcDeFgH
 
   %abcdefgh    %01010101    %aBcDeFgH
  
To clear the bits in A that are ''set'' in xx, use both ORA and EOR, for example:
+
===Clear complementary bits===
 
+
To clear the bits in A that are ''set'' in xx, use both ORA and EOR, for
 +
example:
  
 
       A            xx      after ORA xx  after EOR xx
 
       A            xx      after ORA xx  after EOR xx
 
   %abcdefgh    %01010101    %a1c1e1g1    %a0c0e0g0
 
   %abcdefgh    %01010101    %a1c1e1g1    %a0c0e0g0
  
You can copy a number of bits to a memory location without changing the other bits using EOR and AND. For example, to copy the top four bits of A into a memory location without changing the bottom four bits, use the following:
+
===Copying bits===
 +
You can copy a number of bits to a memory location without changing the
 +
other bits using EOR and AND. For example, to copy the top four bits of A
 +
into a memory location without changing the bottom four bits, use the
 +
following:
  
 
                 A=12345678  dst=abcdefgh
 
                 A=12345678  dst=abcdefgh
Line 35: Line 43:
 
     PLA
 
     PLA
 
     AND #&F0:ORA dst:STA dst
 
     AND #&F0:ORA dst:STA dst
 +
 +
===Swapping data===
 +
Data can be swapped between two different memory locations without using a
 +
temporary location by EORing the data in both directions:
 +
 +
    LDA val1:EOR val2:STA val1
 +
            EOR val2:STA val2
 +
            EOR val1:STA val1
 +
 +
This is in contrast to using a temporary location or register such as with:
 +
 +
    LDA val1:STA tmp
 +
    LDA val2:STA val1
 +
    LDA tmp:STA val2
  
 
[[User:Jgharston|Jgharston]] 18:58, 30 August 2007 (BST)
 
[[User:Jgharston|Jgharston]] 18:58, 30 August 2007 (BST)
 +
[[User:Jgharston|Jgharston]] ([[User talk:Jgharston|talk]]) 07:10, 30 June 2018 (CEST)

Latest revision as of 07:10, 30 June 2018

Contents

Clearing bits

AND xx will clear the bits in A that are also clear in xx, for example:

      A            xx      after AND xx
  %abcdefgh    %01010101    %0b0d0f0h

Setting bits

ORA xx will set the bits in A that are also set in xx, for example:

      A            xx      after ORA xx
  %abcdefgh    %01010101    %a1c1e1g1

Toggling bits

EOR xx will toggle the bits in A that are set in xx, for example:

      A            xx      after EOR xx
  %abcdefgh    %01010101    %aBcDeFgH

Clear complementary bits

To clear the bits in A that are set in xx, use both ORA and EOR, for example:

      A            xx      after ORA xx  after EOR xx
  %abcdefgh    %01010101    %a1c1e1g1    %a0c0e0g0

Copying bits

You can copy a number of bits to a memory location without changing the other bits using EOR and AND. For example, to copy the top four bits of A into a memory location without changing the bottom four bits, use the following:

               A=12345678  dst=abcdefgh
   EOR dst       ********      abcdefgh
   AND #&F0      ****0000      abcdefgh
   EOR dst       1234efgh      abcdefgh
   STA dst       1234efgh      1234efgh

This is much more efficient than the usual code:

   PHA
   LDA dst:AND #&0F:STA dst
   PLA
   AND #&F0:ORA dst:STA dst

Swapping data

Data can be swapped between two different memory locations without using a temporary location by EORing the data in both directions:

   LDA val1:EOR val2:STA val1
            EOR val2:STA val2
            EOR val1:STA val1

This is in contrast to using a temporary location or register such as with:

   LDA val1:STA tmp
   LDA val2:STA val1
   LDA tmp:STA val2

Jgharston 18:58, 30 August 2007 (BST) Jgharston (talk) 07:10, 30 June 2018 (CEST)