0
0
Cnc-programmingConceptBeginner · 3 min read

LDR Instruction in ARM: What It Is and How It Works

The ldr instruction in ARM is used to load a value from memory into a register. It reads data stored at a specific memory address and places it into a CPU register for processing.
⚙️

How It Works

The ldr instruction acts like a fetch command. Imagine you have a box (memory) full of items (data), and you want to pick one item and bring it to your desk (register) to work on it. The ldr instruction tells the processor exactly which box and which item to pick up.

It uses an address to find the exact location in memory. Once found, it copies the data from that memory location into a register, which is a small, fast storage area inside the CPU. This lets the processor quickly access and use the data.

In simple terms, ldr moves data from memory to the CPU’s working area so the processor can use it in calculations or decisions.

💻

Example

This example shows how ldr loads a value from memory into a register.

armasm
    AREA Example, CODE, READONLY
    ENTRY

    LDR R0, =0x12345678  ; Load immediate value into R0
    LDR R1, [R0]         ; Load value from memory address in R0 into R1

    END
Output
R0 = 0x12345678 (address), R1 = value at memory 0x12345678
🎯

When to Use

Use the ldr instruction whenever you need to read data stored in memory during your program. This is common when working with variables, arrays, or hardware registers.

For example, if your program needs to process a number stored in memory, you use ldr to bring that number into a register first. It’s also used to load addresses or constants into registers for further operations.

In real-world applications, ldr is essential for accessing data outside the CPU, such as reading sensor values, fetching instructions, or handling user input stored in memory.

Key Points

  • LDR loads data from memory into a CPU register.
  • It requires a memory address to know where to read from.
  • Commonly used to access variables, constants, and hardware data.
  • Essential for moving data from slower memory to fast CPU registers.

Key Takeaways

The LDR instruction loads data from a memory address into a CPU register.
It is used whenever a program needs to access data stored outside the CPU.
LDR helps the processor work with variables, constants, and hardware data efficiently.
Understanding LDR is fundamental for ARM assembly programming and memory operations.