w1coding.comAll tutorials

Assembly Interupts

Assembly lessons
  1. Environment Setup
  2. Introduction
  3. Syntax
  4. Datamovement
  5. Operators
  6. Control
  7. Loops
  8. Interupts
  9. Functions
  10. Memory
  11. System
  12. Debugging
  13. Macros
  14. Execution
  15. Floating
  16. Interupts
  17. Optimization
  18. Linking
  19. Inline
  20. Registers
  21. Bootloader
  22. Macros
  23. Stack
  24. Call
  25. Technique
  26. Responsive

Interrupts in Assembly

Interrupts are mechanisms used to handle asynchronous events. They can be software-initiated (via the int instruction) or hardware-generated.

Software Interrupts

Software interrupts are invoked using the int instruction followed by an interrupt vector number. For example:

int 0x80    ; Linux system call interrupt
int 0x10    ; BIOS video services
    

Example: Linux System Call

This example demonstrates using int 0x80 for a Linux system call to write a string to the console.

section .data
    message db 'Hello, Interrupt!', 0xA
    msg_len equ $ - message

section .text
    global _start

_start:
    mov eax, 4          ; System call: write
    mov ebx, 1          ; File descriptor: stdout
    mov ecx, message    ; Address of the message
    mov edx, msg_len    ; Length of the message
    int 0x80            ; Trigger interrupt

    mov eax, 1          ; System call: exit
    xor ebx, ebx        ; Exit code: 0
    int 0x80            ; Trigger interrupt
    

Example: BIOS Video Services

This example changes the text color using BIOS interrupt int 0x10.

section .text
    org 0x7C00          ; Code starts at boot sector

_start:
    mov ah, 0x0E        ; Teletype output
    mov al, 'A'         ; Character to display
    int 0x10            ; Trigger BIOS interrupt

    hlt                 ; Halt the CPU
    

Hardware Interrupts

Hardware interrupts are generated by devices (e.g., keyboards, timers). The CPU uses an Interrupt Descriptor Table (IDT) to handle them.

Example: A keyboard interrupt typically uses vector 0x09 (on older systems). Handling these interrupts requires writing an interrupt service routine (ISR).

Key Notes

  • Software interrupts are useful for system-level programming.
  • Modern operating systems often restrict direct hardware interrupt handling for security and stability.
  • Use modern alternatives like syscall or API functions where applicable.