Find one of the assembly languages with everyone (real mode) [original]
Frontier: This year, the next thing, an article in the magazine saw the following sentence: "Mastering the compilation, still the road to the master." However, in actual learning, compilation is often ignored by beginners because of their application. Familiar with the assembly language, will be the "Eternal Sword" when you commission it in software, and the importance is self-evident. There are also many three times in the process of learning, and finally retreat. I hope this article can be with you, regain the two-edged sword. Please indicate the original "Senior Sentence" original.
0. This article tells the basic knowledge, addressing method, command system, macro assembly, structured program design, stack, function, interruption, etc.
1.
Astroduction Description:
The assembly language is a symbolic language, which is easy to understand and mastery than the machine language, and it is easy to debug and maintain. However, the assembly language source program is to translate into a machine language program to execute by the computer. The process of this translation is called "assembly", which translates the assembly source to the language plus project sequence called assembler. Although the assembly language is intuitive than the machine language, it is still invisible. So people have developed advanced programming languages. Advanced programming language is close to the grammatical habit of human natural language, independent of computer hardware, easy to be mastered and used by users.
Characteristics of assembly language:
(1) The assembly language is closely related to the processor.
(2) The assembly language program is high.
(3) Writing assembly language source programs are cumbersome than preparing advanced language sources.
(4) Debugging assembly language procedures are difficult than debugging advanced languages.
The main application of assembly language:
(1) The program performs a shorter time, or occupies a small storage capacity.
(2) The program is closely related to the computer hardware, and the program directly controls the hardware.
(3) The occasion of large software performance needs to be improved.
(4) There is no suitable high-level language.
2.
Value data:
Numerical data is divided into symbol numbers and no symbols. No symbol number is the highest bit represents a value, and the highest bit of symbol represents the symbol. There are different coding methods with a number of symbols, which is commonly used.
The N-bit binary number can be expressed in the range of unsigned integers:
0 ≤i ≤ 2n-1
N-bit binary numbers can be represented by the signed integer:
-2 (N-1) ≤ i ≤ 2 (N-1) -1
ASCII code:
The standard ASCII code is encoded with 7-bit binary number, with a total of 128.
The basic unit of the computer memory is 8 bits. The highest bit of the ASCII code is usually 0, and the highest bit is used as a parity bit.
The first 32 and last 1 encoding in the ASCII code table is a control character that cannot be displayed, used to represent a certain operation.
94 encodings after 20h in the ASCII code table are characters that can be displayed and printed, including digital 0 ~ 9, English letters, punctuation, etc.
BCD code:
Although the binary number is easy, it does not meet people's usage habits, and writing is inconvenient, so it is often used in the computer input and output, which requires implementation of decimal and binary conversion. For the convenience of conversion, it is often used to use binary coding decimal, referred to as BCD code.
The BCD code is 1-bit decimal integer with a 4-bit binary number. There are a variety of methods, which are commonly used is 8421bcd codes.
3.
8086 architecture:
Bus Interface Part (BIU) Execution Part (EU)
____________________________________
| Segment register | | || ________________ | | General Register Group |
| Command pointer | | | |
| ________________ | | ___________________ |
|| || || ||
|| || || ||
|| || || ||
_________________ || ||
| | ______________________________________________
| Address generation and | _______________________________________________________ |
| Bus Control | || || | |
| ________________ | __________________ | |
|| || | Operand | | | | |
|| || __________________ |
|| || || || | |
|| || || || | |
____________________________________ | |
| | | | | | |
| Command queue | _____________ | logo register | ______________ |
| | | _____________ | | _______________ |
| ________________ | | ___________________ |
|| ||
|| ||
|| ||
___________________
| | |
| Logic Algorithmic Unit |
| | |
___________________
4.
8086 register group:
AX:
Accumulator, commonly used to save temporary data §
BX: The base register, mainly used as a pointer, or it can be used as a data storage §__ data register ______
CX:
Counter § §
DX:
Data register, used for port operation or temporary data § § §
§ §_ universal register
SP:
Stack pointer, pointing the stack current address §__ stack pointer register __ §bp:
Attachment pointer, often used to point to high-level language stack frame § § § §
Si:
Source operation number pointer §__ 变 变 register ______
Di: Destination Operation Number Pointer § § §
IP:
Command pointer §__ control register
Flags:
Sign Register §
CS:
Code segment register §
DS:
Data segment register §__ segment register
SS:
Stack segment register §
ES: Additional Segment Register §
Description:
The index and pointer registers include Si, Di, SP, BP4 16-bit registers, primarily to store offset addresses of a memory cell.
Si is the source address register, DI is the destination index register, in string operations, both Si and Di have automatic incremental or reduction.
The SP is the stack pointer register for storing the offset address of the stack of the current stack segment; the BP is the base pointer register for storing the offset address of a storage unit in the stack segment.
The first address of the CS storage code segment, the command pointer register IP indicates the offset address of the instruction in the code segment.
The command pointer IP in the 8086CPU always saves the next time the offset address of the instruction will be taken from the main memory, the value of the offset address is the byte distance of the instruction to the first site of the segment segment. When the target program is run, the content of IP is automatically set by the microprocessor hardware. The program cannot directly access the IP, but some instructions can change the value of IP, such as transfer instructions, subroutine call instructions, and the like.
Sign Register:
There is a very important 16-bit logo register in 8086CPU, which contains nine sign bits, primarily to save a directive execution, and the status information and operational results of the CPU.
A. Conditional sign
(1) carry mark CF
(2) Zero Sign ZF
(3) Symbol sign sf
(4) Overflow sign of
(5) parity flag PF
(6) Auxiliary carry mark AF
B. Status control flag
(1) Directional sign DF
(2) Interrupt allowed sign IF
(3) Track mark TF
5.
Physical address formation:
Scheme 1M byte address space into several logic segments. Each logical segment must meet two conditions: First, the start address of the logical segment (the first site address) must be a multiple of 16; the other is the maximum length of the logic segment is 64K. According to these two conditions, the 1M-byte address space can be divided into 64K logic segments, and minimize itself into 16 logic segments. Logic segments can be connected to logical segments, or may not be connected, but also overlap. Calculation method: Section takes 16 additional offset, the physical address.
6.
Addressing method:
A.
Addressive:
For example: MOV AX, 35
B.
Register addressing:
For example: MOV AX, BX
C.
Direct memory addressing:
For example: Value DW 10h
MOV AX, Value; // Utilization
DS segment register (default) calculates its actual location in Memory
MOV AX, DS: 33H;
MOV SS: 2, AX; // SS * 16 2D.
Indirect memory addressing:
(1) Indirect addressing of registers
For example: MOV AX, [BP]; // AX value is (SS * 16 BP) address pointing
(2) Address address
For example: Table DB 100 DUP (?)
MOV AX, Table [BX]; // BX is a digital subscript
(3) At the base address
For example: MOV AX, [BX] [Si]; // bx * 16 Si
(4) A displacement base address
For example: MOV AX, Table [BX] [Si]; // Point Table BX Si
MOV AX, 10 [BP] [DI]; // Point 10 BP DI
E:
Cross-segment problem:
MOV AX, DS: [BP]
MOV CX, SS: [Si]
In this example, DS:, SS: is a span prefix. At this time, the default state is invalid, and the physical address PA of the operand is formed by the segment register content 4-bit additional offset EA formation. The source operand physical address of the above 2 instructions is:
PA1 = (DS) left shift 4 digits [bp]
PA2 = (SS) left shift 4 digits [Si]
7.
Data transfer class instruction of the instruction system:
A. General Data Transfer Directive:
1. Transfer command MOV
2. Data Exchange Directive XCHG ---- Function: Interchange the source address and destination address
3. Chart Conversion Directive XLAT
B. Stack Operation Directive:
1. Put in stack instructions
2. Find a stack directive POP
C. Sign Register Transfer Directive:
1. Sign to send AH instruction lahf
2. AH send logo command sahf
3. Sign Register Inn Instructions PUSHF
4. Sign Register Out Stack Directive POPF
D. Address Transfer Directive:
1. Transfer offset address instruction LEA
For example: A dB?
LEA BX, A; BX is A's Address
2. Transfer offset address and data segment first site instruction LDS
Statement format: LDS OPD, OPS
Function: Feed the main memory to the specified word unit data into the specified memory, the next word unit data to send the DS register.
3. Transfer offset addresses and additional data segment instructions - - - Function: Send the main memory constitutive unit content to specify the register.
Statement format: Les OPD, OPS
Function: Send the main memory constant unit content specify the register. That is, (OPS) → OPD, (OPS 2) → ES.
E. Input Output Instruction:
1. Enter instruction IN
2. Output instruction OUT
8.
Instruction system arithmetic computing class directive:
A. Access command
1. Add 1 instruction INC
2. Adding an instruction Add
3. Carrying Adding Inform ADC
B. Delivery command
1. Reduce 1 command DEC
2. Deducation SUB
3. Finding instructions NEG
4. Borrow reduction command SBB
5. The comparison command CMP ---- result only affects the flag bit C. Phase Transportation
1. No symbol multiplication instruction MUL
2. Have symbol multiplied command iMul
D. Except for arithmetic instructions
1. No symbol definition DIV
2. Symbol in addition to the instruction IDIV
E. Symbol extension instruction
1. Byte conversion orthodox command CBW ---- extends the symbol bit data in Al to AH
2. Convert words into double word instructions CWD ---- extend symbol bit data in AX to DX
F. Decimal Adjustment Directive
1. Compressed BCD code adjustment command DAA
2. Non-compressed BCD code adjustment command DAS
G. Non-compressed BCD code adjustment instructions
(1) Adding non-compressed BCD code adjustment command AAA
(2) subtractive non-compressed BCD code adjustment command AAS
(3) Non-compressed BCD code adjustment command AAM
(4) Disposing non-compressed BCD code adjustment command AAD
H. Bit operation class instruction
Logical operation instruction
1. Seeking anti-instruction Not
2. Logic multiplied command and
3. Test command TEST
4. Logical Directive OR
5. Press-topinating command xor
Shift command
1. Arithmetic left shift and logical left shift SAL (SHL)
Statement format: Sal OPD, 1 or SHL OPD, 1
Sal OPD, CL or SHL OPD, CL
Function: The (OPD) is moved to the left to move the number of times, the lowest bit is supplied to the corresponding 0, and the content of the CF is the value of the last shift position.
2. Calculator right shift SAR
Statement format: SAR OPD, 1 or SAR OPD, CL
CF function: OPD moves (OPD) to the right number of times and the highest bit remains unchanged; the content of CF is the value of the last shift bit.
3. Logic right shift command SHR
Statement format: SHR OPD, 1 or SHR OPD, CL
Function: The number of times the (OPD) is moved to the right, and the highest bit is added to the corresponding number of 0, the content of the CF is the value of the last shift position.
4. Cyclic left shift instruction ROL
Statement format: ROL OPD, 1 or ROL LPD, CL
Function: Connect the highest level of the destination operation with the lowest bit, and move all the bits in the ring to the left move CL. The content of CF is the value of the last shift position.
5. Cycle right shift in ROR
Statement format: ROR OPD, 1 or ROR OPD, CL
Function: Connect the highest level of the destination operation with the lowest bit to one ring, move all of the bits in the ring to the right movement of the clock, and the content of the CF is the value of the last shift position.
6. Revenge loop left shift command RCL
Statement format: RCL OPD, 1 or RCL OPD, CL
Function: Take the number of destination operations along with the CF flag to move CL to the left cycle.
7. Route with carrying rCR
Statement format: RCR OPD, 1 or RCR OD, CL
Function: Take the number of destination operations along with the CF flag to move to the right cycle.
9. String Operation Class Instructions for Instruction System
1. Transfer command MOVS
Statement format: 1 MOVSB - byte string
2 MOVSW-string transfer
Function: Transferring the data in one byte (or word) memory cell in the source string of the pointer to the point in the storage unit in the destination address of DI, and automatically modifies the pointer to make it point to the next byte (or Word) storage unit. That is: 1 (DS: [Si]) → ES: [DI].
2 When DF = 0, (SI) and (DI) increments.
When DF = 1, (SI) and (DI) reduction.
2. String comparison command CMPS
Statement format: 1 CMPSB - byte string comparison
2 CMPSW-string comparison
Function: Reduce the data in one byte (or word) in the source string referred to in the SI to one byte (or word) memory cell in the destination string referred to in the DI, and according to the phase The decrease setting flag is set, but the result is not saved.
That is: 1 ([Si]) - ([DI]).
2 Modify the string pointer to make it point to the next element in the string.
When DF = 0, (SI) and (DI) increments. When DF = 1, (SI) and (DI) reduction.
3. String search instruction scaS
Statement format: 1 SCASB - byte string search
2 SCASW-string search
Function: Al (byte) or AX (word) is subtracted in a byte (or word) memory unit in the destination string referred to DI, and set the flag bit according to the result reduction result, the result is not saved ,
That is: 1 byte operation: (al) - ([DI]), word operation: (AX) - ([DI]).
2 Modify the next element in the pointing in the string.
When DF = 0, (dI) increments. When DF = 1, (DI) is reduced.
4. Number instructions from the source string LODS
Statement format: 1 LODSB - taken from byte strings
2 LODSW - taken from the string
Function: Take out the data in the source string in the source string referred to in the SI to the AL (or AX).
That is: 1 byte operation: ([Si]) → Al, Word operation: ([Si]) → AX.
2 Modify the pointer Si to point it to the next element in the string.
When DF = 0, (Si) increments. When DF = 1, (Si) is reduced.
5. Delivery of the destination string STOS statement format: 1 Stosb - to the byte string
2 (2) STOSW - deposited in the string
Function: Send the data in Al or AX to the byte (or word) memory cell in the destination string referred to in the DI.
That is: 1 byte operation: (al) → [di], word operation: (AX) → [di].
2 Modify the pointer DI to make it point to the next element in the string. When DF = 0, (DI) increments). When DF = 1, (DI) is reduced.
6. Repeat prefix command rep Rez repnz
(1) REP
The REP prefix is used in MOVS, STOS, and LODS instructions.
Function: Each time a string directive (CX) -1 until (CX) = 0 is executed, the end is repeated.
(2) REPZ
This command is typically used in CMP and SCAS instructions.
Function: Each of the string command (CX) -1 is executed, and it is determined whether the ZF flag is 0, as long as (CX) = 0 or ZF = 0, the end is repeated.
(3) REPNZ
This instruction is typically used in CMPS and SCAS instructions.
Function: Each time the string command (CX) -1 is executed, it is determined whether the ZF flag is 0, as long as (cx) = 0 or ZF = 1, the end is repeated.
9. Instruction system
Control transfer class instruction:
A. Condition transfer instructions JA, JAE JB, JBE JL, JLE, JNA, JNAE, JNB JNBE, JNC, JNE, JNG, JNGE, JNL, JNL, JNO, JNP, JNS , JNZ, JS, JO, JP, JPE, JPO, JCXZ
B. Conditional transfer instruction JMP
C. Circular Directive Loop, Loope, Loopne, Loopnz,
D. Subprogramcler Call, Ret
E. Interrupt Directive Int, IRet
10. The processor control class instruction of the instruction system:
A. Sign bit setting instruction
1. Ground flag operation instruction
2. Directional logo operation instruction
3. Interrupt logo operation instruction
B.CPU Status Control Directive
1. Empty operation directive NOP
2. Bus blocking prefix instruction LOCK
3. Suspend instructions HLT
4. DRC ESC
5. Waiting for the instruction WAIT
11. Macro assembly language variable definition, such as the following example:
Data Segment STR1 DB '1234' STR2 DW 'AB', 'CD', 'A' STR3 DD 'AB' DATA ENDS
12. Symbolic address:
Next: MOV Al, [Si]; instruction with label NEXT
DEC CX
JNE NEXT; ID Number of NEXT
13. Value expression operation with an arithmetic operator:
NUM1 EQU 25 * 4 -50; Num1 = 50
Num2 EQU Num1 / 7; Num2 = 7
Num3 DB Num1 MOD 7; Num3 = 1
Var1 DB 1, 2, 3, 4, 5
VAR2 DB '12345'
Num4 EQU VAR2-VAR1; NUM4 = 5
Num5 EQU 0FH
MOV Al, Num5 SHL 4; (AL) = 11110000B
MOV BL, NUM5 SHR 4; (BL) = 00000000b14. Logical Operator:
MOV Al, 34H and 0FH; 04H → Al
MOV BL, 05H OR 30H; 35H → BL
MOV CX, Not 00ffh; 0FF00H → CX
MOV DX, 789AH XOR 000FH; 7895H → DX
15. Relationship operator:
The relationship operator includes: equal (EQ), inequality (N), less than (LT), greater than (gt), less than or equal to (le) and greater than or equal to (GE).
Num1 DB 10 LT 5; Num1 = 0
Num2 DB 0AAAH GT 7FFFH; Num2 = 0FFFFH
MOV AX, 'A' EQ 41H; 0FFFH → AX
MOV BX, Num2 LT Num1; 0 → BX
16. Value returns operator:
(1) SEG operator
Format: SEG variable or label
Function: Separate the first segment of the segment of the subsequent variable or the label. E.g:
MOV AX, SEG ARR
MOV DS, AX
(2) OFFSET operator
Format: OFFSET Variable or Number
Function: The offset address of the subsequent variable or reference is separated. E.g:
MOV BX, Offset BUF
(3) TYPE operator
Format: Type variable or label
Function: The type of variable or label is separated. If it is a variable, the type of the type of the variable is returned to the number of words corresponding to the number; if it is a label, the value of the representative label type is returned.
(4) LENGTH operator
Format: Length variable
Function: Take out the number of data storage units contained in the variable.
Take out the variables to account for the number of memory cells.
Data segment
A db 'abcdef'
B DW 10 DUP (1,2dup (2))
C DB 3, 20 DUP (0)
Data ends
MOV AX, Length A; 1 → AX
MOV BX, Length B; 10 → BX
MOV CX, Length C; 1 → CX
(5) Size operator
Format: Size variable
Function: Remove the size of the data storage area contained in the variable. Its return value is:
Length variable * Type variable
For example: in the above examples A, B, C three variables:
SIZE A = Length A * Type A = 1
Size B = Length B * Type B = 20
SIZE C = Length C * Type C = 1
(6) byte separation operator
The byte separation operator includes: high and low.
Format: high constant or address expression
Low constant or address expression function: High byte of the offset of 16-bit constant or address expression; Low is used to separate the subsequent 16-bit constant or address expression offset low byte.
17. Pseudo-Directive:
A. Data Defining Pseudo Directive
Commonly used data definitions of pseudo instructions have DB, DW, DD, DQ, DT.
Format: [Variable Name] Data Defining Pseudo-instruction Expression [, ...]
Function: Define the data store, the type is determined by the data definition of the pseudo instruction, and the initial value is given by the expression.
B. Symbol definition 指
1). Equity Pseudo-Directive Format: Symbolic EQU Expression
2). Equation Pseudo-Directive Format: Symbol Name = Expression
3). Define symbolic name pseudo-instruction format: variable name or label type
C. Segment Defination Pseudo Directive Segment / End
1). Segment name
The segment name is the name of the segment, used to point out the assembler starting position for the storage area allocated by this segment.
2). Targeting
(1) Para: indicates that this paragraph must be stored from the address that can be tied to the 16, ie the lowest level of the start address must be 0.
(2) Word: indicates that this paragraph begins to be stored from one even address, the lowest one of the start addresses must be 0.
(3) BYTE: Indicates that the start address of this paragraph can be stored from any address.
(4) Page: The present paragraph must be stored from the address that can be completely removed by 256, that is, the lowest eight digits of the start address must be 0.
D. category
[Example] The relative position distribution of each segment of the connection program is connected to the connection program.
Data1 segment 'data'
Code segment 'code'
Stack1 Segment 'Stack'
Data2 segment 'data'
Stack2 segment 'stack'
After the connection program is connected, the relative positions of each segment are as follows:
Data1 segment 'data'
Data2 segment 'data'
Stack1 Segment 'Stack'
Stack2 segment 'stack'
Code segment 'code'
E. Combination method
There are six types of ways to choose from.
(1) "Do not choose"
(2) public
(3) Stack
(4) Common
(5) Memory
(6) AT expression
F. Assume the pseudo directive Assume
Format: Assum Segment Register: Section [, Segment Meter: Section]
Function: Create a correspondence between segment registers and segments. The directive generally appears in the code segment.
G. Settings Address Counter Pseudo Directive ORG
Format: ORG numerical expression
Function: Assign the value of the numerical expression to the assembly address counter. Numerical expressions have a non-negative integer between 0 to 65535.
H. Source program ends the pseudo directive
Format: END [expression]
Function: This statement flag is the end of the entire program, which is the last statement of the source program.
18.DOS system function call
The method of DOS system function calls can generally be divided into the following steps.
1 Set the entry parameters of the function you want to call.
2 Deposit the function number of the function to call in the AH register.
3 INT 21h instructions automatically transfer to the interrupt subroutine entry.
4 The corresponding interrupt subroutine is running, and the export parameters can be obtained as required.
Common DOS system function call
1. Single-character input (call 1)
2. Single character display (2 call)
3. Print output (call 5)
4. End call (4CH number)
5. Display string (call 9)
6. String input (10 call)
Example: Display strings on the screen "Example of String Display!"
Data segment
STR DB 0DH, 0AH, 'Example of String Display! $ '
Data ends
Stack Segment Stack
DB 100 DUP (0)
Stack ends
Code segment
Assume DS: Data, CS: Code, SS: Stack
Begin:
Mov AX, DATA
MOV DS, AX
Lea DX, STR
MOV AH, 9
Int 21h
MOV AH, 4CH
Int 21h
Code ends
End Begin
Example: Enter a string of characters from the keyboard to the input buffer, then display the input string on the display in the reverse order.
Data Segment Info1 DB 0DH, 0AH, 'Input String: $' Info2 DB 0DH, 0AH, 'Output String: $' BUFA DB 81 DB? DB 80 DUP (0) BUFB DB 81 DUP (0) DATA ENDS
Stack segment
DB 200 DUP (0)
Stack ends
Code segment
Assume DS: Data, SS: Stack, CS: Code
Start:
MOV AX, DATA MOV DS, AX LEA DX, INFO1 MOV AH, 9; No. 9 Call, Display Input Tips INT 21H LEA DX, BUFA MOV AH, 10; 10 call, keyboard input string to buffer BUFA INT 21h Lea Si, BUFA 1 MOV CH, 0; Take the character length → CX MOV CL, [Si] Add Si, CX; Si points to the string Lea Di, BUFB; DI point to string variable bufb
NEXT:
MOV Al, [Si] MOV [DI], Al Dec Si Inc Di Loop Next MoV Byte PTR [DI], '$' Lea DX, INFO2 MOV AH, 9; No. 9 Call, Display Output Tips INT 21hlea DX, BUFB MOV AH, 9; reverse display string INT 21h MOV AH, 4CH INT 21HCODE Ends End START
19. Structured programming:
The corresponding directives are: IF, THEN, ELSE, For, While, Do, et al, such as C language, I will not talk.
20. Parameter passes and stack:
The function parameters can be passed with a stack, such as Push AX, PUSH BX (these two parameters are pressed into the stack), then the stack is as follows:
AX value BP 10
BX value BP 6
Function return address BP 2
BP value BP
The subsequent PUSH operation is pressed into the stack.
21. A simple example presentation assembly tool usage process:
Tools required: Masm.exe and Link.exe, are compilers and connectors, respectively
Program source code:
;
First.asm -a first ask.ASM -A First Assembly Language Program
;
Code segment
;
_Text Segment Word Public 'Code'
Assume cs: _Text, DS: _Data, SS: _STACK
;
Add TWO 16 Bit Values
;
Addem Proc Far
Mov Ax, _data
MOV DS, AX
MOV AX, A
Add Ax, B
MOV C, AX
MOV AX, 4C00H
Int 21h
Addem ENDP
_Text Ends
_Data Segment Word Public 'Data'
A dw 3
B DW 4
C dw?
_Data Ends
_STACK Segment Para Stack 'Stack'
DB 128 DUP (?)
_STACK ENDS
End Addem
Enter the code, open the DOS window, and enter the following statement in the command line:
E: / asm> masm first
E: / asm> link first
An EXE file will be generated.
Pointing First, what is the phenomenon, and enter this command to debug our program:
Debug first.exe
-t points to a statement
-d 0 displays memory data
22. Reprint, please keep your full document, "Tianda Sentence" Original 2005.01.03 21:00 Shenzhen Nanshan Science Park
23. One of the assembly language reviews of this rookie to this end, the second is about to exit (simple narrative design of the procedures for the protection mode), please pay attention ~