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: 718
Ali.Z 12 May 2021, 10:31
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: 20335
Location: In your JS exploiting you and your system
revolution 12 May 2021, 15:06
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: 718
Ali.Z 12 May 2021, 16:49
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
bzt



Joined: 09 Nov 2018
Posts: 77
bzt 01 Sep 2021, 16:14
Or even better, if CPUID[EAX=1].ECX bit 30 is set (available on Ivy Bridge and later):
Code:
    rdrand eax    
This provides a random number with a lot better entropy. See Intel spec, section 3.3.1 and section 5.1.

Cheers,
bzt
Post 01 Sep 2021, 16:14
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1792
Roman 01 Sep 2021, 16:35
fasm support rdrand and RDSEED ?
Or we need write db $0F, $C7, $F0 ;RDRAND EAX

db $48, $0F, $C7, $F8 ;RDSEED RAX

PS: RDRand slow 35%, а RDSeed slow 50% then c functions. But gived best result.
Post 01 Sep 2021, 16:35
View user's profile Send private message Reply with quote
bzt



Joined: 09 Nov 2018
Posts: 77
bzt 01 Sep 2021, 17:21
fasm does support RDRAND natively. There's no C function for this, as it is a single instruction (you have to use intrinsics or inline Assembly in C, which means no performance difference between C and Assembly implementations).

Cheers,
bzt
Post 01 Sep 2021, 17:21
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.