First contact with C language bare function

Source: Internet
Author: User

First on the code:

Naked.cpp:

#include "stdafx.h"

extern "C" int __declspec (naked) add2 (int x,int y)//reference description supports cross-file invocation
{
__asm{//function environment initialization (stack, protect field, fill buffer)
PUSH EBP
MOV Ebp,esp
SUB esp,0x40
PUSH EBX
PUSH ESI
PUSH EDI
LEA Edi,dword PTR ss:[ebp-0x40]
MOV EAX,0XCCCCCCCC
MOV ecx,0x10
REP Stosd
}
__asm{//Function function
MOV Eax,dword PTR ss:[ebp+0x8]
ADD Eax,dword PTR ss:[ebp+0xc]
}
__asm{//Restore existing descending stack return
POP EDI
POP ESI
POP EBX
MOV ESP,EBP
POP EBP
Ret
}
}

====================================================

Base.cpp

#include "stdafx.h"

extern "C" int add2 (int,int); declaring external functions

int _tmain (int argc, _tchar* argv[])
{


Test
int sum=0;

printf ("Add2 (50,76):%d\n", Add2 (50,76));

return 0;



Today deepens understanding of a C language function of the operating mechanism.

First contact with C language bare 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.