Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; to assemble use the following command line:
- ; nasm.exe -f bin kbd.asm -o kbd.com
- bits 16
- org 0x100
- xor ax, ax
- mov es, ax
- ; set our ISR
- cli
- push word [es:9*4+2]
- push word [es:9*4]
- mov word [es:9*4], kbdIRQ1
- mov [es:9*4+2],cs
- sti
- call main
- cli
- pop word [es:9*4]
- pop word [es:9*4+2]
- sti
- ret
- main:
- mov ah, 9
- mov dx, msg
- int 0x21
- keylookup:
- mov bx, kbdbuf
- xor cx, cx
- search:
- mov al, [bx] ; take key status
- test al, al
- jnz found ; key was pressed
- inc bx
- inc cx
- cmp cx, 0x32 ; search until key scan code 'M' is reached (last letter)
- jbe search
- jmp keylookup
- found:
- cmp cx, 1 ; Escape was pressed
- je finished
- ; convert to corresponding ascii code using ascii table
- sub cx, 0x10
- mov bx, ascii
- add bx, cx
- mov al, [bx]
- mov bx, letter
- mov [bx], al
- mov dx, bx
- mov ah, 9
- int 0x21
- ; convert to binary
- mov al, [bx]
- mov bx, bincode
- add bx, 8
- convert:
- dec bx
- mov cl, al
- and cl, 1
- add cl, 0x30
- mov [bx], cl
- shr al, 1
- cmp bx, bincode
- jnz convert
- mov dx, bx
- mov ah, 9
- int 0x21
- ; reverse the binary code
- reverse:
- mov bl, [letter]
- xor al, al
- mov cl, 8
- reverseloop:
- mov dl, bl
- and dl, 1
- shl al, 1
- or al, dl
- shr bl, 1
- dec cl
- jnz reverseloop
- ; save the value in letter
- mov [letter], al
- ; print the reversed binary
- mov bx, bincode
- add bx, 8
- convert2:
- dec bx
- mov cl, al
- and cl, 1
- add cl, 0x30
- mov [bx], cl
- shr al, 1
- cmp bx, bincode
- jnz convert2
- mov dx, bx
- mov ah, 9
- int 0x21
- ; output the key to parallel port
- mov dx, letter
- mov ah, 9
- int 0x21
- jmp keylookup
- finished:
- ret
- kbdIRQ1:
- pusha
- ; read keyboard scan code
- in al, 0x60
- ; bx = keyboard scan code
- xor bh, bh
- mov bl, al
- and bl, 0x7F
- shr al, 7
- xor al, 1 ; al = 1 if pressed, 0 if released
- mov [cs:bx+kbdbuf], al
- ; send EOI to XT keyboard and PCI master
- in al, 0x61
- mov ah, al
- or al, 0x80
- out 0x61, al
- mov al, ah
- out 0x61, al
- mov al, 0x20
- out 0x20, al
- popa
- iret
- kbdbuf:
- times 128 db 0
- msg db "Press any letter on your keyboard (press esc to exit)", 13, 10, "$"
- bin db " ", 13, 10, "$"
- letter db " ", 13, 10, "$"
- bincode db " ", 13, 10, "$"
- ascii db "qwertyuiop asdfghjkl zxcvbnm", "$"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement