当前位置:网站首页>Assembly language interrupt and external device operation --06

Assembly language interrupt and external device operation --06

2022-06-23 14:02:00 Big flicker love flicker

Assembly language interrupt and external device operation --06


This series of articles refers to the fourth edition of assembly language and Assembly language programming Keynote speaker: helijian Sort out


Shift instructions

 Insert picture description here


Example : Logical shift instruction shl and shr

 Insert picture description here


Operating video memory data

Principle of display

 Insert picture description here


Shows the structure of the buffer

 Insert picture description here


A way of displaying information “ direct ” The way

 Insert picture description here


Describes the label of the memory unit

About label

 Insert picture description here
The above writing needs to use offset To indicate the address of the data in the code segment


Data label with colon removed

 Insert picture description here
a[si]---->[si+a]


The data label describes both the memory address and the unit length

 Insert picture description here
mov al,b It's because b The memory unit of the operation is the word size , and al It's the byte size , Do not conform to the


The more common way : The data label in the data segment

All of the above put the data in the code segment , But generally, each section is stored separately .

 Insert picture description here
If the data label is defined as data , The corresponding memory address is the memory address pointed to by the data label

a db 123
b dw 0
#c The label points to the memory unit , Saved two words of data , One is a The memory offset address pointed to by the label , The other is b The memory offset address pointed to by the label 
# here c A pointer that can be regarded as a pointer 
c dw a,b
a db 123
c dw 0
#c Indicates that it points to a memory unit , Two double word data are saved , For each doubleword data , The previous one saves the corresponding offset address , The latter one saves the corresponding segment address 
c dd a,b

When we define data labels as data , Is it particularly similar C A pointer to a pointer in a language


Direct address table of data

Direct address table : Solve problems by looking up tables

 Insert picture description here


The simplest solution

 Insert picture description here

00101011  Is stored as a low byte in al Medium , The front and back four of them are respectively composed of 0-15 A number in , Then go to the character table to locate the character at the corresponding position 
  • Because the minimum operating unit is bytes , So will ah preservation al The senior four of , Complete by shifting four bits to the right , and al Save the lower four digits , Through an and operation

Direct address table

 Insert picture description here


Application example : In order to speed up the operation, we use the look-up table method

 Insert picture description here


Solution

 Insert picture description here

  • First, use a table to record the corresponding sin value , The above uses the method of data labeling , amount to table dw ag0… in ag0 Deposit is sin0 The offset address of the corresponding value ( The pointer points to the pointer )
  • Preparation : Stack the register state that will be used , then es The segment address of the additional segment register points to the video memory space
  • al Storage angle ,bl Stored divisor 30, Then do the division operation , Because it is 8 Bit Division , So the chamber of commerce is kept in al in , The remainder is saved in ah in , We only care about business here
  • Save vendor to bl in ,bh empty , here bl The offset address is stored in , But the unit is bytes
  • bx It needs to be doubled , Because above table The unit of the table is the word , And here are bytes
  • from table According to the offset address , Get the offset address of the corresponding string , Because here, the data label points to the data label , It can be seen as a pointer pointing to a pointer , The value of the pointer is the memory address it executes
  • adopt bx String offset address saved in , Locate the corresponding memory address , And then output them byte by byte , Until I met 0 until
  • Restore the state of the relevant register

Direct address table of code

Use the code's direct addressing table to solve the problem

 Insert picture description here


The realization of various functions

 Insert picture description here
 Insert picture description here
The focus here is not on the specific implementation of each function , Instead, the address of the beginning of each subroutine is obtained by looking up the table in the main program , How this operation is done

  • By means of ah Set the relative offset address of the subroutine to be executed in the table , however ah The saved offset address is in bytes , The unit of the table is the word , So there is a need for *2, Get the real offset address of the subroutine in the table

The advantage of writing address table directly

 Insert picture description here
Looking up tables can speed up , And it can make the program architecture very clear and easy to understand , It is also convenient for program expansion .

The idea of look-up table is similar to the strategy mode , Are used to solve a pile of IF...ELSE Judgmental


Interrupt and its handling

The concept of interruption

 Insert picture description here


8086 Internal interruption of

 Insert picture description here


Interrupt handler

 Insert picture description here

  • because 8086CPU In the interrupt vector table of , Each interrupt program occupies four bytes , The first two bytes hold the offset address of the interrupt program code segment , The last two bytes hold the segment address of the interrupt program code segment .

And because each interrupt program occupies four bytes , therefore IP And N There are rules to follow ,IP=N4,CS=N4+2

When there is an interruption , First look up the table to get the address of the interrupt program , Then set the CS and IP To execute the interrupt program


Case study : In the system 0 Interrupt number

 Insert picture description here
When a division error occurs , Trigger 0 Interrupt number , At this time, I will look up the table and get 0 Address of interrupt program No , Then set the CS and IP Point to the interrupt program address , Then execute the interrupt program


Interrupt the process

 Insert picture description here

It is necessary to protect the site before performing the interruption procedure , That's important , It mainly saves the status of the flag register and the address of the current program


Write interrupt handling program

Interrupt handler and its structure

 Insert picture description here


Write interrupt handling program —— Take division error interrupt as an example

 Insert picture description here


do0 Where should the subroutine be placed ?

 Insert picture description here


Application framework

 Insert picture description here
The installer is to put the program code into the memory space previously designated to store the program , Then change the interrupt vector table 0 Stored in table item No 0 The address of interrupt program No .


do0 Implementation of the installer

 Insert picture description here

Total number of bytes occupied by the program , It can be calculated by the direct difference between two address labels


do0 Implementation of subroutines

 Insert picture description here
Show characters , Write out the corresponding characters to the display buffer , Then set the interrupt vector table to do0 The entry address of the interrupt program , Write to the interrupt vector table 0 In table No

because do0 Interrupt program mov ax,4c00h int 21h Will end the program directly , return DOS System , Therefore, it will not be interrupted after the execution of the program , Return to the original program to continue .


Summary

 Insert picture description here


Step break

from Debug Medium t Order to speak ……

 Insert picture description here


Single step interrupt process and processing

 Insert picture description here


application : Interrupt does not respond

 Insert picture description here
If you are executing to ss Register transfer data instructions , There is an interruption , that CPU The associated interrupt register value is set to 0, Interrupt generation is not allowed , So the next instruction will continue , After executing the next instruction , Then enter the interrupt

So the writing on the right is wrong , Because if it's written like this ,mov ss,ax and mov ax,0 Will be executed continuously together , and mov sp,10 The instruction will be executed in a single step


from int An interrupt caused by an instruction

int n The interruption caused

 Insert picture description here


Write interrupt routines that are called by the provider

 Insert picture description here


Example : interrupt 7ch Interrupt routine for

 Insert picture description here
In execution int n Before the interruption , Will put the current CS and IP Register state stack , There are also flag register states on the stack , Then interrupt the execution of the routine , And then the related states are pushed out of the stack , Perform on-site restore .

 Insert picture description here
If relevant registers are used in the interrupt program , It is also necessary to save the corresponding register status before use , Resume after the program ends


BIOS and DOS Interrupt handling

BIOS—— Basic I / O system

 Insert picture description here


BIOS Interrupt call example

 Insert picture description here


What are they? BIOS interrupt , How to use it? ?

 Insert picture description here


The powerful function of assembly , also DOS interrupt !

 Insert picture description here


int 21HDOS Application of interrupt routine

 Insert picture description here


BIOS and DOS Interrupt routine installation process

 Insert picture description here


Read and write port

Use ports to access peripherals : Take phonation as an example

 Insert picture description here


CPU Neighbors of

 Insert picture description here
CPU These ports control the behavior of various chips , These ports are essentially registers , But it is the register related to the corresponding chip , No cpu Internal slave register , These chips read the values of these registers , Know what you should do


Read and write port

 Insert picture description here


Port read / write process demonstration

in al, 20h 
out 21h, al

 Insert picture description here

 Insert picture description here


I/O Port allocation

 Insert picture description here


Examples of read / write instructions for ports

 Insert picture description here


operation CMOS RAM chip

CMOS RAM chip

 Insert picture description here


Port operation example : extract CMOS RAM Time information stored in

 Insert picture description here


Displays the current month in the middle of the screen

 Insert picture description here


Peripheral connection and interruption

CPU Communicate with external devices through ports “ Connect ”

 Insert picture description here


External interruption : An interrupt caused by an event occurring on an external device

 Insert picture description here


External interrupt processing

 Insert picture description here


PC Computer keyboard processing process

PC Computer keyboard processing process

 Insert picture description here


The scan code of the key on the keyboard ( General code )

 Insert picture description here


PC Computer keyboard processing process —— Cause an interrupt

 Insert picture description here

The control keys and switch keys are stored by the keyboard status bytes , Each bit of this byte represents the state of a key


PC Computer keyboard processing process —— Execute interrupt routine

 Insert picture description here


Input ‘a’ Processing of

 Insert picture description here
 Insert picture description here


Custom keyboard input processing

PC Computer keyboard processing process (int 9 Interrupt routines )

 Insert picture description here


Realization : Show... In turn ’a’~‘z’(v0.2)

 Insert picture description here

The design of empty loop gives an idea :dx and ax Both are assigned a maximum value , And then put ax The value of the register is reduced to 0 end , When it's over dx The value of is also reduced to 0


Realization : Show... In turn ”a”~”z”(v0.4)

 Insert picture description here


Press down Esc Key to change the color of the display

 Insert picture description here


Realization : Press down Esc Key to change the color of the display (v1.0)

 Insert picture description here
In the polling display a–z In the process of , Press any key , If an interrupt is triggered , First, it will call its original interrupt routine , After the execution of the original interrupt routine , If you press yes ESC Key will also change the color of the currently displayed font , If it's another key , There are no additional functions


Methods of rewriting interrupt routines

Rewrite interrupt routine - With int 9 For example

 Insert picture description here


Implementation method

 Insert picture description here

  • To install a new program, first calculate int9 and int9end Byte difference between data labels , Equal to the size of the program , Then cycle through all bytes to 0:204h It's about
  • Save the original interrupt address to 2:200 Unit
  • take int9 The interrupt routine of the interrupt call becomes the address of the interrupt routine we have written , namely 0:204h, And this process should not be interrupted by maskable interrupts , adopt cli Setup completed , After the end , Allow to be interrupted , adopt STI complete
  • Program return

Respond to peripherals with interrupts

How to operate external equipment ?

 Insert picture description here


The processing of keyboard input int 9h Break and int 16h interrupt

 Insert picture description here


demonstration : Input A、B、C、D、E、Shift_A、A It caused (int 9)“ action ”

 Insert picture description here
 Insert picture description here
The above is after input ABCDE After , Status of keyboard input buffer , Enter SHIFT_A

 Insert picture description here
At this point, we press one more A

 Insert picture description here

Shift The flag is restore


demonstration int 16h

use int 16h Read out use int 9h Deposit deferred Data of flushing area

Keyboard buffer implementation

  • common 16 word
  • Use a ring queue
  • Storable 15 One click Key scan code

 Insert picture description here
Read a word from the keyboard buffer in turn , Put in the data buffer register , And then put in AX in ,AH Store scan code ,AL Deposit ASCII


call int 16h Read the keyboard input from the keyboard buffer

 Insert picture description here


Application example : Change the screen color

 Insert picture description here


application : Input of string

The problem to be solved

 Insert picture description here


The processing of the program

 Insert picture description here


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

 Insert picture description here


Realize the putting of character stack 、 Out of stack and display

 Insert picture description here
When there are several subfunctions in a program , Generally do not use one by one judgment , The table lookup method is used to determine which sub function should be called at present


Read write disk

How to operate the disk ?

 Insert picture description here


BIOS Disk direct services provided ——int 13h

 Insert picture description here


use BIOS int 13h Read the disk

 Insert picture description here


use BIOS int 13h Write to the disk

 Insert picture description here


DOS Interrupt support for disk files ——int 21H

 Insert picture description here


Let computers “ Sing a song ”

External devices and how they are controlled ?

 Insert picture description here
Programs can access peripherals directly , It is accessed through a port


And " The computer sings " Related hardware and control

 Insert picture description here
Let the computer sing , Need and 8253 and 8255 Dealing with chips , and CPU Want to control these two chips , It needs to be operated through the ports provided by the corresponding two chips

Because of the unified addressing , So we can easily locate the addresses of these two ports , And then through in and out Instructions write data to operate on it , Get the data we need from the corresponding port .


“ translate ” music

 Insert picture description here


Performance program

 Insert picture description here


原网站

版权声明
本文为[Big flicker love flicker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230941212176.html