Assembly Language Programming 6
Loops
Unconditional Jump.
| JMP | label | Jump to 'label' |
Conditional Jump.
| JZ | label | Jump if Zero flag is set. |
| JNZ | label | Jump if Zero flag is not set. |
| JC | label | Jump if Carry flag is set. |
| JNC | label | Jump if Carry flag is not set. |
There are a number of other jump instructions. (See instruction set)
A Loop is a section of program which needs to be repeated a number of times.
The number of times a loop is repeated (iterations) may be counted by a Control Variable (Counter). This may be a store location or a register.
Example
Suppose we need a program which outputs a 'beep' three times.
The pseudocode may be...
| 1. | Set count to 3. |
| 2. | Output a 'beep'. |
| 3. | Decrement count. |
| 4. | If count is not zero then go to step 2. |
| 5. | End |
We will use the [CL] register for the loop control variable, as this is not used by input or output interrupt routines.
Assembly program is :
| MOV | CL,3 | {set count to 3} | |
| BeepLoop: | MOV | AH,2 | |
| MOV | DL,7 | {ASCII code for a beep} | |
| INT | 21H | {Outputs a beep} | |
| DEC | CL | {Decrement count} | |
| JNZ | Beeploop | {If not zero, repeat loop} |