Skip to content

Latest commit

 

History

History
119 lines (85 loc) · 4.18 KB

File metadata and controls

119 lines (85 loc) · 4.18 KB

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).

Basic Syntax of MOV Instruction:

MOV destination, source
  • destination: 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.


Example 1: Moving Immediate Data into a Register (x86 Architecture)

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 exit

Explanation:

  • MOV 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).

Example 2: Moving Immediate Data into Memory

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 exit

Explanation:

  • MOV BYTE [result], 100: Moves the immediate value 100 into the memory location labeled as result.
  • Here, result is a variable (byte-sized) declared in the .data section.

Example 3: Moving Immediate Data into 16-bit and 8-bit Registers

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 exit

Explanation:

  • MOV AX, 1234h: Moves the hexadecimal value 1234h into the 16-bit AX register.
  • MOV AL, 56h: Moves the hexadecimal value 56h into the 8-bit AL register.

Immediate Data Sizes:

  • 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, RBX in x86-64).

More Examples of Moving Immediate Data:

  1. Move Immediate Data to a 32-bit Register:

    MOV EAX, 0x12345678    ; Move the hexadecimal value 0x12345678 into the 32-bit register EAX
  2. 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
  3. Move Immediate Data to a Variable in Memory:

    MOV DWORD [some_memory], 0xDEADBEEF  ; Move the value 0xDEADBEEF into the memory location 'some_memory'

Important Notes:

  • 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.