Use Int 16h to Read Into Buffer Assembly Language

Wang Shuang 《 assembly language 》 The Fourth Edition Super notes

  • Wang Shuang 《 associates language 》 The Fourth Edition Super notes
    • The outset 17 Chapter Use BIOS Keyboard input and disk reading and writing
      • 17.1 int ix Interrupt routine processing of keyboard input
      • 17.2 Utilize int 16h Interrupt routine reads keyboard buffer
      • 17.3 Input of cord
      • 17.four awarding int 13h The interrupt routine reads and writes to the disk

The first 17 Affiliate Employ BIOS Keyboard input and deejay reading and writing

Most useful programs need to handle user input , Keyboard input is the most bones input . Programs and information ordinarily require long-term storage , Deejay is the most mutual storage device .

BIOS For these two kinds of peripherals I/O Provides the most basic interrupt routines , In this chapter, we discuss their applications and related bug .

17.i int 9 Interrupt routine processing of keyboard input

Keyboard input will trigger 9 Interrupt number ,BIOS Provides int ix Interrupt routines .CPU stay ix After the pause of No , perform int 9 Interrupt routines , from 60h The port reads the scan code , And plow information technology into the corresponding ASCII Code or status information , Stored in the specified space of memory ( Keyboard buffer or status byte ) in .

General keyboard input , stay CPU After execution int 9 Later interrupt routine , It'south all in the keyboard buffer . There are... In the keyboard buffer 16 Word unit , Can be stored 15 The browse code of each key and the corresponding ASCII code .

Let'south follow the logical structure of the keyboard buffer , Have a await at the scan lawmaking entered past the keyboard and the corresponding ASCII How the lawmaking is written to the keyboard buffer .

Be careful : In our class , Only on the basis of logical construction , Hash out BIOS Keyboard buffer read and write problems . In fact, the keyboard buffer is a memory area managed by a ring queue structure , All the same, we volition not discuss the implementation of queues and ring queues , Because it's some other professional person course 《 data structure 》 The content of .

Let's go through the post-obit keys :

A、B、C、D、E、Shift_A、A

The input process , Take a brief look int ix Interrupt routine on the keyboard input processing method .

(one) In the initial state , No keyboard input , The keyboard buffer is empty , There are no elements at this point .

image
(2) Press down A fundamental , Trigger keyboard interrupt ;CPU perform int 9 Interrupt routines , from 60h Port readout A Key laissez passer lawmaking ; And then find the condition byte , Come across if there is Shift、Ctrl Wait for the switch primal to printing ; Found no toggle key pressed , Will A Cardinal scan code leh And corresponding ASCII code , I.e. letters "a" Of ASCII code 61h, Write to keyboard buffer . In the word unit of measurement of the buffer , The high byte stores the scan code , Depression byte storage ASCII code . At this betoken, the contents of the buffer are every bit follows .

image
(iii) Printing down B primal , Trigger keyboard interrupt ;CPU perform int 9 Interrupt routines , from 60h Port readout B Key pass code ; And then discover the status byte , See if there'due south a toggle key pressed ; Institute no toggle key pressed , take B Cardinal browse lawmaking 30h And corresponding ASCII code , I.e. letters "b" Of ASCII code 62h, Write to keyboard buffer . At this point, the contents of the buffer are as follows .

image
(4) Press down C、D、Eastward Post key , The contents of the buffer are as follows .

image
(5) Printing left Shift key , Trigger keyboard interrupt ;int ix Interrupt routine receives left Shift Key pass code , Set upwards 0040:17 Of the status byte at 1 Position equally 1, To the left Shift Key pressed .

(six) Printing downwards A key , Trigger keyboard interrupt ;CPU perform int ix Interrupt routines , from 60h Port readout A Key laissez passer code ; Detect status bytes , Come across if there's a toggle key pressed ; Institute left Shift The key is pressed , Volition A Key scan code 1Eh and Shift_A Corresponding ASCII code , I.eastward. letters "A" Of ASCII lawmaking 41h, Write to keyboard buffer . At this point, the contents of the buffer are as follows .

image
(7) Release the left Shift cardinal , Trigger keyboard interrupt ;int 9 Interrupt routine receives left Shift Key break , Set up 0040:17 Of the status byte at 1 Position every bit 0, To the left Shift The key is released .

(8) Press down A key , Trigger keyboard interrupt ;CPU perform int 9 Interrupt routines , from 60h Port readout A Cardinal laissez passer code ; Then notice the status byte , Run across if there'southward a toggle key pressed ; Found no toggle key pressed , Volition A Key scan code 1Eh and A Corresponding ASCII lawmaking , I.e. messages "a" Of ASCII lawmaking 61h, Write to keyboard buffer . At this indicate, the contents of the buffer are as follows .

image

17.2 Use int 16h Interrupt routine reads keyboard buffer

BIOS Provides int 16h Interrupt routines are called by programmers .int 16h One of the nigh important functions included in interrupt routines is to read a keyboard input from the keyboard buffer , The number of this part is 0.

The following command reads a keyboard input from the keyboard buffer , And remove information technology from the buffer :

mov ah,0
int 16h

result :(ah)= Scan code ,(al)=ASCII code .

Let's proceed the keyboard input procedure in the previous section , to glance at int 16h How to read keyboard buffer .

(1) perform

mov ah,0
int 16h

after , The contents of the buffer are as follows .

image
ah is 1Eh,al is 61h.

(2) perform

mov ah,0
int 16h

after , The contents of the buffer are as follows .

image
ah is 30h,al is 62h.

(3) perform

mov ah,0
int 16h

after , The contents of the buffer are as follows .

image
ah is 2Eh,al is 63h.

(4) perform 4 Time

mov ah,0
int 16h

after , Buffer empty .

image
ah is lEh,al is 61h.

(5) perform

mov ah,0
int 16h

int 16h The interrupt routine detects the keyboard buffer , If the buffer is plant to be empty, the loop waits , Until there'south information in the buffer .

(half dozen) Press down A Postal service key , The contents of the buffer are every bit follows .

image
(vii) Cycle waiting int 16h The interrupt routine detected data in the keyboard buffer , Read it out of the buffer over again Information technology'southward empty .

image
ah is 1Eh,al is 61h.

We tin see from the top ,int 16h Interrupt routine 0 Role No , Do the following work .

(1) Check whether there is data in the keyboard buffer ;
(2) If not, continue to do the second ane Footstep ;
(3) Read the keyboard input in the first word unit of measurement of the buffer ;
(4) Send the read browse lawmaking to ah,ASCII Code in al;
(v) Delete the read keyboard input from the buffer .

so ,BIOS Of int 9 Interrupt routines and int 16h Interrupt routines are a pair of programs that work together ,int ix The interrupt routine writes to the keyboard buffer ,int 16h The interrupt routine reads from the buffer .

They are written and read at dissimilar times ,int nine Interrupt routine is to write data to the keyboard buffer when a key is pressed ; and int 16h The interrupt routine is when the application calls it , Read data out of the keyboard buffer .

When nosotros write a full general program to deal with keyboard input , You lot can call int 16h Read the keyboard input from the keyboard buffer .

Programming , Receiving keyboard input from the user , Input "r", Set the characters on the screen to ruddy ; Input "g", Set the characters on the screen to greenish ; Input "b", Set the characters on the screen to blue .

The process is equally follows , The procedure at the drawing line is more skilled , Please analyze by yourself .

          assume cs:code  code segment  start:  mov ah, 0         int 16h          mov ah,1         cmp al,'r'         je red         cmp al,'yard'         je light-green         cmp al,'b'         je blue         jmp short sret  red: shl ah,ane  green: shl ah,i  blue:   mov bx,0b800h         mov es,bx         mov bx,1         mov cx,2000 due south :     and byte ptr es:[bx],11111000b         or es:[bx],ah         add bx,2         loop s  sret:   mov ax,4c00h         int 21h  code ends  end start                  

17.3 Input of string

Users usually enter not only a single character only a string through the keyboard . Let's discuss the problems and simple solutions in cord input .

The most bones string input program , You need to have the post-obit functions .

(one) You need to display this string at the same time of input ;
(2) Generally, later entering the carriage return , End of string input ;
(three) Tin delete the entered characters .

For this iii Features , We tin imagine in DOS in , When entering the command line .

Write a subroutine to receive string input , Achieve the to a higher place 3 Basic functions . Because you need to brandish , The parameters of the subroutine are every bit follows :

  • (dh)、(dl)= The line of the string displayed on the screen 、 Column position ;
  • ds:si Betoken to the storage space of the cord , String to 0 For the ending .

Let's clarify .

(i) Character input and deletion .

Each new input grapheme is stored later the previous input graphic symbol , And the deletion is from the last grapheme , Let's look at the following process .

An empty string :

Input "a" : a
Input "b" : ab
Input "c" : abc
Input "d" : abed
Delete a character :abc
Delete a character :ab
Delete a graphic symbol :a
Delete a grapheme :

You tin see that in the process of string input , The input and output of characters follow the admission rules of the stack , Last in, first out . such , We can use the stack to manage the string storage space , In other words, the storage space of a cord is actually a character stack . All characters in the graphic symbol stack , From the lesser of the stack to the superlative of the stack , Make a cord .

(2) After entering the wagon return , End of string input .

Afterward entering the railroad vehicle render , You lot tin add... To the string 0, Indicates the cease of the string .

(three) You need to display this string at the same fourth dimension of input .

Every time a new character is entered and a grapheme is deleted , Should redisplay the string , That is, from the bottom to the top of the character stack , Show all the characters .

(4) The processing of the program .

Now we tin can simply determine that the process of the program is every bit follows .

ane、 telephone call int 16h Read keyboard input ;
2、 If information technology's a character , Enter the graphic symbol stack , Display all characters in the graphic symbol stack ; Deport on 1;
three、 If it's backspace , Popular upwardly a graphic symbol from the character stack , Display all characters in the character stack ; Bear on i;
four、 If information technology is Enter central , Press... Into the character stack 0, return .

It tin can exist seen from the processing of the program , The stack of characters 、 Out of the stack and brandish the contents of the stack , It'southward a function that needs to be used in many places , We should write them every bit subroutines .

Subroutines : The stack of characters 、 Out of stack and display .

Parameter clarification :

(ah)= Function number ,0 Ways put on the stack ,1 Show the stack ,2 Presentation display ;
ds:si Point to the character stack infinite ;
about 0 Function No :(al)= Stack character ;
nearly ane Function No :(al)= Characters returned ;
almost 2 Function No :(dh)、(dl)= The line of the string displayed on the screen 、 Column position .

image
image
In the subroutine above , The access rules of the graphic symbol stack are as follows .

image

Some other problem we should pay attending to is , When displaying the characters in the stack , Pay attention to clear the last display on the screen .

We now write a complete subroutine to receive string input , Equally shown below .

          getstr:push ax  getstrs:mov ah,0         int 16h         cmp al,20h         jb nochar       ;ASCII Code less than 20h, The description is not a character          mov ah,0         phone call charstack      ; Graphic symbol stack          mov ah,ii         telephone call charstack      ; Displays the characters in the stack          jmp getstrs  nochar: cmp ah, 0eh      ; Backspace key scan code          je backspace         cmp ah,1ch      ;Enter Key browse code          je enter         jmp getstrs  backspace:mov ah,one         call charstack      ; Characters out of the stack          mov ah,2         call charstack      ; Displays the characters in the stack          jmp getstrs  enter:  mov al,0         mov ah,0         call charstack      ;0 Push button          mov ah,2         telephone call charstack      ; Displays the characters in the stack          pop ax         ret                  

17.4 awarding int 13h The interrupt routine reads and writes to the deejay

We mainly use iii.5 Inch floppy disk, for example , Explain .

3.5 Inch floppy deejay is divided into two sides , Each side has 80 Tracks , Each track is further divided into 18 Sectors , The size of each sector is 512 Bytes .

be :ii Noodles x80 Magnetic track x18 A sector x512 byte =1440KB≈1.44MB

The actual access to the disk is washed by the deejay controller , We tin access the disk by controlling the disk controller . The disk can merely exist read and written in sectors . When reading and writing sectors , To give a face up number 、 Runway number and sector number . Face number and track number from 0 First , And the sector lawmaking comes from 1 Get-go .

If we access the disk by directly controlling the deejay controller , A lot of hardware details need to be involved .BIOS Provides interrupt routines for reading and writing sectors , These interrupt routines practise a lot of complex hardware related work . We can do it by calling BIOS Interrupt routine to admission disk .

BIOS The interrupt routine provided to admission the disk is int 13h. Read 0 Noodles 0 Avenue one The contents of the sector go to 0:200 The procedure is as follows .

mov ax,0
mov es,ax
mov bx,200h

mov al,1
mov ch,0
mov cl,1
mov dl,0
mov dh,0
mov ah,ii
int 13h

Entrance parameters :

(ah)=int 13h The function number of (2 Read sector )
(al)= The number of sectors read
(ch)= Rails number
(cl)= Fan area code
(dh)= Head number ( For floppy deejay, i.eastward. face up number , Because one side uses a magnetic head to read and write )
(dl)= Drive alphabetic character
Floppy drive from 0 Start ,0: floppy disk bulldoze A, 1: floppy disk drive B;
Difficult deejay from 80h Offset ,80h: Hard disk C, 81h: Hd D
es:bx Points to an area of retention that receives data read from a sector

Returns the parameter :

Successful operation :(ah)=0,(al)= Number of sectors read in
operation failed :(ah)= Error code

take 0:200 Content writing in 0 Noodles 0 Artery 1 A sector .

mov ax,0
mov es,ax
mov bx,200h

mov al,1
mov ch, 0
mov cl,1
mov dl,0
mov dh,0

mov ah,3
int 13h

Entrance parameters :

(ah)=int 13h The function number of (3 Represents a write sector )
(al)= The number of sectors written
(ch)= Track number
(cl)= Fan surface area code
(dh)= Head number ( Noodles )
(dl)= Drive letter
Floppy bulldoze from 0 Start ,0: floppy disk drive A, ane: floppy disk drive B;
Hd from 80h Start ,80h: Hard disk drive C, 81h: Hard disk D
es:bx Point to the data that will be written to deejay

Returns the parameter :

Successful functioning :(ah)=0,(al)= The number of sectors written
operation failed :(ah)= Error code

Be careful , Now we're going to utilize int 13h Interrupt routine reads and writes floppy disk . Writing information directly to a deejay sector is dangerous , It's likely to cover of import data . If you transport to the floppy deejay 0 Noodles 0 Avenue 1 Information is written into the sector , To make the floppy disk bachelor under the existing operating system , It has to be reformatted . Before writing the relevant program , You have to detect a free floppy disk . In the use of int 13h When you interrupt a routine, you must pay attending to whether the bulldoze letter is correct , Never write to a sector in the hard disk drive .

Programming : Relieve the contents of the current screen on disk .

analysis :one The content of the screen accounts for 4000 Bytes , need 8 Sectors , use 0 Noodles 0 Dao 1~8 Sectors store the contents of video memory . The procedure is as follows .

          assume cs:code  code segment  commencement:  mov ax,0b800h         mov es,ax         mov bx,0          mov al,8         mov ch,0         mov cl,1         mov dl,0         mov dh,0         mov ah,3         int 13h          mov ax,4c00h         int 21h  code ends  cease start                  

版权声明
本文为[jpSpaceX]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211015191725812T.html

Use Int 16h to Read Into Buffer Assembly Language

Source: https://cdmana.com/2021/10/20211031002244295t.html

Related Posts

0 Response to "Use Int 16h to Read Into Buffer Assembly Language"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel