Character-driven first step-a small attempt

Source: Internet
Author: User

I want to write a fully functional character driver, but I still have to consider it. Record your initial attempt to drive the character

In the absence of an operating system, you need to do the following to make the system respond to the keyboard:

1. Writing a keyboard interrupt program: When you press the key, the program will enter

2. Initialization of 8259a: When you press the key, the 8259a hardware is interrupted, allowing the CPU to enter the keyboard interrupt program

3. In the interrupt vector table, set the keyboard interrupt program Vector

4. Open the interrupt

There is also a complicated problem, how to match the key code with the ASCII code. My approach is to first write a program to display the key code one by one ..

Let's not talk much about it. Next we will introduce our own programs:

1. Compiled in NASM Assembly Language

2. The program loads the startup code of the first sector for the BIOS. Starting from a Virtual Machine (VMware) with a floppy disk

The source code is as follows:

Null equ 0org 7c00hjmp startkeyboard_int: Push BX in Al, 60 h; read the Keyboard Buffer xor bx, BX mov BL, Al Add Bx, v_keymap mov Al, [BX] CMP Al, 0 JZ end mov ah, 0eh mov BX, 07 h int 10 h CMP Al, 13 JZ p_cl JMP end p_cl: mov Al, 10; if it is a carriage return, print the line feed mov ah, 0eh mov BX, 07 h int 10 h end: mov Al, 20 h; OCW: 20 h send interrupt terminator to 8259a out 20 h, Al pop BX iret ;-----------------------------------------; subroutine name: print_kcode; function: Print value in hexadecimal ASCII format; input: Register ax; output: None
Note: This code is used to display the key code; ------------------------------------- print_16b: Push DX mov DL, Al mov Al, Ah call print_8b mov Al, DL call print_8b pop DX retprint_8b: Push DX mov DH, al SHR Al, 4 call print mov Al, DH and Al, 0fh call print pop DX retprint: Push bx cmp Al, 9 ja p_a_f add Al, '0' JMP p_ OK p_a_f: sub Al, 0ah add Al, 'A' p_ OK: mov ah, 0eh mov BX, 07 h int 10 h pop BX RET
; Interrupt start: mov ax, CS mov ds, ax mov SS, ax mov sp, 0 mov ax, 0; set the interrupt vector mov es of the keyboard interrupt program, ax mov word [ES: 84 h], keyboard_int mov [ES: 86 H], DS mov Al, 11 h; Set 8259a main chip out 20 h, Al mov Al, 20 h out 21 h, Al mov Al, 04 H out 21 h, Al mov Al, 01 H out 21 h, Al mov Al, 11 h; Set 8259a slave disk out 0a0h, al mov Al, 28 h out 0a1h, Al mov Al, 02 h out 0a1h, Al mov Al, 01 H out 0a1h, Al mov Al, 0fdh; enable the keyboard to interrupt out 21 H, al sti jmp $ v_keymap: DB null, null, '2017-= ', 8, null; 00 -- 0f dB 'qwertyuiop []', 0x0d, null, 'as '; 10 -- 1f dB 'dfghjkl; ', 0x27, ''', null,' \ zxcv '; 2f -- 2f dB 'bnm ,. /', null, '', null; 30 -- 3f dB null, null, null, null, null; 40 -- 4f dB null, null, null, null; 50 -- 5f dB null, null, null, null; 60 -- 6f dB null, null, null; 70 -- 7f dB null, null, null; 80 -- 8f dB null, null; 90 -- 9f dB null, null; a0 -- Af DB null, null; b0 -- BF dB null, null; c0 -- CF dB null, null; d0 -- DF dB null, null; e0 -- EF dB null, null; f0 -- FF times 510-($-$) db 0 DW 0xaa55; ----------------------------------------------; U.S. keyboard character layout:; char keymap [256] =; {null, ESC, 3, 4, 5, 6, 7, 8, 9, 0,-, =, return, \ t,; q, W, E, R, T, Y, U, I, O, P, [,], press enter, left Ctrl, A, S,; D, F, G, H, J, K, L,;, ', null, left shift ,\, z, X, C, V,; B, n, m ,,,., /, right shift, null, left ALT, space, caps, F1, F2, F3, F4, F5,; F6, F7, F8, F9, F10, null ,...;};; char v_keymap [256] =; {null, null,-, =, null, null,; q, W, E, R, t, Y, U, I, O, P, [,], null, null, A, S,; D, F, G, H, J, K, l,;, ', null, null, \, Z, X, C, V,; B, n, m ,,,., /, null, space, null, null ,...;};; ----------------------------------------------

The program name is key. S.

Compile: NASM-F bin key. S-O key. img

In VMware, select key. IMG as the virtual floppy disk image and start it with a floppy disk to run the program.

Note: currently, the program can only respond to some ASCII code buttons.

This program uses BIOS interruption and works in real mode. If you use protection mode, you cannot use BIOS interrupt programs. You need to write your own interrupt programs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.