5

I want to understand what why we use int 80h and what happens after calling it in assembly code. Like here:

int 80h
mov eax,1
int 80h

How does it know that I want to call the system exit, not just saving decimal number in eax?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
muh ahmed
  • 61
  • 1
  • 2
  • [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](https://stackoverflow.com/q/46087730) has some details about the kernel side of system calls. [Hello, world in assembly language with Linux system calls?](https://stackoverflow.com/q/61519222) explains how `int 0x80` calls into the kernel. – Peter Cordes Aug 13 '20 at 23:51
  • Note that `int` here is completely unrelated to the C/Java integer type. – that other guy Aug 13 '20 at 23:59

1 Answers1

9

INT is the assembly mnemonic for "interrupt". The code after it specifies the interrupt code. (80h/0x80 or 128 in decimal is the Unix System Call interrupt) When running in Real Mode (16-bit on a 32-bit chip), interrupts are handled by the BIOS. When running on top of an Operating System, interrupts are handled by the OS through an Interrupt Descriptor Table (IDT) loaded at boot time.

If the CPU encounters an INT instruction, it will either lookup the interrupt in the BIOS's IVT or the OS's IDT and find the attached handler for that specific interrupt code. It will then jump to that piece of code specified by the IDT/IVT, start executing, and jump back to the code that called the INT when it reaches the special instruction IRET in the handler, denoting that the system is finished with executing the interrupt.

Some interrupt handlers use registers to know more information, sort of like passing arguments to a function in a higher-level language. Therefore, when you write 1 to EAX, the handler which receives your interrupt will see that EAX is set to 1 and know something about what you want, for example the system resource which you want to use.

  • at first thanks for all the people who replied to this, but i want to understand in order to call any system call do i have to call int 80h first and after call the "system call" do i have to end it with int 80h ? – muh ahmed Aug 14 '20 at 14:33
  • 1
    The system call is like a function. You call the function by writing `INT 80h`. You don't need to end the system call, or perform any functionality; it's all handled by the OS. – ObsoleteAwareProduce Aug 14 '20 at 18:47
  • Thanks a lot, i know that i have a lot of question and i am going to ask another one so please see it. – muh ahmed Aug 15 '20 at 10:23
  • i have an error when trying to move variable to AL or AX like: "VARIABLE DB 15h" then when trying to do this "MOV AL, VARIABLE" it gives me "(.text+0x1): relocation truncated to fit: R_386_8 against `.data' " – muh ahmed Aug 15 '20 at 10:26
  • 1
    *(Assuming you're using NASM)* `VARIABLE db 15h` will put a byte (15h) at that point in memory once assembled, with the address "VARIABLE". To reference it in your program, write `MOV AL, [VARIABLE]`. The square brackets around "VARIABLE" mean that you are looking for the value at memory address "VARIABLE", as opposed to the value of "VARIABLE" itself, which is a memory **location**. – ObsoleteAwareProduce Aug 15 '20 at 13:53
  • Thanks again, i asked about it because i wanted to be certain because i was learning on microsoft assembler – muh ahmed Aug 15 '20 at 18:57