Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

assembly

What are loops in assembly language?

Ayesha Naeem

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.

Syntax and explanation

The following code snippet illustrates how a loop is implemented through JMP instruction:

mov AL, 5     ; store the number of iteration in AL
L1:
(loop code)
DEC AL        ; decrement AL
JNZ L1
  • The number of iterations of the loop is stored in AL.
  • At the end of each iteration, the code decrements AL, then takes a conditional jump if AL is not zero.

The following code snippet illustrates how a loop is implemented through the loop instruction:

mov ECX 5   ; store the number of iterations in ECX register
L1:
(loop code)
loop l1
  • The loop instruction always extracts the number of iterations from the ECX register.
  • The loop instruction decrements the value of ECX and compares it with zero.
  • If the value in ECX is equal to zero, the program jumps to the L1 label; otherwise, the program exits the loop.

Examples

The following code illustrates the use of loop instruction using the X86 instruction set to print the first 10 numbers:

section	.text
   global _start        ;must be declared for using gcc
	
_start:	                ;tell linker entry point
   mov rcx,10            ;loop runs until the value of rcx is 0
   mov rax, '1'		    ;rax holds the character that needs to be printed
	
l1:			             ;loop starts
   mov [num], rax	       ;value in rax moved to variable num
   mov rax, 4		       ;4 is the system call number for the write system call
   mov rbx, 1		       ;1 is the file descriptor for the output stream
   push rcx		          ;value of rcx pushed to stack and stored here temporarily

			                ;rbx, rcx and rdx are arguments to the write system call

   mov rcx, num          ;num moved to rcx, as rcx contains the character that will be printed
   mov rdx, 1       	    ;1 is the size (1 byte) of the character that is to be printed
   int 0x80		          ;interrupt that executes the write system call in kernel mode
	
   mov rax, [num]	       ;the first character has been output, value of num moved to eax
   sub rax, '0'		    ;converts character in eax to decimal
   inc rax		          ;increments decimal value in eax by 1
   add rax, '0'		    ;converts decimal back to character
   pop rcx		          ;pops back value of ecx temporarily stored on the stack
   loop l1		          ;loops, value of ecx auto decremented
	
   mov eax,1             ;system call number (sys_exit)
   int 0x80              ;call kernel
section	.bss
num resb 1
  • The rax register stores the iteration number, and the rcx register stores the total number of iterations and is initialized to 10.
  • The l1 block represents the loop code. At each iteration, the iteration count in the rcx register is pushed onto the stack. The current value of rax is moved to rcx, and a write system call is made, which prints the number on the screen.
  • rax is incremented, and the iteration count is popped from the stack into the rcx register. The program then decrements rcx and jumps to l1 using the loop command if rcx is greater than 0.

RELATED TAGS

assembly

CONTRIBUTOR

Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring