Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. ; to assemble use the following command line:
  2. ; nasm.exe -f bin kbd.asm -o kbd.com
  3.  
  4. bits 16
  5. org 0x100
  6.  
  7. xor ax, ax
  8. mov es, ax
  9.  
  10. ; set our ISR
  11. cli
  12. push word [es:9*4+2]
  13. push word [es:9*4]
  14. mov word [es:9*4], kbdIRQ1
  15. mov [es:9*4+2],cs
  16. sti
  17.  
  18. call main
  19.  
  20. cli
  21. pop word [es:9*4]
  22. pop word [es:9*4+2]
  23. sti
  24.  
  25. ret
  26.  
  27. main:
  28. mov ah, 9
  29. mov dx, msg
  30. int 0x21
  31.  
  32. keylookup:
  33.  
  34. mov bx, kbdbuf
  35. xor cx, cx
  36. search:
  37. mov al, [bx] ; take key status
  38.  
  39. test al, al
  40. jnz found ; key was pressed
  41.  
  42. inc bx
  43. inc cx
  44. cmp cx, 0x32 ; search until key scan code 'M' is reached (last letter)
  45. jbe search
  46.  
  47. jmp keylookup
  48.  
  49. found:
  50. cmp cx, 1 ; Escape was pressed
  51. je finished
  52.  
  53. ; convert to corresponding ascii code using ascii table
  54. sub cx, 0x10
  55. mov bx, ascii
  56. add bx, cx
  57. mov al, [bx]
  58.  
  59. mov bx, letter
  60. mov [bx], al
  61.  
  62. mov dx, bx
  63. mov ah, 9
  64. int 0x21
  65.  
  66. ; convert to binary
  67. mov al, [bx]
  68. mov bx, bincode
  69. add bx, 8
  70. convert:
  71. dec bx
  72. mov cl, al
  73. and cl, 1
  74. add cl, 0x30
  75. mov [bx], cl
  76.  
  77. shr al, 1
  78. cmp bx, bincode
  79. jnz convert
  80.  
  81. mov dx, bx
  82. mov ah, 9
  83. int 0x21
  84.  
  85. ; reverse the binary code
  86. reverse:
  87. mov bl, [letter]
  88. xor al, al
  89. mov cl, 8
  90.  
  91. reverseloop:
  92. mov dl, bl
  93. and dl, 1
  94. shl al, 1
  95. or al, dl
  96. shr bl, 1
  97. dec cl
  98. jnz reverseloop
  99.  
  100. ; save the value in letter
  101. mov [letter], al
  102.  
  103. ; print the reversed binary
  104. mov bx, bincode
  105. add bx, 8
  106. convert2:
  107. dec bx
  108. mov cl, al
  109. and cl, 1
  110. add cl, 0x30
  111. mov [bx], cl
  112.  
  113. shr al, 1
  114. cmp bx, bincode
  115. jnz convert2
  116.  
  117. mov dx, bx
  118. mov ah, 9
  119. int 0x21
  120.  
  121. ; output the key to parallel port
  122. mov dx, letter
  123. mov ah, 9
  124. int 0x21
  125.  
  126. jmp keylookup
  127.  
  128. finished:
  129. ret
  130.  
  131. kbdIRQ1:
  132. pusha
  133.  
  134. ; read keyboard scan code
  135. in al, 0x60
  136.  
  137. ; bx = keyboard scan code
  138. xor bh, bh
  139. mov bl, al
  140. and bl, 0x7F
  141. shr al, 7
  142. xor al, 1 ; al = 1 if pressed, 0 if released
  143. mov [cs:bx+kbdbuf], al
  144.  
  145. ; send EOI to XT keyboard and PCI master
  146. in al, 0x61
  147. mov ah, al
  148. or al, 0x80
  149. out 0x61, al
  150. mov al, ah
  151. out 0x61, al
  152. mov al, 0x20
  153. out 0x20, al
  154.  
  155. popa
  156. iret
  157.  
  158. kbdbuf:
  159. times 128 db 0
  160.  
  161. msg db "Press any letter on your keyboard (press esc to exit)", 13, 10, "$"
  162. bin db " ", 13, 10, "$"
  163. letter db " ", 13, 10, "$"
  164. bincode db " ", 13, 10, "$"
  165. ascii db "qwertyuiop asdfghjkl zxcvbnm", "$"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement