当前位置:网站首页>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
- Shift instructions
- Operating video memory data
- Describes the label of the memory unit
- Direct address table of data
- Direct address table of code
- Interrupt and its handling
- Write interrupt handling program
- Step break
- from int An interrupt caused by an instruction
- BIOS and DOS Interrupt handling
- Read and write port
- operation CMOS RAM chip
- Peripheral connection and interruption
- PC Computer keyboard processing process
- Custom keyboard input processing
- Methods of rewriting interrupt routines
- Respond to peripherals with interrupts
- application : Input of string
- Read write disk
- Let computers “ Sing a song ”
This series of articles refers to the fourth edition of assembly language and Assembly language programming Keynote speaker: helijian Sort out
Shift instructions

Example : Logical shift instruction shl and shr

Operating video memory data
Principle of display

Shows the structure of the buffer

A way of displaying information “ direct ” The way

Describes the label of the memory unit
About label

The above writing needs to use offset To indicate the address of the data in the code segment
Data label with colon removed

a[si]---->[si+a]
The data label describes both the memory address and the unit length

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 .

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

The simplest solution

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

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

Solution

- 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

The realization of various functions


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

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

8086 Internal interruption of

Interrupt handler

- 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

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

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

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

do0 Where should the subroutine be placed ?

Application framework

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

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

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

Step break
from Debug Medium t Order to speak ……

Single step interrupt process and processing

application : Interrupt does not respond

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

Write interrupt routines that are called by the provider

Example : interrupt 7ch Interrupt routine for

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 .

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

BIOS Interrupt call example

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

The powerful function of assembly , also DOS interrupt !

int 21HDOS Application of interrupt routine

BIOS and DOS Interrupt routine installation process

Read and write port
Use ports to access peripherals : Take phonation as an example

CPU Neighbors of

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

Port read / write process demonstration
in al, 20h
out 21h, al


I/O Port allocation

Examples of read / write instructions for ports

operation CMOS RAM chip
CMOS RAM chip

Port operation example : extract CMOS RAM Time information stored in

Displays the current month in the middle of the screen

Peripheral connection and interruption
CPU Communicate with external devices through ports “ Connect ”

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

External interrupt processing

PC Computer keyboard processing process
PC Computer keyboard processing process

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

PC Computer keyboard processing process —— Cause an interrupt

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

Input ‘a’ Processing of


Custom keyboard input processing
PC Computer keyboard processing process (int 9 Interrupt routines )

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

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)

Press down Esc Key to change the color of the display

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

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

Implementation method

- 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 ?

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

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


The above is after input ABCDE After , Status of keyboard input buffer , Enter SHIFT_A

At this point, we press one more A

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

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

Application example : Change the screen color

application : Input of string
The problem to be solved

The processing of the program

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

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

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 ?

BIOS Disk direct services provided ——int 13h

use BIOS int 13h Read the disk

use BIOS int 13h Write to the disk

DOS Interrupt support for disk files ——int 21H

Let computers “ Sing a song ”
External devices and how they are controlled ?

Programs can access peripherals directly , It is accessed through a port
And " The computer sings " Related hardware and control

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

Performance program

边栏推荐
- Actual combat | how to make a slam track truth acquisition device?
- Xmake v2.6.8 发布,编译缓存改进
- 栈和队列的基本使用
- 互联网技术发展内卷后的出路——iVX的诞生
- Use openvinotm preprocessing API to further improve the reasoning performance of yolov5
- How to solve the task cache compilation problem caused by gradle build cache
- Groovy map operation
- 微信小程序之获取php后台数据库转化的json
- What are the conditions for a mature knowledge management?
- Technology creates value and teaches you how to collect wool
猜你喜欢

leetcode:42. Rain water connection

Yyds dry inventory solution sword finger offer: judge whether it is a balanced binary tree

How to use androd gradle module dependency replacement

How deci and Intel can achieve up to 16.8x throughput improvement and +1.74% accuracy improvement on mlperf

深入剖析MobileNet和它的变种

KS003基于JSP和Servlet实现的商城系统

爱思唯尔-Elsevier期刊的校稿流程记录(Proofs)(海王星Neptune)(遇到问题:latex去掉章节序号)

Develop a powerful tool for increasing efficiency - vscode plug-in sharing in 2022

vulnhub靶机Os-hackNos-1

如何正确计算导入Excel的行数(POI/NPOI)
随机推荐
Shell process control - 39. Special process control statements
MIT 6.031 reading5: version control learning experience
串口、COM、UART、TTL、RS232(485)区别详解
Ks007 realizes personal blog system based on JSP
Kali use
quartus調用&設計D觸發器——仿真&時序波驗證
Basic use of stacks and queues
32-way telephone +2-way Gigabit Ethernet 32-way PCM telephone optical transceiver supports FXO port FXS voice telephone to optical fiber
Js: get the maximum zindex (Z-index) value of the page
微信小程序之input前加图标
深入剖析MobileNet和它的变种
Interrupt and polling
Develop a powerful tool for increasing efficiency - vscode plug-in sharing in 2022
Face registration, unlock, respond, catch all
One way linked list implementation -- counting
[untitled]
Add Icon before input of wechat applet
Xmake v2.6.8 release, compilation cache improvement
How to solve the task cache compilation problem caused by gradle build cache
leetcode:42. Rain water connection