Converting MOV instructions to machine code issue
Posted: Wed Jan 29, 2020 6:13 am
Hi.
I am trying to understand how to code assembly instructions to machine codes.
At the moment I'm focusing only on MOV mnemonic.
The CPU is in the 32-bit protected mode, therefore the REX.w is cleared.
Besides, I am not going to talk about Mod R/M, SIB, and Prefix bytes in the following.
To learn how a simple coding must be performed, I made several case studies. They are:
To build up the coding bits, I look at the bit 0 of the OpCode byte.
I said if it is cleared, it means a 8-bit register is involved and if set, there is a 16/32-bit register involved.
Then I look at the bit 1 of the OpCode byte.
If set it means a register is the destination and if cleared, a memory is the destination of the instruction.
Then bits [2...7] should manifest the opcode itself. I found that for MOV mnemonic, it would be 100010 in binary.
Based the upper assumptions, and using the proper Mod R/M byte, I could truly code the first three instructions.
But when it comes to the 4th instruction, the bit pattern of the OpCode byte is no longer true. I cannot assume that MOV opcode is 100010.
Now the question:
Does it mean that to code properly a MOV instruction, I have to keep in mind two MOV patterns? One for when no immediate is involved and one only for the immediate case?
Best regards.
Iman.
I am trying to understand how to code assembly instructions to machine codes.
At the moment I'm focusing only on MOV mnemonic.
The CPU is in the 32-bit protected mode, therefore the REX.w is cleared.
Besides, I am not going to talk about Mod R/M, SIB, and Prefix bytes in the following.
To learn how a simple coding must be performed, I made several case studies. They are:
Code: Select all
MOV EAX, DWORD[0xAABBCCDD] : 8B 05 DD CC BB AA
MOV AX, WORD[0xAABBCCDD]: 66 8B 05 DD CC BB AA
MOV AL, BYTE[0xAABBCCDD]: 8A 05 DD CC BB AA
MOV EAX, imm32: B8 DD CC BB AA ( but why this and NOT 8A DD CC BB AA ? )
I said if it is cleared, it means a 8-bit register is involved and if set, there is a 16/32-bit register involved.
Then I look at the bit 1 of the OpCode byte.
If set it means a register is the destination and if cleared, a memory is the destination of the instruction.
Then bits [2...7] should manifest the opcode itself. I found that for MOV mnemonic, it would be 100010 in binary.
Based the upper assumptions, and using the proper Mod R/M byte, I could truly code the first three instructions.
But when it comes to the 4th instruction, the bit pattern of the OpCode byte is no longer true. I cannot assume that MOV opcode is 100010.
Now the question:
Does it mean that to code properly a MOV instruction, I have to keep in mind two MOV patterns? One for when no immediate is involved and one only for the immediate case?
Best regards.
Iman.