This the multi-page printable view of this section.Click here to print.

Return to the regular view of this page.

Binary operations

Operations that work in Binary or individual Bits

Table of Contents

1 - AND

And Accumulator with Memory

AND performs a logical And of the value in the accumulator with that of the memory location with the result stored in the accumulator.

The result will be each bit in the accumulator will be set ONLY if that same bit was set in the original accumulator value and the memory. If the bits were different then the resultant bit will be 0.

AND truth table
Second Operand
First Operand01
000
101

For 8-bit processors n has the value of bit 7 and v the value of bit 6 of the memory location.

For 16-bit processors, when m=0, n has the value of bit 15 and v the value of bit 14 of the memory location.

Second it performs a logical AND of the memory and the accumulator. If the result is zero the z flag is set.

In both operations, the contents of the accumulator and memory are not modified.

Flags Affected
Flags
n-----z-
nSet if most significant bit of result is set
zSet if result is zero, otherwise clear
Instructions
SyntaxOpcode Available on: # of # of Addressing Mode
(hex) 6502 65C02 65816 bytes cycles
AND #const29 x x x 21 22 Immediate
AND addr2D x x x 3 42 Absolute
AND long2F x 4 52 Absolute Long
AND dp25 x x x 2 32, 3 Direct Page
AND (dp)32 x x 2 52, 3 Direct Page Indirect
AND [dp]27 x 2 62, 3 Direct Page Indirect Long
AND addr,X3D x x x 3 42, 4 Absolute Indexed X
AND long,X3F x 4 52 Absolute Long Indexed X
AND addr,Y39 x x x 3 42, 4 Absolute Indexed Y
AND dp,X35 x x x 2 42, 3 Direct Page Indexed X
AND (dp,X)21 x x x 2 62, 3 Direct Page Indexed Indirect X
AND (dp),Y31 x x x 2 52, 3, 4 Direct Page Indirect Indexed Y
AND [dp],Y37 x 2 62, 3 Direct Page Indirect Long Indexed Y
AND sr,S23 x 2 42 Stack Relative
AND (sr,S),Y33 x 2 72 Stack Relative Indirect Indexed Y

Notes:

  1. 65816: Add 1 byte if m=0 (16-bit memory/accumulator)
  2. 65816: Add 1 cycle if m=0 (16-bit memory/accumulator)
  3. 65816: Add 1 cycle if low byte of Direct Page register is not 0
  4. Add 1 cycle if adding index crosses a page boundary

2 - BIT

Test Memory Bits against Accumulator

Bit is a dual-purpose instruction which performs operations against the accumulator and memory. It is usually used immediately preceding a conditional branch instruction

First it set's the n flag to reflect the value of the high bit of the data in memory and the v flag to the next-to-highest bit of that data.

For 8-bit processors n has the value of bit 7 and v the value of bit 6 of the memory location.

For 16-bit processors, when m=0, n has the value of bit 15 and v the value of bit 14 of the memory location.

Second it performs a logical AND of the memory and the accumulator. If the result is zero the z flag is set.

In both operations, the contents of the accumulator and memory are not modified.

Flags Affected
Flags
nv----z-
nTakes value of most significant bit of memory data, not in immediate addressing
vTakes value of the next-to-highest bit of memory data, not in immediate addressing
zSet if logical AND of memory & accumulator is zero, otherwise clear
Instructions
SyntaxOpcode Available on: # of # of Addressing Mode
(hex) 6502 65C02 65816 bytes cycles
BIT #const89 x x 21 22 Immediate
BIT addr2C x x x 3 42 Absolute
BIT dp24 x x x 2 52, 3 Direct Page
BIT addr,X3C x x 3 42, 4 Absolute Indexed X
BIT dp,X34 x x 2 42, 3 Direct Page Indexed X

Notes:

  1. 65816: Add 1 byte if m=0 (16-bit memory/accumulator)
  2. 658116: Add 1 cycle if m=0 (16-bit memory/accumulator)
  3. 65816: Add 1 cycle if low byte of Direct Page register is not 0
  4. Add 1 cycle if adding index crosses a page boundary

3 - EOR - Exclusive OR

Exclusive-OR Accumulator with Memory

EOR performs a bitwise logical Exclusive-OR of the value in the accumulator with that of the memory location with the result stored in the accumulator.

The result will be each bit in the accumulator will be set ONLY if that same bit in the original accumulator value and the memory differ. If the bits were the same then the resultant bit will be 0.

Exclusive OR truth table
Second Operand
First Operand01
001
110

For 8-bit processors n has the value of bit 7 and v the value of bit 6 of the memory location.

For 16-bit processors, when m=0, n has the value of bit 15 and v the value of bit 14 of the memory location.

Second it performs a logical AND of the memory and the accumulator. If the result is zero the z flag is set.

In both operations, the contents of the accumulator and memory are not modified.

Flags Affected
Flags
n-----z-
nSet if most significant bit of result is set
zSet if result is zero, otherwise clear
Instructions
SyntaxOpcode Available on: # of # of Addressing Mode
(hex) 6502 65C02 65816 bytes cycles
EOR #const49 x x x 21 22 Immediate
EOR addr4D x x x 3 42 Absolute
EOR long4F x 4 52 Absolute Long
EOR dp45 x x x 2 32, 3 Direct Page
EOR (dp)52 x x 2 52, 3 Direct Page Indirect
EOR [dp]47 x 2 62, 3 Direct Page Indirect Long
EOR addr,X5D x x x 3 42, 4 Absolute Indexed X
EOR long,X5F x 4 52 Absolute Long Indexed X
EOR addr,Y59 x x x 3 42, 4 Absolute Indexed Y
EOR dp,X55 x x x 2 42, 3 Direct Page Indexed X
EOR (dp,X)41 x x x 2 62, 3 Direct Page Indexed Indirect X
EOR (dp),Y51 x x x 2 52, 3, 4 Direct Page Indirect Indexed Y
EOR [dp],Y57 x 2 62, 3 Direct Page Indirect Long Indexed Y
EOR sr,S43 x 2 42 Stack Relative
EOR (sr,S),Y53 x 2 72 Stack Relative Indirect Indexed Y

Notes:

  1. 65816: Add 1 byte if m=0 (16-bit memory/accumulator)
  2. 65816: Add 1 cycle if m=0 (16-bit memory/accumulator)
  3. 65816: Add 1 cycle if low byte of Direct Page register is not 0
  4. Add 1 cycle if adding index crosses a page boundary

4 - ORA - OR Accumulator with memory

OR Accumulator with Memory

ORA performs a bitwise logical OR of the value in the accumulator with that of the memory location with the result stored in the accumulator.

The result will be each bit in the accumulator will be set if either the same bit in the original accumulator value and the memory are set.

OR truth table
Second Operand
First Operand01
001
111

For 8-bit processors n has the value of bit 7 and v the value of bit 6 of the memory location.

For 16-bit processors, when m=0, n has the value of bit 15 and v the value of bit 14 of the memory location.

Second it performs a logical AND of the memory and the accumulator. If the result is zero the z flag is set.

In both operations, the contents of the accumulator and memory are not modified.

Flags Affected
Flags
n-----z-
nSet if most significant bit of result is set
zSet if result is zero, otherwise clear
Instructions
SyntaxOpcode Available on: # of # of Addressing Mode
(hex) 6502 65C02 65816 bytes cycles
ORA #const09 x x x 21 22 Immediate
ORA addr0D x x x 3 42 Absolute
ORA long0F x 4 52 Absolute Long
ORA dp05 x x x 2 32, 3 Direct Page
ORA (dp)12 x x 2 52, 3 Direct Page Indirect
ORA [dp]07 x 2 62, 3 Direct Page Indirect Long
ORA addr,X1D x x x 3 42, 4 Absolute Indexed X
ORA long,X1F x 4 52 Absolute Long Indexed X
ORA addr,Y19 x x x 3 42, 4 Absolute Indexed Y
ORA dp,X15 x x x 2 42, 3 Direct Page Indexed X
ORA (dp,X)01 x x x 2 62, 3 Direct Page Indexed Indirect X
ORA (dp),Y11 x x x 2 52, 3, 4 Direct Page Indirect Indexed Y
ORA [dp],Y17 x 2 62, 3 Direct Page Indirect Long Indexed Y
ORA sr,S03 x 2 42 Stack Relative
ORA (sr,S),Y13 x 2 72 Stack Relative Indirect Indexed Y

Notes:

  1. 65816: Add 1 byte if m=0 (16-bit memory/accumulator)
  2. 65816: Add 1 cycle if m=0 (16-bit memory/accumulator)
  3. 65816: Add 1 cycle if low byte of Direct Page register is not 0
  4. Add 1 cycle if adding index crosses a page boundary

5 - Rotate Bits

Test Memory Bits against Accumulator

The rotate instructions shifts the contents of the accumulator or memory location one bit either to the left or right.

The ROL & ROR instructions will shift in the carry flag into the value. The ASL & LSR instructions shift in 0. The carry flag is set to the bit that was shifted out of the value.

On all processors the data shifted is 8 bits.

On the 65816 with m=0, the data shifted is 16 bits.

Effect on memory for 8 bit operations.
Operation Opcode 7 6 5 4 3 2 1 0
Shift left ASL C 0
Shift left with carry ROL C C
Shift right LSR 0 C
Shift right with carry ROR C C

ASL - Shift Memory or Accumulator Left

Shift the value left one bit. The left most bit is transferred into the carry flag. The right most bit is cleared.

The arithmetic result of the operation is an unsigned multiplication by two.

LSR - Logical Shift Memory or Accumulator Right

Shift the value right one bit. The right most bit is transferred into the carry flag. The left most bit is cleared.

The arithmetic result of the operation is an unsigned division by two.

ROL - Rotate Memory or Accumulator Left

Shift the value left one bit. The right most bit is set to the initial value of the carry flag. The left most bit is transferred into the carry flag.

ROR - Rotate Memory or Accumulator Right

Shift the value right one bit. The left most bit is set to the initial value of the carry flag. The right most bit is transferred into the carry flag.

Multi-word shifts

These instructions can be combined to handle multiple word values:

Multi-word shift left

To shift left multiple words, use ASL for the first operation then ROL for the subsequent words.

1  ; Shift 16-bit value at &70 left 1 bit.
2  ; This is effectively a multiplication by 2
3  ASL &70 ; Shift left low-order byte
4  ROL &71 ; Shift left high-order byte
5  ; Carry will be set if we overflowed
6  

For higher precision simply add an additional ROL for the next order byte.

Multi-word shift right

To shift right multiple words, use LSR for the first operation then ROR for the subsequent words. Unlike shifting left, here we have to start with the high-order byte first.

1  ; Shift 16-bit value at &70 right 1 bit.
2  ; This is effectively a division by 2
3  LSR &71 ; Shift right high-order byte
4  ROR &70 ; Shift right low-order byte
5  ; Carry will have the remainder
6  

For higher precision just start the LSR on the higher order byte & use ROL for each lower order.

Flags Affected
Flags
n-----zc
nSet if the most significant bit of the result is set
zSet if the result is zero
cThe value of the bit shifted out of the result
Instructions
SyntaxOpcode Available on: # of # of Addressing Mode
(hex) 6502 65C02 65816 bytes cycles
ASL A0A x x x 1 2 Accumulator
ASL addr0E x x x 3 6 Absolute
ASL dp06 x x x 2 51, 2 Direct Page
ASL addr,X1E x x x 3 71, 3 Absolute Indexed X
ASL dp,X16 x x x 2 61, 2 Direct Page Indexed X
LSR A4A x x x 1 2 Accumulator
LSR addr4E x x x 3 6 Absolute
LSR dp46 x x x 2 51, 2 Direct Page
LSR addr,X5E x x x 3 71, 3 Absolute Indexed X
LSR dp,X56 x x x 2 61, 2 Direct Page Indexed X
ROL A2A x x x 1 2 Accumulator
ROL addr2E x x x 3 6 Absolute
ROL dp26 x x x 2 51, 2 Direct Page
ROL addr,X3E x x x 3 71, 3 Absolute Indexed X
ROL dp,X36 x x x 2 61, 2 Direct Page Indexed X
ROR A6A x x x 1 2 Accumulator
ROR addr6E x x x 3 6 Absolute
ROR dp66 x x x 2 51, 2 Direct Page
ROR addr,X7E x x x 3 71, 3 Absolute Indexed X
ROR dp,X76 x x x 2 61, 2 Direct Page Indexed X

Notes:

  1. 65816: Add 2 cycles if m=0 (16-bit memory/accumulator)
  2. 65816: Add 1 cycle if low byte of Direct Page register is not 0
  3. 65C02: Subtract 1 cycle if no page boundary is crossed

6 - TRB & TSB

Test & Set/Reset Memory Bits against Accumulator

TRB - Test & Reset memory against Accumulator

TRB logically AND's the complement of the accumulator with the data at an address and stores the result in that address.

This has the effect of clearing each memory bit which is set in the accumulator, leaving the other bits unchanged.

The z flag is set based on a different operation. It's set if the memory location once set logically AND the accumulator (not it's compliment) is zero.

For 8-bit processors or when m=1, the values in the accumulator & memory are 8-bit.

For 16-bit processors, when m=0, the values in the accumulator & memory are 16-bit.

TSB - Test & Set memory against Accumulator

TSB is identical to TRB except it sets the bits defined in the Accumulator not reset them.

Flags Affected
Flags
------z-
zSet if logical AND of memory & accumulator is zero, otherwise clear
Instructions
SyntaxOpcode Available on: # of # of Addressing Mode
(hex) 6502 65C02 65816 bytes cycles
TRB addr1C x x 3 61 Absolute
TRB dp14 x x 2 51, 2 Direct Page
TSB addr0C x x 3 61 Absolute
TSB dp04 x x 2 51, 2 Direct Page

Notes:

  1. 65816: Add 2 cycles if m=0 (16-bit memory/accumulator)
  2. 65816: Add 1 cycle if low byte of Direct Page register is not 0