It is currently Sat Aug 18, 2018 1:48 am

 All times are UTC - 7 hours

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Transforming a BPL/BMI to BCS/BCC, comparing to "0"Posted: Sun Jul 30, 2017 3:00 pm

Joined: Tue Jul 01, 2014 4:02 pm
Posts: 301
Alright, so...this seems like it should be simpler than I'm making it. I'm probably going to botch the explanation here, but hopefully you guys will be able to decode what I mean.

I had a signed comparison so that I could evaluate for a negative number (checking the value of VAR to see if it is negative...this value has already been determined in a different part of code, this is just checking to see its value - so for instance, at this moment I might have "-27" as a signed value during the check...this isn't the moment where I'm mathing to GET "-27" so there is no simple matter of checking the carry). However, as it turned out I needed more than 128 possible positive values, so BPL/BMI that I was using for this turned out to not be a good evaluation method.

So I played around with BCC/BCS methods instead, but of course the problem is... the math is done in a different part of code to find out the value of VAR, so it's not just a matter of checking the carry in a resulting compare. At this point, if I want more than 128 "positive values", I'd have to evaluate it unsigned...but I can't really do that if I want more than 128 values in the positive.

The only way I could think to do this is kind of wonky - to continue using signed values, but *sacrifice* the upper most possible values. I could determine if it's -16 through -1, and if it is, treat it as negative. If it's either less than -16 as a signed number, or 0 and higher, i'd evaluate it as positive. That would essentially give me the ability to compare 0-240 as *positive* and be able to still have a negative value of down to -16...

It just seems...unnecessarily convoluted. Anyhow - that's my thought with how to approach it, but I figured I'd ask if there are any other thoughts on possible approaches to this?

Top

 Posted: Sun Jul 30, 2017 3:09 pm

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 6596
Couldn't you just add 16 to the number before doing an unsigned comparison then?

Top

 Posted: Sun Jul 30, 2017 3:11 pm

Joined: Tue Jul 01, 2014 4:02 pm
Posts: 301
Sure, or that. Same concept, more easily stated. I was just wondering if there was some other neat trick I might not be thinking about.

Top

 Posted: Sun Jul 30, 2017 5:50 pm

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10714
Location: Rio de Janeiro - Brazil
You can use 16-bit variables and extend your signed range to -32768..32767. You can test bit 15 to tell whether the number is negative. But if 8-bits are enough except for detecting results below 0, extending the math to 16-bit sounds a bit like overkill.

You say you can't use the carry flag because the test is done in a difference place than the operation that makes the number negative, but have you thought about saving the carry flag right after the operation and test it later?

You can use PHP and PLP to save and restore all status flags, including the carry. If using the stack is troublesome for some reason (due to using subroutines, branches that might skip over the stack pop'ing, etc.), you can just shift the carry into a variable and shift it back out when you need to test it:

Code:
;(operation here)
sta Result
rol CarryBackup

;(lots of other code here)

lsr CarryBackup
bcc NegativeResult

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 4 posts ]

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: glutock and 3 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki