Assembly Random Number within Range using Easy 68K (68000)

安稳与你 提交于 2019-12-31 02:46:27

问题


I'm creating a simple black jack game using the Easy 68K simulator and need to use a random number to assign the cards. My cards must be in the range 2 to 11. I seem to be getting the same number every time, and it isn't within the range I expect. My card value needs to end up in D3, so I have the following random number code:

 CLR.L       D0
 CLR.L       D3
 MOVE.B      #8, D0         ;Access time
 TRAP        #15
 AND.L       #$5FFFFF,D1    ;prevent overflow in divu
 DIVU        #10, D1
 SWAP        D1
 ADDQ.W      #1, D1
 MOVE        D1, D3

which I came to by modifying the code on this site: https://notendur.hi.is/voe1/3.%20onn/Tolvuhogun/EASy68K/Examples/tutorial3.X68

I am hoping to find help generating a number 2 through 11. I have been searching the internet for several hours. I know I need to access the time by using Move.B #8, D0, but beyond that, I haven't made much progress. Any help would be very much appreciated!


回答1:


The mask being used in the AND.L ("prevent overflow in divu") was only good for a division by 100 -- you'll need to mask with 0x7FFFF for a division by 10.

Why:

0xFFFFFFFF / #100 = 0x28f5c28 - too big for a single word!
0x5FFFFF   / #100 = 0xF5C2    - that fits

0x5FFFFF / #10  = 0x99999 - too big for a single word!
0x7FFFF  / #10  = 0xCCCC  - that fits

Additionally, the code you have will give you a number from 1 to 10 (0-9 + 1). If you want 2 to 11, you'll have to add 2, not 1.


Here's a more advanced random number generator, borrowed from the Mac OS QuickDraw source code. Note that you may need to translate the syntax somewhat (it was written over 25 years ago!) and/or modify the way it loads and stores its seed.

;--------------------------------------------------------------
;
;  FUNCTION Random: INTEGER;
;
;  returns a signed 16 bit number, and updates unsigned 32 bit randSeed.
;
;  recursion is randSeed := (randSeed * 16807) MOD 2147483647.
;
;  See paper by Linus Schrage, A More Portable Fortran Random Number Generator
;  ACM Trans Math Software Vol 5, No. 2, June 1979, Pages 132-138.
;
;  Clobbers D0-D2, A0
;
;
;  GET LO 16 BITS OF SEED AND FORM LO PRODUCT
;  xalo := A * LoWord(seed)
;
        MOVE.L  GRAFGLOBALS(A5),A0      ;POINT TO QuickDraw GLOBALS
        MOVE    #16807,D0               ;GET A = 7^5
        MOVE    D0,D2                   ;GET A = 7^5
        MULU    RANDSEED+2(A0),D0       ;CALC LO PRODUCT = XALO
;
;  FORM 31 HIGHEST BITS OF LO PRODUCT
;  fhi:=HiWord(seed) * ORD4(a) + HiWord(xalo);
;
        MOVE.L  D0,D1                   ;COPY xalo
        CLR.W   D1
        SWAP    D1                      ;GET HiWord(xalo) as a long
        MULU    RANDSEED(A0),D2         ;MULT BY HiWord(seed)
        ADD.L   D1,D2                   ;ADD LEFTLO = FHI
;
;  GET OVERFLOW PAST 31ST BIT OF FULL PRODUCT
;  k:=fhi DIV 32768;
;
        MOVE.L  D2,D1                   ;COPY FHI
        ADD.L   D1,D1                   ;CALC 2 TIMES FHI
        CLR.W   D1
        SWAP    D1                      ;CALC FHI SHIFTED RIGHT 15 FOR K
;
;  ASSEMBLE ALL THE PARTS AND PRE-SUBTRACT P
;  seed:=((BitAnd(XALO,$0000FFFF) - P) + BitAnd(fhi,$00007FFF) * b16) + K;
;
        AND.L   #$0000FFFF,D0           ;GET LO WORD XALO
        SUB.L   #$7FFFFFFF,D0           ;SUBTRACT P = 2^31-1
        AND.L   #$00007FFF,D2           ;BitAnd(fhi,$00007FFF)
        SWAP    D2                      ;TIMES 64K
        ADD.L   D1,D2                   ;PLUS K
        ADD.L   D2,D0                   ;CALC TOTAL
;
;  IF seed < 0 THEN seed:=seed+p;
;
        BPL.S   UPDATE
        ADD.L   #$7FFFFFFF,D0
UPDATE  MOVE.L  D0,RANDSEED(A0)         ;UPDATE SEED
        CMP.W   #$8000,D0               ;IS NUMBER -32768 ?
        BNE.S   NUMOK                   ;NO, CONTINUE
        CLR     D0                      ;YES, RETURN ZERO INSTEAD
NUMOK   MOVE.W  D0,4(SP)                ;RETURN LO WORD AS RESULT
        RTS



回答2:


You need to use the right mask depending on your interval (minimum and maximum), This code will solve all your problems, it take two arguments : Minimum in D5 and Maximum in D6, it also uses D0, D7 and A1.

RAND_GEN
    SUB     D5,D6             ;You should give Min in D5 and Max in D6
    MOVE    D6,D7
    ADDI     #1,D6
    MULU    #$FFFF,D7
    LEA     SEED,A1
    MOVE.B  #8,d0
    TRAP    #15
    ADD    (A1),D1
    MULU  #$FFFF,D1
    EOR.L     #$F321F23A,D1
    MOVE    D1,(A1)
    AND.L   D7,D1    ;PREVENT OVERFLOW FOR (Max-Min)
    DIVU    D6,D1         ;time count / (Max-Min)
    SWAP    D1              ;swap upper and lower words of D1 to put remainder in low word
    ADD  D5,d1           ;D1.W contains number in the range of (Min,Max)
    RTS

* Put variables and constants here
SEED
    DC.W    1

The SEED will be by default 1 at the start of program and updates itself automatically after each RAND_GEN call, you can also change it at any time.

Tested on EASY68K.



来源:https://stackoverflow.com/questions/8569381/assembly-random-number-within-range-using-easy-68k-68000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!