In assembly language, one of the most common operations is moving data from a variable (which resides in memory) to a register (which is part of the CPU's fast-access storage). The MOV instruction is used to copy data between registers, memory, and constants.
Moving data from a variable to a register typically involves accessing a specific memory location (where the variable is stored) and loading its value into a register for processing.
The MOV instruction in assembly language copies data from one operand to another. The operands can be a register, a memory location, or an immediate value (constant).
MOV destination, source
We will focus here on moving data from a memory location (variable) to a register.
When you want to move data from a variable (which is stored in memory) into a register, the syntax would be:
MOV register, [variable]
In this example, let's assume you have a variable var1 that is a byte-sized variable, and you want to move its value into the AL register.
section .data
var1 db 100 ; Declare a byte variable and initialize it to 100
section .text
MOV AL, [var1] ; Move the value of var1 (100) into the AL register
db 100 declares a byte-sized variable var1 with a value of 100.MOV AL, [var1] loads the value of var1 (which is 100) into the AL register.After this instruction executes, the AL register will contain the value 100.
If the variable is a word (2 bytes), you can use a 16-bit register such as AX to store the value. Here’s how to move a word-sized variable to a register:
section .data
var2 dw 500 ; Declare a word-sized variable and initialize it to 500
section .text
MOV AX, [var2] ; Move the value of var2 (500) into the AX register
dw 500 declares a word-sized variable var2 with a value of 500.MOV AX, [var2] loads the value of var2 (which is 500) into the AX register.Now, the AX register will contain the value 500.
For a double word (4 bytes), you can use a 32-bit register like EAX:
section .data
var3 dd 1000 ; Declare a double word-sized variable and initialize it to 1000
section .text
MOV EAX, [var3] ; Move the value of var3 (1000) into the EAX register
dd 1000 declares a double word-sized variable var3 with the value 1000.MOV EAX, [var3] moves the value of var3 (which is 1000) into the EAX register.Now, the EAX register will contain the value 1000.
If you want to move an entire string or array from memory to a register, you typically do it one byte (or one word) at a time, because registers are small, and a string may not fit into a register at once.
Let’s say you have a string "Hello" stored in memory, and you want to move the first character of the string ('H', ASCII code 72) into the AL register:
section .data
message db 'Hello', 0 ; Null-terminated string
section .text
MOV AL, [message] ; Move the first byte (ASCII value of 'H') into AL
"Hello" is stored in the .data section.MOV AL, [message] moves the first byte of the string ('H', ASCII value 72) into the AL register.After this, the AL register will hold the value 72 (which is the ASCII code for 'H').
In assembly, there are different types of memory addressing modes to access variables:
Direct Memory Addressing:
This is what we've seen in the examples above, where the variable is directly referenced by its name enclosed in brackets. Example: [var1].
Indirect Addressing: You can also use a pointer or index register to indirectly address a memory location. For example, the value in a register can hold the memory address of a variable, and you can access the variable via the register.
MOV BX, offset var1 ; BX = address of var1
MOV AL, [BX] ; Move the byte at the address in BX into AL
MOV SI, 2 ; Set index to 2 (3rd element, because SI is zero-based)
MOV AL, [array + SI] ; Move the 3rd byte of the array into AL
section .data
var1 db 100 ; A byte-sized variable
var2 db 200 ; Another byte-sized variable
pointer dw var1 ; A pointer to var1 (holds the address of var1)
section .text
MOV SI, [pointer] ; SI now holds the address of var1
MOV AL, [SI] ; Move the value at address in SI (100 from var1) into AL
pointer dw var1 stores the address of var1 in the pointer variable.MOV SI, [pointer] loads the address of var1 into the SI register.MOV AL, [SI] then loads the value at the address in SI (which is the value stored in var1) into the AL register.MOV register, [memory].[ ]) indicate memory access.This process of moving data is essential for almost every assembly language operation, as registers are used to perform calculations, control program flow, and manipulate data efficiently.
Open this section to load past papers