Babystep5 doesn't explain how keyboard interrupts work

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Xorda
Posts: 5
Joined: Thu Oct 12, 2017 6:21 am
Libera.chat IRC: Xorda

Babystep5 doesn't explain how keyboard interrupts work

Post by Xorda »

Hello,

I want to learn low-level programming, to program bootloaders and OS'es. I go through the wiki's tutorials and I'm now at Babystep 5
The problem is that it wasn't completely explained how that works. I don't want to copy I want to understand. Could anyone please explain that to me?

The following is the code snippet from Babysteb 5

Code: Select all

;==========================================
; nasmw boot.asm -f bin -o boot.bin
; partcopy boot.bin 0 200 -f0
 
[ORG 0x7c00]      ; add to offsets
   jmp start
 
   %include "print.inc"
 
start:   xor ax, ax   ; make it zero
   mov ds, ax   ; DS=0
   mov ss, ax   ; stack starts at 0
   mov sp, 0x9c00   ; 200h past code start
 
   mov ax, 0xb800   ; text video memory
   mov es, ax
 
   cli      ;no interruptions
   mov bx, 0x09   ;hardware interrupt #
   shl bx, 2   ;multiply by 4
   xor ax, ax
   mov gs, ax   ;start of memory
   mov [gs:bx], word keyhandler
   mov [gs:bx+2], ds ; segment
   sti
 
   jmp $      ; loop forever
 
keyhandler:
   in al, 0x60   ; get key data
   mov bl, al   ; save it
   mov byte [port60], al
 
   in al, 0x61   ; keybrd control
   mov ah, al
   or al, 0x80   ; disable bit 7
   out 0x61, al   ; send it back
   xchg ah, al   ; get original
   out 0x61, al   ; send that back
 
   mov al, 0x20   ; End of Interrupt
   out 0x20, al   ;
 
   and bl, 0x80   ; key released
   jnz done   ; don't repeat
 
   mov ax, [port60]
   mov  word [reg16], ax
   call printreg16
 
done:
   iret
 
port60   dw 0
 
   times 510-($-$$) db 0  ; fill sector w/ 0's
   dw 0xAA55        ; req'd by some BIOSes
;==========================================
Thanks for any help
Octocontrabass
Member
Member
Posts: 5586
Joined: Mon Mar 25, 2013 7:01 pm

Re: Babystep5 doesn't explain how keyboard interrupts work

Post by Octocontrabass »

Wow, I didn't realize we had such a bad tutorial on the wiki. Port 0x61 doesn't have anything to do with the keyboard in any PC less than 30 years old. You should delete the six lines that involve reading and writing port 0x61 from the code before running it.

If you want to learn more, I'd suggest starting with the wiki article for the keyboard controller. From that, you should be able to figure out what the tutorial code is doing.
Xorda
Posts: 5
Joined: Thu Oct 12, 2017 6:21 am
Libera.chat IRC: Xorda

Re: Babystep5 doesn't explain how keyboard interrupts work

Post by Xorda »

Thank you for notifying me about the mistakes in this tutorial. I was wondering about these 6 lines of code
Post Reply