Linking to C + + in Protected Mode

Source: Internet
Author: User

Linking to C + + in Protected Mode

Programs written for x86 processors running in Protected mode can sometimes has bottlenecks

That must is optimized for runtime Efficiency. If They is embedded systems, they may

Stringent memory size Limitations. With such goals in mind, we'll show how to write external

Procedures in assembly language The can is called from C and C + + programs running in Protected Mode. Such programs consist of at Least-modules:the first, written in assembly

language, contains the external procedure; The second module contains the C + + code that

Starts and ends the Program. There is a few specific requirements and features of C + + that

Affect the Assembly Code.

Arguments ( actual argument ) Arguments is passed by C + + programs from right to left, as they appear in

The argument List. After the procedure returns, the calling program was responsible for cleaning up

The Stack. This can is done by either adding a value to the stack pointer equal to the size of the

Arguments or popping an adequate number of values from the STACK.

External Identifiers In the assembly language source, specify the C calling convention in

The. MODEL directive and create a prototype for each procedure called from an external C + +

Program

.586

. Model Flat,c

Asmfindarray PROTO,

srchval:dword, arrayptr:ptr DWORD, Count:dword

declaring the Function In a C program, use the extern qualifier if declaring an external

Assembly Language Procedure. For example, the is how to declare asmfindarray:

extern bool Asmfindarray (long n, long array[], long count);

If the procedure would be called from a C + + program, add a "C" qualifier to prevent C + + name

Decoration:

extern "C" bool Asmfindarray (long n, long array[], long count);

name Decoration is a standard C + + compiler technique that involves modifying a function Name

With extra characters, that indicate, the exact type of each function parameter. It's required in any

language that supports function overloading (both functions have the same name, with different

Parameter lists). From the assembly language Programmer's point of view, the problem with

Name Decoration is, the C + + compiler tells the linker to look for the decorated name rather

than the original one when producing the executable file.

13.3.1 Using Assembly Language to Optimize C + + Code

One of the ways you can use assembly language to optimize programs written in other languages

Is-to-look-for-speed bottlenecks. Loops is good candidates for optimization because any

Extra statements in a loop is repeated enough times to has a noticeable effect on your program ' s

Performance.

Most C + + compilers have a command-line option, automatically generates an assembly

Language listing of the C + + Program. In Microsoft Visual C + +, for example, the listing file can

contain any combination of C + + source code, assembly code, and machine code, shown by the

Options in Table 13-2. Perhaps the most useful was /fas, which shows how C + + statements are

Translated into Assembly Language.

Table 13-2 Visual C + + command-line Options for ASM Code Generation.

Command Line

Contents of Listing File

/fa

Assembly-only Listing

/FAc

Assembly with machine code

/fas

Assembly with source code

/facs

Assembly, Machine code, and source

Findarray Example

Let's create a program this shows how a sample C + + compiler generates code for a function

Named Findarray. later, we'll Write an assembly language version of the function, attempting

To write more efficient code than the C + + Compiler. The following Findarray function (in C + +)

Searches for a single value in an array of long integers:

BOOL Long Long Long Count) {for (int0; i < count; i++) {if(array[i] = = Searchval) return true ;} return false ;}

Findarray Code Generated by Visual C + +

Let's look at the assembly language source code generated by Visual C + + for the Findarray

function, alongside the function ' s C + + source Code. This procedure is compiled to a Release

Target with no code optimization in Effect:

;Listing generated by Microsoft (R) optimizing Compiler Version 15.00.30729.01TITLEc:\users\stud\documents\visual Studio -\projects\findarray\findarray\findarray.cpp. 686P. XMM include Listing.Inc. Model Flatincludelib msvcrtdincludelib oldnamespublic [email protected]@[email protected] 
    ;FindarrayExtrn__rtc_shutdown:procextrn__rtc_initbase:PROC;comdat RTC$TMZ;File c:\users\stud\documents\visual Studio 2008\projects\findarray\findarray\findarray.cppRTC$TMZ SEGMENT__RTC_SHUTDOWN.RTC$TMZ DDFLAT:__RTC_SHUTDOWNRTC$TMZ ENDS;comdat Rtc$imzRtc$imz Segment__rtc_initbase.rtc$imz DDFLAT:__rtc_initbase;Function Compile flags:/odtp/rtcsu/ziRtc$imz ENDS;comdat [email protected]@[email protected]_text segment_i$5245 =-8                        ;size = 4_searchval$ =8                        ;size = 4_array$ = a                        ;size = 4_count$ = -                        ;size = 4[email Protected]@[email protected] PROC;findarray, comdat;line 7    PushEBPmovebp, ESPSubEsp204                ;000000ccH    PushebxPushESIPushEDILeaedi, DWORD PTR [ebp-204]    movEcxWuyi                    ;00000033H    moveax,-858993460                ;CCCCCCCCH    Rep Stosd;line 8    movDWORD PTR _i$5245[ebp],0    jmpshort [email protected]$[email protected]:    moveax, DWORD PTR _i$5245[ebp]Addeax1    movDWORD PTR _i$5245[ebp], eax$[email protected]:    moveax, DWORD PTR _i$5245[ebp]CMPeax, DWORD PTR _count$[ebp]Jgeshort [email protected];line Ten    moveax, DWORD PTR _i$5245[ebp]movecx, DWORD PTR _array$[ebp]movedx, DWORD PTR [ecx+eax*4]    CMPedx, DWORD PTR _searchval$[ebp]jneshort [email protected]; line One    movAl1    jmpshort [email protected]$[email protected]:; line    jmpshort [email protected]$[email protected]:; line    XORal, al$[email protected]:; line    PopEDIPopESIPopebxmovesp, EBPPopEBPret    0[email protected]@[email protected] ENDP;Findarray_text endspublic _wmain;Function Compile flags:/odtp/rtcsu/zi;comdat _wmain_text segment_argc$ =8                        ;size = 4_argv$ = a                        ;size = 4_wmain PROC;Comdat; line    PushEBPmovebp, ESPSubEsp192                ;000000c0h    PushebxPushESIPushEDILeaedi, DWORD PTR [ebp-192]    movEcx -                    ;00000030H    moveax,-858993460                ;CCCCCCCCH    Rep Stosd; line    XOReax, eax; line    PopEDIPopESIPopebxmovesp, EBPPopEBPret    0_wmain endp_text ENDSEND

Specify THE/FA switch for the CL Compiler. Depending on the value of the switch either only assembly code or high-level Code and assembly code is Integrated. The filename gets. asm file Extension. Here is the supported Values:

    • /FA Assembly code;. ASM
    • /FAC Machine and Assembly code;. cod
    • /fas Source and Assembly code;. ASM
    • /facs machine, source, and assembly code;. cod

Click "Project", Double Click "configuration properties", "Choose", "c + +", Click "Output Files"

Three 32-bit arguments were pushed on the stack in the following order:count, array, and
Searchval. Of these three, array is the one-passed by reference Because-c + +, an array
Name is a implicit pointer to the array ' s first Element. The procedure saves EBP on the stack and
Creates space for the local variable i by pushing a extra doubleword on the stack (figure 13–1).

Inside the procedure, the compiler reserves local stack space for the variable i by pushing ECX
(line 9). The same storage is released at the end when the EBP was copied back to ESP (line 14).
There is instructions between the labels $L 284 and $L 285, which constitute the main body
of the Loop. We can easily write an assembly language procedure That's more efficient than the
Code shown Here.

Linking to C + + in Protected Mode

Related 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.