汇编随机数生成器

Kor*_*hev 4 x86 assembly emu8086

我正在使用程序集8086emu,我需要一个用于8个数字的数字生成器。我试图通过@johnfound使用这段代码:

RANDGEN:         ; generate a rand no using the system time

RANDSTART:
   MOV AH, 00h  ; interrupts to get system time        
   INT 1AH      ; CX:DX now hold number of clock ticks since midnight      

   mov  ax, dx
   xor  dx, dx
   mov  cx, 10    
   div  cx       ; here dx contains the remainder of the division - from 0 to 9

   add  dl, '0'  ; to ascii from '0' to '9'
   mov ah, 2h   ; call interrupt to display a value in DL
   int 21h    
RET    
Run Code Online (Sandbox Code Playgroud)

但仅在生成1个数字时才有用。我尝试创建伪随机函数,但是我对汇编语言很陌生,但没有成功。我想知道是否有一种方法可以Math.random()在组装8086中转换java的函数或类似的东西。谢谢

Sep*_*and 6

一个简单的伪随机数生成器将当前数乘以25173,然后再加上13849。现在,该值成为新的随机数。
如果您像以前一样从系统计时器启动(这称为对随机数生成器进行播种),那么这一系列数字将足够随机,可以完成简单的任务!

MOV     AH, 00h   ; interrupt to get system timer in CX:DX 
INT     1AH
mov     [PRN], dx
call    CalcNew   ; -> AX is a random number
xor     dx, dx
mov     cx, 10    
div     cx        ; here dx contains the remainder - from 0 to 9
add     dl, '0'   ; to ascii from '0' to '9'
mov     ah, 02h   ; call interrupt to display a value in DL
int     21h    
call    CalcNew   ; -> AX is another random number
...
ret

; ----------------
; inputs: none  (modifies PRN seed variable)
; clobbers: DX.  returns: AX = next random number
CalcNew:
    mov     ax, 25173          ; LCG Multiplier
    mul     word ptr [PRN]     ; DX:AX = LCG multiplier * seed
    add     ax, 13849          ; Add LCG increment value
    ; Modulo 65536, AX = (multiplier*seed+increment) mod 65536
    mov     [PRN], ax          ; Update seed = return value
    ret
Run Code Online (Sandbox Code Playgroud)

这实现了具有2幂模数的线性同余生成器(LCG)%65536这是免费的,因为乘积+增量的低16位在AX中,而高位不在。