I got this delay routine while searching the internet for ways to introduce a time delay (4-5 sec) in my assembly program. I fed it into a HelloWorld program to display 'Helloworld' box message after 10 sec from the execution of the program.

Here's the code

.386
.model flat, stdcall
option casemap :none

extrn MessageBoxA : PROC
extrn ExitProcess : PROC

.data
        HelloWorld db "Hello There!", 0
; Delay routine

FullVertWait:
        mov   dx,3dah
vr:
        in    al,dx
        test  al,8
        jnz   vr                                 ; wait until Vertical Retrace starts
nvr:
        in    al,dx
        test  al,8
        jz    nvr                                ; wait until Vertical Retrace Ends
    add word[count1],1
    add eax,0
        ret
Delay:
       mov   word[count1],0
ddf:
       call  FullVertWait
       cmp   word [count1],700                   ; 70 = 1 second,so this = 10 seconds
       jne   ddf
       ret

count1    dw  0

; End of delay

.code
start:
    call Delay
        lea eax, HelloWorld
        mov ebx, 0
        push ebx
        push eax
        push eax
        push ebx
        call MessageBoxA
        push ebx
        call ExitProcess

end start

However, this program crashes on execution. Can anyone help me out. Or is there any other simpler way by which i could introduce the delay?
(Using TASM to compile the code)

Edited 4 Years Ago by st_infamous

2 things:
1. All of this code:

FullVertWait:
        mov   dx,3dah
vr:
        in    al,dx
        test  al,8
        jnz   vr                                 ; wait until Vertical Retrace starts
nvr:
        in    al,dx
        test  al,8
        jz    nvr                                ; wait until Vertical Retrace Ends
    add word[count1],1
    add eax,0
        ret
Delay:
       mov   word[count1],0
ddf:
       call  FullVertWait
       cmp   word [count1],700                   ; 70 = 1 second,so this = 10 seconds
       jne   ddf
       ret

HAS TO BE in your code section NOT the data section!!!!
2. Use the Sleep or SleepEx API

also, you cannot use privilaged instructions in user mode. IN is a privilaged instruction.

Edited 4 Years Ago by GunnerInc

This article has been dead for over six months. Start a new discussion instead.