Re: [stella] one last 16 bit compare question

Subject: Re: [stella] one last 16 bit compare question
From: KirkIsrael@xxxxxxxxxxxxx
Date: 10 Mar 2004 05:19:15 -0000
> Be careful when comparing only the integer portion of a fixed point number
> because the fractional portion is considered unsigned.
> int    range
> 2    2.000 to 2.996
> 1    1.000 to 1.996
> 0    0.000 to 0.996
> -1    -0.004 to -1.000
> -2    -1.004 to -2.000
> So if you clip the integer value to +/- 2 your positive range will be higher
> than your negative range.

Here's the code I came up with too make sure the abs(speed) wasn't 
too big:


;making sure ball speed isnt to extreme (abs(speed) not too big)
	LDA slowBallYSpeed+1
	BMI    BallSpeedLessThanMax
	BPL    BallSpeedGreaterThanMin
	STA    slowBallYSpeed+1

So basically I'm just looking at the high byte...
against positive values, I can see the range is
0 < val <= 0000000111111111

but by just looking at the high byte for negative
values, how much more or less negative can it be 
than positive? If its just a few bits I don't care,
but I think it might be just under a whole byte worth--
an assymetry I don't like 

Maybe I should just do what I came up with for 
making sure abs(speed) wasn't too my
friend Ranjit's suggestion, if the speed is negative,
I take it's negative to get a positive, and then do 
that compare (I use Y register as a boolean about whether
the original value was negative, and then, if it's "too small", 
set it to the correct a bit small postive or a bit small
negative value::

	;comparison to make sure speed
	;isn't too slow; it's much easier to 
	;do comparisons w/ positive values,
	;and also we know it's a small positive 
	;value, so once it's positive, if the 
	;hi byte is not zero, it MUST be big enough
	;so we make a copy into temp, and then
	;change the original if we see the guaranteed
	;positive version is too small

	LDA slowBallYSpeed
	STA tempVar
	LDA slowBallYSpeed+1
	STA tempVar+1

	LDY #0 ; Y is boolean, 0 = postive

	LDA tempVar+1	
	BPL ThisAintNegativeSpeed

	LDY #1 ;remember that this is negative

	;negate what's in temp
	lda #0
	sbc tempVar
	sta tempVar
	lda #0
	sbc tempVar+1
	sta tempVar+1

	;we know tempvar is 16 bit, positive value
	LDA tempVar+1
	BNE DoneSeeingBallSpeedIsBigEnough
	LDA tempVar
	bcs DoneSeeingBallSpeedIsBigEnough
	BEQ ChangeBallSpeedToMinPositive
	sta slowBallYSpeed
	sta slowBallYSpeed+1
	JMP DoneSeeingBallSpeedIsBigEnough
	sta slowBallYSpeed
	sta slowBallYSpeed+1


It's a little brutal, and uses two bytes of tempVar,
but still, at least even *I* can understand it :-)

   "Death smiles at us all. All a man can do is smile back." --Gladiator 

Archives (includes files) at
Unsub & more at

Current Thread