GARTHWILSON wrote:
nei02 wrote:
I want to implement a BCC and BCS.
I assume that this is equal to a "less than" .
BCC, or "branch on carry clear," means "branch if less than" if it's after a subtraction.
I should have been more clear and complete. "Carry clear" means there was a borrow because the number you started with was less than the number you subtracted. You normally start the subtraction with the carry flag set. If the number you're starting with is less than the number you're subtracting, there has to be a borrow, and the borrowed bit came from the carry flag, which is now clear. It can also means the result of the subtraction is less than 0. It works the same way for compare instructions (although you don't have to set the carry flag before a compare instruction); for example, if you start with 7 in A, and do a CMP #9, 7 is less than 9, so the carry flag ends up clear, meaning there was a borrow. Here's an example for subtracting $361 from a 2-byte (ie, 16-bit) variable, and putting the result in the same variable:
Code:
SEC ; Set carry flag to start. (This may be unwanted or unneeded
; in a few situations.)
LDA VarLowByte ; Get the low byte of your two-byte (ie, 16-bit) variable,
SBC #$61 ; subtract the low byte of your two-byte constant, and
STA VarLowByte ; put the result back in the variable.
; Now leave C alone. The resulting C value from above gets
; used in the SBC below. If C=clear, the resulting high byte
; below will be 1 less than it otherwise would have been.
LDA VarHighByte ; Now get the high byte of your two-byte variable,
SBC #3 ; subtract the high byte of your two-byte constant, and
STA VarHighByte ; put the result back in the variable. At the end, C=clear
; means the variable was less than $361 before you started.
If you wanted to only subtract 2, you can do that to the low byte and the use BCS to branch around the decrementing of the high byte, like this:
Code:
SEC ; Set carry flag to start (although this will be unwanted or unnecessary in some situations).
LDA VarLowByte ; Get the low byte of your 2-byte variable.
SBC #2 ; Subtract 2,
STA VarLowByte ; and put it back.
BCS 1$ ; If the low byte started with at least 2, there was no need to borrow, so skip the DEC instruction.
DEC VarHighByte ; Else, decrement the high byte to reflect that there was a borrow from it.
1$: <continue>
This is all described very clearly in the manual "
Programming the 65816 including the 6502, 65C02, and 65802" by David Eyes and Ron Liechty. This is a .pdf file of what is definitely the best 65xx programming manual available, and a must-have for every 65xx programmer! It starts with the basics, followed by architecture, the CMOS 65c02's many improvements over the original NMOS 6502 including added instructions and addressing modes and fixing the NMOS's bugs and quirks, and then the natural progression to the 65816; a thorough tutorial, writing applications, then very detailed and diagrammed information on all 34 addressing modes, at least a page of very detailed description for each instruction, with info on every addressing mode available for that instruction, then instruction lists, tables, and groups, of all 255 active op codes (of the '816), plus more. 469 pages.