quohog Posted July 3, 2020 Share Posted July 3, 2020 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! Quote Link to comment Share on other sites More sharing options...
+Andrew Davie Posted July 3, 2020 Share Posted July 3, 2020 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! 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 1 Quote Link to comment Share on other sites More sharing options...
quohog Posted July 3, 2020 Author Share Posted July 3, 2020 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. Quote Link to comment Share on other sites More sharing options...
+Andrew Davie Posted July 3, 2020 Share Posted July 3, 2020 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. 1 Quote Link to comment Share on other sites More sharing options...
quohog Posted July 4, 2020 Author Share Posted July 4, 2020 :HEAD EXPLODING EMOJI: Oh wow, that's brilliant. I'll try that! Thanks, Andrew! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.