Heaven/TQA, on Thu Jul 19, 2007 2:20 AM, said:
Thanks Rob,
have to check on weekend.
btw. could be a RAND(100) done like that?
lda 53770 ; (random value 0-255)
lsr
lsr
clc
adc #36
???
When you say a RAND(100) function, I am guessing that you mean you want the random value returned to be in BCD format. If that is what you mean, then it is possible. We just need to feed the routine min and max numbers in BCD format, and use BCD math internally. My untested BCD version of the routine is below. The routine is slowed a bit by using BCD, but not much.
Cheers!
SUBROUTINE
Answer DS.W 1; Allocate 16-bits
Adder DS.W 1; Allocate 16-bits
Mult DS.B 1;
lobyte EQU 0; byte offsets into Answer and Adder.
hibyte EQU 1
BCDRangedRandom
; NOTE: This routine assumes inputs in BCD format, and returns
; a BCD value in A.
; Given X = min, and Y = max; where 0 <= X <= Y <= 99 [BCD]
; Returns a random number in A, between min and max inclusive
; in BCD format.
SED ; Put the processor in BCD math mode.
STX Answer.hibyte; Start at the minimum posible value.
TYA ; A = max
SEC ; Calculate the number of possible values
SBC min ; that could be returned as max - min + 1
ADC #0 ; Carry is always set by previous subtraction.
STA Adder.lobyte; Fraction to be mulitplied 0 - 255 times.
; and added to min to form the answer.
LDA #0
STA Adder+hibyte; Adder = 00:max-min+1
STA Answer+lobyte; Answer = min:00
LDA 53770;atari800 random register (0-255)
whileMultNotZeroDo ; This loop executes 0 to 8 times.
BEQ .Done
LSR ; check if next bit in Mult is set.
STA Mult
BCC .skipAdd
CLC ; Add Adder to Answer (16-bit addition).
LDA Answer+lobyte
ADC Adder+lobyte
STA Answer+lobyte
LDA Answer+hibyte
ADC Adder+hibyte
STA Answer+hibyte
.skipAdd
; a BCD number can not be shifted to mulitply by 2.
; instead the code must add the BCD number it to itself.
; Carry should always be 0 at this point so no CLC is needed here.
LDA Adder+lobyte
ADC Adder+lobyte
STA Adder+lobyte
LDA Adder+hibyte
ADC Adder+hibyte; Multiply the 16-bit adder by 2 every
STA Adder+hibyte; time through the loop. x1,x2,x4,...,x128
LDA Mult
BCC whileMultNotZeroDo; branch is always taken.
.Done
CLD ; Turn off BCD math mode.
LDA Answer+hibyte; A = random # in range min to max.
RTS ; X = min; Y = max