当前位置:网站首页>Assembly learning Chapter 4 of assembly language (Third Edition) written by Wang Shuang

Assembly learning Chapter 4 of assembly language (Third Edition) written by Wang Shuang

2022-06-22 07:05:00 starmultiple

Chapter four The first program

4.1 The process of a source program from writing to execution

  1. First step : Write assembly source program .( Text editor Edit、 Notepad, etc ) Generate a text file that stores the source program .
  2. The second step : Compile and connect the source program . Use assembly language compiler to compile the source program in the source program file , Generate target file ; Then connect the object file with the linker , Generate executable files that can be run directly in the operating system ( Including procedures and data , And related description information ).
  3. The third step : Execute the program in the executable file .

4.2 Source program

1. Pseudo instruction

Here is an example of a source program 4.2.1( Get into DOS, Use Eidt see 4.3 Edit the source program )

assume cs:codesg

codesg segment
             
             mov ax,0123H
             mov bx,0456H
             add ax,bx 
             add ax,ax

             mov ax,4c00H
             int 21H
codesg ends

end

Above appear 3 A pseudo instruction
(1)XXX segment

​ XXX ends

segment and end When writing an assembler that can be compiled by the compiler , A pair of pseudo instructions that must be used .

codesg segment  ; Define a segment , The name of the segment is “codesg”, This section starts from here 
      :
codesg ends     ; The name is “codesg” That's the end of the paragraph 

(2)end

end Is the end of an assembler , In the process of compiling an assembler , If you run into a fake command end Just finish compiling the source code . therefore , When we write a program, we add pseudo instructions at the end end.

(3)assume

assume( hypothesis ). It assumes that a segment of a register and a program use segment…ends The defined segment is associated with . adopt assume Explain the connection , In case of need , The compiler can associate a segment register with a specific segment .

2. In the source program “ Program ”

A program written in assembly language , Including pseudo instructions and assembly instructions , The ultimate goal of our programming is to let the computer complete certain tasks . The assembly instructions in the source program make up the program that is finally executed by the computer , The pseudo instructions in the source program are handled by the compiler , They don't achieve the ultimate goal of our programming .

In the future, all the contents in the source program file can be called source program , The source program is finally executed by the computer 、 Processing instructions or data , It's called procedure . The program first exists in the source program in the form of assembly instructions , Compiled 、 Connect and change to machine code , Stored in an executable file

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-IinrZ0pZ-1655781089521)(C:\Users\ Zhong Yixin \AppData\Roaming\Typora\typora-user-images\image-20220620162502386.png)]

3. label

Assembly source program , In addition to assembly instructions and pseudo instructions , There are also some labels , such as “codesg”. A label refers to an address . such as codesg stay segment In front of , As the name of a segment , The name of this section will eventually compile the program 、 The linker processes as a segment address

4. Program structure

Let's now discuss the structure of the assembler . before 3 In this chapter, we all Debug Write such as assembly instructions to write assembler , This is really convenient for very short programs . But for freshman programs, we write source programs that can be compiled by the compiler .

The source program is made up of segments . We can store code in these segments 、 data 、 Or treat a segment as a stack space . Now let's finish a small program step by step , Experience simple frameworks .

Mission : Programming operations 2^3. How to write the source program ?

(1) We need to define a segment , The name is abc.

abc segment
    :
abc ends

(2) Write assembly instructions in this section , To fulfill our mission .

abc segment
 
    mov ax,2
    add ax,ax
    add ax,ax
    
abc ends

(3) then , Point out where the program ends

abc segment

    mov ax,2
    add ax,ax
    add ax,ax
    
abc ends

end

(4)abc Used as a code snippet , therefore , Should be abc and cs Connect .( Of course , For this program , It's not necessary to do this .)

Named as an example 4.2.4( We'll use that later )

assume cs:abc

abc seqment

       mov ax,2
       add ax,ax
       add ax,ax
    
abc ends

end

5. Program return

Our program was first stored in the source program in the form of assembly instructions , Compiled 、 Connect and change to machine code , Stored in an executable file , that , How does it work ?

below , We are DOS( A single task operating system ) On the basis of , Briefly discuss this problem .

A journey P2 In the executable , Then there must be a running program P1, take P2 After loading from the executable file, such as memory , take CPU Give control of to P2, such P2 To run .P2 After starting operation P1 Pause .

and P2 After running , Should be CPU Control of the is returned to the program that makes it work , We call this process : Program return .( That is, add the returned program segment at the end of the program )

When we look at it 4.2.1 Examples of pseudo instructions in

mov ax,4c00H
int 21H

The function of these two instructions is program return .

The following table shows the concepts related to closure

Purpose Related instructions The nature of the instruction Instruction executor
Notifies the compiler of the end of a segment Disnomer ends Pseudo instruction Compile time , By compiler
Notifies the compiler that the program is finished end Pseudo instruction Compile time , By compiler
Program return mov ax,4c00H int21H Assembly instruction Execution time , from CPU perform

6. Grammatical and logical errors

After the source program is compiled , The error that occurs at run time is a logic error . Grammatical errors are easy to find , It's also easy to find , It's also easy to solve . And logic errors are usually not easy to detect .

Program 4.2.4 There will be some problems at runtime , Because the program did not return , Of course , This error cannot be shown when compiling , in other words , Program 4.2.4 The right program for the compiler .

Next 4.2.4 rewrite , A syntax error has occurred :

aume cs:abc

abc segment

    mov ax,2
    add ax,ax
    add ax,ax
    
end

aume Not recognized , And the compiler can't know in the process of compiling abc Where does the paragraph end .

We will correct the above procedure :

assume cs:abc

abc segment

    mov ax,2
    add ax,ax
    add ax,ax
    
    mov ax,4cooH
    int 21H
abc ends

4.3 Edit the source program

You can use any text editor to edit the source program , As long as it is finally saved as a plain text file . In our course , Use DOS Under the Edit.

The process is as follows

(1) Get into DOS The way , function Edit  Insert picture description here

(2) stay Edit Edit program in , Pictured  Insert picture description here
And name f:\1.asm

4.4 compile

stay 4.3 After editing the source program in , Get a source program file f:\1.asm. It can be compiled , Generate an object file containing machine code .
Before compiling a source program, we must first find a corresponding compiler . In our course , Using Microsoft masm5.10 Assembly compiler , The file named masm.exe. Suppose the assembler is in F:\ASM Under the table of contents . You can follow the following procedure to compile the source program , With f:\1.asm For example .
(1) Get into DOS The way , Get into F:\ASM Catalog , function masm.exe, Pictured  Insert picture description here In the figure , function masm after , First, some version information is displayed , Then prompt for the name of the source program file to be compiled . Be careful ,“[.ASM]” Prompt us , The default file extension is asm, such as , The file name of the source program to be compiled is “p1.asm”, Just type... At the end “p1” that will do . But if the source file is not in asm For extension , Just enter its full name . For example, the source file name is “p1.txt”, Just enter the full name .
When you enter the source program file name, be sure to indicate the path it is in . If the file is in the current path , Just enter the file name , But if the file is in another directory , Enter the path , such as , Files to compile p1.txt stay “c:\windows\desktop” Next , Enter “c:\windows\desktop” Next , Enter “c:\windows\desktop\p1.txt”.
here , The file we're going to compile is c Under the packing directory 1.asm, So enter here “c:\1.asm”
(2) Enter the name of the source program to compile , Press Enter key , Screen display  Insert picture description here In the figure , After entering the source program file name , The program continues to prompt us to enter the name of the target file to be compiled , The object file is the final result of compiling a source program . Notice what's on the screen :“[1.OBJ]”, Because we have entered the source program file named 1.asm, By default, the compiler will output a target file named 1.obj, So you don't have to specify another file name . Directly by Enter key , The compiler will generate... In the current directory 1.obj file .
here , You can also specify the directory where the generated target file is located , such as , I want the compiler to be in “c:\windows\desktop” Generate the target file under 1.obj. You can enter “c:\windows\destop\1”.
Let's press Enter key , Use the target file name set by the compiler .

(3) After determining the name of the target file , The screen displays as shown in the figure

 Insert picture description here In the figure , The compiler prompts for the name of the list file , This file is the intermediate result produced by the compiler in the process of compiling the source program into the object file . You can have the compiler not generate this file , Directly by Enter Press the key .
(4) After ignoring the list file generation , Screen display  Insert picture description here In the figure , The compiler prompts for the name of the cross reference file , This file is the same as the list file , It is the intermediate result produced by the compiler in the process of compiling the source program into the object file . You can have the compiler not generate this file , direct enter
(5) After ignoring the generation of cross reference file , Pictured  Insert picture description here In the figure , The compilation of the program is over , The last two lines of compiler output tell us that the source program has no warning errors and errors that must be corrected .
Above we compile , In compiler masm.exe Current path to run , A new file will appear :1.obj, This is for the program 1.asm The result of compiling . Of course , If an error occurs during compilation , Then you will not get the target file . In general, there are two types of errors that prevent us from getting the desired object file :
(1) In the program “Severe Errors”:
(2) The given source file cannot be found .
( During the compilation process , We provide an input , Both source program files , The best you can get is 3 Outputs : Target file .obj、 List file .lst、 Cross references .crf, This article only discusses the object file )

4.5 Connect

After compiling the source program to get the object file , We need to connect to the target file , So you get the executable file . Continue the process of the previous section , We're right f:\1.asm Compile to get f:\1.asm\1.obj, Now I will f:\1.asm\1.obj Connect as c:\masm\1.exe.
(1) We use Microsoft's Overlay Linker3.64 The connector , The file named link.exe, The following is an example
Be careful ,“[.OBJ]” Prompt us , The default file extension is obj, For example, the target file name to connect to is “p1.obj”, Just type in here “p1” that will do . But if the file is not obj Extension name , Just enter its full name . For example, the target file name is “p1.bin”, Just enter the full name .
When entering the target file name , Be careful to point out where it's going . here , The file to be connected is in the current directory 1.obj, So enter here “1”.
 Insert picture description here
(2) After entering the name of the target file to connect to , Press Enter key , The screen displays as shown in the figure
 Insert picture description here chart in , After entering the target file name , The program continues to prompt us to enter the name of the executable file to be generated , The executable file is the final result of our connection to the next program . Notice what's on the screen :“[1.EXE]", Because the target file name has been determined as 1.obj, The default executable file name of the program is 1.EXE, So you don't have to specify another file name . Directly by Enter key , The compiler will be in the current directory , Generate 1.EXE file .

here , You can also specify the directory where the generated executable file is located , such as , I want the linker to be in “c:\windows\desktop" Generate executable file under 1.EXE, You can enter “c:lwindowsIdesktoplI".

Let's press Enter key , Use the executable file name set by the linker .

(3) After determining the name of the executable file , The screen display is shown in the following figure .

 Insert picture description here Upper figure in , The connector prompts for the name of the image file , This file is the intermediate result of the linker connecting the object file to an executable file , You can make the linker not generate this file , Directly by Enter Press the key .

(4) Ignored After the image file is generated , The screen displays as shown in the figure 4.13 Shown .

chart 4.13 in , The linker prompts for the name of the library file . The library file contains some subroutines that can be called , If the program calls a subroutine in a library file , It needs to be connected , Put this library file

Connect to the target file , Generate executable files . however , No subroutines are called in this program , therefore , The input of the library file name is ignored here , Directly by Enter Press the key .

 Insert picture description here (5) After ignoring the connection of library files , Show
 Insert picture description here
error : No stack segments , No explanation here

summary :
The function of connection
(1) When the source is large , It can be divided into multiple source files to compile , After each source program is compiled into an object file , And then connect them together with a linker , Generate an executable file :

(2) A subroutine in a library file is invoked in the program , You need to connect this library file with the object file generated by the program , Generate an executable file ;

(3) After a source program is compiled , Get the object file with machine code , Some contents in the target file can not be directly used to generate executable files , The linker processes this into the final executable information . therefore , In only one source file , Without calling a subroutine in a library , You must also use the linker to process the target file , Generate executable files .

Be careful , For the connection process , The executable is the end result we want to get .

4.6 Compile and connect in a simplified way

In the previous content , How to use masm and link Compile and connect . It can be seen that , We compile 、 The ultimate goal of connection is to generate executable files from source program files . The intermediate files generated in this process can be ignored . The following is a simple compilation 、 How to connect .

  1. compile

 Insert picture description here 2. Connect
 Insert picture description here

4.7 Tracking of program execution process

It can be used Debug To track the running process of a program ,
Here is the executable file generated in the previous content 1.exe For example , Explain how to use Debug Track the execution process of the program .

Now we know that , stay DOS When running a program in , By command Load a program from an executable file into memory , And make it work . however , In this way, we can't see the execution process of the program one by one , because command The program load , Set up CS:IP The operation to the entry of the program is continuous , And when CS:IP Point to the entry to the program ,command Gave up CPU Control right ,CPU Start running the program immediately , Until the end of the program .

In order to observe the running process of the program , have access to Debug. Debug You can load the program into memory , Set up CS:IP Point to the entry to the program , but Debug Don't give up on CPU The control of , such , We can use Debug To step through the program , View each The execution result of an instruction .
 Insert picture description here cx The length of the program is stored in . Insert picture description here

原网站

版权声明
本文为[starmultiple]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220701302222.html