assembly 了解Randomize和Random32的工作原理

chhkpiq4  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(95)

我试图创建一个过程,生成一个长度为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和Random 32的工作原理有点困惑。我知道生成的随机值存储在eax中,但我如何检索它,以及如何指定值应该在(例如:1-100之间)?提前感谢您的帮助!

2wnc66cl

2wnc66cl1#

INCLUDE Irvine32.inc

.data
origString BYTE 100 DUP(0)
randomString BYTE 100 DUP(0)

.code
main PROC
    call Randomize
    mov ECX, 20

L1:
    INVOKE Str_copy, ADDR origString, ADDR randomString
    push ECX
    call CreateRandomString
    pop ECX
    mov EDX, OFFSET randomString
    call WriteString
    call Crlf
    loop 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

main ENDP
END main

相关问题