简体   繁体   中英

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

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

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]

The fastest way to find very fast C equivalents of MOVSX and MOVXZ is just integer variable assignment from a type with lower bits to a type with higher bits. Both variables have to be typecasted either to signed type (for MOVSX) or unsigned type (MOVZX).

For example, C equivalent of "movzx ebx, al" would be:

(unsigned int) ebx = (unsigned char) al;

C equivalent of "movsx ebx, al" would be:

(signed int) ebx = (signed char) al;

Just make sure your char type is 8 bit and your int type is 32 bit, and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM