Skip to main content

SUBTRACT

Subtract the right operand from the left operand, storing the difference in the left operand.

OpcodeBytesCyclesFormExample
$9131SUBTRACT B:_, B:_SUBTRACT B:1, B:2
$9233SUBTRACT P:_, P:_SUBTRACT P:2, P:4
$9333SUBTRACT I:_, I:_SUBTRACT I:4, I:8
Condition flagOutput
zero (ZF)If the difference is zero, this flag is 1; otherwise it is 0.
negative (NF)If the signed difference is a negative number, this flag is 1; otherwise it is 0.
overflow (OF)If the difference exceeds the register's signed range, this flag is 1; otherwise it is 0.
carry (CF)If the difference underflows the register's unsigned range, this flag is 0; otherwise it is 1.

Notes

  • SUBTRACT does not support immediate operands such as SUBTRACT I:_, 123. Instead, use a negative addition such as ADD I:_, -123. Chombit's design ensures that negative addition sets the condition flags the same as subtraction.

  • If the previous instruction was WITH CARRY, and CF is 0, then an extra 1 will be subtracted. This is the subtract-with-carry model familiar from ARM and 6502 processors.

  • Note: Some other CPU architectures such as x86 and Z80 use the "subtract-with-borrow" model instead of "subtract-with-carry." For those CPUs, an extra 1 is subtracted when CF=1 (rather than CF=0), and an underflow would be indicated by CF=1 (rather than CF=0).