home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 13.ddi / RTLSYS.ZIP / RAND.ASM < prev    next >
Encoding:
Assembly Source File  |  1992-10-28  |  2.0 KB  |  129 lines

  1.  
  2. ; *******************************************************
  3. ; *                            *
  4. ; *     Turbo Pascal Run-time Library                   *
  5. ; *    Random Number Generator                *
  6. ; *                            *
  7. ; *     Copyright (c) 1988,92 Borland International     *
  8. ; *                            *
  9. ; *******************************************************
  10.  
  11.     TITLE    RAND
  12.  
  13.     INCLUDE    SE.ASM
  14.  
  15. DATA    SEGMENT    WORD PUBLIC
  16.  
  17. ; Externals
  18.  
  19.     EXTRN    RandSeed:DWORD
  20.  
  21. DATA    ENDS
  22.  
  23. CODE    SEGMENT    BYTE PUBLIC
  24.  
  25.     ASSUME    CS:CODE,DS:DATA
  26.  
  27. ; Publics
  28.  
  29.     PUBLIC    RandInt,RandReal,RandFloat,InitRand
  30.  
  31. ; Random standard function (Integer)
  32.  
  33. RandInt:
  34.  
  35.     CALL    NextRand
  36.     MOV    BX,SP
  37.     MOV    CX,DX
  38.     MUL    WORD PTR SS:[BX+4]
  39.     MOV    AX,CX
  40.     MOV    CX,DX
  41.     MUL    WORD PTR SS:[BX+4]
  42.     ADD    AX,CX
  43.     ADC    DX,0
  44.     MOV    AX,DX
  45.     RETF    2
  46.  
  47. ; Random standard function (Real)
  48.  
  49. RandReal:
  50.  
  51.     CALL    NextRand
  52.     XCHG    AX,BX
  53.     MOV    AX,80H
  54.     MOV    CX,32
  55. @@1:    TEST    DH,80H
  56.     JNE    @@2
  57.     SHL    BX,1
  58.     RCL    DX,1
  59.     DEC    AL
  60.     LOOP    @@1
  61.     XOR    AL,AL
  62. @@2:    AND    DH,7FH
  63.     RETF
  64.  
  65. ; Random standard function (8087)
  66.  
  67. RandFloat:
  68.  
  69.     CALL    NextRand        ;Compute next random number
  70.     FILD    CS:ConstScale        ;Load -32
  71.     FILD    RandSeed        ;Load 32-bit random integer
  72.     FADD    CS:ConstDelta        ;Scale to 32-bit positive integer
  73.     FSCALE                ;Scale so 0<=ST<1
  74.     FSTP    ST(1)            ;Remove scaling factor
  75.     FWAIT                ;Wait for result
  76.     RETF
  77.  
  78. ; Scaling constants
  79.  
  80. ConstDelta    DD    2147483648.0
  81. ConstScale    DW    -32
  82.  
  83. ; Compute next random number
  84. ; New := 8088405H * Old + 1
  85. ; Out    DX:AX = Next random number
  86.  
  87. NextRand:
  88.  
  89.     MOV    AX,RandSeed.w0
  90.     MOV    BX,RandSeed.w2
  91.     MOV    CX,AX
  92.     MUL    CS:Factor        ;New = Old.w0 * 8405H
  93.     SHL    CX,1            ;New.w2 += Old.w0 * 808H
  94.     SHL    CX,1
  95.     SHL    CX,1
  96.     ADD    CH,CL
  97.     ADD    DX,CX
  98.     ADD    DX,BX            ;New.w2 += Old.w2 * 8405H
  99.     SHL    BX,1
  100.     SHL    BX,1
  101.     ADD    DX,BX
  102.     ADD    DH,BL
  103.     MOV    CL,5
  104.     SHL    BX,CL
  105.     ADD    DH,BL
  106.     ADD    AX,1            ;New += 1
  107.     ADC    DX,0
  108.     MOV    RandSeed.w0,AX
  109.     MOV    RandSeed.w2,DX
  110.     RET
  111.  
  112. ; Multiplication factor
  113.  
  114. Factor        DW    8405H
  115.  
  116. ; Randomize standard procedure
  117.  
  118. InitRand:
  119.  
  120.     MOV    AH,dosGetTime
  121.     INT    DOS
  122.     MOV    RandSeed.w0,CX
  123.     MOV    RandSeed.w2,DX
  124.     RETF
  125.  
  126. CODE    ENDS
  127.  
  128.     END
  129.