Subtraction removes one value from another. On the NES, it supports game tasks such as moving objects upward, alongside addition and bit manipulation. section 4.1 section 4.1.2
The `sbc` instruction subtracts a source value from the current 8-bit value in the A register and leaves the result in A. Set the carry flag first with `sec`; if carry is clear, `sbc` subtracts an additional 1. section 4.1.2
Listing 4.9 Subtracting a value from the A register
sec #1 sbc <source>
Use `sec` immediately before `sbc` when performing an ordinary subtraction from A. section 4.1.2
A subtraction source can use several addressing forms:
- A direct value, such as `$06 - $05 = $01`. section 4.1.2
- A value stored at an absolute or zero-page memory address. section 4.1.2
- A zero-page or absolute address indexed by X or Y. section 4.1.2
- An indirect memory address indexed by X or Y. section 4.1.2
Move a bullet upward
A moving bullet subtracts 4 from its Y position each update. If subtraction goes below zero, its Y position becomes `$FF`, removing it from the screen so it can be fired again. section 7.6
Listing 7.7 Moving the player’s bullet (megablast.s)
.segment "CODE"
.proc move_player_bullet
lda oam + 16 #1
cmp #$FF #2
beq @exit
sec
sbc #4 #3
sta oam + 16 #4
bcs @exit
lda #$FF #5
sta oam + 16
@exit:
rts
.endproc
The routine checks whether the bullet is visible, subtracts four from its Y coordinate, and hides it after it passes the top edge. section 7.6
