vendredi 13 novembre 2020

Pseudo Random Character

So I'm learning assembly, and I found this code:

; Initialized seed value
    xor [myseed], esp

    ; Generate random string
    call randletter
    mov [message], al       ; Write 1st letter

; myrand gives a pseudo-random number in range 0..max
; inputs:
;   edi - max value
; outputs:
;   eax - generated value
myrand: mov eax, [myseed]       ; Load state of generator
        mov ecx, 4000000001 
        mul ecx                 ; Multiply state var by a constant
        add eax, 3333333333     ; Add another constant
        mov [myseed], eax       ; And save the result
        xor edx, edx
        inc edi
        div edi                 ; Divide state var in edx:eax by (max+1) in edi
        mov eax, edx            ; div instruction writes remainder into edx, so copy to eax
        ret

; randletter gives a random lowercase letter
; inputs: no
; outputs:
;   al - random lowercase letter (ASCII code)
randletter:
        mov edi, 25
        call myrand
        add al, 'a'
        ret


    section    .data

myseed: dd  123456789

Now I know that, this is linear congruential generator in the method myrand.
My question is how the number from myrand can cause the number from 97 to 122 in ASCII in randletter? And why they have add al, a in myrand. How can that help?

UPDATE 1: I added more to the code for clearer view




Aucun commentaire:

Enregistrer un commentaire