How to set 1 second time delay at assembly languag

2019-01-15 09:41发布

问题:

My problem is that I have written a code that is supposed to output a result into a set of LEDs connected to the parallel port. When I ran the code it pretty much did nothing. My instructor told me that the code ran too fast that my eyes did not see what happened.

I have found that there are a couple of ways to do a time delay, I have tried to loop the NOP but I think I cannot really determine what is going on. Is there any better way?

I have here a part of the code where I have to add a time delay into:

org 100h

mov ax, 0
mov dx, 378
out dx, ax
mov ax, 1  

; 1st

mov cx, 1ah
start1st:
mov ax, 1
left:
out dx, ax 
; --------------------------------> how to loop?
mov bx, 2
mul bx
cmp ax, 80h
jl left
dec cx
cmp cx,0
jg start1st
; end 1st 

回答1:

Set 1 million microseconds interval (1 second) By using below instruction .

MOV     CX, 0FH
MOV     DX, 4240H
MOV     AH, 86H
INT     15H

You can set multiple second delay by using 86H and INT 15H

check these links for more details

Waits a specified number of microseconds before returning control to the caller

INT 15H 86H: Wait



回答2:

You can use interrupt 1Ah / function 00h (GET SYSTEM TIME) to get the number of clock ticks (18.2/s) since midnight in CX:DX.

So to wait approximately 1 second using this method you'd execute this interrupt function once, save CX:DX in a variable, then execute the same interrupt in a loop until the absolute value of CX:DX - firstCX:DX is greater than 18.



回答3:

What i finally ended up using was the nop loop

; start delay

mov bp, 43690
mov si, 43690
delay2:
dec bp
nop
jnz delay2
dec si
cmp si,0    
jnz delay2
; end delay

I used two registers which I set them both to any high value and its gonna keep on looping until both values go to zero

What I used here was AAAA for both SI and BP, i ended up with roughly 1 second for each delay loop.

Thanks for the help guys, and yes, we still use MS DOS for this assembly language course :(



回答4:

Alternatively, you can create a process and call it every time you want to delay using only the counter register and stack implementation.

Example below delays roughly 1/4 a sec.

delay       proc
            mov     cx, 003H
    delRep: push    cx
            mov     cx, 0D090H
    delDec: dec     cx
            jnz     delDec
            pop     cx
            dec     cx
            jnz     delRep
            ret
delay       endp


回答5:

DELAY_1SEC: MOV R3,#0AH;10D
LOOP1:      MOV R2,#64H;100D
LOOP2:      MOV R1,#0FAH;250D
LOOP3:      NOP
            NOP
            DJNZ R1,LOOP3;4x250Dx1,085us=1,085ms (0s.001ms010)/cycle
            DJNZ R2,LOOP2;3x100Dx1,085ms=325,5ms (0s.100ms309)/cycle
            DJNZ R3,LOOP1;3x10Dx325,5us=976,5ms (1s.604ms856)/cycle
            RET


回答6:

.DATA TIK DW ?
...
MOV AX,00H
INT 1AH

MOV TIK,DX
ADD TIK, 12H

DELAY:
MOV AX,00H
INT 1AH
CMP TIK, DX
JGE DELAY

I'm from mobile. Sorry for my enters ;)