flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Random number macro

Author
Thread Post new topic Reply to topic
omega_red



Joined: 13 Mar 2004
Posts: 19
Location: Poland
omega_red 03 Oct 2004, 16:05
I want to create a macro that would return a pseudorandom number. I've adapted simple PRNG:
Code:
random_seed = 1

macro random
{
       random_seed = (random_seed*214013+2531011) shr 16
   dd      random_seed
}    

However, this code eventually gives me an error: value out of range. How to make it to truncate value without giving the error?

_________________
Vulnerant omnes, ultima necat
Post 03 Oct 2004, 16:05
View user's profile Send private message Visit poster's website Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 03 Oct 2004, 18:34
try this:
Code:
random_seed = ((random_seed*214013+2531011) shr 16) and 0xffffffff    


you can use %t as an initial value of random_seed
Post 03 Oct 2004, 18:34
View user's profile Send private message Visit poster's website Reply with quote
omega_red



Joined: 13 Mar 2004
Posts: 19
Location: Poland
omega_red 03 Oct 2004, 19:13
Thanks, that's working.
Post 03 Oct 2004, 19:13
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 09 Nov 2004, 15:07
i have made my latest randomz3 routine a macro version Smile

Code:
__lasttimevar=0
__random_seed = %t
__range=$ffffffff

macro db_4random_bytes
{
;randomz3: ; Z3 ecx=range transparent
__timevar=%t
__random_seed=(__random_seed+__timevar)and 0xffffffff

__al=(__timevar and $ff) xor ((__timevar shr 8) and $ff)

__ch_1=(__range shr 8) and $ff
;ror ch,4
__ch_tmp1=(__ch_1 shr 4) and $f
__ch_tmp2=(__ch_1 and $f) shl 4
__ch=__ch_tmp1 or __ch_tmp2

;xor bl,ch
__bl_1=__random_seed and $ff
__bl=__bl_1 xor __ch_1

cl_and_f=__range and $f
__bx_1=__random_seed and $ffff
;rol bx,cl
__bx_tmp1=(__bx_1 shl cl_and_f)
__bx_tmp2=(__bx_tmp1 shr 16)
__bx_tmp1=__bx_tmp1 and $ffff
__bx=__bx_tmp1 or __bx_tmp2

;btc bx,cx
__bx=__bx xor (1 shl cl_and_f)
__bx=__bx xor ((__timevar and $ff)xor((__timevar shr 16) and $ff))

;mov cl,al xor cl,ah
__al=(__timevar and $ff) xor ((__timevar shr 8) and $ff)

;ror ch,4
__ch_tmp1=(__ch_1 shr 4) and $f
__ch_tmp2=(__ch_1 and $f) shl 4
__ch=__ch_tmp1 or __ch_tmp2
;xor bl,ch
__bl=__bl_1 xor __ch_1

;ror bx,cl
__bx_tmp1=__bx_1 shl 16
__bx_tmp2=(__bx_tmp1 shr cl_and_f) and $ffff
__bx_tmp1=__bx_tmp2 shr 16
__bx=__bx_tmp1 or __bx_tmp2

;btc bx,cx
__bx=__bx xor (1 shl cl_and_f)

__bx=__bx xor ((__timevar and $ff)xor((__timevar shr 16) and $ff))

__cl=__bx and $ff

repeat __cl
;rol eax,cl
__timevar_tmp1=(__timevar shl __cl) and $ffffffff
__timevar_tmp2=(__timevar shr (32-__cl)) and $ffffffff
__timevar=__timevar_tmp1 or __timevar_tmp2
__cl=__cl and $f
;btc ax,cx
__timevar=__timevar xor (1 shl __cl)
end repeat

__cl=(__bx shr 8) and $ff
repeat __cl
;ror eax,cl
__timevar_tmp1=(__timevar shl (32-__cl)) and $ffffffff
__timevar_tmp2=(__timevar shr __cl)
__timevar=__timevar_tmp1 or __timevar_tmp2
__cl=__cl and $f
;btc ax,cx
__timevar=__timevar xor (1 shl __cl)
end repeat

__timevar=__timevar mod __range

dd __timevar and $ffffffff
}

db_4random_bytes
db_4random_bytes
db_4random_bytes
    
Post 09 Nov 2004, 15:07
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 09 Nov 2004, 16:09
Sad i have made it cleaned up, and i can't mke it work on multiple bytes, cause %t is not rdtsc, ccan't get current timstamp while compiling.

Code:
__lasttimevar=0
__random_seed = %t
;__range=$ffffffff
__range=1

macro db_4random_bytes ;randomz3 __range=range __random_seed= random seed
{
__timevar=%t
__random_seed=__random_seed + %t and 0xffffffff

__al=(__timevar and $ff) xor ((__timevar shr 8) and $ff)

__ch_1=(__range shr 8) and $ff
;ror ch,4
__ch_tmp1=(__ch_1 shr 4) and $f
__ch_tmp2=(__ch_1 and $f) shl 4
__ch=__ch_tmp1 or __ch_tmp2

;xor bl,ch
__bl_1=__random_seed and $ff
__bl=__bl_1 xor __ch_1

cl_and_f=__range and $f
__bx_1=__random_seed and $ffff
;rol bx,cl
__bx_tmp1=(__bx_1 shl cl_and_f)
__bx_tmp2=(__bx_tmp1 shr 16)
__bx_tmp1=__bx_tmp1 and $ffff
__bx=__bx_tmp1 or __bx_tmp2

;btc bx,cx
__bx=__bx xor (1 shl cl_and_f)
__bx=__bx xor ((__timevar and $ff)xor((__timevar shr 16) and $ff))

;mov cl,al xor cl,ah
__al=(__timevar and $ff) xor ((__timevar shr 8) and $ff)

;ror ch,4
__ch_tmp1=(__ch_1 shr 4) and $f
__ch_tmp2=(__ch_1 and $f) shl 4
__ch=__ch_tmp1 or __ch_tmp2
;xor bl,ch
__bl=__bl_1 xor __ch_1

;ror bx,cl
__bx_tmp1=__bx_1 shl 16
__bx_tmp2=(__bx_tmp1 shr cl_and_f) and $ffff
__bx_tmp1=__bx_tmp2 shr 16
__bx=__bx_tmp1 or __bx_tmp2

;btc bx,cx
__bx=__bx xor (1 shl cl_and_f)

__bx=__bx xor ((__timevar and $ff)xor((__timevar shr 16) and $ff))

__cl=__bx and $ff

repeat __cl
;rol eax,cl
__timevar_tmp1=(__timevar shl __cl) and $ffffffff
__timevar_tmp2=(__timevar shr (32-__cl)) and $ffffffff
__timevar=__timevar_tmp1 or __timevar_tmp2
__cl=__cl and $f
;btc ax,cx
__timevar=__timevar xor (1 shl __cl)
end repeat

__cl=(__bx shr 8) and $ff
repeat __cl
;ror eax,cl
__timevar_tmp1=(__timevar shl (32-__cl)) and $ffffffff
__timevar_tmp2=(__timevar shr __cl)
__timevar=__timevar_tmp1 or __timevar_tmp2
__cl=__cl and $f
;btc ax,cx
__timevar=__timevar xor (1 shl __cl)
end repeat

__timevar=__timevar mod __range

dd __timevar and $ffffffff

}

repeat 120
db_4random_bytes
end repeat
    
Post 09 Nov 2004, 16:09
View user's profile Send private message Visit poster's website 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.