looping
home
Looping: LOOP and the CX Register
The LOOP instruction creates a counting loop Syntax: LOOP target Logic: 1. CX <- CX - 1 2. If CX > 0, jump to target Implementation: The assembler calculates the distance, in bytes, between the current location and the offset of the target label. It is called the relative offset. The relative offset is added to IP as a 1 byte offset. 1 byte offset => max distance is +127 to -128 bytes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use: CX, CL, CH for anything else inside the loop!!!
... mov AL, 0 ; init mov AH, 1 ; number to be added mov CX, 10 ; reps L1: add AL, AH ; sum = sum + num inc AH ; num++ loop L1 ; decrement THEN compare to zero What is in AL? AH?

LOOP Machine Code
The following loop calculates the sum of the integers 5 + 4 + 3 + 2 + 1: ASSEMBLY CODE: ============== mov AX, 0 mov CX, 5 L1: add AX, CX loop L1 where is the sum? what value is in CX at the end? why are we adding CX? MACHINE CODE: ============= offset machine code source code ------ ------------ ----------------- 7] 0100: B8 00 00 mov AX, 0 [ 8] 0103: B9 05 00 mov CX, 5 [ 9] 0106: 03 C1 L1: add AX, CX [ 10] 0108: E2 FC loop L1 [ 11] : [ 12] 010A: C3 ret Looking at the LOOP machine code, we see that -4 (FCh) is added to the current location, causing a jump to location 106. Explain.

Summing and Nested Loops
SUMMING ARRAY ELEMENTS ====================== .data Arr word 12,26,43,13,97,16,73,41 count = ($ - Arr)/2 ;number of elements .code mov AX, 0 ;AX holds the sum ; EITHER of the following will make ESI point to Arr mov SI, offset Arr lea SI, Arr mov CX, count ;holds counter for loop L1: add AX, [SI] ;dereference the pointer (why not WORD PTR[esi]?) add SI, 2 ;go to the next word loop L1
NESTED LOOP =========== To code a loop within a loop, you must save the outer loop's ECX value. Example: outer loop executes 100 times, and the inner loop 20 times. .data count WORD ? .code mov CX,100 ; set outer loop count L1: outer loop code... outer loop code... mov count,CX ; save outer loop count mov CX,20 ; set inner loop count L2: inner loop code... inner loop code... loop L2 ; repeat the inner loop mov CX,count ; restore outer loop count loop L1 ; repeat the outer loop

Looping more than +127 or -128 bytes
LOOP instruction is limited to a one byte offset Since the offset can be forward or back, the offset must be signed The signed numerical limit of one byte in decimal is ______ to ________
JMP is an unconditional jump to a label anywhere within the same procedure. === Can jump anywhere within 32 bit address space Syntax: JMP target Example: top: . . jmp top Ex: suppose we want to do: INCLUDE IRVINE32.INC .DATA COUNT = 7 .CODE main PROC mov ecx, COUNT ABC: ;lots of instructions that assembles into ; more than 128 bytes of code loop ABC ;NO! can't do this exit main ENDP END main
We do instead: ------- INCLUDE IRVINE32.INC .DATA Num = 7 .CODE main PROC mov ECX, Num ABC: ; ; lots of code ; cmp ECX, 0 je CONT ; end loop dec ECX jmp ABC ; keep looping CONT: ; ; continue code ; main ENDP END main ===== OR... ================== INCLUDE IRVINE32.INC .DATA Num = 7 .CODE main PROC mov ECX, Num ;set up the loop count ABC: ; ... ;lots of code ; ... dec ECX ;subtract 1 from the ECX (mimic the LOOP instruction) jne ABC ;keep looping if ECX is NOT zeo (mimic LOOP) ; ... ; continue code here when done looping ; ... main ENDP END main