In this section we cover every available instruction for both 8-bit & 16-bit processors.
This the multi-page printable view of this section.Click here to print.
In this section we cover every available instruction for both 8-bit & 16-bit processors.
Adds the data in the operand with the contents of the accumulator. Add 1 to the result if the carry flag is set. Store the final result in the accumulator.
If the d flag is clear then binary addition is performed. If the d flag set then Binary Coded Decimal (BCD) addition is performed.
On all processors, the data added from memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data added is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
In multi-precision (multi-word) arithmetic, the carry flag should be cleared before the low-order words are added. The addition will generate a new carry flag value based on that addition which will then be passed on to the next word.
For example, to add 1 to a 16-bit value at &70 on 8-bit processors:
1 CLC ; Clear carry before first addition
2 LDA &70 ; Add 1 to low-order byte
3 ADC #1
4 STA &70
5 LDA &71 ; Add 0 to high order byte
6 ADC #0 ; This will add 1 if carry was set
7 STA &71 ; in the low-order byte
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most-significant bit of result is set | ||||||||
v | Set if signed overflow | ||||||||
z | Set if result is zero | ||||||||
c | Set if unsigned overflow, clear if valid unsigned result |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
ADC #const | 69 | x | x | x | 21 | 22, 5 | Immediate |
ADC addr | 6D | x | x | x | 3 | 42, 5 | Absolute |
ADC long | 6F | x | 4 | 52, 5 | Absolute Long | ||
ADC dp | 65 | x | x | x | 2 | 32, 3, 5 | Direct Page |
ADC (dp) | 72 | x | x | 2 | 52, 3, 5 | Direct Page Indirect | |
ADC [dp] | 67 | x | 2 | 62, 3, 5 | Direct Page Indirect Long | ||
ADC addr,X | 7D | x | x | x | 3 | 42, 4, 5 | Absolute Indexed X |
ADC long,X | 7F | x | 4 | 52, 5 | Absolute Long Indexed X | ||
ADC addr,Y | 79 | x | x | x | 3 | 42, 4, 5 | Absolute Indexed Y |
ADC dp,X | 75 | x | x | x | 2 | 42, 3, 5 | Direct Page Indexed X |
ADC (dp,X) | 61 | x | x | x | 2 | 62, 3, 5 | Direct Page Indexed Indirect X |
ADC (dp),Y | 71 | x | x | x | 2 | 52, 3, 4, 5 | Direct Page Indirect Indexed Y |
ADC [dp],Y | 77 | x | 2 | 62, 3, 5 | Direct Page Indirect Long Indexed Y | ||
ADC sr,S | 63 | x | 2 | 42, 5 | Stack Relative | ||
ADC (sr,S),Y | 73 | x | 2 | 72, 5 | Stack Relative Indirect Indexed Y |
The decrement instructions add one to either a register or a memory location.
Unlike subtracting 1 with ADC, these instructions does not use the Carry flag in any way. You can test for wraparound only by testing after every decrement to see if the result is zero or negative.
The d flag does not affect these instructions. The decrement is always in binary mode.
For all processors, the decrement is an 8-bit operation unless m=0 on the 65816 in which case the decrement is 16-bit.
Decrement by 1 the contents of the memory location or accumulator.
Decrement by 1 the X index register.
Decrement by 1 the Y index register.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most significant bit of the result is set | ||||||||
z | Set if result is zero |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
DEC A | 3A | x | x | 1 | 2 | Accumulator | |
DEC addr | CE | x | x | x | 3 | 61 | Absolute |
DEC dp | C6 | x | x | x | 2 | 51, 2 | Direct Page |
DEC addr,X | DE | x | x | x | 3 | 71, 3 | Absolute Indexed X |
DEC dp,X | D6 | x | x | x | 2 | 61, 2 | Direct Page Indexed X |
DEX | CA | x | x | x | 1 | 2 | Implied |
DEY | 88 | x | x | x | 1 | 2 | Implied |
The increment instructions add one to either a register or a memory location.
Unlike adding 1 with ADC, these instructions does not use the Carry flag in any way. You can test for wraparound only by testing after every increment to see if the result is zero or positive.
The d flag does not affect these instructions. The increment is always in binary mode.
For all processors, the increment is an 8-bit operation unless m=0 on the 65816 in which case the increment is 16-bit.
Increment by 1 the contents of the memory location or accumulator.
Increment by 1 the X index register.
Increment by 1 the Y index register.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most significant bit of the result is set | ||||||||
z | Set if result is zero |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
INC A | 1A | x | x | 1 | 2 | Accumulator | |
INC addr | EE | x | x | x | 3 | 61 | Absolute |
INC dp | E6 | x | x | x | 2 | 51, 2 | Direct Page |
INC addr,X | FE | x | x | x | 3 | 71, 3 | Absolute Indexed X |
INC dp,X | F6 | x | x | x | 2 | 61, 2 | Direct Page Indexed X |
INX | E8 | x | x | x | 1 | 2 | Implied |
INY | C8 | x | x | x | 1 | 2 | Implied |
Subtracts the data in the operand with the contents of the accumulator. Subtract 1 from the result if the carry flag is clear. Store the final result in the accumulator.
If the d flag is clear then binary subtraction is performed. If the d flag set then Binary Coded Decimal (BCD) subtraction is performed.
On all processors, the data subtracted from memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data subtracted is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
In multi-precision (multi-word) arithmetic, the carry flag should be set before the low-order words are subtracted. The subtraction will generate a new carry flag value based on that subtraction which will then be passed on to the next word.
For example, to subtract 1 from a 16-bit value at &70 on 8-bit processors:
1 SEC ; Set carry before first subtraction
2 LDA &70 ; Subtract 1 from low-order byte
3 SBC #1
4 STA &70
5 LDA &71 ; Subtract 0 to high order byte
6 SBC #0 ; This will subtract 1 if carry was clear
7 STA &71 ; from the low-order byte
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most-significant bit of result is set | ||||||||
v | Set if signed overflow | ||||||||
z | Set if result is zero | ||||||||
c | Set if unsigned borrow not required, clear if required |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
SBC #const | E9 | x | x | x | 21 | 22, 5 | Immediate |
SBC addr | ED | x | x | x | 3 | 42, 5 | Absolute |
SBC long | EF | x | 4 | 52, 5 | Absolute Long | ||
SBC dp | E5 | x | x | x | 2 | 32, 3, 5 | Direct Page |
SBC (dp) | F2 | x | x | 2 | 52, 3, 5 | Direct Page Indirect | |
SBC [dp] | E7 | x | 2 | 62, 3, 5 | Direct Page Indirect Long | ||
SBC addr,X | FD | x | x | x | 3 | 42, 4, 5 | Absolute Indexed X |
SBC long,X | FF | x | 4 | 52, 5 | Absolute Long Indexed X | ||
SBC addr,Y | F9 | x | x | x | 3 | 42, 4, 5 | Absolute Indexed Y |
SBC dp,X | F5 | x | x | x | 2 | 42, 3, 5 | Direct Page Indexed X |
SBC (dp,X) | E1 | x | x | x | 2 | 62, 3, 5 | Direct Page Indexed Indirect X |
SBC (dp),Y | F1 | x | x | x | 2 | 52, 3, 4, 5 | Direct Page Indirect Indexed Y |
SBC [dp],Y | F7 | x | 2 | 62, 3, 5 | Direct Page Indirect Long Indexed Y | ||
SBC sr,S | E3 | x | 2 | 42, 5 | Stack Relative | ||
SBC (sr,S),Y | F3 | x | 2 | 72, 5 | Stack Relative Indirect Indexed Y |
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.
Second Operand | |||
---|---|---|---|
First Operand | 0 | 1 | |
0 | 0 | 0 | |
1 | 0 | 1 |
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 |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most significant bit of result is set | ||||||||
z | Set if result is zero, otherwise clear |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
AND #const | 29 | x | x | x | 21 | 22 | Immediate |
AND addr | 2D | x | x | x | 3 | 42 | Absolute |
AND long | 2F | x | 4 | 52 | Absolute Long | ||
AND dp | 25 | 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,X | 3D | x | x | x | 3 | 42, 4 | Absolute Indexed X |
AND long,X | 3F | x | 4 | 52 | Absolute Long Indexed X | ||
AND addr,Y | 39 | x | x | x | 3 | 42, 4 | Absolute Indexed Y |
AND dp,X | 35 | 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),Y | 31 | x | x | x | 2 | 52, 3, 4 | Direct Page Indirect Indexed Y |
AND [dp],Y | 37 | x | 2 | 62, 3 | Direct Page Indirect Long Indexed Y | ||
AND sr,S | 23 | x | 2 | 42 | Stack Relative | ||
AND (sr,S),Y | 33 | x | 2 | 72 | Stack Relative Indirect Indexed Y |
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 |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Takes value of most significant bit of memory data, not in immediate addressing | ||||||||
v | Takes value of the next-to-highest bit of memory data, not in immediate addressing | ||||||||
z | Set if logical AND of memory & accumulator is zero, otherwise clear |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
BIT #const | 89 | x | x | 21 | 22 | Immediate | |
BIT addr | 2C | x | x | x | 3 | 42 | Absolute |
BIT dp | 24 | x | x | x | 2 | 52, 3 | Direct Page |
BIT addr,X | 3C | x | x | 3 | 42, 4 | Absolute Indexed X | |
BIT dp,X | 34 | x | x | 2 | 42, 3 | Direct Page Indexed X |
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.
Second Operand | |||
---|---|---|---|
First Operand | 0 | 1 | |
0 | 0 | 1 | |
1 | 1 | 0 |
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 |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most significant bit of result is set | ||||||||
z | Set if result is zero, otherwise clear |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
EOR #const | 49 | x | x | x | 21 | 22 | Immediate |
EOR addr | 4D | x | x | x | 3 | 42 | Absolute |
EOR long | 4F | x | 4 | 52 | Absolute Long | ||
EOR dp | 45 | 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,X | 5D | x | x | x | 3 | 42, 4 | Absolute Indexed X |
EOR long,X | 5F | x | 4 | 52 | Absolute Long Indexed X | ||
EOR addr,Y | 59 | x | x | x | 3 | 42, 4 | Absolute Indexed Y |
EOR dp,X | 55 | 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),Y | 51 | x | x | x | 2 | 52, 3, 4 | Direct Page Indirect Indexed Y |
EOR [dp],Y | 57 | x | 2 | 62, 3 | Direct Page Indirect Long Indexed Y | ||
EOR sr,S | 43 | x | 2 | 42 | Stack Relative | ||
EOR (sr,S),Y | 53 | x | 2 | 72 | Stack Relative Indirect Indexed Y |
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.
Second Operand | |||
---|---|---|---|
First Operand | 0 | 1 | |
0 | 0 | 1 | |
1 | 1 | 1 |
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 |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most significant bit of result is set | ||||||||
z | Set if result is zero, otherwise clear |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
ORA #const | 09 | x | x | x | 21 | 22 | Immediate |
ORA addr | 0D | x | x | x | 3 | 42 | Absolute |
ORA long | 0F | x | 4 | 52 | Absolute Long | ||
ORA dp | 05 | 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,X | 1D | x | x | x | 3 | 42, 4 | Absolute Indexed X |
ORA long,X | 1F | x | 4 | 52 | Absolute Long Indexed X | ||
ORA addr,Y | 19 | x | x | x | 3 | 42, 4 | Absolute Indexed Y |
ORA dp,X | 15 | 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),Y | 11 | x | x | x | 2 | 52, 3, 4 | Direct Page Indirect Indexed Y |
ORA [dp],Y | 17 | x | 2 | 62, 3 | Direct Page Indirect Long Indexed Y | ||
ORA sr,S | 03 | x | 2 | 42 | Stack Relative | ||
ORA (sr,S),Y | 13 | x | 2 | 72 | Stack Relative Indirect Indexed Y |
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.
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 |
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.
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.
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.
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.
These instructions can be combined to handle multiple word values:
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.
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 |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if the most significant bit of the result is set | ||||||||
z | Set if the result is zero | ||||||||
c | The value of the bit shifted out of the result |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
ASL A | 0A | x | x | x | 1 | 2 | Accumulator |
ASL addr | 0E | x | x | x | 3 | 6 | Absolute |
ASL dp | 06 | x | x | x | 2 | 51, 2 | Direct Page |
ASL addr,X | 1E | x | x | x | 3 | 71, 3 | Absolute Indexed X |
ASL dp,X | 16 | x | x | x | 2 | 61, 2 | Direct Page Indexed X |
LSR A | 4A | x | x | x | 1 | 2 | Accumulator |
LSR addr | 4E | x | x | x | 3 | 6 | Absolute |
LSR dp | 46 | x | x | x | 2 | 51, 2 | Direct Page |
LSR addr,X | 5E | x | x | x | 3 | 71, 3 | Absolute Indexed X |
LSR dp,X | 56 | x | x | x | 2 | 61, 2 | Direct Page Indexed X |
ROL A | 2A | x | x | x | 1 | 2 | Accumulator |
ROL addr | 2E | x | x | x | 3 | 6 | Absolute |
ROL dp | 26 | x | x | x | 2 | 51, 2 | Direct Page |
ROL addr,X | 3E | x | x | x | 3 | 71, 3 | Absolute Indexed X |
ROL dp,X | 36 | x | x | x | 2 | 61, 2 | Direct Page Indexed X |
ROR A | 6A | x | x | x | 1 | 2 | Accumulator |
ROR addr | 6E | x | x | x | 3 | 6 | Absolute |
ROR dp | 66 | x | x | x | 2 | 51, 2 | Direct Page |
ROR addr,X | 7E | x | x | x | 3 | 71, 3 | Absolute Indexed X |
ROR dp,X | 76 | x | x | x | 2 | 61, 2 | Direct Page Indexed X |
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 is identical to TRB except it sets the bits defined in the Accumulator not reset them.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
z | Set if logical AND of memory & accumulator is zero, otherwise clear |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
TRB addr | 1C | x | x | 3 | 61 | Absolute | |
TRB dp | 14 | x | x | 2 | 51, 2 | Direct Page | |
TSB addr | 0C | x | x | 3 | 61 | Absolute | |
TSB dp | 04 | x | x | 2 | 51, 2 | Direct Page |
The flag instructions manipulate some of the flags in the status register.
CLC is used prior to addition with the ADC instruction to keep the carry flag affecting the result.
On the 6502 a CLC before a BCC instruction can be used to implement a branch always, which is relocatable. This is unnecessary since the 65C02 with it's BRA instruction.
On the 16-bit processors a CLC followed by XCE instruction is used to switch the 65802 & 65816 processors into native mode.
SEC is used prior to subtraction using the SBC instruction to keep the carry flag affecting the result.
On the 16-bit processors a SEC followed by XCE instruction is used to switch the 65802 & 65816 processors into 6502 emulation mode.
CLD is used to switch the processors into binary mode so that the ADC & SBC instructions will perform binary not BCD arithmetic.
SED is used to switch the processors into decimal mode so that the ADC & SBC instructions will perform BCD not binary arithmetic.
CLI is used to re-enable hardware interrupts.
When the processor starts the interrupt handler it sets the i flag to prevent another interrupt to occur during that handler. If the handler want's to allow interrupts to happen whilst it's handling a previous one it can use CLI to re-enable them. The handler doesn't need to use CLI as the RTI (ReTurn from Interrupt) instruction will clear the i flag automatically.
In user code, CLI can be used to re-enable interrupts after an SEI instruction. This is usually used during time-critical code or code that cannot be interrupted.
SEI is used to disable hardware interrupts.
When the i bit is set, maskable hardware interrupts are ignored. When the processor starts the interrupt handler it sets the i flag to prevent another interrupt to occur during that handler. If the handler want's to allow interrupts to happen whilst it's handling a previous one it can use CLI to re-enable them. The handler doesn't need to use CLI as the RTI (ReTurn from Interrupt) instruction will clear the i flag automatically.
In user code, SEI can be used to disable interrupts when it needs to run time-critical code or code that cannot be interrupted. It should then use CLI once it's finished that time-critical code.
CLV clears the overflow flag.
Unlike other clear flag instructions, there is no set overflow flag available. The only way the overflow flag can be set is either:
On the 6502 a CLC before a BVC instruction can be used to implement a branch always, which is relocatable. This is unnecessary since the 65C02 with it's BRA instruction.
For each bit set in the operand byte, reset the corresponding bit in the status register. For each bit not set in the operand byte leaves the corresponding bit unchanged.
This instruction lets you clear any flag or flags in a single instruction. It is the only direct means of resetting the m & x flags.
In 6502 emulation mode (e=1) neither the b flag or bit 5 (the 6502's non-flag bit) is affected by this instruction.
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | |
---|---|---|---|---|---|---|---|---|
6502 emulation mode e=1 | n | v | d | i | z | c | ||
65816 native mode e=0 | n | v | m | x | d | i | z | c |
For each bit set in the operand byte, set the corresponding bit in the status register. For each bit not set in the operand byte leaves the corresponding bit unchanged.
This instruction lets you set any flag or flags in a single instruction. It is the only direct means of setting the m & x flags.
In 6502 emulation mode (e=1) neither the b flag or bit 5 (the 6502's non-flag bit) is affected by this instruction.
The bit's in the operand & their relationship with the status register is the same as the REP instruction.
Syntax | Action | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | |||
CLC | Clear Carry | 18 | x | x | x | 1 | 2 | Implied |
SEC | Set Carry | 38 | x | x | x | 1 | 2 | Implied |
CLD | Clear Decimal | D8 | x | x | x | 1 | 2 | Implied |
SED | Set Decimal | F8 | x | x | x | 1 | 2 | Implied |
CLI | Enable hardware interrupts | 58 | x | x | x | 1 | 2 | Implied |
SEI | Disable hardware interrupts | 78 | x | x | x | 1 | 2 | Implied |
CLV | Clear Overflow | B8 | x | x | x | 1 | 2 | Implied |
REP #const | Reset status bits | C2 | x | 2 | 3 | Immediate | ||
SEP #const | Set status bits | E2 | x | 2 | 3 | Immediate |
CMP subtracts the data at the address in the operand from the contents of the accumulator, setting the n, z & c flags based on the result. The Accumulator & Memory are unaffected by this operation.
On all processors, the data added from memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data added is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most-significant bit of result is set | ||||||||
z | Set if result is zero | ||||||||
c | Set if register value greater than or equal or Cleared if less than memory value |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
CMP #const | C9 | x | x | x | 21 | 22 | Immediate |
CMP addr | CD | x | x | x | 3 | 42 | Absolute |
CMP long | CF | x | 4 | 52 | Absolute Long | ||
CMP dp | C5 | x | x | x | 2 | 32, 3 | Direct Page |
CMP (dp) | D2 | x | x | 2 | 52, 3 | Direct Page Indirect | |
CMP [dp] | C7 | x | 2 | 62, 3 | Direct Page Indirect Long | ||
CMP addr,X | DD | x | x | x | 3 | 42, 4 | Absolute Indexed X |
CMP long,X | DF | x | 4 | 52 | Absolute Long Indexed X | ||
CMP addr,Y | D9 | x | x | x | 3 | 42, 4 | Absolute Indexed Y |
CMP dp,X | D5 | x | x | x | 2 | 42, 3 | Direct Page Indexed X |
CMP (dp,X) | C1 | x | x | x | 2 | 62, 3 | Direct Page Indexed Indirect X |
CMP (dp),Y | D1 | x | x | x | 2 | 52, 3, 4 | Direct Page Indirect Indexed Y |
CMP [dp],Y | D7 | x | 2 | 62, 3 | Direct Page Indirect Long Indexed Y | ||
CMP sr,S | C3 | x | 2 | 42 | Stack Relative | ||
CMP (sr,S),Y | D3 | x | 2 | 72 | Stack Relative Indirect Indexed Y |
The CPX & CPY instructions subtracts the data at the address in the operand from the contents of the relevant index register, setting the n, z & c flags based on the result. The register & Memory are unaffected by this operation.
The primary use of the CPX or CPY instructions is to test the value of the index register against loop boundaries.
On all processors, the data added from memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data added is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most-significant bit of result is set | ||||||||
z | Set if result is zero | ||||||||
c | Set if register value greater than or equal or Cleared if less than memory value |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
CPX #const | E0 | x | x | x | 21 | 22 | Immediate |
CPX addr | EC | x | x | x | 3 | 42 | Absolute |
CPX dp | E4 | x | x | x | 2 | 32, 3 | Direct Page |
CPY #const | C0 | x | x | x | 21 | 22 | Immediate |
CPY addr | CC | x | x | x | 3 | 42 | Absolute |
CPY dp | C4 | x | x | x | 2 | 32, 3 | Direct Page |
The branch instructions perform a test against one of the processor's flags. Depending on the instruction a branch is taken if it is either clear or set.
If the branch is taken, a 1-byte signed displacement in the second byte of the instruction is sign-extended to 16-bits and added to the Program Counter. If the branch is not taken then the instruction immediately following the 2-byte instruction is executed.
The allowable range of the displacement is -128 to +127 from the instruction immediately following the branch.
BCC tests the Carry flag and branches if it is clear.
It can be used in several ways:
Some assemblers accept BLT (Branch if Less Than) as an alternate mnemonic for BCC.
BCS tests the Carry flag and branches if it is set.
It can be used in several ways:
Some assemblers accept BGE (Branch if Greater Than or Equal) as an alternate mnemonic for BCS.
BEQ tests the Zero flag and branches if it is set.
It can be used in several ways:
BNE tests the Zero flag and branches if it is clear.
It can be used in several ways:
BMI tests the Negative flag and branches if it is set. The high bit of the value most recently affected will set the N flag. On 8-bit operations this is bit 7. On 16-bit operations (65816 only) this is bit 15.
This is normally used to determine if a two's-complement value is negative but can also be used in a loop to determine if zero has been passed when looping down through zero (the initial value must be positive)
BPL tests the Negative flag and branches if it is clear. The high bit of the value most recently affected will set the N flag. On 8-bit operations this is bit 7. On 16-bit operations (65816 only) this is bit 15.
This is normally used to determine if a two's-complement value is positive or if the high bit of the value is clear.
BVC tests the Overflow flag and branches if it is clear.
On the 6502 only 3 instructions alter the overflow flag: ADC, SBC & CLV.
On the 65C02 the BIT instruction also alters the overflow flag.
The PLP & RTI alter the flags as they restore all flags from the stack.
On the 65816 the SEP & REP instructions modify the v flag.
On some processors there's a Set Overflow hardware signal available, but on many systems there is no connection to that pin.
BVS tests the Overflow flag and branches if it is set. It has the same limitations as the BVC instruction.
None. |
Syntax | Branch if | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | |||
BCC nearlabel | Carry clear | 90 | x | x | x | 2 | 21, 2 | Program Counter Relative |
BCS nearlabel | Carry set | B0 | x | x | x | 2 | 21, 2 | Program Counter Relative |
BEQ nearlabel | Equal, z=1 | F0 | x | x | x | 2 | 21, 2 | Program Counter Relative |
BNE nearlabel | Not Equal, z=0 | D0 | x | x | x | 2 | 21, 2 | Program Counter Relative |
BMI nearlabel | Minus, n=1 | 30 | x | x | x | 2 | 21, 2 | Program Counter Relative |
BPL nearlabel | Positive, n=0 | 10 | x | x | x | 2 | 21, 2 | Program Counter Relative |
BVC nearlabel | Overflow clear, v=0 | 50 | x | x | x | 2 | 21, 2 | Program Counter Relative |
BVS nearlabel | Overflow set, v=1 | 70 | x | x | x | 2 | 21, 2 | Program Counter Relative |
The branch instructions sets the Program Counter to a new value from which the next instruction will be taken.
The program counter is loaded with the target address. If a long JMP is executed the bank is loaded from the third byte of the address.
Some assemblers accept JML as an alternate mnemonic for JMP long.
A branch is always taken, no test is performed. It is equivalent to a JMP instruction, except that as it uses a signed displacement it is only 2 bytes in length instead of 3 for JMP. In addition, because it uses displacements, code using BRA is relocatable.
The 1-byte signed displacement in the second byte of the instruction is sign-extended to 16-bits and added to the Program Counter. If the branch is not taken then the instruction immediately following the 2-byte instruction is executed.
The allowable range of the displacement is -128 to +127 from the instruction immediately following the branch.
BRA was introduced with the 65C02 processor.
A branch is always taken, no test is performed. It is equivalent to a BRA instruction, except that BRL is a 3 byte instruction. The two bytes after the opcode form a 16-bit signed displacement from the Program Counter.
The allowable range of the displacement is anywhere within the current 64K program bank.
The advantage of BRL is that it makes code relocatable, although it is 1 cycle slower than the absolute JMP instruction.
BRL was introduced with the 65802 processor.
None. |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
BRA nearlabel | 80 | x | x | 2 | 33 | Program Counter Relative | |
BRL label | 82 | x | 3 | 4 | Program Counter Relative Long | ||
JMP addr | 4C | x | x | x | 3 | 3 | Absolute |
JMP (addr) | 6C | x | x | x | 3 | 51, 2 | Absolute Indirect |
JMP (addr,X) | 7C | x | x | 3 | 6 | Absolute Indexed Indirect | |
JMP long | 5C | x | 4 | 4 | Absolute Long | ||
JMP [addr] | DC | x | 3 | 6 | Absolute Indirect Long |
The JSR & RTS instructions allows for subroutines to be implemented. The work by utilising 2 bytes on the stack consisting of the address before the next instruction to execute when the subroutine returns - not the actual address of that instruction.
On the 16-bit 65816 there are the JSL & RTL instructions. These use 3 bytes on the stack. The extra byte is the return bank address. Like RTS the address on the stack is the address before the next instruction not the actual instruction
For Interrupt routines there's the RTI instruction. That instruction is on the Interrupt page.
Transfer control to a subroutine, pushing the return address onto the stack. The 16-bit address placed on the stack is the address of the 3rd byte of the instruction, not the address of the next instruction.
Subroutines called by JSR must return using the RTS instruction.
Some assemblers recognise JSR as an alternate to the 65816 JSL instruction where if the address is greater than &FFFF then the 24 bit JSL instruction is used instead.
Returns from a subroutine called by JSR. It pulls the 16-bit program counter from the stack, incrementing it by one so that the next instruction is the one immediately after the calling JSR instruction.
Transfer control to a subroutine, pushing the return address onto the stack. The 24-bit address placed on the stack is the address of the 4th byte of the instruction, not the address of the next instruction.
Subroutines called by JSL must return using the RTL instruction.
Returns from a subroutine called by JSL. It pulls the 24-bit program counter from the stack, incrementing it by one so that the next instruction is the one immediately after the calling JSL instruction.
None. |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
JSL long | 22 | x | 4 | 8 | Absolute Long | ||
JSR addr | 20 | x | x | x | 3 | 6 | Absolute |
JSR (addr,X) | FC | x | 3 | 8 | Absolute Indexed Indirect | ||
RTL | 6B | x | 1 | 6 | Implied | ||
RTS | 60 | x | x | x | 1 | 6 | Implied |
Load the accumulator with data from memory.
On all processors, the data loaded from memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data added is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most-significant bit of result is set | ||||||||
z | Set if result is zero |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
LDA #const | A9 | x | x | x | 21 | 22 | Immediate |
LDA addr | AD | x | x | x | 3 | 42 | Absolute |
LDA long | AF | x | 4 | 52 | Absolute Long | ||
LDA dp | A5 | x | x | x | 2 | 32, 3 | Direct Page |
LDA (dp) | B2 | x | x | 2 | 52, 3 | Direct Page Indirect | |
LDA [dp] | A7 | x | 2 | 62, 3 | Direct Page Indirect Long | ||
LDA addr,X | BD | x | x | x | 3 | 42, 4 | Absolute Indexed X |
LDA long,X | BF | x | 4 | 52 | Absolute Long Indexed X | ||
LDA addr,Y | B9 | x | x | x | 3 | 42, 4 | Absolute Indexed Y |
LDA dp,X | B5 | x | x | x | 2 | 42, 3 | Direct Page Indexed X |
LDA (dp,X) | A1 | x | x | x | 2 | 62, 3 | Direct Page Indexed Indirect X |
LDA (dp),Y | B1 | x | x | x | 2 | 52, 3, 4 | Direct Page Indirect Indexed Y |
LDA [dp],Y | B7 | x | 2 | 62, 3 | Direct Page Indirect Long Indexed Y | ||
LDA sr,S | A3 | x | 2 | 42 | Stack Relative | ||
LDA (sr,S),Y | B3 | x | 2 | 72 | Stack Relative Indirect Indexed Y |
Load index register X with data from memory.
On all processors, the data loaded from memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data added is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most-significant bit of result is set | ||||||||
z | Set if result is zero |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
LDX #const | A2 | x | x | x | 21 | 22 | Immediate |
LDX addr | AE | x | x | x | 3 | 42 | Absolute |
LDX dp | A6 | x | x | x | 2 | 32, 3 | Direct Page |
LDX addr,X | BE | x | x | x | 3 | 42, 4 | Absolute Indexed X |
LDX dp,X | B6 | x | x | x | 2 | 42, 3 | Direct Page Indexed X |
Load index register Y with data from memory.
On all processors, the data loaded from memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data added is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most-significant bit of result is set | ||||||||
z | Set if result is zero |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
LDY #const | A0 | x | x | x | 21 | 22 | Immediate |
LDY addr | AC | x | x | x | 3 | 42 | Absolute |
LDY dp | A4 | x | x | x | 2 | 32, 3 | Direct Page |
LDY addr,X | BC | x | x | x | 3 | 42, 4 | Absolute Indexed X |
LDY dp,X | B4 | x | x | x | 2 | 42, 3 | Direct Page Indexed X |
Stores the accumulator into memory.
On all processors, the data written to memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data written is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
None. |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
STA addr | 8D | x | x | x | 3 | 41 | Absolute |
STA long | 8F | x | 4 | 51 | Absolute Long | ||
STA dp | 85 | x | x | x | 2 | 31, 2 | Direct Page |
STA (dp) | 92 | x | x | 2 | 51, 2 | Direct Page Indirect | |
STA [dp] | 87 | x | 2 | 61, 2 | Direct Page Indirect Long | ||
STA addr,X | 9D | x | x | x | 3 | 41 | Absolute Indexed X |
STA long,X | 9F | x | 4 | 51 | Absolute Long Indexed X | ||
STA addr,Y | 99 | x | x | x | 3 | 41 | Absolute Indexed Y |
STA dp,X | 95 | x | x | x | 2 | 41, 2 | Direct Page Indexed X |
STA (dp,X) | 81 | x | x | x | 2 | 61, 2 | Direct Page Indexed Indirect X |
STA (dp),Y | 91 | x | x | x | 2 | 51, 2 | Direct Page Indirect Indexed Y |
STA [dp],Y | 97 | x | 2 | 61, 2 | Direct Page Indirect Long Indexed Y | ||
STA sr,S | 83 | x | 2 | 41 | Stack Relative | ||
STA (sr,S),Y | 93 | x | 2 | 71 | Stack Relative Indirect Indexed Y |
Stores the index register X into memory.
On all processors, the data written to memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data written is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
None. |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
STX addr | 8E | x | x | x | 3 | 41 | Absolute |
STX dp | 86 | x | x | x | 2 | 31, 2 | Direct Page |
STX dp,Y | 96 | x | x | x | 2 | 41, 2 | Direct Page Indexed Y |
Stores the index register Y into memory.
On all processors, the data written to memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data written is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
None. |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
STY addr | 8C | x | x | x | 3 | 41 | Absolute |
STY dp | 84 | x | x | x | 2 | 31, 2 | Direct Page |
STY dp,X | 94 | x | x | x | 2 | 41, 2 | Direct Page Indexed X |
Stores zero to memory.
On all processors, the data written to memory is 8-bit. However, for 16-bit processors with the m flag is clear then the data written is 16-bit with the low-order 8-bits at the effective address and the high-order 8-bits at the effective address plus one.
None. |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
STZ addr | 9C | x | x | 3 | 41 | Absolute | |
STZ dp | 64 | x | x | 2 | 31, 2 | Direct Page | |
STZ addr,X | 9E | x | x | 3 | 51 | Absolute Indexed X | |
STZ dp,X | 74 | x | x | 2 | 41, 2 | Direct Page Indexed X |
The transfer register set of instructions allows for data to be passed between different registers.
In all of these transfer instructions, the source register is unchanged.
For example, on the 6502 to save the X register on the stack you would need to use the following:
1TXA ; Transfer X into A
2PHA ; Push A to the stack
Note: On the 65C02 and later this is replaced by the PHX instruction which doesn't touch the accumulator.
Transfers the Accumulator to X. On the 8-bit processors the registers are all the same size, however on the 16-bit processors the registers can be of different sizes. The following table describes how the data is transferred when a size mismatch occurs:
Source Size | Dest Size | m | x | Action performed |
---|---|---|---|---|
8 | 8 | All types | Value transferred is 8-bit | |
8 | 16 | 1 | 0 |
Value transferred is 16-bit. The 8-bit A becomes the low byte of the index register. The 8-bit hidden B register becomes the high byte of the index register. |
16 | 8 | 0 | 1 | The low byte of A is transferred to the index register |
16 | 16 | 0 | 0 | The full 16-bit A is transferred to the index register |
Transfers the Accumulator to Y. It follows the same rules as TAX.
TCD transfers the 16-bit accumulator C to the direct page register D, regardless of the accumulator/memory flag.
The C accumulator is used to indicate that 16-bits are transferred regardless of the m flag. If the A accumulator is 8-bit due to m=1 or in 6502 emulation mode then C = A as the low 8-bits and the hidden B accumulator as the high 8-bits.
TCS transfers the 16-bit accumulator C to the stack pointer S, regardless of the accumulator/memory flag. The C register is defined above for TCD. An alternate mnemonic for TCS is TAS.
Note: Unlike most transfer instructions, TCS does not affect any flags.
TDC transfers the Direct Page Register to the 16-bit accumulator C. The C register is defined above for TCD. An alternate mnemonic for TDC is TDA.
TSC transfers the stack pointer S to the 16-bit accumulator C, regardless of the accumulator/memory flag. The C register is defined above for TCD. An alternate mnemonic for TCS is TSA.
TSX transfers the stack pointer to X. The stack pointer is unchanged. On 8-bit processors only the low byte is transferred to X. On 16-bit processors (x=0) the full 16-bit value is tranferred to X.
TXA transfers X into the accumulator. On the 8-bit processors the registers are all the same size, however on the 16-bit processors the registers can be of different sizes. The following table describes how the data is transferred when a size mismatch occurs:
Source Size | Dest Size | m | x | Action performed |
---|---|---|---|---|
8 | 8 | All types | Value transferred is 8-bit | |
8 | 16 | 1 | 0 |
Value transferred is 16-bit. The 8-bit index register becomes the low byte of the accumulator. The high-byte of the accumulator is set to 0. |
16 | 8 | 0 | 1 |
Value transferred is 8-bit. The low 8-bits of the index register becomes the low byte of the accumulator. The high-byte of the hidden accumulator B is not affected by the transfer. |
16 | 16 | 0 | 0 | The full 16-bit index register is transferred to the accumulator |
TXS transfers X to the stack pointer to. The X is unchanged. On 8-bit processors only the low byte is transferred to S. On 16-bit processors (x=1) the low 8-bits of X is transferred to S. The high 8-bits of S are zeroed. On 16-bit processors (x=0) the full 16-bit value of X is transferred to S.
Note: Unlike most transfer instructions, TXS does not affect any flags.
TXY transfers X to Y. X is unchanged. The registers are always the same size, so when 8-bit then that's what's transferred. When 16-bit (x=0) then 16-bits are transferred.
TYA transfers Y into the accumulator. It follows the same rules as TXA above.
TYX transfers Y to X. Y is unchanged. The registers are always the same size, so when 8-bit then that's what's transferred. When 16-bit (x=0) then 16-bits are transferred.
On the 16-bit processors the 16-bit C accumulator is formed of two 8-bit accumulators, A for the low 8-bits and B for the upper 8-bits. XBA swaps the contents of the A & B registers.
In 8-bit memory mode, B is usually referred to as the hidden B accumulator, so the XBA instruction can be used to swap the accessible A with B, providing an in-processor scratch accumulator rather than pushing a value to the stack.
The flags are based on the value of the 8-bit A accumulator
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most significant bit of the transferred value is set | ||||||||
z | Set if value transferred is zero |
Src | Dest | Syntax | Opcode | Available on: | # of | # of | Addressing Mode | Notes | ||
---|---|---|---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | |||||
A | X | TAX | AA | x | x | x | 1 | 2 | Implied | |
A | Y | TAY | A8 | x | x | x | 1 | 2 | Implied | |
C | D | TCD | 5B | x | 1 | 2 | Implied | |||
C | S | TCS | 1B | x | 1 | 2 | Implied | Flags are unaffected | ||
D | C | TDC | 7B | x | 1 | 2 | Implied | |||
S | C | TSC | 3B | x | 1 | 2 | Implied | |||
S | X | TSX | BA | x | x | x | 1 | 2 | Implied | |
X | A | TXA | 8A | x | x | x | 1 | 2 | Implied | |
X | S | TXS | 9A | x | x | x | 1 | 2 | Implied | Flags are unaffected |
X | Y | TAX | 9B | x | 1 | 2 | Implied | |||
Y | A | TYA | 98 | x | x | x | 1 | 2 | Implied | |
Y | X | TYX | BB | x | 1 | 2 | Implied | |||
B | A | XBA | EB | x | 1 | 2 | Implied | Exchanges both registers, flags based on A post exchange |
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
n | Set if most significant bit of the transferred value is set | ||||||||
z | Set if value transferred is zero |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
PLA | 68 | x | x | x | 1 | 32 | Implied |
PLB | AB | x | 1 | 4 | Implied | ||
PLD | 2B | x | 1 | 5 | Implied | ||
PLP | 28 | x | x | x | 1 | 4 | Implied |
PLX | FA | x | x | x | 1 | 43 | Implied |
PLY | 7A | x | x | x | 1 | 43 | Implied |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
PEA addr | F4 | x | 3 | 5 | Stack Absolute | ||
PEI (dp) | D4 | x | 2 | 61 | Stack Direct Page Indirect | ||
PER label | 62 | x | 3 | 6 | Stack PC Relative Long | ||
PHA | 48 | x | x | x | 1 | 32 | Implied |
PHB | 8B | x | 1 | 3 | Implied | ||
PHD | 0B | x | 1 | 4 | Implied | ||
PHK | 4B | x | 1 | 3 | Implied | ||
PHP | 08 | x | x | x | 1 | 3 | Implied |
PHX | DA | x | x | x | 1 | 33 | Implied |
PHY | 5A | x | x | x | 1 | 33 | Implied |
BRK forces a software interrupt. It is unaffected by the i interrupt disable flag.
The BRK instruction is a single byte instruction. However, when it is invoked the Program Counter is incremented by 2. This allows for a one-byte signature value indicating which break caused the interrupt.
Even if the signature byte is not required, it must either be there or the RTI instruction which returns control to the caller must manually decrement the return address. As this can be tricky, most operating systems require BRK to take up 2 bytes in memory.
The program counter is incremented by two & pushed onto the stack. The status register (with b break flag set) is pushed onto the stack. The interrupt disable flag is then set, disabling interrupts. The program counter is loaded with the interrupt vector at &FFFE-&FFFF.
It's up to the interrupt handler pointed to by (&FFFE) to test the b flag to determine if the interrupt was from a software (BRK) rather than a hardware (IRQ) interrupt.
1.handler PLA ; copy status from stack
2 PHA ; but don't remove it else RTI will break
3
4 AND #&10 ; check B flag
5 BNE isBrk ; call break handler
6
7.isIRQ ; hardware handler here
8 RTI ; exit hardware handler
9
10.isBrk ; break handler here
11 RTI ; exit BRK handler
The program bank register is pushed onto the stack. The program counter is incremented by two & pushed onto the stack. The status register is pushed onto the stack. The interrupt disable flag is then set, disabling interrupts. The program counter is loaded with the break vector at &00FFE6-&00FFE7.
On the 6502 the decimal d flag is not modified after a BRK is executed. On the 65C02 & 65816 the decimal d flag is reset to 0.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
b | Value of P register on the stack is set | ||||||||
d | On 65C02, 65816 in emulation mode (e=1) reset to 0 for binary arithmetic, unchanged on 6502 | ||||||||
i | set to disable hardware IRQ interrupts |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
BRK | 00 | x | x | x | 21 | 72 | Stack Interrupt |
In the operating system for the BBC Micro (& Acorn Electron), the standard is to write BRK followed by an error number, then the error message ending with a 0
1BRK ; Software break
2EQUB 0 ; Error code
3EQUS "Silly" ; This is a real error message in BBC BASIC, try: AUTO10,1000
4EQUB 0 ; End of message marker
Service ROM's usually write error messages into RAM starting at &0100 and then do a JMP &0100 to run it. They do that as the handler is usually in a Language rom so they would be paged out if they ran BRK from their own ROM.
COP causes a software interrupt similar to BRK but through a separate vector. Unlike BRK, it is possible for it to be trapped by an optional co-processor like a floating point processor or a graphics processor. It is unaffected by the i interrupt disable flag.
Like BRK, COP increments the Program Counter by 2. However assemblers require the second byte to be provided as part of the instruction.
Values &00-&7F are free for use by software handlers.
Values &80-&FF are reserved for hardware implementations.
The program counter is incremented by two & pushed onto the stack. The status register is pushed onto the stack. The interrupt disable flag is then set, disabling interrupts. The program counter is loaded with the interrupt vector at &FFF4-&FFF5. The d flag is reset to 0 after the instruction is executed.
The program bank register is pushed onto the stack. The program counter is incremented by two & pushed onto the stack. The status register is pushed onto the stack. The interrupt disable flag is then set, disabling interrupts. The program bank register is set to 0. The program counter is loaded with the break vector at &00FFE4-&00FFE5. The d flag is reset to 0 after the instruction is executed.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
d | reset to 0 | ||||||||
i | set to disable hardware IRQ interrupts |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
COP const | 02 | x | 2 | 71 | Stack Interrupt |
The RTI instruction is used at the end of an interrupt handler. It pulls both the status register and program counter from the stack. For 16bit processors running in native mode it also pulls the program bank register from the stack.
Unlike RTS, the address on the stack is the actual return address. (RTS expects it to be the address before the next instruction).
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
RTI | 40 | x | x | x | 1 | 61 | Implied |
WAI pulls the RDY pin low. Power consumption reduced to a minimum and RDY is kept low until an external hardware interrupt (NMI, IRQ, ABORT or RESET) is received.
When a hardware interrupt is received, control is vectored though one of the hardware interrupt vectors. An RTI instruction in the invoked handler will return control back to the instruction immediately after the WAI.
If interrupts were disabled at the time WAI was invoked then when the interrupt is received then the relevant interrupt handler is not called and execution resumes immediately with the instruction after the WAI. This allows for processing to be synchronized with the interrupt.
As WAI pulls RDY low it frees up the bus. If BE is also pulled low, the processor can be disconnected from the bus.
None. |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
WAI | CB | x | 1 | 31 | Implied |
The MVN & MVP instructions moves/copies a block of memory from one location to another.
The source, destination and length of the block are taken from the X, Y & C registers.
The source address is in X, the destination address is in Y.
The length of the block minus 1 is in the C double accumulator. So if you are moving 42 bytes then C should have 41.
The two bytes of the operand consists of the source bank in the first byte and the destination bank in the second.
These instructions should be run in 16-bit native mode. If the index registers are in 8-bit mode (x=1) or the processor is in 6502 emulation mode (e=1) then the blocks specified will be in zero page due to the high byte of the index registers will be 0.
If a block move instruction is interrupted, it may be resumed automatically when RTI is executed by the handler, as long as the registers are left intact. The address pushed to the stack when it is interrupted is the address of the block move instruction so it resumes where it left off. The byte currently being moved will complete first before the interrupt is serviced.
MVN copies a block from the start of the source block to the start of the destination block.
The source and destination addresses need to point to the first byte of each block to be moved.
When execution is complete, the C accumulator will be &FFFF X & Y will point to the byte after the end of the source & destination blocks respectively.
MVP copies a block from the end of the source block to the end of the destination block.
The source and destination addresses need to point to the last byte of each block to be moved.
When execution is complete, the C accumulator will be &FFFF X & Y will point to the byte before the start of the source & destination blocks respectively.
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
MVN srcbk, dstbk | 54 | x | 3 | *1 | Block Move | ||
MVP srcbk, dstbk | 44 | x | 3 | *1 | Block Move |
This instruction is the only means to shift a 65802 or 65812 processor between 6502 emulation mode and full 16-bit native mode.
To switch into native mode, clear the carry bit then invoke XCE
1.goNative
2 CLC ; Clear Carry to indicate native mode
3 XCE ; Processor will be in 16-bit native mode once this completes
4 RTS ; Carry will now set if we were originally in emulation or clear if already native.
Once XCE has completed and the processor is in native mode, the following would have occurred.
To switch into 6502 emulation mode, set the carry bit then invoke XCE
1.goEmulation
2 SEC ; Set Carry to indicate native mode
3 XCE ; Processor will be in 16-bit native mode once this completes
4 RTS ; Carry will now set if we were already in emulation or clear if we were originally native.
Once XCE has completed and the processor is in 6502 emulation mode, the following would have occurred.
Flags |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
m | Set to 1 when switching to native mode, otherwise clear | ||||||||
c | Takes emulations previous value |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
XCE | FB | x | 1 | 2 | Implied |
A NOP takes no action and does not effect any registers except the program counter.
NOP's are usually used for timing loops as each NOP takes 2 cycles.
None. |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
NOP | EA | x | x | x | 1 | 2 | Implied |
Do not use this instruction. It will break if/when a future processor is released with additional instructions.
The 65802 & 654816 processors use 255 out of the possible 256 8-bit opcodes. The remaining opcode is this one, labeled WDM which happens to be the initials of William D. Mensch who designed the processors.
To allow additional instructions to be added later this instruction act's as a prefix allowing an additonal 256 opcodes. This is a similar technique to the Z80 & 8080 processors which have 2-byte extension opcodes.
The actual number of bytes and cycles involved will be depended on those extensions, however the byte size will be a minimum of 2 bytes.
On the 65802 & 65816 this instruction will execute as a 2-byte NOP.
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
WDM | 42 | x | 21 | ?1 | Implied |
STP will stop the processors oscillator input, shutting down the processor until a reset occurs by pulling the RES pin low.
As power consumption is a function of frequency in CMOS circuits, stopping the clock cuts power to almost nothing.
None. |
Syntax | Opcode | Available on: | # of | # of | Addressing Mode | ||
---|---|---|---|---|---|---|---|
(hex) | 6502 | 65C02 | 65816 | bytes | cycles | ||
STP | DB | x | 1 | 31 | Implied |