How to Implement a Constant Time Delay in x86 Assembly?

Click For Summary

Discussion Overview

The discussion revolves around methods to implement a constant time delay in x86 assembly language, focusing on various approaches suitable for different operating systems and hardware configurations. Participants explore theoretical and practical aspects of timing mechanisms in assembly programming.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant seeks assistance in creating a constant time delay in x86 assembly.
  • Another participant suggests at least three methods for achieving this: loop cycle count, hardware clock byte fetch, and BIOS timer interrupt, noting that the choice depends on the specific requirements.
  • A participant indicates that hardware clock byte fetch and BIOS timer interrupt would be suitable for a 200ms delay needed for initializing an LCD.
  • There is a request for links or resources on how to implement these methods.
  • One participant mentions that using hardware timers and interrupts should be documented in the x86 programmer's manual and relevant chip datasheets.
  • A participant points out that achieving consistent timing on a PC running a non-real-time OS like Windows is challenging due to task scheduling and interrupts causing jitter.
  • Another participant suggests that using DOS might reduce jitter, but acknowledges that system interrupts could still affect timing.
  • There is a mention of real-time operating systems as a potential solution for consistent timing, though some participants express limited familiarity with them.
  • A participant provides a detailed example of a delay loop using MOV and LOOP instructions, including cycle counts for different scenarios on a Pentium processor.

Areas of Agreement / Disagreement

Participants express differing views on the feasibility of achieving consistent timing on various operating systems, with some advocating for DOS or real-time operating systems while others highlight the challenges posed by non-real-time environments like Windows. No consensus is reached on the best approach.

Contextual Notes

Participants note potential limitations related to timing accuracy due to system interrupts and the dependency on the operating system used. The discussion includes assumptions about the execution environment and the specific hardware being utilized.

Who May Find This Useful

This discussion may be useful for programmers working with x86 assembly language, particularly those interested in timing mechanisms for embedded systems or hardware initialization tasks.

abdo375
Messages
131
Reaction score
0
I need to write a program that will create a constant time delay in x86 assembly, can anybody help?
 
Engineering news on Phys.org
There are at least 3 ways to do this depending on just what you are trying to do.
Loop cycle count - high resolution but cpu dependent.

Or not cpu dependent.
Hardware clock byte fetch - with or without interrupt overlay about 100th second.

Bios timer interrupt - low resolution 18.7ms IIRC.

Any ideas on which one is more suitable?
 
Hardware clock byte fetch and bios timer interrupt would be perfect, I'm trying to write an assembly code that would initialize an LCD so I need a delay of 200ms
 
btw is there a link on how to use any of them.
 
Using the hardware timers and interrupts should be described in your x86 programmer's manual and the datasheet for the chip. Is it not there?
 
actually I'm doing it on a PC.
 
abdo375 said:
actually I'm doing it on a PC.

Oh, that's different. You're not going to get consistent timing on a PC running a non-real-time OS like Windows. The jitter is aweful due to the scheduling of tasks and interrupts from all over the place. If you want consistent timing with a PC, you'll need to make external hardware that makes the real-time waveform and timings, and then just do overall control and monitoring from the PC's jittery responses.

You can get real-time operating systems that you can run on the PC, and they'll probably have plenty of documentation on how to get consistent timings and execution.

You sure you want to do it on a Windows PC?
 
berkeman, 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.

will it work on a non-multi-tasking operating system like DOS for example ?
 
DOS is a good idea, but I think there are still interrupts from system functions (like keyboard and mice, etc.) that cause jitter. There are real-time OS available, but sorry, I'm not that familiar with them. I use uCs mostly, where I control the whole shebang.

I checked wikipedia.org, and they have a pretty good entry on real-time OS:

http://en.wikipedia.org/wiki/Real_time_operating_system
 
  • #10
thanks, berkeman.
 
  • #11
abdo375 said:
I need to write a program that will create a constant time delay in x86 assembly, can anybody help?

Typical Pentium software delay loops can be written using MOV and LOOP instructions.
For example, the following instruction sequence can be used for a delay loop: MOV CX,count DELAY: LOOP DELAY
The initial loop counter value of “count” can be calculated using the cycles required to execute the following Pentium instructions: MOV reg/imm (1 cycle) LOOP label (5/6 cycles)
Note that the Pentium LOOP instruction requires two different execution times. LOOP requires six cycles when the Pentium branches if the CX is not equal to zero after autodecrementing CX by 1. However, the Pentium goes to the next instruction and does not branch when CX = 0 after autodecrementing CX by 1, and this requires five cycles.
This means that the DELAY loop will require six cycles for (count - 1) times, and the last iteration will take five cycles.
For a 100-MHz Pentium clock, each cycle is 10 ns. For 2 ms, total cycles =2ms/10ns= 200,000. The loop will require six cycles for (count - 1) times when CX + 0, and five cycles will be required when no branch is taken (CX = 0). Thus, total cycles including the MOV = 1 + 6 x (count - 1) + 5 = 200,000. Hence, count = 33,333,0. Therefore, CX must be loaded with 33,33310
 

Similar threads

  • · Replies 102 ·
4
Replies
102
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
7
Views
3K