Jump to content



0

question about SBC and SEC


5 replies to this topic

#1 xucaen OFFLINE  

xucaen

    Star Raider

  • 94 posts
  • Looking for new owner for commodore 64
  • Location:Ma

Posted Tue Jan 25, 2005 12:30 PM

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.

Assembly Language Programming for the Atari Computers By Mark Chasin said:

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

#2 vdub_bobby OFFLINE  

vdub_bobby

    Quadrunner

  • 5,831 posts
  • Boom bam.
  • Location:Seattle, WA

Posted Tue Jan 25, 2005 1:31 PM

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

Quote

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

Quote

If the carry is cleared by the operation, it indicates a borrow occurred.
(http://www.6502.org/...opcodes.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.

#3 Thomas Jentzsch OFFLINE  

Thomas Jentzsch

    Thrust, Jammed, SWOOPS!

  • 16,745 posts
  • Always left from right here!
  • Location:Düsseldorf, Germany

Posted Tue Jan 25, 2005 1:39 PM

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

#4 vdub_bobby OFFLINE  

vdub_bobby

    Quadrunner

  • 5,831 posts
  • Boom bam.
  • Location:Seattle, WA

Posted Tue Jan 25, 2005 1:57 PM

Thomas Jentzsch said:

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

#5 Big Player OFFLINE  

Big Player

    River Patroller

  • 3,648 posts
  • Overrated 70's dinosaur
  • Location:Cincinnati, Ohio

Posted Tue Jan 25, 2005 2:04 PM

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.

#6 Tom OFFLINE  

Tom

    Moonsweeper

  • 449 posts
  • Location:Switzerland

Posted Tue Jan 25, 2005 3:24 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users