Keyboard input and disk read and write using the BIOS (learning assembly)

Source: Internet
Author: User

1. Keyboard input will cause number 9th interrupts, and the BIOS provides an int 9 interrupt routine. After the 9th interrupt occurs, the CPU executes an int 9 interrupt routine, reads the scan code from the 60h port and translates it into the corresponding ASCII code or status information, stored in the specified space in memory (keyboard buffer or state byte). The general keyboard input is placed in the keyboard buffer after the CPU finishes executing the INT 9 interrupt routine. The keyboard buffer has 16 characters and can store 15 key scan codes and corresponding ASCII codes.

2. Below, we pass several keys:

A, B, C, D, E, Shift_a, a

Input process, briefly take a look at the Int 9 interrupt routine for the keyboard input processing method:

(1) In the initial state, there is no keyboard input, the keyboard buffer is empty, there is no element at this time:

 

 

 

 

 

 

/td>

 

 

 

 

& nbsp;

 

 

 

 

 

(2) Press the A key to trigger a keyboard interrupt: The CPU performs an int 9 interrupt routine, reads the pass code of the A key from the 60h port, and then detects the status byte to see if there is a shift key pressed or not, and finds that there is no toggle key pressed, then the scan code of the A key is 1eh and the corresponding ASCII code The ASCII code 61h of the letter "A" is written to the keyboard buffer. In the buffer of the word cell, the high byte stores the scan code, and the low byte stores the ASCII code. The contents of this buffer are as follows:

1E61

 

 

 

 

 

 

 

 

& nbsp;

 

 

 

 

 

(3) Press the B key to trigger a keyboard interrupt: The CPU performs an int 9 interrupt routine, reads the pass code of the B key from the 60h port, and then detects the status byte to see if there is a toggle key pressed, and the scan code 30h and the corresponding ASCII code for the B key are found without a toggle key, i.e. the letter "B" The ASCII code 62H, written to the keyboard buffer. The contents of this buffer are as follows:

1E61

3062

(4) After pressing the C, D, e keys, the contents of the buffer are as follows:

1E61

2062

2E63

2064

1265

(5) Press the left SHIFT key to trigger the keyboard interrupt: The INT 9 interrupt routine receives the pass code of the left SHIFT key, and the 1th bit of the state byte at 0040:17 is 1, which means that the left SHIFT key is pressed.

(6) Press the A key to trigger a keyboard interrupt: The CPU performs an int 9 interrupt routine, reads the pass code of a key from the 60h port, detects the status byte, checks if there is a toggle key pressed, and finds that the left SHIFT key is pressed, then the scan code for the A key is 1Eh and shift_a corresponding to the ASCCII code, i.e. The ASCII code 41 of "A" is written to the keyboard buffer, at which point the contents of the buffer are as follows:

1E61

2062

2E63

2064

1265

1E41

(7) Release the left SHIFT key, trigger the keyboard interrupt: Int 9 interrupt routine receives the left SHIFT key of the code breaker, set 0040:17 of the status byte of the 1th bit is 0, the left SHIFT key is released.

(8) Press the A key to trigger a keyboard interrupt: The CPU performs an int 9 interrupt routine, reads the pass code of the A key from the 60h port, and then detects the status byte to see if there is a toggle key pressed, and then finds that there is no toggle key pressed, then the scan code for a key 1Eh and a corresponds to the ASCCII code ASCII code 61h, written to the keyboard buffer, when the contents of the buffer are as follows:

1E61

2062

2E63

2064

1265

1E41

1E61

One of the most important features included in the 3.int 16h interrupt routine is reading a keyboard input from the keyboard buffer, which is numbered 0. The following instruction reads a keyboard input from the keyboard buffer and removes it from the buffer:

MOV ah,0

int 16h

Result: (AH) = Scan code, (AL) =ascii code.

int 16h Interrupt Routine No. 0 function, do the following work:

1) Detect if there is data in the keyboard buffer;

2) do not continue to do the 1th step;

3) Read the keyboard input in the first word cell of the buffer;

4) The scanned code is fed into the AH,ASCII code into AL;

5) Remove the read keyboard input from the buffer.

Visible, the BIOS of the INT 9 interrupt routine and the int 16h interrupt routine is a pair of mutually mated programs, an int 9 interrupt routine is written to the keyboard buffer, and an int 16h interrupt routine is read out of the buffer. When they are written and read out differently, the INT 9 interrupt routine writes data to the keyboard buffer when the key is pressed, while the int 16h interrupt routine reads the data from the keyboard buffer when the application calls it.

4. Attach a program that implements the input ' r ', sets the character on the screen to red, enter ' G ', set the character on the screen to green, enter ' B ', and set the character on the screen to blue.

1AssumeCS:Code2 Code Segment3 4 Start:  movAh05         int16h6         movAh1     ;1 Properties for Blue bytes7         CMPAl'R'8         JERed9         CMPAl'g'Ten         JEGreen One         CMPAl'b' A         JEBlue -         jmpShort Sret -  the Red:    SHLAh1  ;Execute Red:green: Move left 2 times to ... - -  - Green:    SHLAh1  ;... 010 -  + Blue:    movbx,0b800h;... 001 -         movES,BX +         movCx - A  at S:        Addbyte ptres:[bx],11111000b -         or es:[Bx],ah -         AddBx2 - Loop S -         - Sret:    movax,4c00h in         int21h -  to         + code ends - End Start the  *  

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.