ADC
and SBC
add/subtract their parameter, plus/minus the value in the carry bit, to the accumulator. This means that you need to know the value in the carry before you start. If you want to add three to the accumulator, a typical sequence might be:
CLC
ADC #3
As mentioned in another answer, you use the carry to "carry through" the, well, carry when adding multibyte numbers:
CLC
LDA xlo
ADC ylo
STA resultlo ; sets carry if xlo+ylo >$FF, clears otherwise
LDA xhi
ADC yhi ; previously set/cleared carry added in here
STA resulthi
But it gets a bit more sophisticated than that. Since there is no "add/subtract without carry/borrow" instruction, you always need to take into account the state of the carry when you do an addition or subtraction. But rather than blindly putting in CLC
and SEC
instructions everywhere, you often can take advantage of what you know about the code before the addition to use the existing state of the carry.
The simplest case is when you are adding and know the carry must be clear:
BCS somewhere ; if carry set, go somewhere else
LDA someloc ; does not affect carry flag
; ; no need to CLC here; carry must be set if we got here!
ADC #3
Even trickier, you might subtract 1 from the constant you add if you know the carry will be set, letting the carry provide that extra 1:
BCC elsewhere ; if carry clear, go somewhere else
LDA someloc
; ; carry must be set if we reached this point
ADC #MYCONST-1 ; subtract 1 from MYCONST because set carry adds 1
There are also cases where the carry explicitly contains the data you're trying to add. Say we're repeatedly reading an input value from a device and want to count the number of times the lowest bit was set (rather than clear):
LDA input ; we want to add contents of bit 0 of this input
ROR ; rotate bit 0 into carry
LDA count
ADC #0 ; add carry bit to count
STA count
All of this applies similarly to SBC
, except that on the 6502 the carry bit becomes a "borrow bit"; you set it before subtracting and it will still be set if there was no underflow. (Other CPUs, such as the 6800, work the other way around when subtracting.)