.WHILE Directive


The .WHILE directive tests the condition before executing the loop.

 .WHILE  condition
    statements
 .ENDW

 Finding the Min and Max in an Array 
 INCLUDE Irvine32.inc
 .data
   array    WORD  50, 70, 20, 10, 80
   prompt1  BYTE  "Minimum is ", 0
   prompt2  BYTE  "Maximum is ", 0

 .code
 main PROC
   mov    esi, OFFSET array
   mov    ecx, LENGTHOF array - 1
   movzx  eax, WORD PTR [esi]         ; EAX: minimum
   movzx  ebx, WORD PTR [esi]         ; EBX: maximum

   .WHILE ( ecx > 0 )
     add  esi, 2   
       
mov ax, [esi] .ELSEIF ( WORD PTR [esi] > bx ) mov bx, [esi] .ENDIF dec ecx .ENDW mov edx, OFFSET prompt1 ; print the minimum call WriteString call WriteDec call Crlf mov edx, OFFSET prompt2 ; print the maximum call WriteString movzx eax, bx call WriteDec exit main ENDP END main