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 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

  • Software Interrupts: Triggered by instructions like int.
  • Hardware Interrupts: Triggered by external hardware devices.

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.