flat assembler
Message board for the users of flat assembler.
Index
> Windows > Random number: Is there an easier way to generate? |
Author |
|
lamar 18 Jun 2009, 15:15
;Pentium processor and upper
Code: push edx rdtsc pop edx ;in eax - processor ticks count |
|||
18 Jun 2009, 15:15 |
|
pal 18 Jun 2009, 15:58
Neither of these are random numbers. Computer software can only generate pseudo-random numbers. If you are looking for a simple code then check out the Crypto APIs. I did a basic example using the crypto APIs for you:
Code: format pe console entry start include '..\INCLUDE\Win32a.inc' CRYPT_VERIFYCONTEXT equ 0xF0000000 PROV_RSA_FULL equ 0x1 section '.code' code readable executable start: push CRYPT_VERIFYCONTEXT push PROV_RSA_FULL push 0 0 push hProvider call [CryptAcquireContext] test eax,eax je @F push szRandom push 128 push [hProvider] call [CryptGenRandom] test eax,eax je @F push 0 push hProvider call [CryptReleaseContext] push szRandom push szFormat call [printf] add esp,8 nop nop @@: ret section '.idata' import readable writeable library MSCVRT,'MSVCRT.DLL',\ ADVAPI,'ADVAPI32.DLL' import MSCVRT,\ printf,'printf' import ADVAPI,\ CryptAcquireContext ,'CryptAcquireContextA',\ CryptGenRandom ,'CryptGenRandom',\ CryptReleaseContext ,'CryptReleaseContext' hProvider dd 0 szRandom db 129 dup 0 szFormat db '%s',0 Check out FXT book and Numerical Recipies in C for some nice algorithms. I believe that The Art of Programming Volume 2 also has some. |
|||
18 Jun 2009, 15:58 |
|
asmcoder 18 Jun 2009, 19:57
[content deleted]
Last edited by asmcoder on 14 Aug 2009, 14:50; edited 1 time in total |
|||
18 Jun 2009, 19:57 |
|
pal 18 Jun 2009, 20:30
Unless you use a random number generator (hardware)...
|
|||
18 Jun 2009, 20:30 |
|
DMD 23 Jun 2009, 05:42
pal
Not bad, but code very heavy and slowly.... so, you must have so much time and trust MS lamar Processor ticks count isn't random generator eskizo May be this code can help you? Code: invoke GetTickCount ; Pseudo initialization ;----------------------------------------------------------------------- ; original source by unknow ;----------------------------------------------------------------------- sub esp, 10h ; mov dword [esp], eax ; fild dword [esp] ; fbstp tbyte [esp] ; mov eax, dword [esp] ; xor eax, ecx ; xchg eax, ecx ; add esp, 10h ; PS/ If you need another code - let me know! |
|||
23 Jun 2009, 05:42 |
|
revolution 23 Jun 2009, 06:23
For high quality random numbers the MS code by pal will be the best. The Crypto API is very robust and almost completely random (it uses many sources for the seed not available to user programs). Certainly very much better than any RDTSC or GetTickCount alternatives.
|
|||
23 Jun 2009, 06:23 |
|
windwakr 23 Jun 2009, 13:58
revolution wrote: (it uses many sources for the seed not available to user programs) Like what? EDIT: Ok, cryptgenrandom calls rtlgenrandom and uses that to gen its numbers. rtlgenrandom uses this huge list for its generation! How much of this isn't available to the user? rtlgenrandom wrote: The current process ID (GetCurrentProcessID). |
|||
23 Jun 2009, 13:58 |
|
shoorick 23 Jun 2009, 14:24
DMD
what is expecting to be in ecx? i've just tried this code with "mov ecx,eax" after "GetTickCount" and "ror eax,cl" after "xor" - then got something to watch... regards! |
|||
23 Jun 2009, 14:24 |
|
DMD 24 Jun 2009, 07:56
shoorick
Hello, my friend! glad to see you Yes, you are right. I've posted only final phase of the random generator code. For Quote: an easier way to generate If somebody needs full code - let me know I'll post it. |
|||
24 Jun 2009, 07:56 |
|
shoorick 24 Jun 2009, 08:27
he-he i just was tested the code. here is the example with slight modification of DMD posted code
++++++++++++++++++++++++++++ and this variant looks even more nice to me: Code: mov ecx,eax bswap eax ; xor eax, ecx ; ror eax, cl ; sub esp, 10h ; mov dword [esp], eax ; fild dword [esp] ; fbstp tbyte [esp] ; mov eax, dword [esp] ; movzx eax,ax add esp, 10h ; of course, without realistic testing
_________________ UNICODE forever! |
|||||||||||
24 Jun 2009, 08:27 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.