Home > database >  6502 Assembly question: Should I have an instance of SEC for each SBC when there are multiple SBC in
6502 Assembly question: Should I have an instance of SEC for each SBC when there are multiple SBC in

Time:10-24

According to this website, under the "major uses" for CLC, it states:

If there are to be a series of additions (multiple-byte addition), only the first ADC is preceded by CLC since the carry feature is necessary.

Under the "major uses" for SBC it states:

You always SEC (set the carry flag) before an SBC operation so you can tell if you need a "borrow".

In other words for a series of consecutive ADC operations, you only need a CLC before the first, but before a series of consecutive SBC operations, you should have a SEC before each. Is this correct?

CodePudding user response:

If there are to be a series of additions (multiple-byte addition), only the first ADC is preceded by CLC since the carry feature is necessary

This is written kind of misleadingly. If you are using a series of ADC to implement multiple-precision addition, e.g. adding two 16-bit or 32-bit numbers, then you do want CLC before the first one only. But if you have several consecutive ADC that are adding unrelated 8-bit numbers, then you want a CLC before each of them. Otherwise, a carry from one addition would be propagated into an unrelated sum.

Then the case for SBC is exactly analogous. If you are doing several unrelated 8-bit subtractions, you want to SEC before each one. If you are using a series of SBC to implement 16-bit or 32-bit subtraction, etc, then you want SEC before the first one only.

CodePudding user response:

There is no difference between adding and substracting numbers in 6502. So, CLC and SBC behaves the same. Clearing or setting carry flag before the next operation is completely your decision. You should also understand CLC can be used with substraction and SBC can be used with addition. For example;

lda #$40
clc
adc $10
sta $1000
inc $1000

In this example we add $10 memory address value to $40 and then increase the ending result by one. Instead we can do this.

lda #$40
sec
adc $10
sta $1000

This will give you the same result without an extra INC operation.

CLC and SEC use cases are very similar and has no difference like your original question. But you should not directly relate them to addition and substraction. You should always ask the question "which carry state do i need at this stage?" and if required, use CLC and SEC accordingly.

  • Related