Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .model small
- .stack 100h
- .data
- old9 dd ?
- hello_msg db 'hello$'
- what_msg db 'what$'
- .code
- start:
- mov ax, @data
- mov ds, ax
- mov es, ax
- jmp setup
- KeyboardInterruptHandler proc
- push ax
- push bx
- push cx
- push dx
- push ds
- push es
- mov ax, @data ; DS = @data segment
- mov ds, ax
- mov ax, 40h ; ES = 40h (BIOS Data Areas BDA)
- mov es, ax ; See https://stanislavs.org/helppc/bios_data_area.html
- mov bx, es:[1ch] ; Keyboard buffer tail ptr at 40h:1ch (41ch)
- pushf ; Push flags on stack because old interrupt will end with IRET
- call [old9] ; Call the old keyboard interrupt handler
- test byte ptr es:[17h], 8h
- ; Test Bit 3 of Keyboard flag (byte 1) in DOS BDA at 40h:17h
- ; See https://stanislavs.org/helppc/kb_flags.html
- jnz print_hello ; If ALT is pressed print hello
- cmp bx, es:[1ch] ; Did the keyboard tail ptr change? If not no key to process
- je end_handler
- ; Get the Scan code directly from the keyboard buffer
- ; ASCII value at [bx] and SCAN code at [bx+1]
- mov al, es:[bx+1]
- cmp al, 39h ; If SPACE is pressed print what
- je print_what
- jmp end_handler ; Otherwise exit
- ; Beware!! DOS is not re-entrant so calling int 21h functions from
- ; inside an interrupt handler can fail!
- print_hello:
- mov dx, offset hello_msg
- jmp print_message
- print_what:
- mov dx, offset what_msg
- print_message:
- mov ah, 9
- int 21h
- end_handler:
- ; The original keyboard handler will have sent EOI to PICs
- ; meaning we don't need to send 20h to port 20h
- pop es
- pop ds
- pop dx
- pop cx
- pop bx
- pop ax
- iret
- KeyboardInterruptHandler endp
- setup:
- cli
- mov ax, 0h
- mov es, ax
- mov bx, es:[9 * 4]
- mov word ptr old9, bx
- mov bx, es:[9 * 4 + 2]
- mov word ptr old9 + 2, bx
- mov word ptr es:[9 * 4], offset KeyboardInterruptHandler
- mov es:[9 * 4 + 2], cs
- sti
- mov ah, 1
- int 21h
- cli
- lds dx, dword ptr old9
- mov ax, 2509h
- int 21h
- sti
- mov ax, 4c00h
- int 21h
- end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement