EOR

From C64-Wiki
Jump to navigationJump to search

EOR (short for "Exclusive OR") is the mnemonic for a machine language instruction which performs a bit-wise boolean "Exclusive-or" between each of the eight bits in the accumulator and their corresponding bits in the memory address specified. The eight resulting bits form a byte, which is stored in the accumulator.

Addressing modes[edit]

Opcode Addressing
mode
Assembler
format
Length
in bytes
Number of
cycles
Dec Hex
73 49 Immediate EOR #nn 2 2
77 4D Absolute EOR nnnn 3 4
93 5D Absolute,X EOR nnnn,X 3 4*
89 59 Absolute,Y EOR nnnn,Y 3 4*
69 45 Zeropage EOR nn 2 3
85 55 Zeropage,X EOR nn,X 2 4
65 41 Indexed-indirect EOR (nn,X) 2 6
81 51 Indirect-indexed EOR (nn),Y 2 5*

EOR supports eight different addressing modes, as shown in the table at right. In the assembler formats listed, nn represents a single-byte (8-bit) figure, and nnnn is a two-byte (16-bit) address.
With some addressing forms (marked with an asterisk, *, in the "Number of cycles" column) the execution time for EOR depends on the circumstances: In cases where the indexing requires the CPU to "reach across" a page boundary from the base address, the execution time is 1 cycle longer than listed here.

CPU flags[edit]

EOR affects 2 of the CPU's status flags:

  • The negative flag is set if the result is negative, i.e. has its most significant bit set.
  • The zero flag is set if the result is zero, or cleared if it is non-zero.

Examples[edit]

One's complement/NOT using EOR[edit]

Some CPU architectures supports a one's complement, a simple "not" (inverting all bits) by their instruction set with an separate implicit instruction. The 6502 family has to use the more general EOR with the proper argument to accomplish this:

EOR #$FF

to operate on the accumulator.

Detect if two bytes have opposite signs[edit]

EOR can be used in a simple trick to check if two bytes have opposite signs:

LDA addr1
EOR addr2 ; N flag is set if signs were opposite, else cleared
; use BPL or BMI here to branch

Correction for signed comparisons[edit]

SEC
LDA Value1        ; take the difference (signed values) Value1 - Value2
SBC Value2        ; can't use CMP because it does not set the Overflow Flag
BVC +             ; skip if no overflow
EOR #$80          ; the sign has to be inverted in case of an overflow
+
BPL IsPositive

Merge accumulator bits with memory[edit]

Merge the upper 4 bits (according to the mask from the AND instruction) from the accumulator with the lower 4 bit of location Value and store the result there.

EOR Value
AND #$F0     ; keep upper 4 bits from A
EOR Value    ; upper 4 bits now restored, lower 4 bits "ored" in from memory
STA Value    ;