了解 Randomize 和 Random32 的工作原理

问题描述 投票:0回答:1

我正在尝试创建一个过程来生成长度为 L 的随机字符串,其中包含所有大写字母。此过程接收 EAX 中的字符串长度 (L),以及指向 ESI 中将保存随机字符串的字节数组的指针。返回指向 ESI 中保存随机字符串的字节数组的指针。

;.386
;.model flat,stdcall
;.stack 4096
;ExitProcess PROTO, dwExitCode:DWORD
INCLUDE Irvine32.inc

str_len = 10

.data
str_index BYTE str_len DUP(0),  0

.code
main proc
    call Clrscr
    mov esi, OFFSET str_index
    call Randomize
    mov ecx, 20
    
L1:
    call Random32
    call CreateRandomString
    loop L1

    invoke ExitProcess,0
main endp

CreateRandomString PROC
; Receives: Length of the string (L) in EAX, the pointer
; to a byte array in ESI where the random string will be saved
; Returns: Pointer to a byte array in ESI held the random string
;-----------------------------------------------------
mov ecx, LENGTHOF str_index
L2:
    mov eax, 26
    call RandomRange
    add eax, 65
    mov[esi], eax
    call WriteChar
    loop L2
call Crlf
ret
CreateRandomString ENDP

end main

这是我到目前为止的实现,它返回长度为 11 的随机字符串。我对 Randomize 和 Random32 的工作原理有点困惑。我知道生成的随机值存储在 eax 中,但如何检索它,以及如何指定该值应在的范围(例如:1-100 之间)?感谢您提前的帮助!

assembly random irvine32
1个回答
0
投票

包括 Irvine32.inc

.数据 origString BYTE 100 DUP(0) 随机字符串 BYTE 100 DUP(0)

.代码 主程序 调用随机化 移动ECX,20

L1: INVOKE Str_copy、ADDR origString、ADDR randomString 推送ECX 调用创建随机字符串 流行ECX mov EDX, OFFSET 随机字符串 调用写字符串 呼叫Crlf 循环L1

INVOKE ExitProcess,0

CreateRandomString PROC
    mov EAX, 100
    call RandomRange
    inc EAX
    mov ECX, EAX
    mov ESI, OFFSET randomString
L2:
    mov EAX, 26
    call RandomRange
    add EAX, 65
    mov [ESI], EAX
    inc esi
    loop L2

    ret
CreateRandomString ENDP

主要ENDP 主线结束

© www.soinside.com 2019 - 2024. All rights reserved.