Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What are procedures in assembly language?

Rukhshan Haroon

Programmers worldwide commonly use functions to make code written in high-level languages, such as C++ and Python, less repetitive, more modular, and readable. Assembly code is usually larger in size and more complex than code written in most high-level languages.

To make assembly code more modular, readable, and shorter in size, we use procedures. Procedures are very similar to functions, as each procedure performs a particular task.


To define a procedure, the following syntax is used:


As shown above, each procedure has a unique name and must end with ret to exit gracefully.

To call a procedure, we use the call instruction. The call instruction takes in one operand, which is the name of the procedure that the programmer wishes to call.

call print

In the code snippet above, print is the name of our procedure.


The following program demonstrates how a procedure may be used to print or output data on the console.

Instead of calling all the instructions required to print a string, we can define the relevant instructions in a procedure. In this way, we could call multiple instructions by simply calling that procedure via the call instruction. As visible, using a procedure to output data onto the console takes much fewer lines of code and has made our code modular.

Similarly, procedures can be used to perform other standard operations, such as taking input from the user or writing to any other FILE stream.

section  .text
   global _start    
   mov   ecx, msg0         ;ecx contains the message needs to be printed. msg0 stored in ecx
   mov   edx, len0         ;edx contains the length of the message to be printed. len0 stored in edx
   call  print             ;print procedure called

   mov   ecx, msg1         ;same as above for msg1
   mov   edx, len1
   call  print

   mov   ecx, msg2         ;same as above for msg2
   mov   edx, len2
   call  print

   mov   eax, 1            ;system call number for the exit system call saved in eax
   int   0x80  ;           ;kernel interrupt to shift from user mode to kernel mode to execute the system call

   mov   ebx,1 ;           ;ebx contains the file descriptor of the file descriptor (1 for stdout)
   mov   eax,4 ;           ;eax contains the system call number. 4 is the system call number for the write system call
   int   0x80  ;           ;kernel interrupts to execute the write system call in kernel mode
   ret                     ;returns to the address where the print procedure was last called
section .data
   msg0 db "Welcome to Educative!", 0x0A  ;defines message to be printed. 0xA is \n in hex.
   len0 equ $ - msg0                      ;stores length of the message

   msg1 db "We are glad to have you here.", 0x0A   ;defines message to be printed. 0xA is \n in hex.
   len1 equ $ - msg1                               ;stores length of the message

   msg2 db "You are almost an expert at assembly language now!", 0x0A   ;defines message to be printed. 0xA is \n in hex.                                                  
   len2 equ $ - msg2                                                    ;stores length of the message

segment .bss




Rukhshan Haroon
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring