Assembly Language---------Wang Shuang Chapter 4

The process of a source program from writing to execution

The process of a source program from writing to execution is divided into three steps:

  1. Write assembly source program
  2. Compile and link the source program
  3. Executing the program
    in the executable file According to the description information in the executable file, the operating system loads the machine code and data in the executable file into the memory, and performs related initialization. For example, set CS:IP to point to the first instruction to be executed.
    The following is a simple assembly language 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

1. Pseudo instructions
In the assembly language source program, there are two types of instructions, one is assembly instructions and the other is pseudo instructions. Assembly instructions are instructions with corresponding machine code, which can be compiled into machine instructions and finally executed by the CPU. The pseudo-instruction has no corresponding machine instruction and will not be executed by the CPU in the end. Pseudo instructions are instructions executed by the compiler. The compiler performs related compilation work according to pseudo instructions.
There are three kinds of directives in the above source program.
XXX segment
XXX ends

segment and ends are a pair of pseudo-instructions used in pairs. This is a pair of pseudo-instructions that must be used when writing an assembler that can be compiled by a compiler. segment indicates the beginning of a segment, and end indicates the end of a segment. A segment must have a name to identify it, using the format:
segment name segment

The section name ends when
an assembler is composed of multiple sections. These sections are used to store code, data or as stack space. All the information of a source program that will be processed by the computer: instructions, data, and stacks are divided into different segments.

end

End is an end tag in an assembler. During the process of compiling the assembler, the compiler will end the compilation of the source program if it encounters the pseudo-instruction end. Don't confuse end and ends, end is the end of the assembler, and ends is the end of the segment.

assume

The meaning of this pseudo-instruction is hypothetical . We don't need to understand the function of this instruction deeply. We just need to remember to associate the segment with a specific purpose with the relevant segment register. For example, in the above program, we use codesg segment... codesg ends to define a segment named codesg, which is a code segment, so at the beginning of the program we use assume cs: codesg to associate the segment address of the code segment with the CS in the CPU.

Label

In the assembly source program, in addition to assembly instructions and pseudo-instructions, there are some labels, such as "codesg". A label refers to an address, such as codesg in front of the segment, as the name of a segment, the name of this segment will eventually be compiled and processed by the linker as the segment address of a segment.

The operating mechanism of the program

Our program is first stored in the source program in the form of assembly instructions. After being compiled and linked, it is converted into machine code and stored in an executable file.
If a program P2 is in the executable file, there must be a running program P1. Load P2 from the executable file into the memory, and transfer the control of the CPU to P2, so that P2 can run. After P2 starts running, P1 suspends running, and when P2 finishes running, the control of the CPU should be returned to the program P1 that enables it to run.
Now we know that the program that returns control of the CPU to make it run is called program return . In assembly language, we implement the program return with the following two sentences.

mov ax,4c00H
int 21h

Concepts related to the end

purpose Related instructions Nature of instruction Command executor
Notify the compiler of the end of a section Section name ends Pseudo-instruction When compiling, executed by the compiler
Notify the compiler of the end of the program end Pseudo-instruction When compiling, executed by the compiler
Program return mov ax,4c00H int 21H Assembly instructions When executed, it is executed by the CPU

Write assembler

Edit source program

We can edit the source program in many ways, such as txt text file or other editors can be used to edit the source program, here I use notepad++.

Compile the source program

Compilation and editing look a lot like they are actually two concepts. Editing is writing source programs, and compiling is translating source programs into object codes. Here we can use the masm software provided by Microsoft to write assembly language and run it under win10. I will post the URL at the end of the article. If you need it, you can see it yourself.

link

The file generated after compiling the source program is XXX.obj, which is not the executable file that can be executed finally, and the step of linking must be carried out. Linking is the process of linking other source programs or subroutines of other libraries used in your source program to finally generate executable files.
How to implement the above steps under WIN10Look at this link URL: Windows10 build assembly environment-detailed steps

Guess you like

Origin blog.csdn.net/weixin_47617598/article/details/115016589