SlideShare a Scribd company logo
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

More Related Content

PPTX
COAL loops completeCOAL loops completeCOAL loops completeCOAL loops completeC...
PPT
chapt5 and 06assemblylanguagecodesandmachinelanguage.ppt
PPT
Assem -lect-6
PDF
How To Beat An Advanced CrackMe
PDF
N_Asm Assembly arithmetic instructions (sol)
PPTX
[ASM]Lab4
PPT
chapt_5+6AssemblyLanguagecompleteclear.ppt
PPTX
[ASM]Lab5
COAL loops completeCOAL loops completeCOAL loops completeCOAL loops completeC...
chapt5 and 06assemblylanguagecodesandmachinelanguage.ppt
Assem -lect-6
How To Beat An Advanced CrackMe
N_Asm Assembly arithmetic instructions (sol)
[ASM]Lab4
chapt_5+6AssemblyLanguagecompleteclear.ppt
[ASM]Lab5

Similar to Loop Updated.pptxLoop Updated.pptxLoop Updated.pptxLoop Updated.pptxLoop Updated.pptxLoop Updated.pptx (20)

PPTX
Loop instruction, controlling the flow of progam
PPT
Chapt 06
PPT
Chapt 06
PDF
How to recover malare assembly codes
PDF
The Stack and Buffer Overflows
PPTX
[ASM]Lab7
PPT
PPTX
[ASM]Lab8
PPTX
[ASM]Lab6
PPTX
Procedure.lecture number pptx slide form
PPTX
X86 operation types
PPTX
Assembly language (addition and subtraction)
PPT
Al2ed chapter7
PDF
3CA949D5-0399-46AE-8A97-F01C8599B2DA.pdf
PPT
Control structures ii
PDF
loops in C ppt.pdf
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
PPTX
Arithmetic and logical instructions set
PPTX
LECTURE_5 Program Control Instructions.pptx
PDF
lec15_x86procedure_4up.pdf
Loop instruction, controlling the flow of progam
Chapt 06
Chapt 06
How to recover malare assembly codes
The Stack and Buffer Overflows
[ASM]Lab7
[ASM]Lab8
[ASM]Lab6
Procedure.lecture number pptx slide form
X86 operation types
Assembly language (addition and subtraction)
Al2ed chapter7
3CA949D5-0399-46AE-8A97-F01C8599B2DA.pdf
Control structures ii
loops in C ppt.pdf
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Arithmetic and logical instructions set
LECTURE_5 Program Control Instructions.pptx
lec15_x86procedure_4up.pdf
Ad

More from babanazar7204 (7)

PPTX
Breadth First Search.pptxBreadth First Search.pptxBreadth First Search.pptx
PPT
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
PPTX
Regression-Analysis-Introduction (1).pptx
PPTX
Computer Organization and assembly language Computer Organization and assembl...
PPTX
Computer Organization and assembly language
PPTX
Computer Organization and assembly language
PDF
Networking models in tersm of OSI modal complete working in 1 ppt
Breadth First Search.pptxBreadth First Search.pptxBreadth First Search.pptx
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
Regression-Analysis-Introduction (1).pptx
Computer Organization and assembly language Computer Organization and assembl...
Computer Organization and assembly language
Computer Organization and assembly language
Networking models in tersm of OSI modal complete working in 1 ppt
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
master seminar digital applications in india
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Complications of Minimal Access Surgery at WLH
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Insiders guide to clinical Medicine.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Institutional Correction lecture only . . .
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Lesson notes of climatology university.
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
Cell Types and Its function , kingdom of life
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPH.pptx obstetrics and gynecology in nursing
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
master seminar digital applications in india
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Complications of Minimal Access Surgery at WLH
VCE English Exam - Section C Student Revision Booklet
Insiders guide to clinical Medicine.pdf
RMMM.pdf make it easy to upload and study
Institutional Correction lecture only . . .
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Lesson notes of climatology university.
TR - Agricultural Crops Production NC III.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Renaissance Architecture: A Journey from Faith to Humanism

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

  • 1. Computer Organization and Assembly Language Loop Introduction
  • 2.  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
  • 3.  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.
  • 4.  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
  • 5.  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
  • 6.  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!
  • 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 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
  • 9.  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:
  • 10. .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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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.
  • 17. 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
  • 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 a no. 7  49  36  25  16  9  4  1
  • 20. 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
  • 21.  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