In assembly language, moving (or copying) data into a register is typically done using the MOV instruction. The MOV instruction is one of the most fundamental and frequently used instructions in assembly language programming. It is used to transfer data from one location to another, including between registers, memory, and immediate values (constants).
The MOV instruction has the following general syntax:
MOV destination, source
An immediate value is a constant (literal) number specified directly in the instruction.
MOV register, immediate_value
MOV AX, 100 ; Move the integer 100 into register AX
100 (an immediate integer) is moved into the AX register.MOV AX, -50 ; Move the integer -50 into register AX
-50 (a negative integer) is moved into the AX register.MOV AX, 0x1A ; Move the hexadecimal value 0x1A (26 in decimal) into AX
0x1A is a hexadecimal literal representing the decimal value 26, and it's moved into AX.You can move the contents of one register into another register.
MOV destination_register, source_register
MOV BX, AX ; Move the value in AX into BX
AX is copied into the BX register. After this instruction, both AX and BX will have the same value.MOV EBX, EAX ; Move the value in 32-bit register EAX into 32-bit register EBX
EAX (a 32-bit register) contains an integer, and it is moved into EBX (another 32-bit register).You can load an integer value from a memory location into a register.
MOV register, [memory_location]
MOV AX, [num] ; Load the integer at memory location 'num' into register AX
num is loaded into the AX register.MOV EAX, [myVar] ; Load the value at the memory address 'myVar' into 32-bit register EAX
myVar is a memory location containing a 32-bit integer. The value of myVar is loaded into EAX.You can also store a value from a register into memory.
MOV [memory_location], register
MOV [num], AX ; Store the value of AX into memory location 'num'
AX is stored at the memory location labeled num.MOV [myVar], EAX ; Store the value in 32-bit register EAX into memory location 'myVar'
EAX is stored in the memory location myVar.Below is an example program demonstrating various ways of moving integer values into registers.
section .data
num1 dw 50 ; Define a word (16-bit integer) with value 50
num2 dw -30 ; Define a word with value -30
section .text
global _start
_start:
; Moving immediate values into registers
MOV AX, 100 ; AX = 100 (move immediate value)
MOV BX, -50 ; BX = -50 (move negative integer)
; Moving values from memory to registers
MOV AX, [num1] ; AX = 50 (load value of num1 into AX)
MOV BX, [num2] ; BX = -30 (load value of num2 into BX)
; Moving values between registers
MOV CX, AX ; CX = 50 (move value of AX into CX)
; Store the value of AX back to memory location num1
MOV [num1], AX ; Store the value in AX (50) into memory location 'num1'
; Exit (Linux system call to exit the program)
MOV AX, 1 ; Syscall number for exit
INT 0x80 ; Call kernel
Immediate Value Movement:
100 and -50 into registers AX and BX, respectively.Memory to Register Movement:
num1 (which contains 50) and num2 (which contains -30) into registers AX and BX.Register to Register Movement:
AX is copied into CX.Register to Memory Movement:
AX (which is 50) is stored back into the memory location num1.Size of Registers: Ensure that the size of the register matches the size of the data being moved. For example, if you want to move a 16-bit integer, use a 16-bit register like AX or BX. For 32-bit values, use EAX or EBX. For 64-bit values (in 64-bit systems), use RAX, RBX, etc.
Memory Alignment: In certain architectures, memory must be aligned for efficient access. For example, on x86 systems, 16-bit values should be aligned to even memory addresses, and 32-bit values should be aligned to addresses that are divisible by 4.
Immediate Values: When moving immediate values, remember that the number must be within the range that can be represented by the size of the operand. For example, in an 8-bit register, the immediate value should be in the range of -128 to 127 (for signed values).
The MOV instruction is crucial for moving integer values into registers in assembly language. It can handle various data types and sources, including immediate values, memory locations, and other registers. By using MOV, you can efficiently transfer data and manipulate it within your program, which is essential for low-level programming, system programming, and performance-critical applications.
Open this section to load past papers