SBX

From C64-Wiki
Jump to navigationJump to search

SBX (short for "SuBtract and store in X") is an illegal opcode that ANDs the contents of the A and X registers (leaving the contents of A intact), subtracts an immediate value, and then stores the result into X.

This illegal opcode is a combination of an immediate and an implied command: CMP, DEX

As the subtraction is performed in the CPU based on the CMP function, this subtract operation is not affected by the state of the Carry Flag, doesn't change the state of the Overflow Flag, and doesn't respect the decimal mode.

Function: X = A & X - #{imm}

Addressing mode[edit]

Opcode Addressing
mode
Assembler
format
Length
in bytes
Number of
cycles
Dec Hex
203 CB Immediate SBX #nn 2 2

SBX only supports the Immediate addressing mode, as shown in the table on the right.

CPU flags[edit]

SBX affects 3 of the CPU's status flags analog to CMP according to the value ending up in register X:

  • The negative flag is set if the result is negative, i.e. has its most significant bit set.
  • The zero flag is set if the result is zero, or cleared if it is non-zero.
  • The carry flag is set or cleared depending on the result.

Examples[edit]

Decrement X by more than 1[edit]

SBX can be used in a simple trick to decrement X by more than 1:

TXA      ; A = X, so A & X = X
SBX #$xx ; where xx is the value to decrease by

Negate a 16 bit number[edit]

LAX #$00 ; A=0 and X=0
SBX #lo  ; sets carry automatically for upcoming SBC
SBC #hi  ; negated value is in A/X

This routine is especially useful if self-modifying code is used to set the values of #lo and #hi

Links[edit]