N7: C embedded ASM dynamic call function (stdcall)

Source: Internet
Author: User

#include <cstdio>#include <cstdlib>#include <cstring>#include <memory>#include <string>#include <cstdarg>#include <map>#include <vector>#include <windows.h>using namespace std;#define CCT_AALIGN0x4#define CCT_SALIGN0x8#define CCT_E_SUCCESS0xF0000000#define CCT_E_UNKNOWNTYPE 0xF0000001#define CCT_E_WRONGNATYPE 0xF0000002#define CCT_E_MULTRETUAN0xF0000004#define CCT_RT0x10000000#define CCT_UNK0x1000#define CCT_INT0x1001#define CCT_320x1002#define CCT_DOUBLE0x1004#define CCT_640x1008#define CCT_NA0x1010//----------------------------------------------------------------------------struct cct { unsigned t; unsigned rn; unsigned bc; unsigned mc; char* b; cct* n;cct():t(CCT_UNK), rn(0), bc(0), mc(0), b(0), n(0){};void newbuf(unsigned size) { if (b) free(b); b=(char*)malloc(size); }virtual ~cct() { if (b) free(b); }}; // end of cct#define cct_astype(a,t) (t&)(*(t*)a.b)//----------------------------------------------------------------------------#define ASM_RET_SPACE(rtc,rta) \_asmmov eax, dword ptr[rtc]\_asmcmp eax, 4\_asmjle rsD\_asmsub esp, eax\_asmmov dword ptr[rta], esp\_asm rsD:\#define ASM_RET_ADDR(rta) \_asmpush dword ptr[rta]\#define ASM_SAVERETURN(v_buf, rtc) \_asmmov edx, dword ptr[v_buf]\_asmmov ecx, dword ptr[rtc]\_asmcmp ecx, 4\_asmjg rtL\_asmmov dword ptr[edx], eax\_asmjmp srD\_asm rtL:   cmp ecx, 0\_asmjz srD\_asmmov esi, dword ptr[eax]\_asmmov dword ptr[edx], esi\_asmadd eax, 4\_asmadd edx, 4\_asmsub ecx, 4\_asmjmp rtL\_asm srD:\#define ASM_MOVE_32P(v_buf) \_asmsub esp, 4\_asmmov edx, dword ptr[v_buf]\_asmmov eax, dword ptr[edx]\_asmmov dword ptr[esp], eax\#define ASM_MOVE_64P(v_buf) \_asmsub esp, 8\_asmmov ecx, esp\_asmmov edx, dword ptr[v_buf]\_asmmov eax, dword ptr[edx]\_asmmov dword ptr[ecx], eax\_asmadd ecx, 4\_asmmov edx, dword ptr[v_buf]\_asmmov eax, dword ptr[edx+04h]\_asmmov dword ptr[ecx], eax\#define ASM_MOVE_naP(v_buf, v_bc, v_mc) \_asmmov ecx, dword ptr[v_bc]\_asminc ecx\_asmmov eax, 4\_asmmul ecx\_asmmov edx, dword ptr[v_buf]\_asmsub esp, eax\_asmmov esi, esp\_asmdec ecx\_asm napL:cmp ecx, 0\_asmjz TAIL\_asmmov eax, dword ptr[edx]\_asmmov dword ptr[esi], eax\_asmadd edx, 04h\_asmadd esi, 04h\_asmdec ecx\_asmjmp napL\_asm TAIL:mov ecx, dword ptr[v_mc]\_asmcmp ecx, 3\_asmjnzM2\_asmmov al, byte ptr[edx]\_asmmov byte ptr[esi],al\_asminc edx\_asminc esi\_asmmov ax, word ptr[edx]\_asmmov word ptr[esi],ax\_asmjmp naPD\_asm M2:cmp ecx, 2\_asmjnz M1\_asmmov ax, word ptr[edx]\_asmmov word ptr[esi],ax\_asmjmp naPD\_asm M1:cmp ecx, 1\_asmjnz naPD\_asmmov al, byte ptr[edx]\_asmmov byte ptr[esi],al\_asm naPD: \#define ASM_STDCALL_CALL(v_pfunc) \_asm call v_pfunc\void cc_call(cct* a, void* p) {int err=0;int rtc=0;int rta=0;cct *r=0;int v_eax=0;char* v_rb=0;while (a&&!err) {char* b=a->b;int v_bc=a->bc; int v_mc=a->mc;if (a->t&CCT_RT) {if (rtc) { err=CCT_E_MULTRETUAN; }else {switch(a->t&~CCT_RT) {case CCT_INT: case CCT_32: rtc=sizeof(int); break;case CCT_DOUBLE: case CCT_64: rtc=sizeof(double); break;case CCT_NA: { if (a->bc+a->mc) {rtc=(a->bc+1); rtc<<=2; }else { err=CCT_E_WRONGNATYPE; }} break;default: err=CCT_E_UNKNOWNTYPE;}if (rtc) { r=a;r->newbuf(rtc);r->t=CCT_RT;v_rb=r->b;ASM_RET_SPACE(rtc, rta);}}} else {switch(a->t) {case CCT_INT: case CCT_32: { ASM_MOVE_32P(b);} break;case CCT_DOUBLE: case CCT_64: { ASM_MOVE_64P(b);} break;case CCT_NA: { ASM_MOVE_naP(b, v_bc, v_mc);} break;default: err=CCT_E_UNKNOWNTYPE;}}a=a->n;}if (!err) { if (rtc) {ASM_RET_ADDR(rta);}ASM_STDCALL_CALL(p);if (rtc) {ASM_SAVERETURN(v_rb, rtc);}} else { printf("err:\t%d\n", err); }}//----------------------------------------------------------------------------struct Large1{ char c; short s; int i; };struct Large2{ char c; double d; };struct Large3{ char b[5]; };struct Large4{ char b[4]; };struct Large5{ char b[6]; };struct Large6{ char b[13]; }; int aa(int a, Large1 l) { unsigned mass=0x10000000; return a+l.c+l.s+l.i; }double bb(double a, char b) { return a+b; }void __cdecl cc(double a) { printf("cc:\t%f\n", a); }void __stdcall dd(Large2 a) { printf("dd:\t%f\n", a.d); }void __stdcall ee(Large3 a) { printf("ee:\t%d\n", sizeof(Large3)); }void __stdcall ff(short s, char c) { printf("ff:\t%d,%c\n", s, c); }void __stdcall gg(const char* s) { printf("gg:\t%s\n", s); }void __stdcall hh(void (__stdcall*p)(const char* s)) { printf("hh:\t0x%x\n", p); }void __stdcall ii(int i) { printf("ii:\t%d\n", i); }void __stdcall jj(double d) { printf("jj:\t%f\n", d); }void __stdcall kk(Large4 l) { printf("kk:\t%s\n", l.b); }void __stdcall ll(Large4 l) { printf("ll:\t%s\n", l.b); }Large6 __stdcall mm(Large6 l) { return l; /*mov         eax,dword ptr [ebp+8]  mov         ecx,dword ptr [l]  mov         dword ptr [eax],ecx  mov         edx,dword ptr [ebp+10h]  mov         dword ptr [eax+4],edx  mov         ecx,dword ptr [ebp+14h]  mov         dword ptr [eax+8],ecx  mov         dl,byte ptr [ebp+18h]  mov         byte ptr [eax+0Ch],dl  mov         eax,dword ptr [ebp+8]  */}Large6 __stdcall nn(Large6 l) { int len=strlen(l.b);for(int i=0;i<len;++i) { l.b[i]+=1; }printf("nn:\t%s\n", l.b);return l;}int __stdcall oo() { printf("oo:\t99\n"); return 99; }//----------------------------------------------------------------------------int main() {{/* int gap=0x2468;Large1 l; l.c=0x40; l.s=0x24; l.i=0x68;unsigned mass=0x00000;gap=aa(0x123, l);double a=1.0;char b=0x20;a=bb(a,b);*/}{ double d=12.34;_asm {fldqword ptr[d]subesp, 8fstpqword ptr[esp]call  [cc]addesp, 8 // __cdecl}} {Large2 l2; l2.c=0x68; l2.d=56.78;/*sub         esp,10h  mov         eax,esp  mov         ecx,dword ptr [l]  mov         dword ptr [eax],ecx  mov         edx,dword ptr [ebp-24h]  mov         dword ptr [eax+4],edx  mov         ecx,dword ptr [ebp-20h]  mov         dword ptr [eax+8],ecx  mov         edx,dword ptr [ebp-1Ch]  mov         dword ptr [eax+0Ch],edx  call        dd */ dd(l2);} {Large3 l3;/*sub         esp,8  mov         eax,esp  mov         ecx,dword ptr [l3]  mov         dword ptr [eax],ecx  mov         dl,byte ptr [ebp-34h]  mov         byte ptr [eax+4],dl  call        ee*/ ee(l3);} {short s=0x45; char c='A';/*movzx       eax,byte ptr [c]  push        eax  movzx       ecx,word ptr [s]  push        ecx  call        ff*/ ff(s, c);} {const char* s="abc";/*mov         eax,dword ptr [ebp-5Ch]  push        eax  call        gg*/ gg(s);}{/*push        offset ggcall        hh*/ hh(&gg);}/*_asm {push 999call dword ptr[Sleep]} */{cct p;p.newbuf(sizeof(int));p.t=CCT_INT;cct_astype(p, int)=13579;cc_call(&p, &ii);}{cct p;p.newbuf(sizeof(double));p.t=CCT_DOUBLE;cct_astype(p,double)=5678.9;cc_call(&p, &jj);}{cct p;p.newbuf(sizeof(Large4));p.t=CCT_32;strcpy(p.b, "abc");cc_call(&p, &kk);}{cct p;p.newbuf(sizeof(Large5));p.t=CCT_NA;p.bc=1;p.mc=2;strcpy(p.b, "abcde");//cc_call(&p, &ll);}{Large6 l;strcpy(l.b, "god");Large6 r=mm(l);printf("mm:\t%s\n", r.b);}{cct pr; pr.t=CCT_NA+CCT_RT;pr.bc=3; pr.mc=1;cct p; p.newbuf(sizeof(Large6));p.t=CCT_NA; p.bc=3; p.mc=1;strcpy(p.b, "abcdefg");pr.n=&p;cc_call(&pr, &nn);printf("nn:\t%s\n", pr.b);}{cct pr2; pr2.t=CCT_INT+CCT_RT;cc_call(&pr2, &oo);printf("oo:\t%d\n", *pr2.b+12);}printf("END!\n");return 0;}//----------------------------------------------------------------------------//#define CCT_ROUND(s,round) ((s)+(round)-1)&~((round)-1)int cct_align(int PackAlign, const vector<int>& List) {int result=0;/*int count=List.size();if (count > 0) {int i=1;int max_size=0;int min_size=0;for (auto n : List) {max_size=max(max_size,n);min_size=min(PackAlign,n);if (i) { i=0; result=n; printf("-%d\n", result); }else {result=CCT_ROUND(result+n, min_size);printf("*%d\n", result);}}min_size=min(PackAlign,max_size);result=CCT_ROUND(result, min_size);printf("+%d\n", result);}*/return result;}

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.