开发者

Assembly how to translate JNE to C Code without ZF flag access

开发者 https://www.devze.com 2023-04-13 02:32 出处:网络
ASM to C Code emulating nearly done.. just trying to solve these second pass problems. Lets say I got this ASM function

ASM to C Code emulating nearly done.. just trying to solve these second pass problems.

Lets say I got this ASM function

401040  MOV EAX,DWORD PTR [ESP+8]
401044  MOV EDX,DWORD PTR [ESP+4]
401048  PUSH ESI
401049  MOV ESI,ECX
40104B  MOV ECX,EAX
40104D  DEC EAX
40104E  TEST ECX,ECX
401050  JE 401083
401052  PUSH EBX
401053  PUSH EDI
401054  LEA EDI,[EAX+1]
401057  MOV AX,WORD PTR [ESI]
40105A  XOR EBX,EBX
40105C  MOV BL,BYTE PTR [EDX]
40105E  MOV ECX,EAX
401060  AND ECX,FFFF
401066  SHR ECX,8
401069  XOR ECX,EBX
40106B  XOR EBX,EBX
40106D  MOV BH,AL
40106F  MOV AX,WORD PTR [ECX*2+45F81C]
401077  XOR AX,BX
40107A  INC EDX
40107B  DEC EDI
40107C  MOV WORD PTR [ESI],AX
40107F  JNE 401057
401081  POP EDI
401082  POP EBX
401083  POP ESI
401084  RET 8

My program would create the following for it.

int Func_401040() {
    regs.d.eax = *(unsigned int *)(regs.d.esp+0x00000008);
    regs.d.edx = *(unsigned int *)(regs.d.esp+0x00000004);
    regs.d.esp -= 4;
    *(unsigned int *)(regs.d.esp) = regs.d.esi;
    regs.d.esi = regs.d.ecx;
    regs.d.ecx = regs.d.eax;
    regs.d.eax--;
    if(regs.d.ecx == 0)
        goto label_401083;
    regs.d.esp -= 4;
    *(unsigned int *)(regs.d.esp) = regs.d.ebx;
    regs.d.esp -= 4;
    *(unsigned int *)(regs.d.esp) = regs.d.edi;
    regs.d.edi = (regs.d.eax+0x00000001);
    regs.x.ax = *(unsigned short *)(regs.d.esi);
    regs.d.ebx ^= regs.d.ebx;
   开发者_开发技巧 regs.h.bl = *(unsigned char *)(regs.d.edx);
    regs.d.ecx = regs.d.eax;
    regs.d.ecx &= 0x0000FFFF;
    regs.d.ecx >>= 0x00000008;
    regs.d.ecx ^= regs.d.ebx;
    regs.d.ebx ^= regs.d.ebx;
    regs.h.bh = regs.h.al;
    regs.x.ax = *(unsigned short *)(regs.d.ecx*0x00000002+0x0045F81C);
    regs.x.ax ^= regs.x.bx;
    regs.d.edx++;
    regs.d.edi--;
    *(unsigned short *)(regs.d.esi) = regs.x.ax;
    JNE 401057
    regs.d.edi = *(unsigned int *)(regs.d.esp);
    regs.d.esp += 4;
    regs.d.ebx = *(unsigned int *)(regs.d.esp);
    regs.d.esp += 4;
    label_401083:
    regs.d.esi = *(unsigned int *)(regs.d.esp);
    regs.d.esp += 4;
    return 0x8;
}

Since JNE 401057 doesn't use the CMP or TEST

How do I fix that use this in C code?


The most recent instruction that modified flags is the dec, which sets ZF when its operand hits 0. So the jne is about equivalent to if (regs.d.edi != 0) goto label_401057;.

BTW: ret 8 isn't equivalent to return 8. The ret instruction's operand is the number of bytes to add to ESP when returning. (It's commonly used to clean up the stack.) It'd be kinda like

return eax;
regs.d.esp += 8;

except that semi-obviously, this won't work in C -- the return makes any code after it unreachable.

This is actually a part of the calling convention -- [ESP+4] and [ESP+8] are arguments passed to the function, and the ret is cleaning those up. This isn't the usual C calling convention; it looks more like fastcall or thiscall, considering the function expects a value in ECX.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号