I'm studying how emulators like Bochs and QEMU work and had a question -- if I emulate an x86 guest operating system on an x86 host and the guest executes some instruction that assigns a register (for example, mov eax 3), is it guaranteed or even likely that the value will actually be assigned to the eax register on the actual hardware in code run by the emulator?
I'd assume that there would be no reason not to, since the QEMU process is its own separate process (so it doesn't have to share general purpose registers with any other process) and since both the host and the guest architectures are the same and therefore have the same general purpose registers.
Also, if I was to execute a program in QEMU that has two instructions:
mov eax 3
add eax 2
Would it just combine the two into a single mov eax, 5 instruction if it could determine that doing so would not alter the program's execution, since it executes programs by executing blocks of instructions instead of each instruction individually?
Please note that I'm referring to pure emulation in this case (Bochs or non-KVM QEMU) and not hardware virtualization.
qemu-x86_64-staticdoes not special case x86 on x86 emulation, it generates totally different code. Surprisingly, it is clever enough to optimize themov+addcombination. You can run it usingqemu-x86_64-static -d in_asm,out_asm test.elfand see for yourself.mov eax, 5is not equivalent tomov eax, 3 / add eax, 2. For example, the former does not change the carry flag whereas the latter resets it. You'd need a lot more context to decide that the former was a safe substitution for the latter.