Day 83 Learning Record: Basics of Computer Hardware Technology: Assembly Language Programming

1. Assembly language instructions

The statements of assembly language are formed on the basis of the instruction system, and are divided into two categories according to their functions and compilation conditions: instructional statements (symbolic instructions) and indicative statements (pseudo-instructions).
Instructional statements are executable statements, corresponding to machine instructions, which command the CPU to perform certain operations, and their functions are performed by hardware. The instructional statement is a descriptive statement without corresponding machine instructions. It only provides compilation information for the assembler and connection information for the linker, and its function is completed by the corresponding software.
The format of the instructional statement and the instructional statement is basically the same, which are:
label: symbol instruction; comment
variable name pseudo-instruction; comment
In the instructional statement, the label is not necessary, usually when a certain instruction is used as the target of the transfer instruction , add a label before the instruction, and a colon must be used between the label and the instruction.

1. Basic grammatical components

1. Name
The name is used to identify a certain grammatical component, and there are two types: system regulation and user definition.
(1) Labels and variables
A label is a name introduced with a colon, which can only appear in a certain code segment. It represents the instruction address and provides a transfer target for the transfer instruction. A variable is a symbolic representation of a data or data area in the memory. It is usually defined in the data segment, additional segment or stack segment, and represents the storage address of the memory operand, or the variable name represents a certain unit.
Both labels and variables have 3 attributes, namely:
①Segment attribute
②Offset attribute
③Type attribute
(2) Constant
constant has a definite value at assembly time, which has three forms: immediate value, string constant and symbol constant .
2. Operators
(1) Numerical operators
① Arithmetic operators
② Logical operators
③ Relational operators
EQ (equal to), NE (not equal to), GT (greater than), LT (less than), GE (greater than or equal to), LE (less than or equal to).
(2) Operators for modifying attributes
Assembly language provides PTR operators.
Type specifier PTR address expression
The type specifiers are: BYTE (byte), WORD (word), DWORD (Chinese character), FAR (far), NEAR (near).
In assembly language, the concept of the PTR operator is a bit different than in high-level languages. Usually, assembly language uses address addressing mode to access data in memory, and the function of the PTR operator is to pass the address of the data to the pointer variable, so that the pointer variable can be used in the assembly program to operate the data in the memory .
The syntax of the PTR operator may vary in different assembly languages. For example, in x86 assembly language, the PTR operator is represented by square brackets "[]", and its format is:

MOV BX, [PTR WORD_VAR]

The above code uses the PTR operator to get the address of a variable named WORD_VAR and stores it into the BX register. Note that the square brackets "[]" are part of the PTR operator, which means that the content in the brackets is interpreted as a memory address, so that the data stored at the address can be manipulated.

It should be noted that in assembly language, the address of a variable is usually expressed in the form of a segment address and an offset address, so this needs to be taken into account when using the PTR operator.
(3) Operators that return attributes or values
​​①SEG operator
Format: SEG variable name or label
Function: Calculate the segment base address of the variable name (or label)
②OFFSET operator
Format: OFFSET variable name or label
Function: Calculate the variable name (or label) address offset
③TYPE operator
Format: TYPE variable name or label
Function: calculate the type of variable or label, for byte variable, the return value is 1; for font variable, the return value is 2; for double word variable The return value is 4.
④ $ operator: The $ operator returns the current value of the assembly counter. For specific usage, see "Byte Definition Directive".
(4) Square bracket operators and address expressions Address expressions enclosed in square brackets are commonly used addressing methods for accessing memory operands. Another use of square brackets is to indicate the subscript of the array, which can be a constant, an arithmetic expression, or an expression of 16-bit addressing mode.

2. Pseudo-instructions

1. Data definition pseudo-instructions
include DB, DW, and DD, which are used to define variables and allocate storage areas.
(1) Byte definition pseudo-command
Format: Variable name DB A string of byte data separated by commas
Function: Convert the defined byte data into binary numbers and store them sequentially from the specified variable unit.
(2) Word definition pseudo-command
Format: variable name DW, a string of font data separated by commas
Function: store the defined font data sequentially from the specified variable unit, the storage rule of each font data is: low byte storage into the low address unit, and the high byte into the high address unit.
(3) Double-word definition pseudo-instruction
Format: variable name DD, a string of double-word data separated by commas
Function: store the numbers defined by DD sequentially from the specified variable name, each number occupies 4 units, each number The storage rule is also that the low byte is stored in the low address unit, and the higher byte is stored in the higher address unit.
2. Symbol definition directive
(1) Equivalence directive
Format: variable name EQU expression
Function: Assign the value of the expression to a variable.
(2) Equal sign directive
format: variable name = expression
3, segment definition directive
(1) SEGMENT and ENDS directive
Use SEGMENT and ENDS to define a segment format:
segment name SEGMENT positioning parameter link parameter 'category name'
segment Definition body
Segment name ENDS
①Positioning parameter:
BYTE means byte address: that is, the target code of this logic segment can be stored sequentially from any address.
WORD represents the word address: that is, the object code of the logic segment, which is stored sequentially from the even address.
PARA (or default) indicates the section address (one section is equal to 16B): that is, the object code of the logic segment is stored sequentially from an address divisible by 16.
PAGE represents the page address: that is, the target code of the logical segment, which is stored sequentially from an address divisible by 256.
②Link parameters:
PUBLIC, MEMORY, COMMON, STACK, AT expression, when the default segment attribute parameter in the segment definition, it indicates that the segment is an independent logical segment.
③'Category name': The category name is a character string chosen by the programmer. It must be enclosed in single quotation marks when using it. The category name is optional.
(2) ASSUME pseudo-instruction
Format: ASSUME Segment register name: Segment name, Segment register name: Segment name, ...
Function: The ASSUME statement informs the assembler which segment register is used to address the logical segment.
(3) ORG pseudo-command
Format: ORG numerical expression
Function: It is used to specify the offset of the target program or the starting storage unit of the data area.
4. Process and macro definition pseudo-instructions
(1) Process definition pseudo-instructions
A process is also called a subroutine, which is a part of a program. It must use RET as a return instruction, and use a process definition statement to delimit the subroutine.
(2) Macro definition pseudo-instruction
The concept of macro is very similar to the process.
(3) Conditional assembly directive
(4) Source program end directive

2. Assembly language programming method

(1) Define tasks
(2) Draw flowcharts
(3) Assign memory working units and registers
(4) Programming and debugging

1. Branch structure program design

(1) Simple branch structure program:
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
2. Multi-way branch structure program
insert image description here
insert image description here
insert image description here

2. Cyclic programming

1. Single cycle program
insert image description here
insert image description here
2. Multiple cycle program
insert image description here
insert image description here
insert image description here

3. Subroutine design

1. Calling and returning of the subroutine
The call and return of the subroutine are completed using the CALL instruction and the RET instruction.
2. Parameter transfer of subroutine
In the use of subroutine, an important problem to be solved is parameter transfer.
①Use registers to transmit parameters.
② Use the stack to transfer parameters.
③ Use the memory unit to transmit parameters.
insert image description here
insert image description here
insert image description here
Method 2:
insert image description here
insert image description here
insert image description here
insert image description here
3. Subroutine nesting and recursion
(1) Subroutine nesting
(2) Recursive subroutine
insert image description here
insert image description here
insert image description here

insert image description here
insert image description here

DOS and BIOS function call

1. DOS interrupt and function call
(1) DOS special interrupt
(2) DOS callable interrupt
insert image description here
insert image description here
insert image description here
insert image description here
2. BIOS interrupt call
(1) keyboard I/O interrupt call
insert image description here
(2) printer I/O interrupt call
insert image description here
(3) display I/O interrupt call
insert image description here

5. Modular program design

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

3. Example of assembly language programming

1. Numerical calculation

1. Multi-byte subtraction
Example 4.16 Try to calculate the difference between the 4-bit BCD numbers of two combinations. Assume that the two operands involved in the operation are respectively stored in the storage unit whose address is headed by DAT1 and DAT2, and the result is stored in the RESULT unit, and the data is stored in the form of low front and high back.
insert image description here
2. Multibyte multiplication
insert image description here

2. Code conversion

1. Conversion from decimal code to binary code
insert image description here
insert image description here
2. Conversion between binary code and ASCII code
insert image description here

			MOV   AH ,4CH     ;返回DOS
			INT   21H
	CODE	ENDS
			END	  START

3. Character data processing

1. Searching for characters
insert image description here
insert image description here
2. Deleting characters
insert image description here
insert image description here
What is the function of assembly language instructions?
Assembly language instructions are used to write low-level machine instructions that directly manipulate the computer's hardware and registers. Assembly language is a low-level language that is closer to computer hardware and architecture.

The main functions of assembly language instructions include the following aspects:

Control flow: Assembly language instructions can implement the control flow of the program, such as conditional branches (such as if statements), loops (such as while loops), and jumps (such as goto statements). These instructions alter the path of program execution based on certain conditions or flags.

Data manipulation: Assembly language instructions can perform various operations on data, such as loading data into registers, writing back from registers to memory, performing arithmetic operations between registers (such as addition, subtraction, etc.) and logical operations (such as and, or, non etc).

Accessing memory: Assembly language instructions access the computer's memory, reading and writing data. Data in memory can be read or modified by specifying memory addresses and using different memory access modes.

Handling Interrupts and Exceptions: Assembly language instructions are used to handle interrupts and exceptions generated by computer hardware. By setting the interrupt vector table and writing the corresponding interrupt handler, it can respond to external events (such as signals from hardware devices) or abnormal conditions (such as division errors or page fault exceptions).

Operating system calls: Assembly language instructions make operating system calls, enabling programs to interact with the operating system. Through the interface provided by the operating system, functions such as file operation, network communication, and process management can be realized.

Assembly language instructions act as a bridge between the high-level language and the underlying computer hardware. They are responsible for converting the code written in the high-level language into machine instructions and directly interacting with and controlling the computer hardware.

Guess you like

Origin blog.csdn.net/weixin_45694614/article/details/131290647