Author Topic: Random seed for random algorithm example  (Read 551 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5770
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Random seed for random algorithm example
« on: April 13, 2018, 12:57:32 AM »
Simple and fast enough. The algo does not need the 1000 iterations, 5 or 10 would do but its fast enough to run a high count.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

entry_point proc

    LOCAL lcnt  :QWORD

    mov lcnt, 20

  lbl:
    call reseed
    conout str$(rax),lf
    sub lcnt, 1
    jnz lbl

    waitkey
    .exit

entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 NOSTACKFRAME

 reseed proc
  ; --------------------------------------
  ; random algorithm random seed generator
  ; --------------------------------------
    LOCAL var1  :QWORD

    lea r10, var1                           ; load the address
    mov r9, 1000                            ; set the counter
    mov r11, 12345678                       ; put something in r11

  @@:
    invoke QueryPerformanceCounter, r10     ; call the API
    mov rax, [r10]                          ; write value in var1 to rax
    bswap rax                               ; byte swap rax
    xor r11, rax                            ; xor combine with r11
    sub r9, 1                               ; decrement counter
    jnz @B                                  ; loop again if not 0

    mov rax, r11                            ; return xorred content in rax

    ret

 reseed endp

 STACKFRAME

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

wreckItRalph

  • Regular Member
  • *
  • Posts: 6
Re: Random seed for random algorithm example
« Reply #1 on: April 27, 2018, 03:42:48 AM »
Thats SO much shorter than Mersenne-Twister implementations in assembly Ive seen!

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5770
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Random seed for random algorithm example
« Reply #2 on: April 27, 2018, 08:36:24 AM »
Ralph,

Note that this algo is for seeding a random number generator, it is not a random number generator in itself.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

pinok

  • Regular Member
  • *
  • Posts: 3
Re: Random seed for random algorithm example
« Reply #3 on: Today at 12:29:28 AM »
LOCAL var1  :QWORD

fill the stack with garbage after calling QueryPerformanceCounter.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5770
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Random seed for random algorithm example
« Reply #4 on: Today at 02:07:54 AM »
 :biggrin:

Perhaps you could elaborate.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

pinok

  • Regular Member
  • *
  • Posts: 3
Re: Random seed for random algorithm example
« Reply #5 on: Today at 04:17:03 AM »
So?

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

include \masm32\include64\masm64rt.inc

.code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

LOCAL lcnt : QWORD

mov lcnt, 20

lbl :
   call reseed
   conout str$(rax), lf
   sub lcnt, 1
   jnz lbl

   waitkey
   .exit

   main endp

   ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

   NOSTACKFRAME

   reseed proc
   ; --------------------------------------
   ; random algorithm random seed generator
   ; --------------------------------------
   LOCAL var1 : QWORD

   mov r9, 1000; set the counter
   mov r11, 12345678; put something in r11

   @@:
   invoke QueryPerformanceCounter, addr var1; call the API
   mov rax, var1; write value in var1 to rax
   bswap rax; byte swap rax
   xor r11, rax; xor combine with r11
   sub r9, 1; decrement counter
   jnz @B; loop again if not 0

   mov rax, r11; return xorred content in rax

   ret

   reseed endp

   STACKFRAME

   ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

   end

pinok

  • Regular Member
  • *
  • Posts: 3
Re: Random seed for random algorithm example
« Reply #6 on: Today at 04:20:40 AM »
in your example, r10 = 0 after the QueryPerformanceCounter. With the stack everything is fine :-), because the result is immediately recorded there.