Assembly language time delay

Discussion in 'Programmer's Corner' started by Abdo375, Jan 7, 2007.

  1. Abdo375

    Thread Starter New Member

    Jan 7, 2007
    7
    0
    I need to write a program that will create a constant time delay in x86 assembly, can anybody help?
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Hi,

    You should be able to get an idea of each instruction's execution time. The usual way to do a delay is to run a loop in which a variable increments (or decrements) to some terminal value. Since the loop is invariant, the time it takes to execute is predictable. You can also nest loops for much longer timing periods. Get the first loop tweaked to a useful interval, and just go from there.
     
  3. Abdo375

    Thread Starter New Member

    Jan 7, 2007
    7
    0
    yes, but this will make it CPU dependent, I was hoping to run it on any computer with the same results.
     
  4. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Hi,

    Since it's x86 code, how can it not be CPU-dependent? Assembler runs at machine speed. It's hard to imagine any block of code not running faster on a 3 GHz machine than on an old P2 450 MHz.

    I'm sure that games have some means of "padding" run time so game play is pretty even across some large range of CPU clocks. Perhaps that might be an area to look into for CPU-independent timing loops.
     
  5. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Have a look at system tick call. If the resolution satisfy your requirement then you probably could make it work easier than using loops.
     
  6. Abdo375

    Thread Starter New Member

    Jan 7, 2007
    7
    0
    beenthere, there seems to be a way but it's not working for me, the 5th bit of the 61h port toggles every 15us, which can be used for CPU-clock dependent time delay but like I said it's not working for me.
     
  7. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Hi,

    Can you be more specific about how it's not working for you? Are you not able to catch the bit toggling, or is the interval not good to work with?
     
  8. Abdo375

    Thread Starter New Member

    Jan 7, 2007
    7
    0
    actually, the problem is for some reason the interval is not accurate.
     
  9. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    What OS are you working with? And what is your intended resolution?

    As far as I know, for XT or AT port 61h is the PPI control interface. I have never heard any timer of any kind at bit 5 of port 61.

    As I mentioned above, your best bet is to use BIOS tick call (int 8) or the derived int 1C or read the tick count directly. These will give you 55ms resolution. If you need something finer, then you have to go directly to the timer chip (probably around 0.8us, I can't remember the exact value).

    Any other methods are just a PITA :D
     
  10. Abdo375

    Thread Starter New Member

    Jan 7, 2007
    7
    0
    it worked, although not totally processor speed independent.

    n9352527, XT I don't know (I'll look it up), but starting from AT and up to pentium, the 4th bit (I correct my self) of the 61h port is toggled every 15.085us.
     
  11. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    I've had a look at my notes, and that is an old IBM PC standard. The last implementation I could dig up was for an old 286 IBM.

    Make your life easy and go with other standard and proven methods like the ones I mentioned above.
     
  12. Abdo375

    Thread Starter New Member

    Jan 7, 2007
    7
    0
    Already did.
    thanks for your help.
     
Loading...