Calling assembly code in VC environment

Source: Internet
Author: User

In the VC environment, the assembly code is called.

Programming Environment: VC6 and MASM 6.11

The main program is as follows:

Masm_C.cpp:Defines the entry point for the console application.

//

<?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

#include <stdio.h>

extern "C" int _cdecl Add (int a);

void Main ()

{

int a = 1;

printf ("Before add (a) a =%d/n", a);

A = Add (a); Add implements a simple addition to a sub-function, which is implemented using a compilation

printf ("After add (a) a =%d/n", a);

Return

}

Its assembly code is:

; Add.asm

.386

Public _add

_text segment byte Public ' code '

Assume Cs:_text

_add proc Near

Push EBP

MOV Ebp,esp

mov eax,[ebp+8]; here ebp+8 is the value of the passed parameter a

inc EAX; add eax as a return value

Pop EBP

Ret

_add ENDP

_text Ends

End

This section of assembly language is compiled with ML but not connected

D:/MASM611/BIN>ML/C/coff Add.asm

Microsoft (R) Macro assembler Version 6.11

Copyright (C) Microsoft Corp 1981-1993. All rights reserved.

Assembling:add.asm

Generate Add.obj.

The next step is to link CPP and ASM together. Copy the add.obj to the Masm_c directory, and project-----setting------link to add add.obj to the project options in link, separated from the other lib spaces. Passed after compilation.

The result is:

Before add (a) a = 1

After add (a) a = 2

Press any key to continue

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.