Advertisement
cookertron

Installing custom PIT handler (x86 assembler)

Jan 10th, 2019
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .model small
  2.  
  3. .386
  4.  
  5. .stack 100h
  6.  
  7. .data
  8. old_pit_seg     dw      ?
  9. old_pit_off     dw      ?
  10.  
  11. test_string     db      "TEST $"
  12.  
  13. counter         dw      0
  14.  
  15. .code
  16. start: 
  17.     xor     ax, ax
  18.     mov     ds, ax
  19.     mov     bx, 20h ; ds:bx points to PIT interrupt vector
  20.    
  21.     ; read handler address
  22.     cli
  23.     mov     ax, [bx] ; offset to PIT handler
  24.     mov     cx, [bx + 2] ; segment
  25.     sti
  26.    
  27.     ; push PIT handler segment:offset onto the stack
  28.     push    ax
  29.     push    cx
  30.    
  31.     ; get address of custom PIT Handler at tick
  32.     mov     ax, offset tick
  33.     mov     cx, cs
  34.  
  35.     ; set new handler in the interrupt vector table at ds:bx
  36.     cli
  37.     mov     [bx], ax
  38.     mov     [bx + 2], cx
  39.     sti
  40.    
  41.     ; point ds to program data segment
  42.     mov     ax, @data
  43.     mov     ds, ax
  44.    
  45.     ; return segment:offset for original PIT interrupt handler and store
  46.     pop     cx
  47.     pop     ax
  48.     mov     old_pit_off, ax
  49.     mov     old_pit_seg, cx
  50.  
  51. quit:
  52.     ; quit the program
  53.     mov ax,4c00h
  54.     int 21h
  55.  
  56. tick: ; the new custom PIT interrupt handler (does not handle BIOS time)
  57.     pushf
  58.     pusha
  59.  
  60.     ; restore program data segment and print "TEST " at the current cursor pos
  61.     mov     ax, @data
  62.     mov     ds, ax
  63.     mov     ax, 0900h
  64.     lea     dx, test_string
  65.     int     21h
  66.    
  67.     ; increment the counter until 20, if 20 then restore original PIT handler else exit interrupt
  68.     inc     counter
  69.     cmp     counter, 20
  70.     jne     tick_exit
  71.    
  72.     xor     ax, ax
  73.     mov     es, ax
  74.     mov     bx, 20h ; es:bx now points to PIT vector in interrupt table
  75.  
  76.     ; get the original segment:offset for the PIT interrupt handler
  77.     mov     ax, old_pit_off
  78.     mov     cx, old_pit_seg
  79.    
  80.     ; restore original PIT interrupt vector
  81.     cli
  82.     mov     es:[bx], ax
  83.     mov     es:[bx + 2], cx
  84.     sti
  85.    
  86. tick_exit:
  87.     ; acknowledge tick
  88.     mov     al, 20h
  89.     out     20h, al
  90.    
  91.     ; restore registers and flags
  92.     popa
  93.     popf
  94.     iret ; return from interrupt
  95.    
  96. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement