Assembly - Loop

Prahlad Godara ------ From DOOSEEP

A loop is a block of statements that are repeatedly executed until a condition is satisfied. The assembly language uses JMP instruction to implement loops. However, the processor set can use the LOOP instruction to implement loops conveniently.

Content

loop in assembly language

Loop is used to repeatedly run any code block or group of instructions.

loop instruction work in assembly language

The LOOP instruction assumes that the ECX register contains the loop count. When the loop instruction is executed, the ECX register is decremented and the control jumps to the target label, until the ECX register value, i.e., the counter reaches the value zero.

Assembly Loop Syntax

The processor instruction set includes a set of loop instructions to implement recursion. The JMP instruction can be used to implement a loop. Where, label is the target label that identifies the target instruction as in the jump instructions.

Syntax
 LOOP 	label

For example

 MOV	CL, 10
  L1:
  -LOOP-BODY-
  DEC	CL
  JNZ	L1

Assembly Loop Exampal

The following program prints the number 1 to 8 on the screen −


  section	.text
  global _start        ;must be declared for using gcc
 
_start:	                ;tell linker entry point
  mov ecx,9
  mov eax, '1'
 
l1:
  mov [num], eax
  mov eax, 4
  mov ebx, 1
  push ecx
 
  mov ecx, num        
  mov edx, 1        
  int 0x80
 
  mov eax, [num]
  sub eax, '0'
  inc eax
  add eax, '0'
  pop ecx
  loop l1
 
  mov eax,1             ;system call number (sys_exit)
  int 0x80              ;call kernel
section	.bss
num resb 1 

When the above code is compiled and executed, it produces the following result −

 12345678:


Tags- Loop instructions in nasm assembly language ,education, Assembly loops, assembly language, type of Assembly loop , example program in assembly language, Conditions Operators, This blogcreates content similar to stackoverflow geeks for geeks tutorialspoint w3schools and dooseep