Category:6502 Assembly: Difference between revisions

m
Line 31:
<b>Flag terminology: A bit or flag is "clear" if it equals 0 and "set" if it equals 1.</b>
 
===Negative===
* N = Negative. This bit equals 1 if the last math operation resulted in a number that was negative (i.e. between #$80 and #$FF, inclusive). There are no commands called "Clear Negative" or "Set Negative" but you can easily do so by loading a register with a "positive" or "negative" value. <code>BMI</code> branches if the negative flag is set, <code>BPL</code> branches if it's clear.
 
Denoted with the letter N.
* V = Overflow. If a number crosses the "boundary" between #$7F and #$80, this flag is set. Note that adding #1 to #$FF does <i>not</i> set this flag. The <code>CLV</code> command clears the overflow flag, but there is no command explicitly for setting it. <code>BVS</code> branches if the overflow flag is set, <code>BVC</code> branches if it's clear.
* - = unused
* B = Break. This flag is set if the BRK command was executed. The BRK basically does the same thing as "JMP ($FFFE)", and is mainly used for debugging. There are no commands for clearing or setting this flag, and there is no explicit "Branch if Break Set" or "Branch If Break Clear"
* D = Decimal. This flag is set if Decimal Mode is active. There are no explicit branches based on its status. <code>CLD</code> clears this flag and <code>SED</code> sets it. A proper reset routine should clear this flag at the start.
* I = Interrupt. This flag is set if Interrupts are disabled. Note that this only disables the IRQ (Interrupt Request) line and has no effect on the NMI (Non-Maskable Interrupt.) A proper reset routine should set this flag at the start, but some systems like the NES do so automatically. <code>CLI</code> clears this flag and <code>SEI</code> sets it. There are no explicit branches on this condition.
* Z = Zero. This flag is set if the last math operation resulted in zero, or if zero was loaded into A, X, or Y. This is mostly used for testing the equality of two values, using the <code>CMP</code>, <code>CPX</code>, or <code>CPY</code> instructions. <code>BNE</code> branches if the zero flag is clear, <code>BEQ</code> branches if it's set. There are no explicit commands to set or clear this flag, but you can easily do so by loading values into registers.
* C = Carry/Borrow. This flag is set under the following conditions:
1. A <code>CMP/CPX/CPY</code> operation was performed and the value in the register is greater than or equal to the operand.
If the register was strictly less than the operand, the carry flag will be clear.
 
This bit equals 1 if the last math operation resulted in a number that was negative (i.e. between #$80 and #$FF, inclusive).
2. A bit shift or rotate caused a value of 1 to be "pushed out" of the operand.
 
* Set with: N/A (There is no explicit command for this but you can do it by loading a value #$80 or greater into a register, or with <code>BIT $addr</code> where <code>$addr</code> is a zero-page or absolute address containing a value #$80 or greater)
3. An ADC or SBC operation resulted in a "wraparound" from #$FF to #$00.
* Cleared with: N/A (There is no explicit command for this but you can do it by loading a value #$7F or less into a register, or with <code>BIT $addr</code> where <code>$addr</code> is a zero-page or absolute address containing a value #$7F or less)
* <code>BMI</code> branches if this flag is clear.
* <code>BPL</code> branches if this flag is set.
===Overflow===
Denoted with the letter V.
 
If a number crosses the "boundary" between #$7F and #$80, this flag is set. Note that adding #1 to #$FF does <i>not</i> set this flag.
The carry is incredibly useful for 16-bit math, among other things. It is set with <code>SEC</code> and cleared with <code>CLC</code>. <code>BCC</code> branches if the carry is clear, and <code>BCS</code> branches if it's set. There are no <code>ADD</code> or <code>SUB</code> commands on the 6502, but you can achieve the same result with <code>CLC ADC</code> and <code>SEC SBC</code>, respectively.
 
* Set with: N/A (There is no explicit command for this but you can do it with <code>BIT $addr</code> where <code>$addr</code> is a zero-page or absolute address containing a value where bit 6 is set, i.e. the left hex digit equals 4, 5, 6, 7, C, D, E, or F)
* Cleared with: <code>CLV</code>
* <code>BVC</code> branches if this flag is clear.
* <code>BVS</code> branches if this flag is set.
<br><br>
 
===Break===
Denoted with the letter B.
 
* B = Break. This flag is set if the <code>BRK</code> command was executed. The BRK basically does the same thing as "<code>JMP ($FFFE)"</code>, and is mainly used for debugging. There are no commands for clearing or setting this flag, and there is no explicit "Branch if Break Set" or "Branch If Break Clear"
* Set with: <code>BRK</code>
* Cleared with: <code>RTI</code>
* There are no branches associated with this command.
<br><br>
 
===Decimal===
Denoted with the letter D.
 
* D = Decimal. This flag is set if Decimal Mode is active. There are no explicit branches based on its status. <code>CLD</code> clears this flag and <code>SED</code> sets it. A proper reset routine should clear this flag at the start.
* Set with: <code>SED</code>
* Cleared with: <code>CLD</code>
* There are no branches associated with this command.
<br><br>
===Interrupt===
 
Denoted with the letter I.
 
* I = Interrupt. This flag is set if Interrupts are disabled. Note that this only disables the <code>IRQ</code> (Interrupt Request) line and has no effect on the <code>NMI</code> (Non-Maskable Interrupt.) A proper reset routine should set this flag at the start, but some systems like the NES do so automatically. <code>CLI</code> clears this flag and <code>SEI</code> sets it. There are no explicit branches on this condition.
* Set with: <code>SEI</code>
* Cleared with: <code>CLI</code>
* There are no branches associated with this command.
<br><br>
===Zero===
Denoted with the letter Z.
* Z = Zero. This flag is set if the last math operation resulted in zero, or if zero was loaded into A, X, or Y. This is mostly used for testing the equality of two values, using the <code>CMP</code>, <code>CPX</code>, or <code>CPY</code> instructions. <code>BNE</code> branches if the zero flag is clear, <code>BEQ</code> branches if it's set. There are no explicit commands to set or clear this flag, but you can easily do so by loading values into registers.
* Set with: N/A (there is no explicit command for this but you can easily do it by loading #0 into a register.)
* Cleared with: N/A (there is no explicit command for this but you can easily do it by loading a nonzero value into a register)
* <code>BNE</code> branches if this flag is clear.
* <code>BEQ</code> branches if this flag is set.
<br><br>
===Carry===
Denoted with the letter C.
 
* C = Carry/Borrow. This flag is set under the following conditions:
 
1.* A <code>CMP/CPX/CPY</code> operation was performed and the value in the register is greater than or equal to the operand. If the register was strictly less than the operand, the carry flag will be clear.
 
2.* A bit shift or rotate caused a value of 1 to be "pushed out" of the operand.
 
3.* An <code>ADC</code> or <code>SBC</code> operation resulted in a "wraparound" from #$FF to #$00.
<br><br>
 
The carry has an effect on math operations:
* If the carry flag is set, <code>ADC</code> will add an additional 1 to the result.
* If the carry flag is clear, <code>SBC</code> will subtract an additional 1 to the result.
<br><br>
 
The carry is incredibly useful for 16-bit math, among other things. It is set with <code>SEC</code> and cleared with <code>CLC</code>. <code>BCC</code> branches if the carry is clear, and <code>BCS</code> branches if it's set. There are no <code>ADD</code> or <code>SUB</code> commands on the 6502, but you can achieve the same result with <code>CLC ADC</code> and <code>SEC SBC</code>, respectively.
* Set with: <code>SEC</code>
* Cleared with: <code>CLC</code>
* <code>BCC</code> branches if this flag is clear.
* <code>BCS</code> branches if this flag is set.
<br><br>
 
==Decimal Mode==
1,489

edits