### Author Topic: What does the keyboard send in port 0x60 in relation to dos characters  (Read 20891 times)

#### ThatGuy22

• Jr. Member
• Posts: 40
##### What does the keyboard send in port 0x60 in relation to dos characters
« on: December 13, 2010, 07:47:12 PM »
I wrote a program that prints out what the keyboard sends in port 0x60, but I have no idea how to translate these numbers to characters.

#### Frank Kotler

• NASM Developer
• Hero Member
• Posts: 2667
• Country:
##### Re: What does the keyboard send in port 0x60 in relation to dos characters
« Reply #1 on: December 13, 2010, 08:11:01 PM »
Lookup table. See if the attached helps.

Best,
Frank

#### ThatGuy22

• Jr. Member
• Posts: 40
##### Re: What does the keyboard send in port 0x60 in relation to dos characters
« Reply #2 on: December 13, 2010, 10:04:46 PM »
Ok so the numbers returned by port 0x60 correspond to key numbers on the keyboard, thanks
-But I get a different value when it is released how does that number correspond to the first number?
« Last Edit: December 13, 2010, 10:07:06 PM by ThatGuy22 »

#### Frank Kotler

• NASM Developer
• Hero Member
• Posts: 2667
• Country:
##### Re: What does the keyboard send in port 0x60 in relation to dos characters
« Reply #3 on: December 14, 2010, 12:08:42 AM »
High bit is set. There are a few keys you need to keep track of the up/down state - shift, alt, control... - most of the time you can ignore the release interrupt.

Best,
Frank

#### ThatGuy22

• Jr. Member
• Posts: 40
##### Re: What does the keyboard send in port 0x60 in relation to dos characters
« Reply #4 on: December 14, 2010, 03:46:32 AM »
When is the heigh bit set, when the key stroke is up, or down?
Also when you say heigh bit do you mean the left most bit such as 10000000 - binary value, 1 = set. In addition I am not entirely shure what the size of data is from port 0x60, I don't know if this is a variable length or it is always a certen length. Length being byte, word, dword, ext. Is the size always the same for all ports?
« Last Edit: December 14, 2010, 03:49:56 AM by ThatGuy22 »

#### Frank Kotler

• NASM Developer
• Hero Member
• Posts: 2667
• Country:
##### Re: What does the keyboard send in port 0x60 in relation to dos characters
« Reply #5 on: December 14, 2010, 02:11:57 PM »
Key-down gives you the basic scancode, key-up gives you the basic scancode "or"ed with 80h. Yeah, it's the leftmost bit.

Some ports may allow you to read/write more than a byte. I'm not familiar with "all" the ports. The instructions exist:

Code: [Select]
`in al, dxin ax, dxin eax, dx`
I don't think you'll find anything more than a byte useful for keyboard, RTC, timer, etc. that you'll "probably" be using... at first, at least. Consult RBIL's "ports.lst".

Best,
Frank

#### ThatGuy22

• Jr. Member
• Posts: 40
##### Re: What does the keyboard send in port 0x60 in relation to dos characters
« Reply #6 on: December 14, 2010, 02:41:39 PM »
So to get back to the original scan code you do this:
Code: [Select]
`in al, 0x60or al, 0x80` -So I understand, the left most bit tells you when the key is up or down and then once you get that you can get the key number?

Also how do you get the left most bit only, Ive seen other people do it except I still don't get how.
They also use and, or, xor sometimes - I fail to understand these completely, so if if you use one of them can you explain to me exactly what it does.

#### Frank Kotler

• NASM Developer
• Hero Member
• Posts: 2667
• Country:
##### Re: What does the keyboard send in port 0x60 in relation to dos characters
« Reply #7 on: December 14, 2010, 04:51:42 PM »
What you probably want is:

Code: [Select]
`in al, 60htest ah, 80hjz key_down; check for the few keys you care if they're up or down; else, ignorekey_down:; check for the few keys you care if they're up or down; if so, set a bit somewhere to keep track of 'em; if not, check for "modifiers" (is shift key down?); look it up; stuff it in keyboard buffer`
As you can see from that "int9.asm" I posted (originally from Randy Hyde's AoA, unless I'm mistaken) there's more to it than that - a number of "special cases" that need  to be checked - but that's the general idea.

The code you posted:

Code: [Select]
`in al, 0x60or al, 0x80`
Will convert a "down" scancode into an "up" scancode - probably not very useful.

If you need to know what the instructions do, Intel and AMD have nice manuals. There was an instruction set reference in old Nasm manuals (removed because we didn't have anyone to maintain it). It is obsolete, buggy, and doesn't have any information on flags. I "rescued" it, because it's in "Nasm syntax"... and because I'm "used to it".

http://home.myfairpoint.net/fbkotler/nasmdocr.html

Might be a good idea to learn the instructions before you attempt to write an OS with 'em.

Best,
Frank

#### ThatGuy22

• Jr. Member
• Posts: 40
##### Re: What does the keyboard send in port 0x60 in relation to dos characters
« Reply #8 on: December 14, 2010, 08:28:03 PM »
ok thanks, i'll take a look at more advanced instructions.
Ive been only been using mov, jmp, cmp.

Also if I were to take the high bit of the key up scan code and make it 0 would it give me the down scan code?
-If so this would mean that the reason I get a different character from dos is because, the high bit is modifying the number.
« Last Edit: December 14, 2010, 08:45:53 PM by ThatGuy22 »

#### Frank Kotler

• NASM Developer
• Hero Member
• Posts: 2667
• Country:
##### Re: What does the keyboard send in port 0x60 in relation to dos characters
« Reply #9 on: December 14, 2010, 10:35:34 PM »
Yeah, I guess.

Code: [Select]
`in al, 60hand al, 7Fh`
That's still only going to give you the scancode, though. The only scancode I know off the top of my head is from the escape key: 1. (they're just row/column of the key, I think). If you try to print a 1 in dos, it'll be a "smiley face", as I recall. What may serve you well at this point would be to display al as binary...

Code: [Select]
`;--------------al2bin:    push ax    push bx    push cx    mov bx, 7  ; "just in case" for int 10h    mov cx, 8  ; 8 bits to do.top:    rcl al, 1  ; high bit rotates to low bit - and into carry flag    push ax  ; save our "number"    mov al, '0'    adc al, 0  ; "add with carry" - make al '0' or '1'    mov ah, 0Eh    int 10h    pop ax    loop .top; do a newline? you may not want this...    mov al, 13    mov ah, 0Eh    int 10h    mov al, 10    mov ah, 0Eh    int 10h    pop cx    pop bx    pop ax    ret;---------------`
That's untested (as 16-bit code - modified from a routine that worked in Linux). If it broke, you can fix it...

Best,
Frank

#### ThatGuy22

• Jr. Member
• Posts: 40
##### Re: What does the keyboard send in port 0x60 in relation to dos characters
« Reply #10 on: December 15, 2010, 01:18:55 AM »
ok thanks.