Computer Organization and
Assembly Language
Loop Introduction
 The LOOP instruction, formally known as
Loop According to ECX Counter, repeats a
block of statements a specific number of
times. ECX is automatically used as a
counter and is decremented
 Its syntax is
 LOOP destination
 The execution of the LOOP instruction
involves two steps: First, it subtracts 1
from ECX. Next, it compares ECX to
zero.
 If ECX is not equal to zero, a jump is
taken to the label identified by destination.
Otherwise, if ECX equals zero, no jump
takes place, and control passes to the
instruction following the loop.
 Rarely should you explicitly modify ECX
inside a loop. If you do, the LOOP instruction
may not work as expected. In the following
example, ECX is incremented within the loop.
It never reaches zero, so the loop never stops:
mov ecx,5
top:
Inc ecx
loop top
 In the following example, we add 1 to AX
each time the loop repeats.When the
loop ends, EAX 5 and ECX 0:
mov eax,0
mov ecx,5
L1:
inc eax
loop L1
 A common programming error is to
inadvertently initialize ECX to zero
before beginning a loop. If this happens,
the LOOP instruction decrements ECX
to FFFFFFFFh, and the loop repeats
4,294,967,296 times!
while( eax < ebx)
eax = eax + 1;
Consider the following example:
top:
cmp eax,ebx ; check loop condition
jae next ; false? exit loop
inc eax ; body of loop
jmp top ; repeat the loop
next:
If you need to modify ECX inside a loop, you can
save it in a variable at the beginning of the loop and
restore it just before the LOOP instruction:
.data
count DWORD ?
.code
mov ecx,100 ; set loop count
top:
mov count,ecx ; save the count
mov ecx,20 ; modify ECX
mov ecx,count ; restore loop count
loop top
 When creating a loop inside another
loop, special consideration must be given
to the outer loop counter in ECX.You
can save it in a variable:
.data
count DWORD ?
.code
mov ecx,100 ; set outer loop count
L1:
mov count,ecx ; save outer loop count
mov ecx,20 ; set inner loop count
L2:
loop L2 ; repeat the inner loop
mov ecx,count ; restore outer loop count
loop L1
INCLUDE Irvine32.inc
.data
intarray DD 10000h,20000h,30000h,40000h
.code
main PROC
mov edi,OFFSET intarray ; 1: EDI = address of intarray
mov ecx,LENGTHOF intarray ; 2: initialize loop counter
mov eax,0 ; 3: sum = 0
L1: ; 4: mark beginning of loop
add eax,[edi] ; 5: add an integer
add edi,TYPE intarray ; 6: point to next element
loop L1
Call writedec ; 7: repeat until ECX = 0
exit
main ENDP
END main
Character Print
 INCLUDE Irvine32.inc
 .code
 main PROC
 continue:
 CALL ReadChar ;char in AL
 cmp al,0 ;extended key?
 je stop ;yes, then exit
 CALL WriteChar ;no, print char
 jmp continue
 stop:
 exit
 main ENDP
 END main
LOOPZ and LOOPE Instructions
 The LOOPZ (loop if zero) instruction
works just like the LOOP instruction
except that it has one additional
condition: the Zero flag must be set in
order for control to transfer to the
destination label.The syntax is
 LOOPZ destination
LOOPZ and LOOPE Instructions cont.
 The LOOPE (loop if equal) instruction is
equivalent to LOOPZ, and they share the
same opcode.They perform the following
tasks:
ECX = ECX - 1
if ECX > 0 and ZF = 1 ; jump to destination
LOOPNZ and LOOPNE Instructions
 The LOOPNZ (loop if not zero)
instruction is the counterpart of LOOPZ.
The loop continues while the unsigned
value of ECX is greater than zero (after
being decremented) and the Zero flag is
clear.The syntax is
 LOOPNZ destination
LOOPNZ and LOOPNE Instructions contd.
 The LOOPNE (loop if not equal) instruction is
equivalent to LOOPNZ, and they share the
same opcode.They perform the following
tasks:
ECX = ECX - 1
if ECX > 0 and ZF = 0 , jump to destination
Otherwise, nothing happens, and control passes
to the next instruction.
Example code
INCLUDE Irvine32.inc
.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP(0)
.code
main PROC
mov esi,0 ; index register
mov ecx,SIZEOF source ; loop counter
Example contd.
L1:
mov al,source[esi] ; get a character from
source
mov target[esi],al ; store it in the target
inc esi ; move to next character
loop L1
exit
main ENDP
END main
 Enter a no. 7
 49
 36
 25
 16
 9
 4
 1
Code of previous slide Task
 include irvine32.inc
 .data
 s1 byte "Enter a number:",0
 .code
 main Proc
 mov edx,offset s1
 call writestring
 call readint
 mov ecx,eax
 L1:
 mov eax,ecx
 mul eax
 call writedec
 call crlf
 loop L1
 exit
 main endP
 end
 Enter a number: 9
 You have entered odd number
 Are you want to play again press any
number except 0? 1
 Enter a number: 8
 You have entered even number
 Are you want to play again press any
number except 0? 0

Loop Updated.pptxLoop Updated.pptxLoop Updated.pptxLoop Updated.pptxLoop Updated.pptxLoop Updated.pptx

  • 1.
    Computer Organization and AssemblyLanguage Loop Introduction
  • 2.
     The LOOPinstruction, formally known as Loop According to ECX Counter, repeats a block of statements a specific number of times. ECX is automatically used as a counter and is decremented  Its syntax is  LOOP destination
  • 3.
     The executionof the LOOP instruction involves two steps: First, it subtracts 1 from ECX. Next, it compares ECX to zero.  If ECX is not equal to zero, a jump is taken to the label identified by destination. Otherwise, if ECX equals zero, no jump takes place, and control passes to the instruction following the loop.
  • 4.
     Rarely shouldyou explicitly modify ECX inside a loop. If you do, the LOOP instruction may not work as expected. In the following example, ECX is incremented within the loop. It never reaches zero, so the loop never stops: mov ecx,5 top: Inc ecx loop top
  • 5.
     In thefollowing example, we add 1 to AX each time the loop repeats.When the loop ends, EAX 5 and ECX 0: mov eax,0 mov ecx,5 L1: inc eax loop L1
  • 6.
     A commonprogramming error is to inadvertently initialize ECX to zero before beginning a loop. If this happens, the LOOP instruction decrements ECX to FFFFFFFFh, and the loop repeats 4,294,967,296 times!
  • 7.
    while( eax <ebx) eax = eax + 1; Consider the following example: top: cmp eax,ebx ; check loop condition jae next ; false? exit loop inc eax ; body of loop jmp top ; repeat the loop next:
  • 8.
    If you needto modify ECX inside a loop, you can save it in a variable at the beginning of the loop and restore it just before the LOOP instruction: .data count DWORD ? .code mov ecx,100 ; set loop count top: mov count,ecx ; save the count mov ecx,20 ; modify ECX mov ecx,count ; restore loop count loop top
  • 9.
     When creatinga loop inside another loop, special consideration must be given to the outer loop counter in ECX.You can save it in a variable:
  • 10.
    .data count DWORD ? .code movecx,100 ; set outer loop count L1: mov count,ecx ; save outer loop count mov ecx,20 ; set inner loop count L2: loop L2 ; repeat the inner loop mov ecx,count ; restore outer loop count loop L1
  • 11.
    INCLUDE Irvine32.inc .data intarray DD10000h,20000h,30000h,40000h .code main PROC mov edi,OFFSET intarray ; 1: EDI = address of intarray mov ecx,LENGTHOF intarray ; 2: initialize loop counter mov eax,0 ; 3: sum = 0 L1: ; 4: mark beginning of loop add eax,[edi] ; 5: add an integer add edi,TYPE intarray ; 6: point to next element loop L1 Call writedec ; 7: repeat until ECX = 0 exit main ENDP END main
  • 12.
    Character Print  INCLUDEIrvine32.inc  .code  main PROC  continue:  CALL ReadChar ;char in AL  cmp al,0 ;extended key?  je stop ;yes, then exit  CALL WriteChar ;no, print char  jmp continue  stop:  exit  main ENDP  END main
  • 13.
    LOOPZ and LOOPEInstructions  The LOOPZ (loop if zero) instruction works just like the LOOP instruction except that it has one additional condition: the Zero flag must be set in order for control to transfer to the destination label.The syntax is  LOOPZ destination
  • 14.
    LOOPZ and LOOPEInstructions cont.  The LOOPE (loop if equal) instruction is equivalent to LOOPZ, and they share the same opcode.They perform the following tasks: ECX = ECX - 1 if ECX > 0 and ZF = 1 ; jump to destination
  • 15.
    LOOPNZ and LOOPNEInstructions  The LOOPNZ (loop if not zero) instruction is the counterpart of LOOPZ. The loop continues while the unsigned value of ECX is greater than zero (after being decremented) and the Zero flag is clear.The syntax is  LOOPNZ destination
  • 16.
    LOOPNZ and LOOPNEInstructions contd.  The LOOPNE (loop if not equal) instruction is equivalent to LOOPNZ, and they share the same opcode.They perform the following tasks: ECX = ECX - 1 if ECX > 0 and ZF = 0 , jump to destination Otherwise, nothing happens, and control passes to the next instruction.
  • 17.
    Example code INCLUDE Irvine32.inc .data sourceBYTE "This is the source string",0 target BYTE SIZEOF source DUP(0) .code main PROC mov esi,0 ; index register mov ecx,SIZEOF source ; loop counter
  • 18.
    Example contd. L1: mov al,source[esi]; get a character from source mov target[esi],al ; store it in the target inc esi ; move to next character loop L1 exit main ENDP END main
  • 19.
     Enter ano. 7  49  36  25  16  9  4  1
  • 20.
    Code of previousslide Task  include irvine32.inc  .data  s1 byte "Enter a number:",0  .code  main Proc  mov edx,offset s1  call writestring  call readint  mov ecx,eax  L1:  mov eax,ecx  mul eax  call writedec  call crlf  loop L1  exit  main endP  end
  • 21.
     Enter anumber: 9  You have entered odd number  Are you want to play again press any number except 0? 1  Enter a number: 8  You have entered even number  Are you want to play again press any number except 0? 0