RANDOM.ASM 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;
  2. ; Command & Conquer Red Alert(tm)
  3. ; Copyright 2025 Electronic Arts Inc.
  4. ;
  5. ; This program is free software: you can redistribute it and/or modify
  6. ; it under the terms of the GNU General Public License as published by
  7. ; the Free Software Foundation, either version 3 of the License, or
  8. ; (at your option) any later version.
  9. ;
  10. ; This program is distributed in the hope that it will be useful,
  11. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ; GNU General Public License for more details.
  14. ;
  15. ; You should have received a copy of the GNU General Public License
  16. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;
  18. ;***************************************************************************
  19. ;** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  20. ;***************************************************************************
  21. ;* *
  22. ;* Project Name : Library routine *
  23. ;* *
  24. ;* File Name : RANDOM.ASM *
  25. ;* *
  26. ;* Programmer : Christopher Yates *
  27. ;* *
  28. ;* Last Update : 20 August, 1990 [CY] *
  29. ;* *
  30. ;*-------------------------------------------------------------------------*
  31. ;* Functions: *
  32. ;* *
  33. ;* UBYTE Random(VOID); *
  34. ;* *
  35. ;* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *
  36. IDEAL
  37. P386
  38. MODEL USE32 FLAT
  39. Global Random :NEAR
  40. Global Get_Random_Mask :NEAR
  41. Global RandNumb :DWORD
  42. DATASEG
  43. RandNumb DD 12349876H
  44. CODESEG
  45. ; ----------------------------------------------------------------
  46. ;
  47. ; Here are prototypes for the routines defined within this module:
  48. ;
  49. ; UBYTE Random(VOID);
  50. ; int Get_Random_Mask(int maxval);
  51. ;
  52. ; ----------------------------------------------------------------
  53. ;-----------------------------------------------------------------
  54. ; RANDOM
  55. ;
  56. ; UBYTE Random(VOID);
  57. ;
  58. ;*
  59. PROC Random C near
  60. USES esi
  61. lea esi, [RandNumb] ; get offset in segment of RandNumb
  62. xor eax,eax
  63. mov al,[esi]
  64. shr al,1 ; shift right 1 bit (bit0 in carry)
  65. shr al,1
  66. rcl [BYTE PTR esi+2],1 ; rcl byte 3 of RandNumb
  67. rcl [BYTE PTR esi+1],1 ; rcl byte 2 of RandNumb
  68. cmc ; complement carry
  69. sbb al,[esi] ; sbb byte 1 of RandNumb
  70. shr al,1 ; sets carry
  71. rcr [BYTE PTR esi],1 ; rcr byte 1 of RandNumb
  72. mov al,[esi] ; reload byte 1 of RandNumb
  73. xor al,[esi+1] ; xor with byte 2 of RandNumb
  74. ret
  75. ENDP Random
  76. ;-----------------------------------------------------------------
  77. ; GET_RANDOM_MASK - returns an AND value that is large enough that it
  78. ; encloses the 'maxval' parameter.
  79. ;
  80. ; int Get_Random_Mask(int maxval);
  81. ;
  82. ;*
  83. PROC Get_Random_Mask C near
  84. USES ecx
  85. ARG maxval:DWORD
  86. ; This function takes as a parameter a maximum value, for example, 61. It
  87. ; then tries to create an AND mask that is big enough to enclose that number.
  88. ; For our example case, that AND mask would be 0x3F. It does this by scanning
  89. ; for the highest bit in the number, then making an all-1's mask from that
  90. ; bit position down to bit 0.
  91. bsr ecx,[maxval] ; put bit position of highest bit in ecx
  92. mov eax,1 ; set one bit on in eax
  93. jz ??invalid ; if BSR shows maxval==0, return eax=1
  94. inc ecx ; get one bit higher than count showed
  95. shl eax,cl ; move our EAX bit into position
  96. dec eax ; dec it to create the mask.
  97. ??invalid:
  98. ret
  99. ENDP Get_Random_Mask
  100. ;----------------------------------------------------------------------------
  101. END
  102.