×

Search anything:

Exit program in Assembly code

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article at OpenGenus, we have explored how to exit a program written in Assembly.

Table of contents:

  1. Introduction to exit
  2. Exit in Assembly code

Introduction to exit

Following is a simple C code using exit:

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Exiting the program\n");
    exit(0);
}

How does the exit() call convert in Assembly code?

Exit in Assembly code

To exit an assembly code, use the following:

mov eax, 1
xor ebx, ebx
int 0x80

This is for x86 assembly architecture. The steps are:

  • The value of register eax is set to 1 which corresponds to system call for exit
  • Register ebx is set to 0 which is the exit status. 0 denotes normal exit.
  • The last statement int 0x80 raises an interrupt 0x80 to trigger the exit system call.

If you want to dive further in this domain of counting set bits, go through the paper titled "Faster Population Counts Using AVX2 Instructions" by Wojciech Mula, Nathan Kurz and Daniel Lemire.

With this article at OpenGenus, you must have the complete idea of how to implement exit in Assembly code.

Exit program in Assembly code
Share this