In this short video, we will talk about how to use a load instruction to read data from
the memory, and a store instruction to write data to the memory.
In modern computers, a value in the memory cannot be a source operand of the processor
core directly. When we need to modify a memory value, the processor has to execute a load
instruction to read the value from the memory into one of the processor s registers first.
Similarly, the result of an arithmetic or logic operation cannot be directly saved into
the memory. Instead, the result is saved into a register first, then the processor executes
a store instruction to write the value into the memory. The key reason is that registers
are much faster than memory. Saving values in registers temporally help improves the
processor speed if those values will be used again. This will help reduce the number of
memory accesses.
Here is an example LDR instruction. LDR stands for load to register. It reads a 32-bit word
from the memory and saves the value into the destination register. For example, in this
instruction, register r0 specifies the memory address which the data is read from. Register
r1 is the destination register. Suppose register r0 holds this memory address, which points
to this location. This LDR instruction will load a word, that is, 4 bytes, into the destination
register r1. Assume little-endian is used. Then, the value of register r1 will be: Dead
beef.
When a word needs to be saved into the memory, the processor core needs to run the STR instruction.
STR stands for store register. In this example, it saves the 32-bit value held in register
r1 into the memory at this address specified in register r0. Assume register r0 has this
value, pointing to this memory location. And register r1 holds a special value, Bad Cafe.
Assume the system uses little endian, here are the memory values when the STR instruction
completes. The least significant byte FE is stored at the lowest memory address.
We have shown the LDR and STR instructions, which load or store a word. This table lists
instructions that load a byte, a halfword, a signed byte, and a signed halfword from
the memory. When a signed byte or a signed halfword is loaded into a 32-bit register,
sign-extension should be performed to make the value 32 bits. This table lists instructions
that store a word, a byte and a halfword to the memory.
In this example, we use the LDRB instruction to load a byte from the memory address specified
in register r0. This LDRB instruction will change register r1 to this value.
The next example uses the LDRH instruction to load a halfword, and saves the value into
register r1. This LDRH instruction will modify register r1 to this value.
When LDRSB loads a signed byte from the memory, sign extension must be performed. The byte
stored at the target memory address is EF in hex. Its sign bit is 1. We copy the sign
bit to the left until the value has a total of 32 bits. Therefore, this is the final result
of register r1.
Similarly, when loading a signed halfword from the memory by using the instruction LDRSH,
sign extension will be performed when the result is saved into register r1.
This example illustrates the store halfword instruction STRH. The least significant 16
bits of register r1 are stored into the memory, as shown here. This instruction does not modify
the values at these two memory locations.