Contents
The MOV Opcode
So far, you would have read about registers and memory, but there was no mention on how to transfer data to the memory location or register. So let us embark on the journey to learn how to move data from memory to register, from register to memory, from register to register and setting the value in the registers and memory. Of course I will describe some tricks (ie some size optimization) along the way.
Loading memory or register with a constant
To load a register with a constant you do the following:
mov eax, 10
In the above example, the opcode
mov moves the constant 10 to eax. This means that the value in eax is now 10 after the instruction.
To load a memory with a constant you do the following:
mov [memory], 10
In the above example, the opcode
mov moves the constant 10 to [memory]. The brackets tells the assembler that the label is a memory for most assembler (Though some assemblers ignores it. Refer to the assembler manuals for more details). This means that the value in [memory] is now 10 after the instruction.
[h2]So how does
mov work?[/h2]
The opcode
mov works in the following method (simplified):
mov dest, source
dest = source
Where dest can be register or memory and source can be register, memory or constant.However, do note that both the source and dest cannot be memory at the same time. You cannot use
mov opcode to copy from memory to memory directly. Moving data from a memory location to another will be discussed later. This convention might look strange to HLL coders but fear not. One will get used to it after a while. Most opcode are in the form
opcode dest, source. Of course if you do not like the above convention, you can use other assemblers like HLA or GAS.
To move data at certain memory location to register:
mov eax, [memory] ; or any other register
To move data from register to memory location:
mov [memory], eax ; or any other register
To move data from register to register:
mov eax, ecx ; or any other register
Moving data from a memory location to another
As mentioned above, the
mov opcode cannot move data from one memory location to another memory location. Don't panic, you still can move data from a memory location to another by another method. You either temporary copy to an available register and then copy it to the memory location or you make use of the stack.
mov eax, [memory] ;or any other available register
mov [memory2], eax
OR
push [memory]
pop [memory2]