assemblyx86opcodefasmmov

Different opcode for mov instruction than expected in x86


Let's say I have a very simple instruction mov eax,12345h. I assembled it using fasm and generated a bin file. When i checked the opcode of above instruction using hex editor it gave 66 B8 45 23 01 00 as the equivalent hex code. Now from what I have studied, I was expecting the first byte to be 0xb8 to represent the "load a 32-bit constant into eax" instruction and rest four bytes to be same as above. The aritcle that I mentioned also states that the same 0xb8 instruction that loads a 32-bit constant into eax can be used with a 0x66 prefix to load a 16-bit constant but clearly I am not loading a 16 bit constant in my program. Still the resulting opcode in my case starts with 66 as the first byte. Now I am not able to understand from where this 66 came ? Is there any different behavior in case of fasm ?


Solution

  • The 66h prefix byte is the Operand Size prefix. When used in 32 bit code, it specifies a 16 bit operand size, and when used in 16 bit code it specifies a 32 bit operand.

    So you're assembling 16 bit x86 code. When loading a 32 bit value into a 32 bit register, the operand size prefix is required.