Jump to content
Sign in to follow this  
xucaen

question about SBC and SEC

Recommended Posts

Hi all, I hope this is the right forum for posting general questions about 6502 assembler..

For this question I am refering to the book Assembly Language Programming for the Atari Computers By Mark Chasin published 1984. I have read the online version at atariarchives and this question refers specifically to appendix one .

 

SEC     ;be sure C is set

LDA #24 ;1st number

SBC #26 ;2nd number

?       ;answer now in accumulator

 

What is the answer at the "?"? We set the Carry bit before we started, and it's obvious that we needed to borrow before we could perform the subtraction. Therefore, it's apparent that the Carry bit following this subtraction will be zero. When it is used for borrowing, the Carry bit has a value of 256; we began with the number 256 + 24 = 280 and we subtracted 26, leaving an answer of 254.

   Using the SBC instruction, we can subtract any number from another. Note that here we have confined our examples to numbers which can be expressed in a single byte. Double-precision arithmetic was used in several examples in Chapters 7 to 10.

 

In this example, after SBC is completed, will the carry bit be set to zero or one? I had thought it would be zero, but after reading this I'm not so sure. If the Cary bit has a value of 256, then should I assume that any value less that 256 is zero? Or maybe a better way to look at might be that if the carry bit is set, then the value 256 is used in the borrow, then after the borrow the carry is zero?

 

Jim

Share this post


Link to post
Share on other sites

The clearest explanation of how the carry works with sbc and adc I have seen is here: http://www.geocities.com/oneelkruns/asm1step.html

  Subtraction follows the same format:

   

  SEC

  SBC ...

  .

  .

  SBC ...

  .

  .

  .

   

  In this case set the carry flag first and then do the subtractions.  

  Symbolically,

   

  A - M - ~C  -->  A  ,  where ~C is the opposite of C

 

So...

sec

lda #24

sbc #26

So A will hold 24 - 26 - 0 = -2 = %1111 1110 (twos complement) = 254

As far as what the value of the carry will be afterwards...:

If the carry is cleared by the operation, it indicates a borrow occurred.
(http://www.6502.org/tutorials/6502opcodes.htm#SBC)

 

Since a borrow took place, the carry will be cleared.

 

Incidentally, if you know the carry state and don't have time/space to change it, you can do something like this:

	lda #127

sec

sbc ProtY

adc #PROTHEIGHT-1;use '-1' so we don't have to clear carry.

sta ProtYTemp

In this case I knew that #127 - ProtY would never require a borrow and that it would never clear the carry.

Share this post


Link to post
Share on other sites

Buy a better book! The carry bit is one or zero. It's only a single bit, there are no other values.

 

Anyway, back to your question: Carry is 0 (=clear)

 

This has nothing to do with the result of the subtraction, but if an underflow happened.

 

Some examples:

10-40=-30; C=0

40-10= 30; C=1

10-200= -190; C=0

Share this post


Link to post
Share on other sites
Buy a better book! The carry bit is one or zero. It's only a single bit, there are no other values.

 

Anyway, back to your question: Carry is 0 (=clear)

 

This has nothing to do with the result of the subtraction, but if an underflow happened.

 

Some examples:

10-40=-30; C=0

40-10= 30; C=1

10-200= -190; C=0

 

Don't forget these fun examples:

 

-20-(-30)=10; C=0

-20-(-10)=-10; C=1

-20-127=109!; C=0

Share this post


Link to post
Share on other sites

Is Assembly Language Programming for the Atari Computers a bad book? I had the impression it wasn't as good as the other 6502 asm books at Atari Archives but I wasn't sure. The information was already covered in Atari Roots and the two Machine Language books, so I only skimmed it.

 

We need the 6502 experts to let us know which of these books are good or not.

Share this post


Link to post
Share on other sites

the quoted part basically suggests that the carry flag can be viewed as a 9th bit (bit 8), which is actually not a bad way of looking at it.

 

but i agree, it is explained badly in that book.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...