简体   繁体   中英

Loops and conditionals in TASM Assembly 8086 DOS

I want to run this loop with a conditional at the end in this fashion

mov cx, 10
mov di, 0

loop:

...

inc di
dec cx

cmp di, 5
jne loop

...     

jnz loop

but it seems like it won't work unless I decrement cx immediately before

jnz loop

this is preventing me from decrementing cx each time that di != 5 . I think I am misunderstanding the proper use of cx

JNZ jumps if the zero flag is clear. There are many x86 instructions that will modify the zero flag besides DEC .

It sounds like you want something like this:

cmp di, 5
je no_dec
dec cx      ; decrement CX when di != 5
no_dec:
...
jncxz loop  ; jump if CX != 0
            ; if JNCXZ isn't supported on the target CPU you could
            ; replace it with CMP CX,0 / JNZ loop

Btw, LOOP is a poor choice for a label name since LOOP is an instruction on x86. In fact, you can replace code like this:

dec cx
jnz label

with

loop label  ; decrements CX and jumps if not zero
xor di,di
mov cx,10
_theLoop:
    ; ...
    inc di    ; I wonder why are you incrementing DI manually...
    cmp di,5
    ja _done
    loop _theLoop
_done:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM