【问题标题】:Does the following code actually implement the LCG algorithm in assembly下面的代码是否真的在汇编中实现了LCG算法
【发布时间】:2021-06-22 13:05:50
【问题描述】:

听说在 Logical Congruential Generator 算法中,我们应该使用之前生成的数字来生成新的数字。但是,我找到了以下代码:

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

我看到它每次都使用系统时间,而不是以前的数字。我对吗? 我正在使用 TASM。

【问题讨论】:

    标签: assembly random x86 tasm


    【解决方案1】:

    CalcNew 函数正确使用了之前的种子。只需查看CalcNew: 标签和ret 之间的代码即可。

    AH=0 / int 1AH + mov [PRN], dx 成为 LCG 的种子。
    在整个程序中执行一次,就像在代码中一样。

    对于以后的调用,只需call CalcNew(并将结果处理到您想要的任何范围内)。请注意,调用代码在第一次调用之前使用当前时间的低位播种,但总共进行了两次调用。它不会在它们之间重新播种。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多