News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Problems with GetTickCount

Started by jimeeg, October 18, 2006, 07:01:40 PM

Previous topic - Next topic

jimeeg

i am trying to run a simple number generator and all it kicks out is the same number every time from the loop.  i don't know whether the GetTickCount is only being accessed once or the program is too simple and the PC is running it so fast the tick count isn't changing ...  below is the code. 

rng:
    call GetTickCount     
    mov ecx, eax               
    add ecx, eax               
    add ecx, eax               
    mov edx, eax               
    add edx, ecx
    add edx, ecx
    add edx, ecx
    add edx, ecx
    shl edx, 4
    add edx, eax
    shl edx, 8
    sub edx, eax 
    add eax, edx               
    add eax, edx
    add eax, edx
    add eax, edx
    add eax, ebx                             
    print str$(eax)             
    print chr$(13,10,13,10)
   
    sub cnt, 1
    jnz rng

what i would like to know is if i need to put in a timer function such as this shown in red:

rng:
    call GetTickCount     
    mov ecx, eax               
    add ecx, eax               
    add ecx, eax               
    mov edx, eax               
    add edx, ecx
    add edx, ecx
    add edx, ecx
    add edx, ecx
    shl edx, 4
    add edx, eax
    shl edx, 8
    sub edx, eax 
    add eax, edx               
    add eax, edx
    add eax, edx
    add eax, edx
    add eax, ebx                             
    print str$(eax)             
    print chr$(13,10,13,10)
    eax, 7777
    add timer, eax
time:
    sub timer, 1
    jnz time

    sub cnt, 1
    jnz rng

this is strictly an aritificial timer, but it seemed to work other than putting out negative numbers. 

do you all have a different suggestion?
thx

Boucly

Quote
rng:
    call GetTickCount     
    mov ecx, eax               
    add ecx, eax               
    add ecx, eax               
    mov edx, eax               
    add edx, ecx
    add edx, ecx
    add edx, ecx
    add edx, ecx
    shl edx, 4
    add edx, eax
    shl edx, 8
    sub edx, eax 
    add eax, edx               
    add eax, edx
    add eax, edx
    add eax, edx                 ; what are these add's for
    add eax, ebx                   ; any chance the ebx is suppose to be edx
    print str$(eax)             
    print chr$(13,10,13,10)
    eax, 7777                  ; ??      mov?
    add timer, eax
time:
    sub timer, 1
    jnz time
    sub cnt, 1                      ; what's cnt
    jnz rng

I never tried to make my own timer before so... sorry I can't help you much... oh and you might want to set the variable timer to 0 right before the label rng if you haven't done so already.

Good luck,
Boucly

tenkey

Resolution of GetTickCount is 1 millisecond. That's only 1KHz.

How many numbers do you want per second?
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

raymond

Try the following to produce a list of different numbers. The list will be different every time you run the code.
   call GetTickCount
   or   al,1
rng:
   mov  ecx,0F120Bh
   mul  ecx
   push eax
;mov  ecx,52
;mul  ecx
;mov  eax,edx
   print str$(eax)
   print chr$(13,10,13,10)
   pop  eax
   sub cnt, 1
   jnz rng


The 0F120Bh is a large prime and is not a magical number; I just picked it at random from my prime sieve file. Any other large prime will work as well.
The result of the multiplication will exceed 32 bits in the EDX:EAX register pair but you retain only the content of the  EAX register which is the result mod 2^32.
The GetTickCount was used to get the initial seed. Other means of getting some random initial number can work as well. The or   al,1 is to guantee that the initial number is odd. The result mod 2^32 is retained for use as the next seed and would itself always be odd.

The above is an old trick to generate pseudo-random numbers which are good enough for most non-critical applications (such as shuffling a deck of cards). However, the generated numbers are not considered random enough for applications such as critical encrypting.

The 3 additional lines of code starting with the ";" would be an example for generating a pseudo-random number between 0 and 51.

Have fun

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

jimeeg

Quote from: tenkey on October 19, 2006, 12:21:44 AM
Resolution of GetTickCount is 1 millisecond. That's only 1KHz.

How many numbers do you want per second?

it is not that i want a certain amount of numbers per second, what i want is for the GetTickCount to be called on each iteration of the code without having to mess with it artificially.

so, i need to know is the code correct for grabbing the GTC every iteration but it is running so fast that the count never changes?  OR is the code wrong and it only grabs the GTC once???

thanks for the help on this

Tedd

The loop will GetTickCount each iteration - no problems there.
The value GetTickCount returns only changes once each millisecond (or 1000 time per second), however, the speed of your cpu will probably be closer to a few hundred or thousand million instructions each second. So, you'll need a few million loops before the tick-count changes.
This is the reason it keeps returning the same value.

What you should really do is use a random number generator (RNG) that works from a 'seed' value. Then, the first thing you do is set this seed value to GetTickCount, and don't mess with it again. Each time you call the RNG it will return a new random number and update the seed value itself. This also means you can call it a thousand times a millisecond and it will give different values each time (unless the 'random' values happen to match once or twice :P)
No snowflake in an avalanche feels responsible.

MichaelW

On every system that I have tested, the effective resolution of the tick count was 10ms.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    mov ebx, 200
    mov esi, rv(GetTickCount)
  @@:
    cmp esi, rv(GetTickCount)
    je  @B
    push eax
    sub eax, esi
    print ustr$(eax),9
    pop esi
    dec ebx
    jnz @B

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      11      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      11      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      10      10      10
10      10      10      10      10      10      10      11      10      10
eschew obfuscation