"Assembly language" - Chapter IV Wang Shuang first program

Now we begin to write a complete assembly language program, compile and link the program with the compiling and linking them into an executable file (such as .exe), running in the operating system.

4.1 procedure from a source to write to the implementation of

....

Operating system in accordance with the description information in the executable file, the executable file machine code and data loaded into memory, and initializing it (such as setting the CS: IP to point to the first instruction to be executed), and then executed by the CPU

4.2 source code

1. directive:

In the assembler language source program, comprising two instructions, one is the assembly instructions, one is directive. Assembly instructions are machine code corresponding to the command, directive no corresponding machine code, not ultimately executed by the CPU.

Directive is an instruction executed by the compiler.

4.1 program illustrated three directives:

1 XXX segment,XXX ends

This directive one pair is used, segment a segment description starts, ends the end of a segment description

A section must have a name to identify, using the format:

Section names: segment

Section names: ends

An assembler is composed of a plurality of segments, these segments are used to store the code, data, or use as a stack space. All information in a source program to be processed by a computer: instructions, data, stack is divided into different segments.

A meaningful assembler instruction must have at least one segment, the segment used to store the code.

2 end

end end tag is a compilation of the program

Do not confuse the end and ends

3 assume

We later programming, assume there will be written with segments and associated segment registers associated to a particular purpose.

2. The source of the "Program"

Note that, after all the contents of the source file may be referred to as a source, the final source program executed by a computer, instructions or data processing, the program becomes.

3. label

Assembler source program in addition to the outer assembly instructions and directives i, and some, such as numeral "codesg", the name of this segment will eventually be compiled, linked to the segment address a program processing section.

4. The structure of the program

The program returns

Our first program is present in the assembly instructions in the form of source code, compiled after the connection into machine code, stored in the executable file, then run it how to get it?

Below, we based on DOS (a single-tasking operating system) on to briefly discuss this issue.

A program P2 in the executable file, you must have a running program P1, P2 will be loaded into memory from the executable file after the control of the CPU to the P2, P2 can be run. After P2 starts running, P1 suspended. And when P2 finished running, it should be returned to the control of the CPU to enable it to run the program.

We call this process routine returns. So, how to return it? It should be added at the end of the block to return the program:

Look at the program 4.1 two instructions:

mov ax,4c00H

int 21H

These instructions implemented feature is the routine returns.

At this stage, just know that the use of these two instructions at the end of the program the program returns can be achieved.

 6. syntax errors and logic errors

Program at compile time error was discovered compiler syntax error

Errors that occur at run-time is a logical error

4.3 editing source code

4.4 compiler

Note that in the process of compiling, we provide an input, that is the source folder. Can get up to three outputs: the target file (obj), a list of files (list), cross-reference file (.crf), these three output file, the file is the result of our ultimate goal to get, while the other two only intermediate As a result, in the compilation of course, we do not discuss these two types of files.

4.5 connection

Connecting role:

See the book

Note that for the process connection, the executable file is the end result we want to get.

4.6 in a simplified manner to compile and link

4.7 1.exe execution

4.8 Who executable program loaded into the memory and make it run?

1.exe look at the process of implementation of section 4.7 (Relevant Problems)

(1) At the prompt "c: \ masm" Enter the name of the executable file back to "1", press Enter, then, please think 4.1

(2) The program is run in 1.exe

(3) end of the run, return, display the prompt again "c: \ masmm", please think 4.2

Issue 4.1

At this point, there is a program running in the 1.exe program loaded into memory, this is what the program is running? Program after it is loaded into memory, how to make the program run?

Issue 4.2

After the run, return to where?

If you have more in-depth knowledge of DOS, then it is easy to answer. If you do not understand, see the following sections (see the book)

There is a DOS program command.com, this program has become a command interpreter in DOS, which is the shell DOS system

When DOS starts, to complete other important initialization, running command.com

In DOS, command processing various input: command or program to be executed in the file name

The question now answer questions 4.1 and 4.2 proposed:

(1): When performing 1.exe directly in DOS, the command is running, the program will 1.exe loaded into memory

(2): command setting the CPU CS: IP to point the first instruction in the program (i.e. the program entry), so that the program is run.

(3): After the run, return to the command, CPU continues to run the command

Compiled from written procedure to be performed:

 

Tracking the implementation process 4.9 program

For simple mistake, carefully check the source can be found, and for the deeply hidden errors, it is necessary for the execution of the program trace analysis will be easier to find.

Now we know, run a program in DOS when the command is loaded into memory from the program executable file, and enable it to perform. However, we can not see the instruction by instruction execution of the program, because the program load command, set CS: IP points to the entrance of the operating procedure is to complete a row, and when the CS: IP entry point to a program, give up command the control of the CPU, CPU immediately began to run the program until the end of the program.

In order to observe the process of running the program, you can use the Debug, Debug the program can be loaded into memory, set the CS: IP entry point to the program, but Debug does not give up control of the CPU, so that we can use the Debug commands related to a single step through the program, view the execution results of each instruction.

The program is loaded into memory of what? How do we know?

Here, we need to explain the program loading process .EXE file in the DOS system. Figure 4.20

In DOS using "Debug 1.exe" When you run Debug on 1.exe tracking, sequential program is loaded: command to load Debug, Debug load 1.exe. The sequence returns: the program returns from the Debug 1.exe, to return from the Debug command.

Experiment 3 programming, compiling, linking, tracing

PSP content: 256 bytes

After the real content of the program content is the PSP

 

Why CS: IP and DS the contents of the same?

 Figure 4.20

在该内存区的段地址存入ds中,初始化其他相关寄存器后,设置CS:IP指向程序的入口!!

Guess you like

Origin www.cnblogs.com/JasonPeng1/p/12116498.html