BCS

From C64-Wiki
Jump to navigationJump to search

BCS (short for "Branch if Carry is Set") is the mnemonic for a machine language instruction which branches, or "jumps", to the address specified if, and only if the carry flag is set. If the carry flag is clear when the CPU encounters a BCS instruction, the CPU will continue at the instruction following the BCS rather than taking the jump.

BCS in comparisons[edit | edit source]

Main article: Comparisons in machine language

BCS and its counterpart BCC are often used in conjunction with instruction like CMP, CPX, CPY or SBC to compare unsigned numbers, like for 8-bit integers as follows:

LDA NumA            Read the value "NumA"
CMP NumB            Compare against "NumB"
BCS Higher_or_same  Go to label "Higher_or_same" if "NumA" >= "NumB"
...                 Execution continues here if "NumA" < "NumB"

For signed integers, this method fails if NumA and NumB have different arithmetic signs (i.e. one negative, the other positive) — instead, BPL and BMI should be used for greater than/less than-style comparisons of signed bytes.

Addressing mode[edit | edit source]

Opcode Addressing
mode
Assembler
format
Length
in bytes
Number of
cycles
Dec Hex
176 B0 Relative BCS nn 2 2*

BCS only supports the Relative addressing mode, as shown in the table at right. In the assembler formats listed, nn is a one-byte (8-bit) relative address. The relative address is treated as a signed byte; that is, it shifts program execution to a location within a number of bytes ranging from -128 to 127, relative to the address of the instruction following the branch instruction.
The execution time for BCS is not a fixed value, but depends on the circumstances. The listed time is valid only in cases where BCS does not take the branch. If it does take the branch, execution takes one additional clock cycle. Furthermore, if the branching crosses a page boundary, yet another cycle must be added to the execution time listed.

CPU flags[edit | edit source]

BCS does not affect any of the CPU's status flags.