Shelly
Shelly

Reputation: 23

Delay program using int 21h with ah = 2Ch

I'm working on a project for school in assembly 8086 (using DOSBox), and I was trying to delay my program for 0.5 second.

I tried to create a loop which compares the current time to the initial time, using int 21h, function 2Ch with the hundredths value in DL, but it seems to be too slow...

mov ah, 2Ch
int 21h
mov al, dl ;hundredths
mov bx, 0
wait_loop:
    one_hun:
        int 21h
        cmp al, dl
        je one_hun
    mov al, dl
    inc bx
    cmp bx, 50
    jne wait_loop

Upvotes: 2

Views: 1123

Answers (1)

Ped7g
Ped7g

Reputation: 16626

I would rather use BIOS int 1Ah, which is used as source for that int 21h service any way, and this may be a bit simpler to use.

But beware by default (unless you plan to reprogram the timer chip) this is ticking 18.2 times per second, so to wait for half a second you can wait either 9 (cca. 440 to 494.51ms) or 10 (cca. 495 to 549.45ms) ticks, the precision will be limited to the default +-~50ms).

If you will reprogram the timer chips, you may get a bit higher precision, but don't expect something like [ten] thousands of second to work perfectly reliably under DOS (probably emulated under modern OS).


About your current code: the hundredths in the dl are not incrementing by one, so you are counting in the bx the number of those 18.2Hz ticks, not the hundredths (i.e. your code waits for ~2.7s, right?).

Also don't do je in similar code, always make the condition <= or >=, because if for whatever reason (OS didn't run your code for a while) miss that exact 50 hundredths difference, you will create loop which will run almost infinitely (until it will hit that exact 50 by accident after one of the overflows).

To do it your way, you have to calculate the delta:

    mov ah, 2Ch
    int 21h
    mov al, dl ;hundredths
wait_loop:
        nop        ; burn the CPU a bit less
        int 21h
        sub dl,al  ; calculate delta of hundredths (-99..+99)
        jnc delta_positive
        add dl,100 ; adjust the delta to be positive 1-99
delta_positive:
        cmp dl,50
        jb wait_loop

Upvotes: 5

Related Questions