Jump to content
IGNORED

Assembly Question - how best to reverse the carry flag?


vdub_bobby
 Share

Recommended Posts

So normal 16-bit subtraction with carry:

 

sec
lda NumberOne
sbc #CONSTANT
sta NumberOne
lda NumberTwo
sbc #0
sta NumberTwo

But I have a situation where I am adding to NumberOne but I still want to decrement NumberTwo when NumberOne overflows

clc
lda NumberOne
adc #CONSTANT
sta NumberOne
lda NumberThree
sbc #0		;--won't work !!
sta NumberThree

So I've come up with these two ways:

    ;Method #1
    clc
    lda NumberOne
    adc #CONSTANT
    sta NumberOne
    lda NumberTwo
    bcs ClearCarry
    clc
    .byte $24    ;--skip (via BIT ZP) over next byte
ClearCarry
    sec
    sbc #0
    sta NumberTwo
    
    ;Method #2
    clc
    lda NumberOne
    adc #CONSTANT
    sta NumberOne
    php           ;push processor flags onto stack (carry in lowest bit)
    pla
    eor #1        ;flip it
    lsr           ;and shove it back into the carry flag
    lda NumberTwo
    sbc #0
    sta NumberTwo

Seems like there must be a better way?  Anyone have some sneaky trick to share?

Link to comment
Share on other sites

For the record, though, if you really did want to invert the carry flag, this gem is from one Lee Davison: http://web.archive.org/web/20130212081017/http://mycorner.no-ip.org/6502/shorts/togglecarry.html

 

	ROL	A		; Cb into b0		
	EOR	#$01		; toggle bit
	ROR	A		; b0 into Cb
  • Like 3
Link to comment
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...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...