How to Implement a Constant Time Delay in x86 Assembly?

Click For Summary
SUMMARY

This discussion focuses on implementing a constant time delay in x86 assembly language, specifically for initializing an LCD with a required delay of 200ms. Three main methods are identified: loop cycle counting, hardware clock byte fetch, and BIOS timer interrupts. The loop cycle counting method is highlighted as high resolution but CPU-dependent, while the BIOS timer interrupt is noted for its low resolution of approximately 18.7ms. The conversation emphasizes the challenges of achieving consistent timing on a non-real-time operating system like Windows and suggests exploring real-time operating systems for better timing accuracy.

PREREQUISITES
  • Understanding of x86 assembly language programming
  • Familiarity with CPU clock cycles and timing mechanisms
  • Knowledge of BIOS timer interrupts and hardware clock byte fetch
  • Experience with real-time operating systems (RTOS)
NEXT STEPS
  • Research the implementation of BIOS timer interrupts in x86 assembly
  • Study the use of hardware clock byte fetch for timing in assembly programs
  • Explore real-time operating systems suitable for x86 architecture
  • Learn about optimizing delay loops using MOV and LOOP instructions in assembly
USEFUL FOR

Assembly language programmers, embedded systems developers, and anyone working on timing-critical applications in x86 architecture.

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
2K
  • · 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