In Assembly Language (ASM), moving immediate data into a register or memory is a common operation. The MOV instruction is used for this purpose. This instruction transfers data from one place (source) to another (destination).
MOV destination, sourcedestination: Where the data will be moved to (e.g., a register or memory location).source: The data to be moved (this can be a register, memory location, or immediate value).
In the case of moving immediate data, the source will be an immediate value (a constant value), and the destination will typically be a register or memory.
section .data ; Data segment (if needed)
; You can define variables here
section .text ; Code segment
global _start ; Make the entry point visible to the linker
_start:
MOV EAX, 10 ; Move the immediate value 10 into the EAX register
MOV EBX, 20 ; Move the immediate value 20 into the EBX register
; Exit the program (Linux syscall example)
MOV EAX, 60 ; Syscall number for exit (in x86-64 Linux)
MOV EDI, 0 ; Exit code 0
syscall ; Invoke the syscall to exitMOV EAX, 10: Moves the immediate value 10 into the EAX register.MOV EBX, 20: Moves the immediate value 20 into the EBX register.- The final lines show how the program exits gracefully using a Linux syscall (
syscall).
section .data
result db 0 ; Reserve a byte of memory initialized to 0
section .text
global _start
_start:
MOV BYTE [result], 100 ; Move immediate value 100 into the memory location 'result'
; Exit the program (Linux syscall example)
MOV EAX, 60 ; Syscall number for exit
MOV EDI, 0 ; Exit code 0
syscall ; Invoke the syscall to exitMOV BYTE [result], 100: Moves the immediate value 100 into the memory location labeled asresult.- Here,
resultis a variable (byte-sized) declared in the.datasection.
section .text
global _start
_start:
MOV AX, 1234h ; Move the immediate value 1234h (hexadecimal) into the 16-bit register AX
MOV AL, 56h ; Move the immediate value 56h into the 8-bit register AL
; Exit the program (Linux syscall example)
MOV EAX, 60 ; Syscall number for exit
MOV EDI, 0 ; Exit code 0
syscall ; Invoke the syscall to exitMOV AX, 1234h: Moves the hexadecimal value1234hinto the 16-bit AX register.MOV AL, 56h: Moves the hexadecimal value56hinto the 8-bit AL register.
- 8-bit data: Can be moved into 8-bit registers (e.g.,
AL,BL). - 16-bit data: Can be moved into 16-bit registers (e.g.,
AX,BX). - 32-bit data: Can be moved into 32-bit registers (e.g.,
EAX,EBX). - 64-bit data: Can be moved into 64-bit registers (e.g.,
RAX,RBXin x86-64).
-
Move Immediate Data to a 32-bit Register:
MOV EAX, 0x12345678 ; Move the hexadecimal value 0x12345678 into the 32-bit register EAX
-
Move Immediate Data to a 64-bit Register (x86-64 Architecture):
MOV RAX, 0x1122334455667788 ; Move the 64-bit value into the 64-bit register RAX
-
Move Immediate Data to a Variable in Memory:
MOV DWORD [some_memory], 0xDEADBEEF ; Move the value 0xDEADBEEF into the memory location 'some_memory'
- The size of the immediate value and the destination must be compatible. For example, an 8-bit value can't be moved into a 16-bit register without truncation or explicit type casting.
- Addressing modes: When dealing with memory, you can use various addressing modes to specify the exact memory location.