home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / asmlib.lbr / RANDOM.AZM / RANDOM.ASM
Encoding:
Assembly Source File  |  1991-06-25  |  2.4 KB  |  114 lines

  1. ;----------------------------------------------------------------
  2. ;         This is a module in the ASMLIB Library.
  3. ;
  4. ; This module generates PSEUDO RANDOM NUMBERS by using a seed array
  5. ; and doing adds and shifts on the bytes in the array. The returned
  6. ; value from the function is in the accumulator.
  7. ;
  8. ; The entry points are as follows.
  9. ;
  10. ;
  11. ; randinit
  12. ; rand8
  13. ; rand16
  14. ; randp16
  15. ;
  16. ;            Written        R.C.H.         12/10/83
  17. ;            Last Update     R.C.H.         13/10/83
  18. ;----------------------------------------------------------------
  19. ;
  20.     name    'random'
  21.     public    rand8,rand16,randp16,randinit
  22.     maclib    z80
  23. ;
  24. randinit:
  25.     ldax    d
  26.     cpi    5
  27.     rc            ; Error if less than 5 elements in the array
  28.     push    d
  29.     push    b
  30.     mov    b,a        ; Load counter
  31.     xchg
  32.     inx    h        ; Now HL -> first seed byte
  33.     ldar            ; Get refresh register value
  34.     dcr    b        ; Do one less than the required
  35. initloop:
  36.     add    m
  37.     rrc
  38.     mov    m,a
  39.     inr    m
  40.     mov    a,m
  41.     inx    h
  42.     djnz    initloop
  43. ; Restore and exit gracefully
  44.     xchg            ; Restore HL
  45.     pop    b
  46.     pop    d        ; Restore other registers
  47.     ret
  48. ;
  49. ; Return an 8 bit random number in A
  50. ;
  51. rand8:    
  52.     ldax    d        ; A = number of seeds
  53.     cpi    5        ; Check if less than 5 seed values
  54.     rc            ; Return with a carry to indicate an error
  55. ; Here we load the number of cells into B then decrtement so as to skip
  56. ; these which are operated on later.
  57.     push    b
  58.     push    d        ; Saver address of seed array
  59.     mov    b,a
  60.     dcr    b
  61.     dcr    b
  62.     inx    d        ; DE -> first seed in the array
  63.     xchg            ; Put memory pointer into HL
  64. ;
  65. ;Loop for N-2 times.
  66. loop:    inr    m        ;INCREMENT SEED VALUE.
  67.     mov    a,m
  68.     inx    h        ;HL POINTS TO NEXT SEED VALUE IN ARRAY.
  69.     add    m
  70.     rrc            ;ROTATE RIGHT CIRCULAR ACCUMULATOR.
  71.     mov    m,a
  72.     djnz    loop
  73. ;
  74. ; Last iteration to compute random byte in register a.
  75.     inx    h         ; HL -> last byte in the array
  76.     add    m
  77.     cma            ; complement the accumulator
  78.     rrc            ; rotate it right
  79.     mov    m,a
  80.     xra    a        ; Clear carry
  81.     mov    a,m        ; Re-load value, carry not set.
  82. ;
  83. ; Restore the registers and return with the value in A
  84. ERROR:    
  85.     xchg            ; Restore HL
  86.     pop    d
  87.     pop    b
  88.     ret
  89. ;
  90. ; Return a 16 bit random number in HL.
  91. ;
  92. rand16:
  93.     call    rand8
  94.     mov    h,a        ; msb byte
  95.     call    rand8
  96.     mov    l,a        ; lsb byte
  97.     ret
  98. ;
  99. ; Return a positive 16 bit random number
  100. ;
  101. randp16:
  102.     call    rand16
  103.     mov    a,h
  104.     ani    07fh        ; Mask off top bit
  105.     mov    h,a        ; restore
  106.     mov    a,l        ; echo lsb in a
  107.     ret
  108. ;
  109.     end
  110.  
  111.  
  112.  
  113.