# Assembly Loop: How many times does NOP run?

1. Apr 5, 2013

1. The problem statement, all variables and given/known data

How many NOP instructions are run in the nested loop below?

OUTER DW 4000 ;outer loop count
INNER DW 62000 ;inner loop count

MOV DX,OUTER
WAIT1: MOV CX,INNER
WAIT2:
NOP
NOP
NOP
NOP
LOOP WAIT2
DEC DX
JNZ WAIT1
RET
DELAY ENDP

3. The attempt at a solution

I am creating a delay loop for use with an 8253 timer which is used to create a tone on a PC's internal speaker. The program is written in assembly. I am having a hard time figuring out how many times NOP executes in the code above.

I believe it is inner + outer = (4)(62000) + (4)(4000) = 264,000.

Any insight would be appreciated.

2. Apr 6, 2013

### Staff: Mentor

I think you're way off. For each iteration of the outer loop, the inner loop runs 62,000 times, so the NOP instructions run 62,000 * 4 = 248,000 times.

That's for each iteration of the outer loop, which runs 4000 times. What does that mean for the statements in the inner loop body?

3. Apr 6, 2013

### rcgldr

If you want a delay that isn't processor dependent, why not read the countdown timer from the 8253 itself?

4. Apr 6, 2013

One of the points of the exercise is to determine the speed of the computer's processor by how long it takes the NOP loop to run (how long the tone plays).

You're right, processor independence would normally be desired, but not in this case.

5. Apr 6, 2013

### rcgldr

You could run the loop and read the timer before and after the loop. However the timer may wrap around if your loop takes too long. One way to avoid this would be to intercept the timer interrupt and use an interrupt count as the "upper" bits of the counter timer. Depending on how the timer is programmed, it may count down by 1 or count down by 2 (if it's by 2, there's a sequence of port I/O writes and reads to get the upper bit of the counter).