简体   繁体   中英

I don't understand what's going on with SBC

I've just started to learn 6502 because I want to create an Atari 2600 game.

I have tried this code:

LDA #$01
STA $01
LDX #$02
TXA
SBC $01
BRK

And I get the value A=$00, and flags Z and C set to 1. But I think the value in A must be $01.

If I change the values because I probably doing wrong the subtract:

LDA #$02
STA $01
LDX #$01
TXA
SBC $01
BRK

I get the value A=$fe, and flag N set to 1.

What's happening?

SBC is subtract with carry. If C is 0 prior to the SBC instruction, it subtracts one more than you expect.

Put SEC before the SBC.

进位标志是SBC指令的输入,将其设置为 1 执行不借位的减法。

The instruction set is clear here: http://www.obelisk.me.uk/6502/reference.html#SBC . It says:

This instruction subtracts the contents of a memory location to the accumulator together with the not of the carry bit

To avoid the problem, always use SEC instruction before SBC as below:

LDA #$01
STA $01
LDX #$02
TXA
SEC        ; for correct next subtraction with SBC
SBC $01
BRK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM