Amina Nasrin
Amina Nasrin

Reputation: 89

Assembly language: counter loop

I have written an assembly code that includes:

    XOR BL,BL
    MOV CX,0 
TOP: 
    INC BL,1
    MOV AH,2 
    MOV DL, BL 
    INT 21H
    LOOP TOP

The loop is executed a really large number of times (more than 10,000 for sure). What could be the possible reason behind the loop execution of such a high time? I am very new in assembly language and found nothing efficient to my code related to CX=0. Thanks in Advance.

Upvotes: 2

Views: 13473

Answers (2)

Your counter cx was not properly initialized. The instruction loop does two things:

dec cx            ;◄■■■ DECREASE THE COUNTER.
jnz label         ;◄■■■ IF COUNTER IS NOT ZERO, JUMP TO LABEL TO REPEAT.

In your code the counter cx was initialized as zero, so, when the loop instruction executes, it does cx - 1, which is 0 - 1, so cx becomes 0ffffh and your loop will repeat 0ffffh times.

Move another value to your counter cx, for example, mov cx, 10, so your loop will repeat 10 times.

Upvotes: 3

Frank C.
Frank C.

Reputation: 8096

The Intel loop instruction decrements the CX register first and then checks for zero condition.

Set CX to 1 prior to TOP: to test.

Upvotes: 1

Related Questions