2007-01-17 15:02:43 +00:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winnt.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "misc.h"
|
|
|
|
#include "any_op.h"
|
|
|
|
|
|
|
|
CPU_INT ConvertToIntelProcess( FILE *outfp, CPU_INT eax, CPU_INT ebp,
|
|
|
|
CPU_INT edx, CPU_INT esp,
|
|
|
|
PMYBrainAnalys pMystart,
|
|
|
|
PMYBrainAnalys pMyend, CPU_INT regbits,
|
|
|
|
CPU_INT HowManyRegInUse)
|
|
|
|
{
|
|
|
|
|
|
|
|
CPU_INT stack = 0;
|
|
|
|
CPU_UNINT tmp;
|
|
|
|
CPU_INT setup_ebp = 0 ; /* 0 = no, 1 = yes */
|
|
|
|
|
2007-01-17 16:23:39 +00:00
|
|
|
/* Fixme at moment we can not optimze code */
|
|
|
|
//if (HowManyRegInUse > 9)
|
|
|
|
if (HowManyRegInUse > 4)
|
2007-01-17 15:02:43 +00:00
|
|
|
{
|
|
|
|
setup_ebp =1; /* we will use ebx as ebp */
|
2007-01-17 15:31:47 +00:00
|
|
|
stack = HowManyRegInUse * regbits;
|
2007-01-17 15:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(outfp,"BITS 32\n");
|
|
|
|
fprintf(outfp,"GLOBAL _main\n");
|
|
|
|
fprintf(outfp,"SECTION .text\n\n");
|
|
|
|
fprintf(outfp,"; compile with nasm filename.asm -f win32, ld filename.obj -o filename.exe\n\n");
|
|
|
|
fprintf(outfp,"_main:\n");
|
|
|
|
|
|
|
|
/* setup a frame pointer */
|
2007-01-17 15:31:47 +00:00
|
|
|
|
|
|
|
if (setup_ebp == 1)
|
|
|
|
{
|
|
|
|
fprintf(outfp,"\n; Setup frame pointer \n");
|
|
|
|
fprintf(outfp,"push ebx\n");
|
|
|
|
fprintf(outfp,"mov ebx,esp\n");
|
|
|
|
fprintf(outfp,"sub esp, %d ; Alloc %d bytes for reg\n\n",stack,stack);
|
|
|
|
}
|
2007-01-17 16:23:39 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
0 EAX
|
|
|
|
1 ECX
|
|
|
|
2 EDX
|
|
|
|
5 EBX
|
|
|
|
6 ESP
|
|
|
|
7 EBP
|
|
|
|
8 ESI
|
|
|
|
9 EDI
|
|
|
|
*/
|
|
|
|
}
|
2007-01-17 15:02:43 +00:00
|
|
|
|
|
|
|
fprintf(outfp,"; Start the program \n");
|
2007-01-17 15:31:47 +00:00
|
|
|
while (pMystart!=NULL)
|
2007-01-17 15:02:43 +00:00
|
|
|
{
|
|
|
|
/* fixme the line lookup from anaylysing process */
|
|
|
|
|
|
|
|
/* mov not full implement */
|
|
|
|
if (pMystart->op == OP_ANY_mov)
|
|
|
|
{
|
|
|
|
printf("waring OP_ANY_mov are not full implement\n");
|
|
|
|
|
|
|
|
if ((pMystart->type & 8)== 8)
|
|
|
|
{
|
|
|
|
/* dst are register */
|
|
|
|
tmp = stack - (pMystart->dst*regbits);
|
|
|
|
|
|
|
|
if ((pMystart->type & 16)== 16)
|
|
|
|
{
|
|
|
|
/* source are imm */
|
|
|
|
|
2007-01-17 15:31:47 +00:00
|
|
|
if (pMystart->dst == eax)
|
2007-01-17 15:02:43 +00:00
|
|
|
{
|
|
|
|
if (pMystart->src == 0)
|
|
|
|
fprintf(outfp,"xor eax,eax\n");
|
|
|
|
else
|
|
|
|
fprintf(outfp,"mov eax,%llu\n",pMystart->src);
|
|
|
|
}
|
|
|
|
else if (pMystart->dst == ebp)
|
|
|
|
{
|
|
|
|
if (pMystart->src == 0)
|
|
|
|
fprintf(outfp,"xor ebp,ebp\n");
|
|
|
|
else
|
|
|
|
fprintf(outfp,"mov ebp,%llu\n",pMystart->src);
|
|
|
|
}
|
|
|
|
else if (pMystart->dst == edx)
|
|
|
|
{
|
|
|
|
if (pMystart->src == 0)
|
|
|
|
fprintf(outfp,"xor edx,edx\n");
|
|
|
|
else
|
|
|
|
fprintf(outfp,"mov edx,%llu\n",pMystart->src);
|
|
|
|
}
|
|
|
|
else if (pMystart->dst == esp)
|
|
|
|
{
|
|
|
|
if (pMystart->src == 0)
|
|
|
|
fprintf(outfp,"xor esp,esp\n");
|
|
|
|
else
|
|
|
|
fprintf(outfp,"mov esp,%llu\n",pMystart->src);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-17 16:23:39 +00:00
|
|
|
if (setup_ebp == 1)
|
|
|
|
fprintf(outfp,"mov dword [ebx - %d], %llu\n", tmp, pMystart->src);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(outfp,"unsuported optimze should not happen it happen :(\n", tmp, pMystart->src);
|
|
|
|
}
|
2007-01-17 15:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* end pMyBrainAnalys->type & 8 */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return */
|
|
|
|
if (pMystart->op == OP_ANY_ret)
|
|
|
|
{
|
2007-01-17 15:31:47 +00:00
|
|
|
if (pMyBrainAnalys->ptr_next == NULL)
|
|
|
|
{
|
2007-01-17 16:23:39 +00:00
|
|
|
if (setup_ebp == 1)
|
|
|
|
{
|
|
|
|
fprintf(outfp,"\n; clean up after the frame \n");
|
|
|
|
fprintf(outfp,"mov esp, ebx\n");
|
|
|
|
fprintf(outfp,"pop ebx\n");
|
|
|
|
}
|
2007-01-17 15:31:47 +00:00
|
|
|
}
|
2007-01-17 15:02:43 +00:00
|
|
|
fprintf(outfp,"ret\n");
|
|
|
|
}
|
|
|
|
pMystart = (PMYBrainAnalys) pMystart->ptr_next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|