Jump to content
IGNORED

how do I know if sum is <0 or >255?


Recommended Posts

Short version: 

I'm adding a signed number to an unsigned number. What flag(s) will tell me if the sum has fallen below zero or above 255?

 

Longer version: 

 

I'm driving a rocket around with driving controllers, 16 directions, but the movement is chunky. I wanted more precision so I tried added a fractional component to the x and y coordinates. So PlayerX is the horizontal position, and PlayerXFrac is the fractional portion. I have the player's horizontal velocity in PlayerXVel, and it's either positive or negative. I add it to PlayerXFrac, and if that takes PlayerXFrac <0 i want to dec PlayerX. If the addition takes PlayerXFrac >255, I want to inc PlayerX.

 

But I can't figure out the right flag or flags to detect if the addition of PlayerXVel to PlayerXFrac passes the 0 or 255 boundary. 

 

I could check the N flag to see if I've gone under zero (bmi) but the N flag is also set if the result is anywhere $80-$FF, right?

Carry flag is cleared if I go <0 but it's also clear if I add a positive number. 

 

I feel like there's some super simple test I'm just missing, or else I just set this up to be more complicated than it needs to be. Any thoughts? I appreciate any and all suggestions!

 

Thanks! :D

 

 

Link to comment
Share on other sites

57 minutes ago, quohog said:

Short version: 

I'm adding a signed number to an unsigned number. What flag(s) will tell me if the sum has fallen below zero or above 255?

 

Longer version: 

 

I'm driving a rocket around with driving controllers, 16 directions, but the movement is chunky. I wanted more precision so I tried added a fractional component to the x and y coordinates. So PlayerX is the horizontal position, and PlayerXFrac is the fractional portion. I have the player's horizontal velocity in PlayerXVel, and it's either positive or negative. I add it to PlayerXFrac, and if that takes PlayerXFrac <0 i want to dec PlayerX. If the addition takes PlayerXFrac >255, I want to inc PlayerX.

 

But I can't figure out the right flag or flags to detect if the addition of PlayerXVel to PlayerXFrac passes the 0 or 255 boundary. 

 

I could check the N flag to see if I've gone under zero (bmi) but the N flag is also set if the result is anywhere $80-$FF, right?

Carry flag is cleared if I go <0 but it's also clear if I add a positive number. 

 

I feel like there's some super simple test I'm just missing, or else I just set this up to be more complicated than it needs to be. Any thoughts? I appreciate any and all suggestions!

 

Thanks! :D

 

 

 

You could sign-extend to 16 bits before the addition.

Here I use "X" to hold the sign extended high byte of PlayerXVel

  ldx #0
  lda PlayerXVel
  bpl signOK
  ldx #255
signOK

  clc
  adc PlayerXFrac
  sta PlayerXFrac
  txa
  adc PlayerX
  sta PlayerX

 

 

  • Thanks 1
Link to comment
Share on other sites

Thanks for the response! That's really interesting.

 

I'm confused though. Wouldn't it always decrement PlayerX if you were subtracting, whether PlayerXFrac went <0 or not?

 

Lets say PlayerXFrac is 4, PlayerXVel is -1. Then the X register would get -1. PlayerXFrac would end up a 3, still within it's range, but PlayerX would also get a one subtracted from it, wouldn't it? Or am I reading that wrong?

 

I only want PlayerX to be decremented if PlayerXFrac dips below 0. 

Link to comment
Share on other sites

8 minutes ago, quohog said:

Thanks for the response! That's really interesting.

 

I'm confused though. Wouldn't it always decrement PlayerX if you were subtracting, whether PlayerXFrac went <0 or not?

 

Lets say PlayerXFrac is 4, PlayerXVel is -1. Then the X register would get -1. PlayerXFrac would end up a 3, still within it's range, but PlayerX would also get a one subtracted from it, wouldn't it? Or am I reading that wrong?

 

I only want PlayerX to be decremented if PlayerXFrac dips below 0. 

PlayerXFrac = 4

PlayerXVel = -1 (255)

so, X = 255

 

So, we go...

 

4 + -1 --> 3 --> PlayerXFrac

leaving the carry flag SET (we overflowed)

then,  PlayerX + -1 (ie: the sign extension in X reg) + 1 (carry set) --> PlayerX -1+1 --> PlayerX

 

So no, PlayerX won't have 1 subtracted, because of the carry/overflow from the first addition.

 

  • Thanks 1
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...
  • Recently Browsing   0 members

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