News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Self-calibrating 100-microsecond delay loop

Started by MichaelW, May 23, 2005, 06:34:50 AM

Previous topic - Next topic

MichaelW

This is a test piece for a self-calibrating 100-microsecond delay loop that does not depend on any special timers or processor features. It's a component I need for a project that must access the ATA/ATAPI Command/Control Block registers through I/O ports.

One detail that I cannot test is the maximum loop count for a fast processor (I currently have nothing faster than a P3-500). I would appreciate it if someone would run this on a reasonably fast processor and post the results.

The source requires MASM32 v8.2 SP2.


; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .486                       ; create 32 bit code
    .model flat, stdcall       ; 32 bit memory model
    option casemap :none       ; case sensitive

    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\kernel32.inc
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\kernel32.lib
    include \masm32\macros\macros.asm
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    call  delay                 ; calibrate
    REPEAT 100
        invoke GetTickCount
        mov   esi, eax
        mov   ecx, 1000
        .REPEAT
            call  delay
        .UNTILCXZ
        invoke GetTickCount
        sub   eax, esi
        print ustr$(eax),13,10
    ENDM

    mov   eax, input(13,10,"Press enter to exit...")
    exit
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; This proc self-calibrates on the first call and thereafter
; delays for ~100 microseconds before returning.
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
;
delay proc uses eax ebx ecx edx edi esi

    GLOBAL delayLoops dd 0

    .IF delayLoops
        mov   ecx,delayLoops
        call  delayloop
    .ELSE
        ; ----------------------------------------------
        ; Determine the loop count for a 100 ms delay.
        ; ----------------------------------------------
        mov   ebx, 1000         ; load initial loop count
        mov   edi, 80000h       ; load initial increment
        .REPEAT
            .REPEAT
                ; ------------------------------------------
                ; Sync with the timer to improve accuracy.
                ; ------------------------------------------
                invoke GetTickCount
                mov   esi,eax
                .REPEAT
                    invoke GetTickCount
                .UNTIL eax > esi

                add   ebx, edi  ; add increment to count

                print "+"

                invoke GetTickCount
                mov   esi, eax
                add   esi, 97   ; reduced to allow for variation

                mov   ecx, ebx
                call  delayloop

                invoke GetTickCount
            .UNTIL eax > esi    ; until delay is > 97 ms

            print ustr$(ebx),13,10
            print uhex$(edi),13,10

            sub   ebx, edi      ; subtract increment from count
            sub   ebx, 100
            shr   edi, 4        ; decrease increment
        .UNTIL edi < 80h        ; until increment < 80h
       
        print ustr$(ebx),13,10

        ; ----------------------------------------------------
        ; Adjust the loop count for a 100 microsecond delay.
        ; ----------------------------------------------------
        xor   edx, edx
        mov   eax, ebx
        mov   ebx, 1000         ; <== modify to adjust delay
        div   ebx
        mov   delayLoops, eax

        print ustr$(eax),13,10,13,10

    .ENDIF
    ret
   
delay endp

delayloop proc uses edi
    .REPEAT                 ; run delay loop
        REPEAT 3
            xor   edx, edx
            xor   eax, eax
            mov   edi, 1
            div   edi
        ENDM
    .UNTILCXZ
    ret
delayloop endp

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start




[attachment deleted by admin]
eschew obfuscation

hutch--

Michael,

These are the results I get on a PIV 2.8 gig Prescott.


+++++++3671016
00080000
++++++++++++++++3670916
00008000
+3640096
00000800
++3638204
00000080
3637976
3637

109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
125
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
125
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
125
109
110
109
110
109

Press enter to exit...
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MichaelW

Thanks Hutch,

That tells me what I needed to know. The processor would need to be ~1170 times faster than yours before the procedure could overflow EBX, so unless there is some sort of major breakthrough, this technique should continue to work far longer than anyone will be using it. Compared to my 5-year old P3-500, the maximum loop count for your system is 7 times greater, and the final loop count 9.3 times greater.
eschew obfuscation

lingo

 :thumbu
P4 Prescott 3.6 GHz
overclocked at 4.22 GHz



+++++++++4719592
00080000
+++4293508
00008000
+4262688
00000800
+4260668
00000080
4260440
4260

109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
141
109
109
110
109
110
109
109
125
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
125
109
110
109
141
109
109
110
109
109
110
109
110
109

Press enter to exit...

Regards,
Lingo

hitchhikr

AMD 1.8ghz.

+++1573864
00080000
+++++++++++++++++1606532
00008000
++1577760
00000800
+++++++++1576764
00000080
1576536
1576

110
109
109
94
109
94
93
110
125
109
109
110
94
94
109
94
109
125
109
94
94
94
93
110
109
109
110
125
94
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109

use QueryPerformanceFrequency() / QueryPerformanceCounter() instead of GetTickCount() if available, it's much more stable.

doomsday

Very nice! :)

regards,
- Brent

2.21 Gig Athlon 64 3500+

++++2098152
00080000
+++++++++1868676
00008000
+++1841952
00000800
+++1840188
00000080
1839960
1839

110
94
125
93
110
125
109
94
109
125
94
125
109
110
109
109
110
94
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
94
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
109
110
109
94
109
110
109
109
110
109
109
110
109
110
109
109
110
109
109
110
109
110
93
110
109
109
110
109
110
109
109
110
109
109
110
109
110
109
94
125
109
94

Press enter to exit...

pbrennick

AMD (Athlon) 1.00 Ghz

Quote
++1049576
00080000
+++++++++820100
00008000
+++++++++++++813856
00000800
++++812220
00000080
811992
811

100
100
100
130
101
90
100
100
100
100
100
101
100
100
100
100
100
100
101
100
100
130
100
100
101
100
100
100
100
100
100
101
100
100
100
100
100
100
101
100
100
100
100
100
100
101
100
100
100
100
100
100
101
100
100
100
100
100
100
101
100
100
100
100
100
100
101
100
100
100
100
100
100
101
100
100
110
100
100
100
101
100
100
100
100
100
100
101
100
100
100
100
100
100
101
100
100
100
100
100

Press enter to exit...

Paul


MichaelW

Thanks for the testing, guys. I really need to move up to a system that was actually built in this century :green

eschew obfuscation

hutch--

 :bg

Why,

If my old timer had not worn out, I would still use them. The only old box I have left is a PIII with the noisiest pair of scsi's on the planet so when you turn it on, it sounds like an engineering machine shop.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php