The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: merak316 on January 20, 2007, 10:53:59 PM

Title: Help Please.
Post by: merak316 on January 20, 2007, 10:53:59 PM
I'm wondering how I can get it to press the spacebar every 3 minutes and be able to close if instructed, so far I have tried with the sleep api but its no good for it cant close if needed until the end of the sleep, is there a way of getting the space bar to be done randomly between set minutes etc. heres the code.

Title: Re: Help Please.
Post by: merak316 on January 21, 2007, 08:52:17 AM
BUMP.
Title: Re: Help Please.
Post by: rags on January 21, 2007, 04:14:50 PM
I know this is a dumb question, but what does "BUMP" mean when used as a posting?
Regards,
      Rags
Title: Re: Help Please.
Post by: TNick on January 21, 2007, 04:20:37 PM
have a look (http://www.urbandictionary.com/define.php?term=bump)   :P
Title: Re: Help Please.
Post by: merak316 on January 21, 2007, 06:39:48 PM
So noone interested then.
Title: Re: Help Please.
Post by: TNick on January 21, 2007, 06:44:58 PM
store the time in Time1
do an infinite loop
- read the state of spacebar on each loop
  IF spacebar is down, read the time in Time1
  ELSE read the time in Time2
    IF Time2-Time1 >=3 minutes exit


Quote from: merak316 on January 21, 2007, 06:39:48 PM
So noone interested then.

No, not really ...  :green2 :green2 :green2 :green2
Nick
Title: Re: Help Please.
Post by: merak316 on January 21, 2007, 08:33:35 PM
ok ive done that works great but how could i randomise the timer instead of a set so many milleseconds?
Title: Re: Help Please.
Post by: TNick on January 21, 2007, 08:46:45 PM
:) When I said that you should get the time I was thinking about GetSystemTimeAsFileTime or GetSystemTime. But why do you need to randomize the timer? What are you trying to do?

Nick
Title: Re: Help Please.
Post by: merak316 on January 21, 2007, 09:02:01 PM
Trying to get it to simulate spacebar being pressed, also at random intervals between a set time period for eg 1-3 minutes.


Title: Re: Help Please.
Post by: MichaelW on January 21, 2007, 09:56:39 PM
One method of getting a random interval between 1000 and 3000 ms would be to get a random number in the interval 0 to 2000 by calling the nrandom function specifying a base of 2001, then add 1000 to the return value and use the result for the next timer interval.

You should be able to reduce the CPU utilization, without any significant delays in the polling loop, by inserting a "invoke Sleep,0" in the loop.
Title: Re: Help Please.
Post by: merak316 on January 21, 2007, 10:03:37 PM
I'll give that a go invoke sleep,1 seemed to do it thanks, and Ill try that other suggestion as well cheers.
Title: Re: Help Please.
Post by: merak316 on January 21, 2007, 10:17:37 PM
How would i go about using nrandom ive tried some variations can't seem to get it ticking.
Title: Re: Help Please.
Post by: MichaelW on January 21, 2007, 10:30:28 PM
Something like this:

    .IF uMsg == WM_TIMER
      ...
      invoke nrandom,2001
      add eax, 1000
      invoke SetTimer,hWin,1,eax,NULL
    .ENDIF

Title: Re: Help Please.
Post by: merak316 on January 21, 2007, 10:41:08 PM
   @gogo:
   cmp         time,1000
   je          @event11
   invoke      Sleep,1000
   mov eax,    dword ptr [time]
   invoke      nrandom,2000
   add eax,    1000
   mov         dword ptr [time],eax
   cmp         [uMsg],WM_TIMER
        invoke      GetAsyncKeyState,VK_F11
   cmp         eax,0
   jz          @3
   mov         time,0   
        jmp         @1

not working.
Title: Re: Help Please.
Post by: MichaelW on January 22, 2007, 12:20:07 AM
You need to call SetTimer from the WM_TIMER message handler to set the new interval (time-out value). In my example I was assuming that you are using a single timer with the identifier 1. When SetTimer is called with nIDEvent set to the identifier of an existing timer, the timer is replaced with a new one that has the time-out value specified in the call.

MSDN: SetTimer (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/timers/timerreference/timerfunctions/settimer.asp)

It would be easier for us to help you if you would post all relevant parts of your code, and place the code between code tags (assuming you have scripting enabled, you can click the "#" button above the edit window to insert a pair of tags).
Title: Re: Help Please.
Post by: merak316 on January 22, 2007, 12:31:48 AM
edit
Title: Re: Help Please.
Post by: MichaelW on January 22, 2007, 02:39:52 AM
You are not doing what I thought you were doing. Without a window, message loop, and window procedure you cannot receive WM_TIMER messages, unless you set up a specific callback function to handle the messages (TimerProc in the documentation). This is doable, but it would probably be easier to implement something like this pseudo-code:

OuterLoop:
  tickCount = GetTickCount
  Generate random number between 1000 and 3000
  terminalCount = tickCount + random number
InnerLoop:
  ...
  poll keyboard, etc
  ...
  tickCount = GetTickCount
  if tickCount > terminalCount then
    simulate press of spacebar
    goto OuterLoop
  end if 
  goto InnerLoop
Title: Re: Help Please.
Post by: merak316 on January 22, 2007, 04:00:54 AM
edit
Title: Re: Help Please.
Post by: sinsi on January 22, 2007, 05:32:44 AM
merak316, what exactly are you trying to do with your code? Right now, it seems to go nowhere - if you tell us
what your goal is, we could help you more (I think) e.g. why do you need the space bar pressed?
Title: Re: Help Please.
Post by: merak316 on January 22, 2007, 06:09:13 AM
edit
Title: Re: Help Please.
Post by: MichaelW on January 22, 2007, 07:52:22 AM
You are getting descriptions and pseudo-code instead of code because this looks like homework. You can generate the random number as I described, by calling nrandom. To understand why the timer is not working, and cannot work with your code, try the MSDN link that I provided.

If instead of:

.486
.model flat, stdcall
option casemap :none

include      C:\masm32\include\windows.inc
include      C:\masm32\include\kernel32.inc
include      C:\masm32\include\user32.inc
includelib   C:\masm32\lib\kernel32.lib
includelib   C:\masm32\lib\user32.lib

You use just:


include \masm32\include\masm32rt.inc


Then you will be able to use the MASM32 macros and functions to display the value of registers, variables, etc. This will allow you to monitor what the code is actually doing, instead of having to guess.
Title: Re: Help Please.
Post by: merak316 on January 22, 2007, 08:42:36 PM
edit
Title: Re: Help Please.
Post by: mnemonic on January 22, 2007, 08:59:50 PM
Quote from: merak316 on January 22, 2007, 08:42:36 PM
Still nothin.
Bad error description.
Title: Re: Help Please.
Post by: merak316 on January 22, 2007, 09:50:31 PM
edit