user3466304 user3466304 - 1 year ago 115
C++ Question

How do cpu instructions work?

In a 32 bit processor as I understand, each instruction is 32 bits. So, for the MOV instruction in assembly, how do you only use 32 bits for the op code plus the parameters? So for:

MOV register, [address]

Doesn't the address take up 32 bits by itself? So wouldn't that take up the entire instruction?
Maybe I have it all wrong, but I'm trying to implement a VM in C/C++. Help?

Answer Source

x86 instructions have variable length. The CPU starts reading instruction with first byte, identifies the "opcode" then keeps reading following bytes depending on actual instruction.

I stopped debugger (Visual Studio) at random point and its disassembly window has an option "Show Code Bytes" which gives an example of instruction length. Have a look below:

enter image description here

In particular, have a look at line with mov [ebp-15Ch], eax which is close to mentioned in your question. The corresponding bytes include A4 FE FF FF which is 32-bit value for -15Ch.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download