Assembly Language Learning Chapter fourth-the first program

Source: Internet
Author: User

This blog series reference from << assembly Language >> third edition, author: Wang Shuang

In the previous chapters we learned some instructions intermittently, but never complete an executable file, an. exe file, in assembly language. From this chapter we will begin to use assembly language and compile the compiler to link a complete executable file.


4.1 The process of a source program from write-out to execution

As shown, for a complete assembler to the execution of the procedure:


Process can be summarized in the following steps:

(1) Compiling assembler program according to assembly language grammar rules and target program engineering. This step is written in a text editor or in some Ides.

(2) Compile a link by compiling the assembler program compiled by the compiler. The process of compiling the. obj target file, and then linking the. obj file to an executable file. (The executable file consists of two parts: part of the program and data (the program is assembly language compiled by the compiler to translate the machine code, the data is defined in the program data), the other part of the relevant descriptive information (including the program size, the amount of memory required to run the program)).

(3) The machine code of the CPU executing executable file. The operating system can load the file into memory based on the description of the executable file, executing the first program instruction, such as CS:IP.


4.2 Source Program

We write under DOS Edit tool as shown in the section of the assembler, and it is in the root of the C disk to save as Vpoet.asm Note that the assembly source program suffix is asm.


There are three pseudo-directives in the above program (assembly instructions and pseudo-directives exist in assembly language, where the assembly instructions are translated by the compiler into machine instructions and then executed by the CPU, and the pseudo-directives are translated and executed by the compiler).

Pseudo-directive

1. Pseudo-directive XX segment

XX ends

Define a segment,XX segment represents the beginning of the segment,xx ends represents the end of the segment. A assembler is often composed of multiple segments, code snippets, data segments, and stack waits. These segments do not have to exist, but often we separate the code, data, and stack space, which facilitates later maintenance and management of the program. A meaningful assembler has at least one segment that is used to store the code.


2. Pseudo-directive end

The pseudo-directive end represents the assembler's technique, and the compiler often does not know when the program is finished at compile time, so it is necessary to find the pseudo-directive of end to know that the program has ended.


3. Pseudo-directive assume

The pseudo-directive assume is used to associate a segment register with a segment in the program, and the definition of this segment is defined by the pseudo-directive XX segment and XX ends. For example, we define a CODESG segment in the program above and then associate the segment with the code snippet through assume CS:CODESG


"Programs" in the source program

The source program refers to the entire assembler, but it is obvious that the assembler is only the assembly instructions will be translated into machine code and CPU execution, and the pseudo-instruction is not executed by the CPU, so we will be the source program assembly instructions called the program. The program compiles a link into an executable program, as shown in:


Label

In addition to assembly instructions and pseudo-instructions, the assembler also has a label, such as CODESG, which represents a segment, and the segment represented by the assume is associated with the segment register. The number is converted to a segment address after the final compilation and linking.


Return of the program

There are two lines of code in the above program:

MOV ax,4c00h

int 21H

These two lines of code implement the function that the function returns, then what is function return? The function returned is to return the CPU control to the program that called the function after the program has finished running. For example, we are running the program P1, at this time we P1 to invoke the program P2 to make it run, then P1 call P2 after the control of the CPU to P2, waiting for the P2 after the completion of the program returns the CPU control back to P1.

An end-related instruction or pseudo-directive that has been removed:



Syntax errors and logic errors

Syntax errors are syntax errors written by the program, such as assume written asume. This error will be prompted at the time of compilation.

Logic errors are errors in program logic, such as forgetting function returns. This error will be at the time of operation.

It is clear that grammatical errors are easy to detect and correct, and logic errors tend to be more subtle. 】


4.3 compiling

This blog post system is consistent with the compilers used in Wang Shuang books as masm5.0. To compile the link, you first need to download the masm5.0 compiler, and then unzip it.

Now we will unzip the MASM into the C-packing directory under the MASM folder.

Next we go to the command line, switch to the MASM directory, type the command MASM command, as shown in the following:


Now we're going to compile the assembler vpoet.asm just written in the C root directory:

Always enter ignores the build list file. LST and cross-reference files. CRF and other intermediate files. When the 0 Warning errors and 0 servere errors are displayed, the compilation is successful.

The generated vpoet.obj target file can be seen in the MASM directory



4.4 Links

The previous section has the Vpoet.obj target file, and then we just need to link to get the Vpoet.exe executable file.

From the command line or into the MASM directory, enter the link command:


All the way to the return, ignoring the image file. Map and links to ignore library files. Finally a warning tells us that there is no stack, this warning can be ignored regardless.


About the role of links:

1 when the source program is very large, you can separate the source program into multiple source program files to compile the target files separately and then link them together to build the executable file.

2 subroutine that calls a library file in the program, you need to link the library file with the target file generated by the program to generate the executable file.

After 31 source program compiles, obtains the machine code the target file, the target file some content also cannot produce the executable file directly, the link program will process these content as the final executor.


4.4 Compile and link in a simplified way, exe execution

Many intermediate file information is generated during the previous compilation and linking process. Here we compile and link in a much simpler way.

Compile: MASM file name. asm; (Remember to add semicolons )


Link: link vpoet; (Remember to add a semicolon )


Perform:


Do not have any output after execution, do not doubt, this function of function originally did not do any output display.



4.8 Who loads the program in the executable file into memory and makes it run?

1. When Vpoet.exe is executed directly in DOS, the running command runs the Vpoet directly into memory.

2.command sets the cs:ip of the CPU to point to the first instruction of the program, which allows the program to run.

3. After the program is finished, return to COMMAND,CPU to continue running COmmand




4.9 Tracking of program execution process

We will now debug the generated Vpoet.exe program using the Debug debugging tools we have described earlier.

First enter the Vpoet.exe directory at the command line, execute the command debug Vpoet.exe


Use R to view the contents of the Register:



Now that the program is loaded into memory, how do we look at the contents of the program, where the program is loaded into memory is unknown.

The following is a description of the DOS EXE loading process:


(1) The segment address of memory that the CPU segment register DS stores after the program loads, with an offset address of 0, the memory area where the program resides is the address: ds:0

(2) The first 256 bytes of the memory area are the PSP area, which is used to communicate to DOS. Starting from 256 is the part of the program.

So the address of the program is: sa+10h:0

To view instructions with command u:


In fact, the address is 14fc:0000 how to draw the address. Because of DS=14EC, 14EC+10=14FC is correct.

In addition CS=14FC ip=0000 can conclude that the address of the first instruction executed by the CPU is 14fc:0000 the address is also the starting address of the program and is verified again.


Stepping through the assembly instruction with the command T until int 21h is required to execute the instruction with P.

This will appear:


Indicates that the program has completed and returned, at this point, by command Q back to command.


OK, this chapter has been from a completion of the assembly process of its compilation, link and execution process, tired!!!


Assembly Language Learning Chapter fourth-the first program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.