Understanding how Randomize and Random32 work

144 Views Asked by At

I'm trying to create a procedure that generates a random string of length L, containing all capital letters. This procedure receives the length of the string (L) in EAX, and the pointer to a byte array in ESI where the random string will be saved. Returns the pointer to a byte array in ESI held the random string.

;.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

This is my implementation so far which returns random strings so length 11. I'm a bit confused on how Randomize and Random32 work. I know the random value generated is stored in eax but how do I retrieve it, and how do I specify the range in which the value should be between (ex: between 1-100)? Thanks for the help in advance!

1

There are 1 best solutions below

1
mar On
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