Teach you to use Windows APIs to write a Thread class (do not use static) -- (2)

Source: Internet
Author: User

In this article, we mainly analyze the compilation and processing of function calls on the vsplatform. First, let's look at a simple example. The Code is as follows:

        void Hello(){        }        int main(){             Hello();        }

Then compile with Cl-fa main. cpp under vs command prompt. You will get an assembly file main. ASM with the following content:

; Listing generated by Microsoft (R) Optimizing Compiler Version 16.00.30319.01  TITLE E:\bossjue\main.cpp .686P .XMM include listing.inc .model flatINCLUDELIB LIBCMTINCLUDELIB OLDNAMESPUBLIC ?Hello@@YAXXZ     ; Hello; Function compile flags: /Odtp_TEXT SEGMENT?Hello@@YAXXZ PROC     ; Hello; File e:\bossjue\main.cpp; Line 1 push ebp mov ebp, esp; Line 2 pop ebp ret 0?Hello@@YAXXZ ENDP     ; Hello_TEXT ENDSPUBLIC _main; Function compile flags: /Odtp_TEXT SEGMENT_main PROC; Line 4 push ebp mov ebp, esp; Line 5 call ?Hello@@YAXXZ    ; Hello; Line 6 xor eax, eaxpop ebp ret 0_main ENDP_TEXT ENDSEND

Don't be afraid. I will explain these codes to you. These codes don't actually need to be looked at. You just need to look at the tags; line 1, line 2 ...... These lines are all done. First, in Lin 4, this is the start of the main function. The stack frame is saved first, and then Lin 5 jumps to the hello function to execute the code in hello. That is to say, if we write the following code, it should be correct.

void Hello(){}int main(){    __asm{        call Hello    }}


 

You can compile the program and see that the program runs normally. This indicates that the call is successful. Here, the final key step of function call is call.

The above is just a simple hello function. Below we will test it with a little difficulty. We will write the following code:

int Hello(){    return 100;}int main(){    Hello();}


 

The difference between this code and the above Code is that the function Hello has a return value. Similarly, we use Cl-fa main. cpp to compile main. ASM and Open main. ASM to see the following content:

; Listing generated by Microsoft (R) Optimizing Compiler Version 16.00.30319.01 TITLE E:\bossjue\main.cpp .686P .XMM include listing.inc .model flatINCLUDELIB LIBCMTINCLUDELIB OLDNAMESPUBLIC ?Hello@@YAHXZ     ; Hello; Function compile flags: /Odtp_TEXT SEGMENT?Hello@@YAHXZ PROC     ; Hello; File e:\bossjue\main.cpp; Line 1 push ebp mov ebp, esp; Line 2 mov eax, 100    ; 00000064H; Line 3 pop ebp ret 0?Hello@@YAHXZ ENDP     ; Hello_TEXT ENDSPUBLIC _main; Function compile flags: /Odtp_TEXT SEGMENT_main PROC; Line 5 push ebp mov ebp, esp; Line 6 call ?Hello@@YAHXZ    ; Hello; Line 7 xor eax, eax pop ebp ret 0_main ENDP_TEXT ENDSEND

In line2, we can see that there is a mov eax, 100, and we think that 100 is returned in hello. We can guess that in Vs, The eax is used to pass the return value. In fact, this is indeed the case, you can test it using the following code:

#include <iostream>using namespace std;int Hello(){    __asm{        mov eax, 100   }}int main(){    cout<<Hello()<<endl;}

We can see that the output is 100, indicating that our results are correct, and the return value of the function is stored in eax.

Next we will try again to pass the parameter to hello, to further illustrate the principle code for passing the parameter:

void Hello(int iVA, int iVB){}int main(){    Hello(3, 5);}


 

The code for compilation is as follows:

; Listing generated by Microsoft (R) Optimizing Compiler Version 16.00.30319.01  TITLE E:\bossjue\main.cpp .686P .XMM include listing.inc .model flatINCLUDELIB LIBCMTINCLUDELIB OLDNAMESPUBLIC ?Hello@@YAXHH@Z     ; Hello; Function compile flags: /Odtp_TEXT SEGMENT_iVA$ = 8      ; size = 4_iVB$ = 12      ; size = 4?Hello@@YAXHH@Z PROC     ; Hello; File e:\bossjue\main.cpp; Line 1 push ebp mov ebp, esp; Line 3 pop ebp ret 0?Hello@@YAXHH@Z ENDP     ; Hello_TEXT ENDSPUBLIC _main; Function compile flags: /Odtp_TEXT SEGMENT_main PROC; Line 5 push ebp mov ebp, esp; Line 6 push 5 push 3 call ?Hello@@YAXHH@Z    ; Hello add esp, 8; Line 7 xor eax, eax pop ebp ret 0_main ENDP_TEXT ENDSEND

As you can see, the parameter is actually passed into the stack using push, and then a call. Of course, the main function is to clear the stack (add ESP)

To test the function, write the following code:

#include <stdio.h>int main(){    char *lpstrFormat = "%s %d";    char *lpStr   = "Hello, this year is: ";    __asm{        push 2012        push lpStr        push lpstrFormat        call printf        add esp, 12   }}

Then run the command line to get the desired result.
Of course, the call method determines how to pass the parameter and who clears the stack. There are generally three call Methods: _ stdcall, _ cdecl, _ fastcall, you can write your own articles on the Internet or in a few days.

At this point, the function call process has been fully completed. If you want to know the call process of the class member function, I will explain it in the next article.

 

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.