We've just updated MediaWiki and its underlying software. If anything doesn't look or work quite right, please mention it to us. --RanAS

SBC: Difference between revisions

From SnesLab
Jump to: navigation, search
(added Carr page)
 
(56 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''SBC''' (Subtract with Carry/Borrow) is 65x instruction that performs a subtraction.
{| class="wikitable" style="float:right;clear:right;width:50%"
 
{| class="wikitable" style="float:right;clear:right;width:30%"
!colspan="8"|Basic Info
!colspan="8"|Basic Info
|+
|+
Line 9: Line 7:
|'''Speed'''
|'''Speed'''
|+
|+
|immediate
|[[Immediate]]
|E9
|E9
|2 bytes
|2/3 bytes
|2 cycles
|2 cycles*
|+
|+
|absolute
|[[Absolute]]
|ED
|ED
|3 bytes
|3 bytes
|4 cycles
|4 cycles*
|+
|+
|absolute long
|[[Absolute Long Addressing|Absolute Long]]
|EF
|EF
|4 bytes
|4 bytes
|5 cycles
|5 cycles*
|+
|+
|direct page
|[[Direct Page|Direct Page]]
|E5
|E5
|2 bytes
|2 bytes
|3 cycles
|3 cycles*
|+
|+
|direct page indirect
|[[Direct Page Indirect Addressing|Direct Page Indirect]]
|F2
|F2
|2 bytes
|2 bytes
|5 cycles
|5 cycles*
|+
|+
|direct page indirect long
|[[Direct Page Indirect Long]]
|E7
|E7
|2 bytes
|2 bytes
|6 cycles
|6 cycles*
|+
|+
|absolute indexed by X
|[[Absolute Indexed by X]]
|FD
|FD
|3 bytes
|3 bytes
|4 cycles
|4 cycles*
|+
|+
|absolute long indexed by X
|[[Absolute Long Indexed by X]]
|FF
|FF
|4 bytes
|4 bytes
|5 cycles
|5 cycles*
|+
|+
|absolute indexed by Y
|[[Absolute Indexed by Y]]
|F9
|F9
|3 bytes
|3 bytes
|4 cycles
|4 cycles*
|+
|+
|direct page indexed by X
|[[Direct Page Indexed by X]]
|F5
|F5
|2 bytes
|2 bytes
|4 cycles
|4 cycles*
|+
|+
|direct page indirect indexed by X
|[[Direct Page Indexed Indirect by X]]
|E1
|E1
|2 bytes
|2 bytes
|6 cycles
|6 cycles*
|+
|+
|direct page indirect indexed by Y
|[[Direct Page Indirect Indexed by Y]]
|F1
|F1
|2 bytes
|2 bytes
|5 cycles
|5 cycles*
|+
|+
|direct page indirect long indexed by Y
|[[Direct Page Indirect Long Indexed by Y]]
|F7
|F7
|2 bytes
|2 bytes
|6 cycles
|6 cycles*
|+
|+
|stack relative
|[[Stack Relative]]
|E3
|E3
|2 bytes
|2 bytes
|4 cycles
|4 cycles*
|+
|+
|stack relative indirect indexed by Y
|[[Stack Relative Indirect Indexed by Y]]
|F3
|F3
|2 bytes
|2 bytes
|7 cycles
|7 cycles*
|}
|}


{| class="wikitable" style="float:right;clear:right;width:30%"
{| class="wikitable" style="float:right;clear:right;width:30%"
!colspan="9"|Flags Clobbered
!colspan="9"|Flags Affected
|+
|[[N Flag|N]]
|[[V Flag|V]]
|[[M Flag|M]]
|[[X Flag|X]]
|[[D Flag|D]]
|[[I Flag|I]]
|[[Z Flag|Z]]
|[[C Flag|C]]
|+
|+
|N
|N
|V
|V
|M
|X
|D
|I
|Z
|C
|+
|
|
|.
|.
|.
|.
|.
|.
|.
|.
|
|Z
|
|C
|}
|}
'''SBC''' (Subtract with Carry/Borrow) is the main 65x subtraction instruction.  The [[accumulator]] serves as both the minuend and where the difference is stored.  The operand serves as the subtrahend.
If the [[carry flag]] (aka borrow flag) is clear, one more is subtracted.  As there is no subtract-without-carry instruction, [[SEC]] should be run beforehand or the carry flag should otherwise be ensured to be set, otherwise the difference may be one greater than expected.
If the difference is negative, the carry flag will be cleared, otherwise it will be set.
If the [[decimal flag]] is set, then [[binary coded decimal]] subtraction is performed, otherwise binary subtraction.
==== Syntax ====
<pre>
SBC #const
SBC addr
SBC long
SBC dp
SBC (dp)
SBC [dp]
SBC addr, X
SBC long, X
SBC addr, Y
SBC dp, X
SBC (dp, X)
SBC (dp), Y
SBC [dp], Y
SBC sr, S
SBC (sr, S), Y
</pre>
==== Cycle Penalties ====
SBC takes an extra cycle for each of the following:
* if the accumulator is 16 bits wide, in all addressing modes
* when utilizing the [[direct register]], if the low byte of the direct register is nonzero
* in certain addressing modes if adding an index crosses a [[page]] boundary
If you only need to subtract one, consider [[DEC]] instead.  Unlike the 65c02, the 65c816 does not have a cycle penalty when subtracting in decimal mode.
In [[Eyes & Lichty]] on page 498, opcode F5 has a "0" superscript typo on the # of cycles column.


=== See Also ===
=== See Also ===
* [[ADC]]
* [[ADC]]
* [[SBC (SPC700)]]
* [[SUB (Super FX)]]
* [[DEX]]
* [[DEY]]


=== External Links ===
=== External Links ===
* [[Eyes & Lichty]] page on SBC: https://archive.org/details/0893037893ProgrammingThe65816/page/n523
* [[Eyes & Lichty]], [https://archive.org/details/0893037893ProgrammingThe65816/page/497 page 497] on SBC
* [[Labiak]] page on SBC: https://archive.org/details/Programming_the_65816/page/n189
* [[Labiak]], [https://archive.org/details/Programming_the_65816/page/n189 page 179] on SBC
* [[MCS6500 Manual]] page on SBC: https://archive.org/details/mos_microcomputers_programming_manual/page/n29
* 2.2.2 on [[MCS6500 Manual]], [https://archive.org/details/mos_microcomputers_programming_manual/page/n29 page 14] on SBC
* [[Carr]] page on SBC: https://archive.org/details/6502UsersManual/page/n284
* [[Carr]], [https://archive.org/details/6502UsersManual/page/n284 page 271] on SBC
* [[Leventhal]], [https://archive.org/details/6502-assembly-language-programming/page/n140 page 3-91] on SBC
* snes9x implementation of SBC: https://github.com/snes9xgit/snes9x/blob/master/cpuops.cpp#L1133
* undisbeliever on SBC: https://undisbeliever.net/snesdev/65816-opcodes.html#sbc-subtract-with-borrow-from-accumulator
* Pickens, John. http://www.6502.org/tutorials/6502opcodes.html#SBC
* Clark, Bruce. http://www.6502.org/tutorials/65c816opcodes.html#6.1.1.1


[[Category:ASM]]
[[Category:ASM]]
[[Category:Group One Instructions]]
[[Category:Group One Instructions]]
[[Category:Inherited from 6502]]
[[Category:Inherited from 6502]]

Latest revision as of 17:18, 22 October 2024

Basic Info
Addressing Mode Opcode Length Speed
Immediate E9 2/3 bytes 2 cycles*
Absolute ED 3 bytes 4 cycles*
Absolute Long EF 4 bytes 5 cycles*
Direct Page E5 2 bytes 3 cycles*
Direct Page Indirect F2 2 bytes 5 cycles*
Direct Page Indirect Long E7 2 bytes 6 cycles*
Absolute Indexed by X FD 3 bytes 4 cycles*
Absolute Long Indexed by X FF 4 bytes 5 cycles*
Absolute Indexed by Y F9 3 bytes 4 cycles*
Direct Page Indexed by X F5 2 bytes 4 cycles*
Direct Page Indexed Indirect by X E1 2 bytes 6 cycles*
Direct Page Indirect Indexed by Y F1 2 bytes 5 cycles*
Direct Page Indirect Long Indexed by Y F7 2 bytes 6 cycles*
Stack Relative E3 2 bytes 4 cycles*
Stack Relative Indirect Indexed by Y F3 2 bytes 7 cycles*
Flags Affected
N V M X D I Z C
N V . . . . Z C

SBC (Subtract with Carry/Borrow) is the main 65x subtraction instruction. The accumulator serves as both the minuend and where the difference is stored. The operand serves as the subtrahend.

If the carry flag (aka borrow flag) is clear, one more is subtracted. As there is no subtract-without-carry instruction, SEC should be run beforehand or the carry flag should otherwise be ensured to be set, otherwise the difference may be one greater than expected.

If the difference is negative, the carry flag will be cleared, otherwise it will be set.

If the decimal flag is set, then binary coded decimal subtraction is performed, otherwise binary subtraction.

Syntax

SBC #const
SBC addr
SBC long
SBC dp
SBC (dp)
SBC [dp]
SBC addr, X
SBC long, X
SBC addr, Y
SBC dp, X
SBC (dp, X)
SBC (dp), Y
SBC [dp], Y
SBC sr, S
SBC (sr, S), Y

Cycle Penalties

SBC takes an extra cycle for each of the following:

  • if the accumulator is 16 bits wide, in all addressing modes
  • when utilizing the direct register, if the low byte of the direct register is nonzero
  • in certain addressing modes if adding an index crosses a page boundary

If you only need to subtract one, consider DEC instead. Unlike the 65c02, the 65c816 does not have a cycle penalty when subtracting in decimal mode.

In Eyes & Lichty on page 498, opcode F5 has a "0" superscript typo on the # of cycles column.

See Also

External Links