Wang Shuang-assembly language-comprehensive study four-programming without using main function

Source: Internet
Author: User

(a) The purpose of the study

Using C language Programming, do we have to use the main function?

(ii) The research process

1) The initial procedure

First, we write a C language program that does not write the main function.

The procedure is as follows:

No errors were found during the compilation process. The following errors were found during the linking process:

Link appears undefined symbol ' _main ' in module c0s

Such an error message, perhaps the main function is related to the C0s.obj file.

At this point we think, C language compiled after the file suffix name is what? Is. obj. What is the file name after assembly language compilation? Also. obj. Are these two files linked?

In theory, their content should be similar. And all should be compiled by Link.exe.

We try Link:

We found no errors.

2) program with main function

We write a program with a function of main

We compile the link. Get M.exe

3) Find a different

We have compiled and connected the C language program with main and without main to the. exe, so we'll look for their differences. The code is the same, and the difference is only the main one, and the difference is focused on main. This allows us to focus on the functionality of main.

First, let's take a look at the file details.

We see that the size of F.exe and M.exe is still a lot worse. In conjunction with what we saw in previous debugs, there are many unknown programs before the first statement of the program after compiling the link. In our analysis, it is possible that there is a main function in M, which adds a lot more statements to the program during the compilation of the connection.

We run the two programs separately.

The running results of the program are as follows:

We see both of the running scenarios as shown above, the difference being that the M.exe runs back into DOS, and F.exe runs directly after it dies.

What is the reason? We think of a RET instruction in the main function when we look at the function implementation in two. At the time we analyzed this because the C language would also implement the main function for subroutines. Did the program not return?

We continue to analyze the differences. We use debug to load.

First look at both the-R and-G scenarios:

Here we intuitively see, program m Code snippet length is 0EB8, program F code snippet length is 001D. And the program M can return correctly after execution, and the program m cannot return after execution.

After reviewing its disassembly code:

F.exe as follows:

M.exe as follows:

The middle section does not repeat

We can see that there is a difference in the implementation within the function, in the F.exe, there is a lack of three instructions, RET; PUSH BP; MOV bp,sp; (semicolon only to differentiate statements). This is the statement that the main function returns and resumes the register BP.

There is also a difference, is the addition of 01FA before the part, in the F.exe is not.

Let's analyze this added piece of code.

Based on our previous understanding, first, main was used as a subroutine, and second, a lot of code was added to main at compile time. Is it that the added code calls main and implements the function that the program returns correctly?

We look at the previously added code, we know that the offset address of main is 01FA, then we can find a statement similar to call 01FA. We found here at the end:

This shows that our conjecture is correct, the main function before these programs, called the main function. Furthermore, we found the following code:

We do this here:

What does that mean? Main before adding the program has so two functions, first, call the main function; second, make the program return correctly. Of course, there are other features, but these two features are the most basic.

4) The role of c0s

As we have just seen, a program without the main function will have a COS file error when linking, and the c0s file is an. obj file. So, is he able to be linked to an. exe file? If you can, then we can see the assembly code.

Although there was an error, C0S.exe was generated. We debug load view.

We see that the code in c0s is basically the same as the code that was added before main. In other words, we can assume that the program in front of main is related to c0s.

5) procedure to generate EXE

Quoting the words in the book:

Tc.exe connect c0s.obj with user. obj to generate the. exe. The EXE program generated by this method runs the following procedure:

The program in the ①c0s.obj runs first, and initializes the relevant initialization. If you request resources, set up related registers such as DS,SS.

The program in ②c0s.obj calls the main function, which starts from the user program.

The ③ user program runs the end of the program returned from the main program to C0s.obj.

The ④c0s.obj program then runs, carrying out the related resource release, environmental recovery work.

⑤c0s.obj program calls the DOS int 21h routine 4CH number interrupt function, the program returns.

6) Write your own c0s.obj

Based on the above understanding, we have prepared such a program in the Compendium:

Compile and copy it to the Minic folder, replacing the original c0s.obj.

We are now compiling the F.C we originally wrote and found the link to be successful.

After we debug load the anti-compilation, its code is as follows:

We see that the offset address of call here is 0012, which is the first statement of our F function. After running discovery:

The program returns normally. This means that the c0s.obj we have written already implements the function returned by the call.

7) Study a program

We write a program like this:

We see that this program differs from the last program in that buffer does not request memory, but instead directly assigns a value of zero. We guessed that the location of the write was ds:[0]. We verify that:

We see that a-f this letter is written to the ds:[0] place.

(iii) Appendix Study

We found c0.asm such a file in the TC2.0 environment. We thought that this would be associated with c0s.oobj.

We opened it and found that its statement was so consistent with the statement after we c0s the link:

We tried to compile the link c0.asm and found that there was a situation where Rules.asi,emuvars.asi was not found. We copy it and compile it successfully. Find its disassembly code as follows:

The same code that follows the C0s.obj link. This is what we can know, C0.asm is the source code of c0s.

And we open the other two files with Notepad:

Most of this is defined by constants.

(iv) Research sentiment

The main function or the F function? It doesn't matter. It is important to add changes to the program during the connection process. In order to ensure that the program can be called and returned normally, as well as some other functions implemented, C language added so long code. Seeing this substance through main will allow us to learn more deeply.

Wang Shuang-assembly language-comprehensive study four-programming without using main function

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.