interupts

Interrupts in Assembly

Interrupts in Assembly

Interrupts are a mechanism used to signal the processor to stop its current operations and execute a special routine, known as an interrupt handler.

Types of Interrupts

Using the int Instruction

The int instruction is used to generate a software interrupt. The interrupt vector determines the routine to execute.

Example: Printing a Character

section .data
    char db 'A'        ; Character to print

section .text
    global _start

_start:
    mov rax, 0x01      ; System call: write
    mov rdi, 1         ; File descriptor: stdout
    mov rsi, char      ; Address of the character
    mov rdx, 1         ; Number of bytes to write
    syscall            ; Trigger the system call

    mov rax, 60        ; System call: exit
    xor rdi, rdi       ; Exit code: 0
    syscall
    

This example uses a Linux system call to print a character to the screen.

Example: Generating an Interrupt

section .text
    global _start

_start:
    mov ah, 0x09       ; DOS interrupt for printing a string
    mov dx, msg        ; Address of the string
    int 0x21           ; Call interrupt 21h

    mov ah, 0x4C       ; DOS interrupt for exit
    int 0x21           ; Exit the program

section .data
    msg db 'Hello, World!$', 0x0
    

This example demonstrates a DOS interrupt on x86 systems to print a string.