Announcement

Collapse
No announcement yet.

Sleep (Api/PB/Asm)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sleep (Api/PB/Asm)

    I understand the following:
    In API Sleep is
    Code:
    VOID Sleep(
    
        DWORD dwMilliseconds 	// sleep time in milliseconds 
       );
    In PB Default
    Code:
    SLEEP 5000
    But in assembly what would be the equivalent of "Sleep" ?????
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    One way is to use the rdtsc timestamp counter instruction in a loop.

    The Sleep API function calls SleepEx, which calls ZwDelayExecution - the service call to KeDelayExecutionThread.
    -

    Comment


    • #3
      This is right out of the help file under Intermixing ASM and BASIC code

      Code:
       
      SUB(delayedBeep)
       
      ' some asm here
       
      ! PUSH EAX ' Save the scratch registers
      ! PUSH ECX
      ! PUSH EDX
       
      SLEEP 5000
       
      ! POP EDX ' Restore the scratch registers
      ! POP ECX
      ! POP EAX
       
      'more asm here
       
      beep
       
      End Sub

      This is the easist way to call an api in the middle of asm.
      Last edited by Michael Mayerhoffer; 16 Aug 2008, 08:05 PM.
      A dozen what.

      Comment


      • #4
        Thats a good hint. Because why should you want speed up the Sleep comand to waste less time )?

        Comment


        • #5
          I think hurry up and wait comes to mind
          A dozen what.

          Comment


          • #6
            In API Sleep is
            [Code
            VOID Sleep( ...
            [/code]

            But in assembly what would be the equivalent of "Sleep" ?????
            Unbelievable. An "assembly language" question I can actually answer!

            Code:
            ASM  CALL  <options and proc>
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Example for the Manly answer.


              Code:
               
              #COMPILE EXE
              #DIM ALL
               $INCLUDE  "WIN32API.INC"
               
              FUNCTION PBMAIN () AS LONG
               
                DIM szDllName  AS ASCIIZ * 255    ' DLL
                 DIM szAPIName AS ASCIIZ * 30    ' api name
                 DIM lpAdd  AS DWORD             ' function addy
                 DIM dMilsec AS DWORD          'pass millseonds
                 DIM hRet AS LONG                'handle to loaded lib
               
                  szDllName  = "KERNEL32.DLL"
                  szAPIname = "Sleep"
                  dMilsec = 5000
                  hRet = LoadLibrary(szDllName)
               
                   lpAdd  = GetProcAddress( hRet, szAPIName )  'retuns addy of function
               
                    ! push dMilsec  ; push 1st parameter on the stack
                    ! call lpAdd       ; call the function
               
                   FreeLibrary hRet
               
                      BEEP
               
              END FUNCTION
              A dozen what.

              Comment


              • #8
                Interesting......
                It seems that all replies involve calling the Windows API

                Is that because I can no longer use Interrupts directly like the days of ole' ????
                Or if 'Sleep' even involves an Interrupt at all? (Been too long to remember which is which)

                Although I do like MCM's display of things cause it comes the closest to a pure ASM example.
                Engineer's Motto: If it aint broke take it apart and fix it

                "If at 1st you don't succeed... call it version 1.0"

                "Half of Programming is coding"....."The other 90% is DEBUGGING"

                "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                Comment


                • #9
                  Cliff,

                  the example for the Manly answer, is how you have to set things up to do it as MR MM suggested.

                  You want to use sleep or any other api call, that is how you would do it first or second example

                  Sleep stops the execution of your program at a predetermined point in the program and for a given time. Lets the operating system or other programs take advantage of you program sleeping if you will.

                  Interrupt is a whole different animal and in short. An interrupt is used to forces your program to stop what it is doing at any given point and let some other procedure do its thing until it returns control. When that procedure is done, it returns control to the program at the point of interruption. The interrupt can be a hardware and or a software event. The program being interrupted has to have a way to receive the interrupt, then clean up what it is doing to allow the interrupt to do its thing. Then be ready to start running again when interrupt procedure is finished and returns control back.

                  Hope that helps.

                  A dozen what.

                  Comment


                  • #10
                    right question - wrong answers ?

                    Originally posted by Cliff Nichols View Post
                    I understand the following:
                    In API Sleep is
                    Code:
                    VOID Sleep(
                    
                        DWORD dwMilliseconds     // sleep time in milliseconds 
                       );
                    In PB Default
                    Code:
                    SLEEP 5000
                    But in assembly what would be the equivalent of "Sleep" ?????
                    Perhaps I am the one that missed this, but I understood Cliff's question to be: how do you implement the equivalent of sleep using JUST assembly languague? ... No API calls ... No High Level code. How would you simulate "sleep" with machine instructions only? Now ... having said that, I have no idea personally, but I would like to see the answer too.

                    Unfortunatly the last time I played with assembly was VAX 11/780 Macro Assembler in about 1981.
                    Last edited by Mark Farrar; 19 Aug 2008, 12:17 PM.

                    Comment


                    • #11
                      ... sleep using JUST assembly languague? ... No API calls ... No High Level code
                      AFAIK, that's not possible.

                      SLEEP (PB) and Sleep[Ex](Windows) do more than delay. They immediately cause the remainder of the thread's timeslice to be returned to Windows, and I have to believe you have to tell Windows about that, which means you have to ... call some API function.

                      MCM
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        yea but

                        yea but .... ok ... and I am laughing here

                        Ultimately it's all just binary anyway ... from a for/next all the way to API calls.

                        In theory you could code ANYTHING in machine code (or assembler) ... output of a compiler is just 10101000111001 anyway...

                        ok ok ... yes ... I know this would be Very NOT practical and it is probably not in the spirit of the original question ... but it is not "impossible" ... just a wildly insane way of doing it.

                        No offense intended Michael ... I'm just being literal for the sake of argument.

                        regards,
                        Mark
                        Last edited by Mark Farrar; 19 Aug 2008, 01:26 PM.

                        Comment


                        • #13
                          Mark,
                          MCM is right.

                          The timeslice belongs to the operating system. The OS allows your code to use the time slice and it can take it from your code when it chooses. If your code doesn't want it then it only has one option, that is to give it back to the OS for the OS to reallocate. That involves a call to the OS.

                          Paul.

                          Comment


                          • #14
                            pure theory ONLY

                            Paul...

                            I understand completely what you are saying and agree with you.

                            My point, and it was an abstract philosophical point, was that in the end it is all just machine instructions. That includes operating systems ... sooner or later it all boils down to push, pop, jump etc etc ... filling registers and allocating memory.

                            so ... IN THEORY ... and I emphasize that this is an intellectual exercise only ... you could write code in assembler that bypasses everything including the OS to do just about anything.

                            pure computer science here ... this has nothing to do with practical or applied programing ...

                            regards,
                            Mark

                            Comment


                            • #15
                              Mark,
                              you could write code in assembler that bypasses everything including the OS to do just about anything.
                              No, you can't write such code.
                              Your program is running within the OS, the OS is in control, it decides. There is nothing you can do to take control from the OS.
                              Even if you write a whole OS within your code it would still not have the information that the real OS has access to and would then not be able to decide where to return the timeslice to if you tried to write your own SLEEP code. Your code would not even be aware of any other application that might want the timeslice.


                              Paul.

                              Comment


                              • #16
                                > ...I emphasize that this is an intellectual exercise....

                                Fooled me.
                                Michael Mattias
                                Tal Systems (retired)
                                Port Washington WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment


                                • #17
                                  wow

                                  Originally posted by Michael Mattias View Post
                                  > ...I emphasize that this is an intellectual exercise....

                                  Fooled me.

                                  wow ... "fooled me"

                                  So much for an open exchange of ideas and free thinking in a non retributional environment that encourages productive debate.

                                  ok gentlemen ... you win ... I withdraw my comments. I don't think of the PB forum as a place for judgement and recrimination ... winning this argument is just not that important to me.

                                  Comment


                                  • #18
                                    Today's Lexigraphical Fact
                                    Sometimes sarcasm is mistaken for the real thing.

                                    Today's Science Fact
                                    Sometimes skin is thick, other times not so.

                                    Today's 'How to Use the Forums' Related Lifestyle Suggestion
                                    Take nothing literally or personally.
                                    Michael Mattias
                                    Tal Systems (retired)
                                    Port Washington WI USA
                                    [email protected]
                                    http://www.talsystems.com

                                    Comment


                                    • #19
                                      Mark, Paul, and MCM... Thank you all for Cementing what I 1st thought.

                                      Mark for understanding what I wondered "Theoretically may exist" but did not think it did because the OS. (Eloquently put by the way how it all boils down to binary)

                                      Paul for explaining that in my particular case, it really boils down to the OS, and what it understands as a "Sleep" would mean, and handle it appropriately.

                                      MCM for showing me probably the best way of Handling the interoperability of the 2 concepts.

                                      Why use Sleep in the 1st place???? (Well we all know its to allow something else to happen while tightly looping on a purpose, but so as to not use ALLLLLLL your time looking for something that may not occur immediately)


                                      Since we are all mostly in the same part of the yard (maybe on different angles of view of the fence) looking at the same Knothole I have to ask (and this may be a bad way of phrasing things, but its the best I can figure how to ask)

                                      Lets say I have the following situation:
                                      1. I am looping on a reply or some result I want (tightly looped)
                                      2. My result occurs during my loop, but still within my "Time-Slice"
                                      3. I can't see the result, because I still own my "Time-Slice"
                                      4. Lets say my "Time Slice" = 10ms but my loop only takes 20 nano-seconds, and the OS full "Time Slice is 1ms (so I waste my time waiting the other 10ms minus the 20 nano-seconds waiting for my slice to be done timing out


                                      Like I said, the above may be a bad example (but if you have trouble with time, then make Nano-Seconds = Seconds and 1ms = 1 Minute, if it puts it on a better scale)

                                      If I read things right, I could just use "GetMessage" and "TranslateMessage", but is this equivalent to "Sleep"??? or is it more like "Ok...let others happen, but I still own my "TimeSlice" till its taken away from me?", Or is it more better to sleep???, or am I just splitting hairs on what may be apples and Oranges, and I just don't see it???"

                                      I know I know....I jumped from a simple question to "Solve PI to the 42nd decimal point" kinda question, (and probably lost everyone), but I had to ask just in case you actually get what I mean and can possibly offer input
                                      Engineer's Motto: If it aint broke take it apart and fix it

                                      "If at 1st you don't succeed... call it version 1.0"

                                      "Half of Programming is coding"....."The other 90% is DEBUGGING"

                                      "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                                      Comment


                                      • #20
                                        >Why use Sleep in the 1st place????

                                        Good question.

                                        Usually what we see here is SLEEP used as a laz.., er, poor man's substitute for synchronization objects , WaitForSingleObject or WaitForMultipleObjects, and/or a second (or third, or fourth) thread of execution.

                                        As far as 'time slice' management... I wouldn't worry about that. Windows is a pre-emptive multi-tasking environment (vs Win/3, which features cooperative multitasking) and as a rule you can do nothing to change the timeslice assignments (there is one exception I can think of but I'm not going to mention it because I can already see the abuse).

                                        MCM
                                        Michael Mattias
                                        Tal Systems (retired)
                                        Port Washington WI USA
                                        [email protected]
                                        http://www.talsystems.com

                                        Comment

                                        Working...
                                        X