flat assembler
Message board for the users of flat assembler.
![]() Goto page Previous 1, 2 |
Author |
|
BreakDebugJeff 09 Nov 2004, 10:51
I am new here, but I saw another user post the RDTSC instruction in this colum, and thought that I would add my two cents:
Code: inline int randrnd(int max) { int a; _asm { rdtsc ; call read time-stamp counter instruction ; returns timer value in edx:eax cdq ; convert double word to quad word ; edx:eax <-- sign extension of EAX idiv max ; divide edx:eax by max integer ; stores result in EAX <- Quotient, EDX <- Remainder ; EDX = edx:eax % max cmp edx, 0 ; compare value to zero? jnl after ; jump if not less than to after: label neg edx ; otherwise, change sign to positive after: mov a, edx ; store random number in a } return(a); // return random number } This is an inline assembly program function that works in Visual C++.NET Let me know what you think. Jeff ![]() ![]() ![]() |
|||
![]() |
|
Matrix 09 Nov 2004, 12:24
Hy,
wel, it doesn't seem to be a random to me, however i 'd do this instead: Code: inline int randrnd(int max) { int a; _asm { rdtsc ; call read time-stamp counter instruction ; returns timer value in edx:eax cdq ; convert double word to quad word ; edx:eax <-- sign extension of EAX idiv max ; divide edx:eax by max integer ; stores result in EAX <- Quotient, EDX <- Remainder ; EDX = edx:eax % max or edx,edx ;cmp edx, 0 ; compare value to zero? jns after ;jnl after ; jump if not less than to after: label neg edx ; otherwise, change sign to positive after: mov a, edx ; store random number in a } return(a); // return random number } |
|||
![]() |
|
beppe85 09 Nov 2004, 13:05
If you need a non-negative integer, treat it as such!
Code: rdtsc xor edx, edx ; non-negative quad-word div max ; hope it work so Not tested, but I believe it does the same thing. Note that it returns a number 0 <= n < max, ie. the max is not the max, it's the number at the right of the real max! |
|||
![]() |
|
Matrix 09 Nov 2004, 16:13
btw.: here 's my latest random number generator, better then the ones i wrote recently
Code: org 256 call set320x200x256 call initrandomz mainloop: mov ecx,320 call randomz3 mov bx,ax mov ecx,200 call randomz3 mov cx,ax push ecx mov ecx,256 call randomz3 pop ecx call putpixel320x200x256 ; al=color bx=x cx=y push eax call bkeycheck cmp al,27 pop eax jnz mainloop call set80x25t int 20h initrandomz: ; modifies edx, eax rdtsc ret randomz3: ; Z3 ecx=range transparent push ebx ; number returned is: 0 <= n < ecx push edx mov ebx,eax rdtsc mov dx,cx mov cl,al xor cl,ah ror ch,4 xor bl,ch and cx,$f rol bx,cl btc bx,cx xor bx,ax ror eax,16 xor bx,ax ror eax,16 mov cl,al xor cl,ah ror ch,4 xor bl,ch and cx,$f ror bx,cl btc bx,cx mov cl,bl .minorloop: rol eax,cl and cx,$f btc ax,cx loopw .minorloop mov cl,bh .majorloop: ror eax,cl and cl,$f btc ax,cx loopw .majorloop mov cx,dx mul ecx mov eax,edx pop edx pop ebx ret putpixel320x200x256: ; al=color, bx=x, cx=y push es push $A000 pop es mov di,cx ; y shl cx,2 add di,cx ; 5y shl di,6 ; 320y add di,bx stosb pop es ret bkeycheck: ;returns: AH = BIOS scan code AL = ASCII character note: enhanced mov ah,0x11 ;Return: ZF set if no keystroke available, ZF clear if keystroke available int 0x16 ;only checks buffer without removing key ret set320x200x256: mov ax,$13 int $10 ret set80x25t: mov ax,$03 int $10 ret |
|||
![]() |
|
Bitdog 12 Nov 2004, 02:37
I tried a few random number generators, but I ended up finding one that worked by putting it in a loop (CX=5000 loops), saving & counting the random output, keeping count of how many times it returned 0, then 1, etc
0-255, then alph organize the text file output 0-255 I found that the nicest looking, intelegent, best promoted RANDOM number gen was usually the worst. They would miss numbers (lots of them) and would have one number get an unusually high number of hits compaired to other numbers. It's suspose to be an even amount of hits for each number when the generator is called repeatedly alot. Any way, since most programs that use a RAND GEN work in a loop, which calls the generator on consistant intervals, the save/count loop seemed to act much like a commonly used program. Well, it's just some thoughts anyway. Bitdog |
|||
![]() |
|
Matrix 12 Nov 2004, 03:09
Bitdog wrote: I tried a few random number generators, but I ended up finding one that worked by putting it in a loop (CX=5000 loops), saving & counting the random output, keeping count of how many times it returned 0, then 1, etc Bitdog, is it air you breathing? ![]() somtimes things are not what they seem to be. |
|||
![]() |
|
Bitdog 14 Nov 2004, 16:45
So what's wrong with checking the output of a Rand Gen ?
|
|||
![]() |
|
Matrix 15 Nov 2004, 01:37
Nothing wrong , test it excessively.
i tried to plot random xmax ymax color pixels to 1024x768 24 bit and it looked random, and no white spots or somethin' so its great for me. and it has MIPSSS!! not like storing large arrays of previus numbers and doing whit whatever matrix transformations. |
|||
![]() |
|
edfed 17 Oct 2007, 00:17
how can we use these random generators to have a simple function
it need to be really fast and random it is a background program(in timer interrupt also) and the function is called by [code] call rnd32 call rnd16 call rnd8 call rndfp and all returns a value in eax,ax,al or st0 no? one thing: MATRIX, I've tried your rnd, it is not very rnd and if ecx is too big, it crash and it is not the faster |
|||
![]() |
|
Matrix 28 Oct 2007, 21:16
sorry for late reply,
though it won't be useful either, i wanted to make a simple range setting option using ECX as maximum and 0 for minimum, if you have other ideas that are better i'm interested too. the random generated by the function is 32bit without the maximum setting at the end. if testing it with putpixel, do not set above the limits, or the putpixel will overflow and write below the video memory buffer. |
|||
![]() |
|
edfed 28 Oct 2007, 21:47
you just have to look at /projects and ideas/RANDOM
with many guys, we have discuss about simples randoms functions that are finally usefull for all X86 based systems needs some (not really need ) optimisations |
|||
![]() |
|
MCD 29 Oct 2007, 11:10
I like it. I used to use a similar one
|
|||
![]() |
|
Goto page Previous 1, 2 < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.