Transforming a BPL/BMI to BCS/BCC, comparing to "0"
Page 1 of 1

Author:  JoeGtake2 [ Sun Jul 30, 2017 3:00 pm ]
Post subject:  Transforming a BPL/BMI to BCS/BCC, comparing to "0"

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?

Author:  rainwarrior [ Sun Jul 30, 2017 3:09 pm ]
Post subject:  Re: Transforming a BPL/BMI to BCS/BCC, comparing to "0"

Couldn't you just add 16 to the number before doing an unsigned comparison then?

Author:  JoeGtake2 [ Sun Jul 30, 2017 3:11 pm ]
Post subject:  Re: Transforming a BPL/BMI to BCS/BCC, comparing to "0"

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.

Author:  tokumaru [ Sun Jul 30, 2017 5:50 pm ]
Post subject:  Re: Transforming a BPL/BMI to BCS/BCC, comparing to "0"

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:

  ;(operation here)
  sta Result
  rol CarryBackup

  ;(lots of other code here)

  lsr CarryBackup
  bcc NegativeResult

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group