More Loop Instructions

We've already seen the instruction:

<label> LOOP <short-label>

This instruction uses the CX-register (or ECX-register) as the counter variable and short-label is the label of an instruction that is within -128 to 127 from the end of the loop instruction.

When the loop instruction is encountered the following happens:

Decrement the CX-register by 1
If CX != 0 goto short-label Else Execution continues with instruction following loop

Note: None of the flags in the flags register are affected.

Other loop instructions exist as follows:

(i48) <label> LOOPE <short-label>
or <label> LOOPZ <short-label>

When the loope or loopz instruction is encountered the following happens in Real Mode:

Decrement the CX-register by 1
If (CX != 0) && (ZF == 1) goto short-label Else Execution continues with instruction following loop

Note1: I like to think of this instruction as "loop while equal". Why does it make sense to think of this instruction this way?

Note2: In Protected Mode, ECX is used.

Note3: The instruction LOOPZD forces ECX to be used.

(i49) <label> LOOPNE <short-label>
or <label> LOOPNZ <short-label>

When the loope or loopz instruction is encountered the following happens in Real Mode:

Decrement the CX-register by 1
If (CX != 0) && (ZF == 0) goto short-label Else Execution continues with instruction following loop

I like to think of this instruction as "loop while not equal". Why does it make sense to think of this instruction this way?

Can you think of situations that the loope and loopne instructions are good for?

Special MASM Macros

Single-Aternative IF

.IF <conditional-expression>
instruction list
.ENDIF

where conditional-expression is <destination> <relational-op> <source> and <relational-op> is one of the following: <,<=,>,>=,==, or !=

The following macro would probably be expanded as follows:

.IF X != AX          CMP X,AX
INC X                JE @C0001
.ENDIF               INC X
             @C0001:

The double-alternative if has the following form:

.IF <conditional-expression>
true instruction list
.ELSE
false instruction list
.ENDIF

The repeat-until loop has the following form:

.REPEAT
instruction list
.UNTIL <conditional-expression>

Q#1: What do you think the following macro would expand to at assembly time? One other thing: constants are translated into hex.

mov bx,0
.REPEAT
inc ax
add bx,ax
.UNTIL bx > 100

The while loop has the following form:

.WHILE <conditional-expression>
instruction list
.ENDW

There are other conditional and loop macros but this is all of the ones I'm going to talk about. Further, Chapter 6 discusses nested control structures and Compound Expressions using AND & OR, but due to the experience all of us have with high level programming languages, the same logic applies at this lower level. Please see me if you have any questions.

Let's solve a nested loop problem before continuing on in Chapter 7.

P#2: Write an assembly language program segment that will print the following pattern. Ask the user for the number of rows in the pattern. Code this solution first with macros and then without macros.

*
**
***
****
*****

Note: I'm also going to skip Bit Testing Instructions on pp. 198-200. Please know that they exist and feel free to use them if necessary.


©Douglas J. Ryan / ryandj@pacificu.edu