• Please review our updated Terms and Rules here

C64 Op Code....SBC..????

AAGDOS

Experienced Member
Joined
Jul 4, 2018
Messages
104
Location
Hartford, CT
All,

Please excuse what appears to be a stupid question! Is my SBC messed up or am I messed up??? I am subtracting 02-05 = -03 = FD. But my code gives FC????

LDA $$05
STA $0350
LDA #$02
SBC $0350
BRK

SBC is supposed to be "Subtract Memory from Accumulator....", and I assume the result remains in the Accumulator. However after BRK, the Accumulator Register shows FC???

Any simple explanation would be helpful! Thanks

Anthony G
 

daver2

Veteran Member
Joined
Jun 19, 2012
Messages
8,128
Location
UK - Worcester
SBC = Subtract with Carry. So what is the state of your carry flag?

I am writing this at night, so I haven't checked my answer, but I would guess that is your problem.

Dave
 

AAGDOS

Experienced Member
Joined
Jul 4, 2018
Messages
104
Location
Hartford, CT
Dave,

I changed the code to start with CLC as a second guess. It made no difference. After the BRK, the SR = B0, so the carry flag = 0.

Does this answer anything?? Thanks.

Anthony G
 

bsd64

New Member
Joined
Aug 6, 2019
Messages
5
Location
Georgia
From the datasheet:
SBC: A - M - (NOT C) -> A

It would appear that 'FC' is the correct answer for the case when the carry flag is 0.
From the tutorial on 6502.org, "There is no way to subtract without the carry which works as an inverse borrow. i.e, to subtract you set the carry before the operation. If the carry is cleared by the operation, it indicates a borrow occurred."
 
Last edited:

AAGDOS

Experienced Member
Joined
Jul 4, 2018
Messages
104
Location
Hartford, CT
Dave,

I have no idea!! I didn't see anything about that in the C64 Reference Manual (CBM). I'll try that in the morning! Thanks.

Anthony G
 

commodorejohn

Veteran Member
Joined
Jul 6, 2010
Messages
3,168
Location
California, USA
Yes, the value of the carry flag is flipped around compared to what you might expect with the subtract instruction, so carry clear means borrow-in. I believe this makes the ALU logic simpler, but it's a little confusing the first time you run up against it.
 

resman

Veteran Member
Joined
Jan 1, 2014
Messages
522
Location
Lake Tahoe
Your question is more about the 6502 CPU than just the Commodore and there are a lot of resources to help. http://6502.org/ is probably the go-to site for all things 6502, but can be a little bit like drinking from a fire hose. To answer your question, the 6502 doesn't have a SUB instruction. To get the same functionality, precede the SBC instruction with SEC (set carry - see commodorejohn's explanation) for effectively a SUB instruction. Welcome to the the odd quirks of the 6502.
 

daver2

Veteran Member
Joined
Jun 19, 2012
Messages
8,128
Location
UK - Worcester
It is pretty much the same for a lot of processors. The ALU generally performs addition. So, to perform subtraction, you add the 2's complement of the second operand.

To form the two's complement, you logically invert the second operand and add 1 - which is the same as inverting the second operand and adding the carry flag.

So, the carry flag is truly the carry for an addition, but the logically inverse for a subtraction.

Dave
 

AAGDOS

Experienced Member
Joined
Jul 4, 2018
Messages
104
Location
Hartford, CT
Thanks to Dave and others for the insight! This was more subtle than I had anticipated, and setting SEC at the start of the code works! I have done more of a "deep dive" into the books by 1) Bush and Holmes, and 2) Leventhal., and found far more explanation. Thanks also for pointing me toward the 6502.org web site. I had not known of this.

SEC
LDA #$05
STA $0350
LDA #$02
SBC $0350
BRK

02 - 05 = -3 = FD

Anthony G
 
Top