再次將C++的代碼優化
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
// 模擬ASM的 PUSH, POP 操作
template<class T>
class Stack
{
public:
?void push(T data)
?{
??vstack.push_back(data);
?}
?
?void pop(T& data)
?{
??if (vstack.empty())
??{
???data = 0;
??}
??else
??{
???data = vstack.back();
???vstack.pop_back();
??}
?}
?
private:
?vector<T> vstack;
};
typedef unsigned long DWORD;
Stack<DWORD> thisStack;
__int64 n402182 = 0xFEDCBA98;
std::vector<DWORD> g_stack;
void XChange(DWORD& a, DWORD& b)
{
?DWORD c = 0;
?c = a;
?a = b;
?b = c;
}
int SHR(DWORD& a)
{
?int nBit = a & 0x01;
?a >>= 0x01;
?return nBit;
}
void XOR_HIGH_LOW(DWORD& a)
{
?char value[2] = {0};
?memcpy(value, &a, sizeof(short));
?value[1] |= value[0];
?memcpy(&a, value, sizeof(short));
?
}
DWORD fun(DWORD nECX)
{?
?printf("0x%X\n", nECX);
??? if( nECX <= 8 )?????? // cmp ecx, 80
??? {
??printf("return\n");
??return nECX;???????? // jle short 004011F0
?}
?
??? thisStack.push(nECX);??? //? push ecx
??? DWORD nESI? = nECX;????? //? mov esi, ecx
??? nECX &= 0xFF;??????????? //? and ecx, 0FF
??? DWORD nEDI = 0x549417E7; // nEAX 值?? mov edi, eax? //////////////////////? EAX 值
???
??? if( nECX > 8 )??????????? // cmp ecx, 8; jle short 004011B0
?{
??nEDI = 0x02F23D32;??? // nEBX 值? mov edi, ebx /////////////////////?? EBX 值
??nECX /= 16;?????????? // shr ecx, 4
?}
?
?do
?{
??_asm rol nEDI, 8?????? // rol edi, 8
?}
??? while( !SHR(nECX) );???????? //SHR ecx, 1; jnz short 004011B0
???
??? nESI /= 256;?
??? nEDI &= nESI;
??? nEDI &= 0xFF;
??? thisStack.pop(nECX);
?
??? nESI = 0x80;??? // mov esi, 80
?
?do
?{
??int nResult = (nESI & nEDI);
??
??while( nResult != 0 )?????????? // test esi, edi; je short 004011EC
??{
???nEDI ^= nESI;
???nECX &= 0xFF00;?? // and ecx, 0FF00
???XOR_HIGH_LOW(nESI);????????? // xor ch, cl
???nECX ^= nESI;??????????????? // xor esi, ecx
???// n402182++;?? // push ecx; inc dword ptr [402182] // n402182 地址
???nECX = fun(nECX);?? // call 00401190
???nESI = 0x80;??? // mov esi, 80
???nResult = (nESI & nEDI);
??}
?}while( SHR(nESI) != 1);
?
?return nECX;
}
void main()
{
?fun(0x0FF01);
?//printf("0x%x\n", fun(0x0FF01));
}
?