Hooking Interrupt

Hooking interrupt int90h
;This program shows how to hook an interrupt
;I am hooking int 90h here.
;It shows,how we can print at any place.
;It shows use of iret.
;iret is used to restore cs,ip and flags
;from flag, because they are pushed on
;stack when an interrupt occurs.
org 100h
jmp start
myname db "Assembly 8086",0
col db 150
start:
;moving extrasegment to 0 location          
mov ax,0          
mov es,ax
;putting our interrupt      
mov es:[0x90*4], offset [interupt_printname]
mov es:[0x90*4+2],cs
   int 90h;<-- here is our interupt
ret
interupt_printname:
    push es
    push cs
    pop ds
    mov ax,0xb800
    mov es,ax;for video
    lea si,myname;loading string address in bx as requirement
     mov dl,150
     mov dh,10
    mov di,dx
printstr:
     cmp [si],0
     je print_completed
     mov bl,[si]
     mov bh,0202
     mov es:[di],bx
     add di,2;now we should move 2 byte forward
     inc si
     jmp printstr
print_completed:
     pop es
iret;using iret to return


Hooking divide by zero interrupt

; hooking divide by zero interrupt
;here I am using bp to get value from stack
org 100h
jmp start
message: db 'You divided by zero. Donot ever do that!'
printstr:
push bp
mov bp, sp
push es
push ax
push cx
push si
push di
push ds ; push segment of string
mov ax, [bp+4]
push ax ; push offset of string
mov cx, 28h ; save length in cx
mov ax, 0xb800
mov es, ax ; point es to video base
mov al, 80 ; load al with columns per row
mul byte [bp+8] ; multiply with y position
add ax, [bp+10] ; add x position
shl ax, 1 ; turn into byte offset
mov di,ax ; point di to required location
mov si, [bp+4] ; point si to string
mov ah, [bp+6] ; load attribute in ah
cld ; auto increment mode
nextchar: 
lodsb ; load next char in al
stosw ; print char/attribute pair
loop nextchar ; repeat for the whole string
exit: 
popa
ret 8
myisrfor0: 
pusha ; push all regs
push cs
pop ds ; point ds to our data segment
mov ax, 30
push ax ; push x position
mov ax, 10
push ax ; push y position
mov ax, 0x12 
push ax ; push attribute
mov ax, message
push ax ; push offset of message
call printstr ; print message
popa
iret ; return from interrupt

genint0: 
mov ax, 0xf00f ; load a big number in ax
mov bl, 0x3 ; use a very small divisor
div bl ; interrupt 0 will be generated automatically
ret   

start:
xor ax, ax
mov es, ax ; load zero in es
mov es:[0*4], offset myisrfor0 ; store offset at n*4
mov es:[0*4+2], cs ; store segment at n*4+2
call genint0 ; generate interrupt 0

mov ax, 4c00h ; terminate program
int 0x21



No comments:

Post a Comment