mouseDataStatus db 0
mouseDataX db 0
mouseDataY db 0
irq12: PUSHAD
PUSH DS, ES
XOR eax, eax
MOV DS, ax
MOV ES, ax
IN al, 0x64
.checkData: TEST al, 0x1 ;1 <data available?>
JZ .iexit
TEST al, 0x20 ;Test bit 5 <mouse data?>
JNZ .mouseDataAvailable
IN al, 0x60 ;Removing unexpected keyboard data
JMP short .checkData
.mouseDataAvailable: ;Get the 3 byte packet
IN al, 0x60
MOV byte[CS:mouseDataStatus], al
CALL waitForRead
IN al, 0x60
MOV byte[CS:mouseDataX], al
CALL waitForRead
IN al, 0x60
MOV byte[CS:mouseDataY], al
;Packet Ready
.calcX: MOV dx, word[0x7108] ;Check X data negative
TEST byte[CS:mouseDataStatus], 0x10
JZ .posX ;X is positive
NEG byte[CS:mouseDataX]
INC byte[CS:mouseDataX]
MOVZX cx, byte[CS:mouseDataX]
SUB dx, cx ;Convert Negative X data to positive and substract it
PUSH dx
JMP short .calcY
.posX: MOVZX cx, byte[CS:mouseDataX] ;Add positive X data
ADD dx, cx
PUSH dx
.calcY: MOV dx, word[0x710A] ;Check Y data negative
TEST byte[CS:mouseDataStatus], 0x20
JZ .posY ;Y is positive
NEG byte[CS:mouseDataY]
INC byte[CS:mouseDataY]
MOVZX cx, byte[CS:mouseDataY]
SUB dx, cx ;Convert Negative Y data to positive and substract it
PUSH dx
JMP short .endCalc
.posY: MOVZX cx, byte[CS:mouseDataY] ;Add positive Y data
ADD dx, cx
PUSH dx
.endCalc: CALL UnDisplayCursor ;Remove cursor from old position
CALL DisplayCursor ;Set cursor to the new position (X and Y are on the top of the stack)
ADD sp, 4
.iexit: POP DS, ES
POPAD
IRET ;Exit Interrupt