My-Bad-2

int.asm

Nov 1st, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 2.71 KB | Source Code | 0 0
  1. [BITS 64]
  2.  
  3. %macro pushall 0
  4.  
  5.     push rax
  6.     push rbx
  7.     push rcx
  8.     push rdx
  9.     push rsi
  10.     push rdi
  11.     push rbp
  12.     push r8
  13.     push r9
  14.     push r10
  15.     push r11
  16.     push r12
  17.     push r13
  18.     push r14
  19.     push r15
  20.  
  21. %endmacro
  22.  
  23. %macro popall 0
  24.  
  25.     pop r15
  26.     pop r14
  27.     pop r13
  28.     pop r12
  29.     pop r11
  30.     pop r10
  31.     pop r9
  32.     pop r8
  33.     pop rbp
  34.     pop rdi
  35.     pop rsi
  36.     pop rdx
  37.     pop rcx
  38.     pop rbx
  39.     pop rax
  40.  
  41. %endmacro
  42.  
  43. %macro Interrupt_Name 1
  44.  
  45.     dq __interrupt_%1
  46.  
  47. %endmacro
  48.  
  49. %macro Interrupt_Err 1
  50.  
  51. __interrupt_%1:
  52.     push qword %1
  53.     jmp __interrupt_call
  54.  
  55. %endmacro
  56.  
  57. %macro Interrupt_Noerr 1
  58.  
  59. __interrupt_%1:
  60.     push qword 0 ; no error
  61.     push qword %1
  62.     jmp __interrupt_call
  63.  
  64. %endmacro
  65.  
  66. section .text
  67.  
  68. extern interrupt_handler
  69.  
  70. __interrupt_call:
  71.     cld
  72.     pushall ; store the values in 64-bit registers
  73.  
  74.     mov rdi, rsp ; copy the value in rsp to rdi register
  75.     call interrupt_handler ; call the C-function
  76.     mov rsp, rax ; copy the value in rax to rsp register
  77.  
  78.     popall ; remove the last pushed values from the 64-bit registers
  79.     add rsp, 16 ; pop error code and interrupt number
  80.  
  81.     iretq ; returns the control from the interrupt handler to the process interrupted
  82.  
  83. Interrupt_Noerr 0 ; Divide by zero
  84. Interrupt_Noerr 1 ; Debug single step
  85. Interrupt_Noerr 2 ; Non-Maskable interrupt (NMI input on processor)
  86. Interrupt_Noerr 3 ; Debug breakpoints (INT3)
  87. Interrupt_Noerr 4 ; Arithmetic overflow (INTO)
  88. Interrupt_Noerr 5 ; Bounds range exceeded (BOUND)
  89. Interrupt_Noerr 6 ; Invalid Opcode (UD2)
  90. Interrupt_Noerr 7 ; Device not available (WAIT/FWAIT)
  91. Interrupt_Err   8 ; Double fault
  92. Interrupt_Noerr 9 ; Coprocessor segment overrun
  93. Interrupt_Err   10 ; Invalid TSS
  94. Interrupt_Err   11 ; Segment not present
  95. Interrupt_Err   12 ; Stack-segment fault
  96. Interrupt_Err   13 ; General protection fault
  97. Interrupt_Err   14 ; Page fault
  98. Interrupt_Noerr 15 ; Reserved
  99. Interrupt_Noerr 16 ; x87 FPU error
  100. Interrupt_Err   17 ; Alignment check
  101. Interrupt_Noerr 18 ; Machine check
  102. Interrupt_Noerr 19 ; SIMD floating-point exception
  103. Interrupt_Noerr 20 ; Reserved
  104. Interrupt_Noerr 21 ; Reserved
  105. Interrupt_Noerr 22 ; Reserved
  106. Interrupt_Noerr 23 ; Reserved
  107. Interrupt_Noerr 24 ; Reserved
  108. Interrupt_Noerr 25 ; Reserved
  109. Interrupt_Noerr 26 ; Reserved
  110. Interrupt_Noerr 27 ; Reserved
  111. Interrupt_Noerr 28 ; Reserved
  112. Interrupt_Noerr 29 ; Reserved
  113. Interrupt_Err   30 ; Reserved
  114. Interrupt_Noerr 31 ; Reserved
  115.  
  116. %assign i 32
  117. %rep 224
  118.     Interrupt_Noerr i
  119. %assign i i+1
  120. %endrep
  121.  
  122. section .data
  123. global __interrupt_handlers
  124.  
  125. ; Will be used while initializing IDT
  126. __interrupt_handlers:
  127. %assign i 0
  128. %rep 256
  129.     Interrupt_Name i
  130. %assign i i+1
  131. %endrep
Add Comment
Please, Sign In to add comment