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).