Assembly Converting MOV / MOVZX and MOVSX to C code (no inline asm)

SSpoke picture SSpoke · Oct 14, 2011 · Viewed 10.8k times · Source

For the asm emulator i'm trying to write to convert ASM code to equivalent working code just working.. best code would be the one that can either be done in one line or two-three the most, don't care about speed.

From my understanding. MOVZX would be the same as MOV.. if done in C++.

MOV conversion.

MOV ESI,DWORD PTR [ESP+8]

would be like

regs.d.esi = *(unsigned int *)(regs.d.esp+0x00000008);

MOVZX conversion.

MOVZX EAX,BYTE PTR DS:[EDI]

would be like

regs.d.eax = *(unsigned char *)(regs.d.edi);

pretty much the same thing no change what so ever.

Now MOVSX i'm having trouble converting to a simple C code.. seems to be the same as the two above.. except it attempts to append as much fully set bits in front of the value moved as possible.. like

000000C7 becomes FFFFFFC7

Answer

ughoavgfhw picture ughoavgfhw · Oct 14, 2011

movsx is move with sign-extend. Those set bits are a copy of the sign bit from the original value, and would be clear if the original wasn't negative. It works just like your other conversions, except you need to use a signed type instead of an unsigned one.

regs.d.eax = *(signed char *)(regs.d.edi); // movsx eax, byte ptr ds:[edi]