Data Transfer Instructions in Assembly

Data transfer instructions in assembly language move data between registers, memory, and immediate values.

Move (mov)

Copies the value from the source operand to the destination operand.

mov eax, 5 ; EAX = 5 mov ebx, eax ; EBX = EAX (copy value from EAX to EBX) mov ecx, [esi] ; ECX = value at the memory address stored in ESI

Push (push) and Pop (pop)

Push places a value onto the stack, and pop retrieves the top value from the stack.

mov eax, 10 ; EAX = 10 push eax ; Push the value of EAX onto the stack pop ebx ; Pop the value from the stack into EBX

Load Effective Address (lea)

Computes the effective address of the source operand and loads it into the destination operand.

lea ebx, [esi+10] ; EBX = Effective address of (ESI + 10)

String Instructions (movs, cmps, scas, lodsb, stosb, etc.)

Specialized instructions for moving strings of data.

movs eax, dword ptr [esi] ; Move a double-word from the memory address in ESI to EAX

Exchange (xchg)

Swaps the contents of two operands.

mov eax, 5 mov ebx, 10 xchg eax, ebx ; Swap the values of EAX and EBX

Load Immediate (mov with immediate value)

Loads an immediate value into a register or memory location.

mov ecx, 20 ; Load the immediate value 20 into ECX mov dword ptr [esi], 42 ; Store the immediate value 42 at the memory address in ESI

Load Segment Register (mov with segment register)

Loads a value into a segment register (e.g., cs, ds, ss).

mov ds, eax ; Load the value in EAX into the data segment register

The following assembly language instructions transfer data between different memory locations:

; Load the value of the memory location at the address stored in the register ESI into the register EAX. mov eax, [esi] ; Store the value of the register EAX in the memory location at the address stored in the register EDI. mov [edi], eax ; Push the value of the register EAX onto the stack. push eax ; Pop the value from the top of the stack into the register EAX. pop eax ; Allocate 100 bytes of memory on the stack and store the address of the allocated memory in the register EAX. sub esp, 100h mov eax, esp

Here are some additional examples of how data transfer instructions can be used in assembly language:

; Copy the value of the memory location at the address stored in the register ESI to the memory location at the address stored in the register EDI. mov [edi], [esi] ; Initialize the array `my_array` with the values 1, 2, 3, 4, and 5. mov edi, my_array mov eax, 1 mov [edi], eax inc edi mov eax, 2 mov [edi], eax ... ; Call the function `add_numbers` with the arguments 10 and 20. push 20 push 10 call add_numbers ; The add_numbers function adds the two arguments and stores the result in the register EAX. ; Return from the function. ret

Conclusion

Data transfer instructions such as mov, push, pop, and lea, enable the movement of data between registers, memory locations, and immediate values. These instructions are fundamental for managing data within a program, facilitating operations like copying, pushing onto or popping from the stack, and loading effective addresses.