Re: [stella] ASM comparison question

Subject: Re: [stella] ASM comparison question
From: "B. Watson" <atari@xxxxxxxxxxxxxx>
Date: Wed, 25 May 2005 11:13:31 -0400
On Wed, 25 May 2005, Dennis Debro wrote:

> I seem to be missing something. I'm trying to take a random number and
> make it stay between 9 and 149. The code I have is...

Does it absolutely *have* to be 9 to 149? If you can live with a
range of 9 to 137, it's simple:

 	jsr getRandomByte  ; byte is 0-255
 	lsr a              ; now it's 0-127, carry may or may not be set
 	adc #9             ; now it's 9-137, done
 	sta whereever

This has the advantage of running in a constant amount of time (assuming
your getRandomByte subroutine runs in constant time).

Another idea would be to have a getRandom0to7 routine (possibly an
alternate entry point to getRandomByte?) that returns a random number
from 0 to 7:

 	jsr getRandomByte
 	lsr a
 	sta tmp ; need one byte of RAM for this
 	jsr getRandom0to7
 	adc tmp ; now A contains 0-134
 	adc #9  ; now it's 9-143

That still doesn't get the range you originally wanted, but it's closer.
getRandom0to7 could be replaced with a getRandomNybble that returns a
number from 0 to 15, which would give you a final result between 9 and
151 instead (closer yet, but still no cigar...)

I assume the 9 to 149 range is being used for horizontal position of some
random badguy or bomb? Depending on what it is, my attempted solutions
might give the player a `safe zone' at the right edge of the screen,
where the bomb/alien/whatever is guaranteed to never appear.. you could
fix that by limiting the player's horizontal movement, of course.

Here's another idea. This one gives you the range you want, but doesn't
run in constant time (which probably is fine: your original code didn't).

randomLoop:
 	jsr getRandomByte
 	cmp #141
 	bcc add9 ; if A < 141, continue...
 	bcs randomLoop ; else A >= 141, try again

add9:     ; at this point, A is 0-140
 	adc #9 ; carry is known to be clear, we're really adding 9
           ; A is now 9-149, store it or whatever

Hopefully I've got my bcc/bcs logic straight: it's been a long time
since I've had time to do any 6502 asm. I really miss it, though...

Wow, that was fun! (Maybe it's even correct?)

--
B.
Archives (includes files) at http://www.biglist.com/lists/stella/archives/
Unsub & more at http://stella.biglist.com

Current Thread