Category:6502 Assembly: Difference between revisions

Content added Content deleted
mNo edit summary
Line 26: Line 26:
* Adding or subtracting
* Adding or subtracting
* Bit shifts/rotates
* Bit shifts/rotates
<br><br>
Storing values into memory, jumping, or returning from subroutines will not set the flags. Furthermore, each command sets the flags differently, and some don't set the flags at all!


<b>Flag terminology: A bit or flag is "clear" if it equals 0 and "set" if it equals 1.</b>
Storing values into memory, jumping, or returning from subroutines will not set the flags.
Flag terminology: A bit or flag is "clear" if it equals 0 and "set" if it equals 1.


* 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.
* 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.
Line 39: Line 40:
* 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.
* 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:
* 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.
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.
2. A bit shift or rotate caused a value of 1 to be "pushed out" of the operand.


3. An ADC or SBC operation resulted in a "wraparound" from #$FF to #$00.
3. An ADC or SBC operation resulted in a "wraparound" from #$FF to #$00.


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.
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.