flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > PRNG

Author
Thread Post new topic Reply to topic
Ali.Z



Joined: 08 Jan 2018
Posts: 442
Ali.Z
all it does is generate a pseudo random number, check for any duplicates and abort the program.

Code:
  include 'win32a.inc'
  format PE console 4.0

section '.text' code readable executable

    rdtsc               ; in case you cant execute rdtsc in user-mode, then use time functions.
    not       eax
    mov       [next],eax
    mov       ebx,numbers
    xor       esi,esi
    not       esi
  generate:
    inc       esi
    test      esi,400h
    jnz       bye       ; i dont think any prng will hit this jump.
    call      rand      ; i dont really like calling rand, i would just put rand code here instead.
    xor       edi,edi
    not       edi
  check_for_duplicates:
    inc       edi
    cmp       eax,[ebx+edi*4]
    jz        duplicate_found
    cmp       esi,edi
    ja        check_for_duplicates
    mov       [ebx+esi*4],eax
    jmp       short generate
  duplicate_found:
    cinvoke   sprintf,formatted,text,eax,esi,edi
    mov       ebx,eax
    invoke    GetStdHandle,STD_OUTPUT_HANDLE
    invoke    WriteFile,eax,formatted,ebx,temp,0
  bye:
    xor       eax,eax
    ret

  rand:
    mov       eax,[next]
    imul      eax,1103515245
    add       eax,12345
    mov       [next],eax
    shr       eax,10h
    and       eax,7FFFh
    ret

section '.data' data readable writeable

  temp dd 0
  next dd 1
  numbers rd 400h
  text db 'duplicated number %d, found in iteration %d, first copy was found in iteration %d.',0
  formatted rb 200h

section '.idata' import data readable writeable

  library kernel32,'kernel32.dll',\
          ntdll,'ntdll.dll'

  import kernel32,\
         WriteFile,'WriteFile',\
         GetStdHandle,'GetStdHandle'

  import ntdll,\
         sprintf,'sprintf'

section '.reloc' fixups data readable discardable    

_________________
Asm For Wise Humans
Post 12 May 2021, 10:31
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18071
Location: In your JS exploiting you and your system
revolution
Ali.Z wrote:
check for any duplicates
If you need to avoid duplicates, then a better method might be to sequentially generate a list of values you need within some range, scramble the list into "random" order, and read off the values from the list.

For example, if you are doing a lottery draw like pick 6 from 40, then generate a complete list of 40 integers, randomise the order of the list, read off the first 6 values.
Post 12 May 2021, 15:06
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 442
Ali.Z
thank you, yes it is good idea.
and its simple to modify current code to skip over duplicated numbers instead of aborting the program.
and obviously save the list of desired/generated unique numbers, or just let it compute it everytime on the fly.

_________________
Asm For Wise Humans
Post 12 May 2021, 16:49
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.