Lesson 03: Addressing Modes
The term Addressing Modes refers to the various ways a processor can specify instructions to access data. If the data number for instruction is in memory, the microprocessor will form a memory address to access it. If the data number is in a register inside the microprocessor, data accessing from memory is not needed. The addressing mode is associated more specifically with the operands, and a single instruction could exercise multiple addressing modes for each of the operands. There are various methods of giving source or destination addresses in instruction, thus there are various types of Addressing Modes. Here you will find the different types of Addressing Modes that are supported in the ARM Cortex-M microcontroller.
Immediate Addressing
A constant number can be put inside an instruction code. The address mode that places a constant data number inside the instruction is called immediate addressing. Immediate addressed instructions do not contain an address.
Syntax:
- 16-bit Thumb instructions: <instruction> {<cond>}{S} Rd, #imm8
- 32-bit ARM/Thumb-2 instructions: <instruction> {<cond>}{S} Rd, #imm16
- The # sign shows that preceding term is a constant data, not the address
- Immediate values are 16-bits (does not correspond to 255), so the range of valid immediate values is 0 through to 255 in decimal, or 0x00 through 0xFF in hexadecimal.
Example:
MOV R0, #25 ; Move the constant decimal value 25 to R0 register MOV R1, #0x2F ; Move the constant hexadecimal value 2Fh to R1 register MOV R2, #2_1101 ; Move the constant binary value 00001101b to R0 register CMP R0, #22
Register Addressing
Some instructions can operate on data inside the microprocessor registers. This is referred to as the Register Addressing Mode.
Indexed Addressing
ARM instructions do not support memory to memory data processing operations. Instead, they use special load and store instructions to move the data between the processor's internal registers and the memory. Therefore, all the values in the memory must be loaded into the registers before they can be used. This might sound inefficient, but in practice is not:
- Load values from memory into registers by using the LDR instruction.
- Process data in the registers by using a number of data processing instructions which are not slowed down by the memory access.
- Store results from the registers out to the memory by using the STR instruction.
ARM is a RISC processor, and its instructions have a fixed-length (Thumb-1 are 16-bits fixed-length instructions, and ARM/Thumb-2 are 32-bit fixed-length instructions). There are not enough fields to store a 32-bit address in an instruction code. ARM has 32 registers, and each register is 32-bit wide, therefore, ARM processor uses registers as pointer register find the oprand address in memory, which is called an indexed addressing mode. The register that contains an address or a location of the data is called an index register or base address register.
Indexed addressing mode requires three read operations to access an operand. It is very important because the content of the register containing the pointer to the operand can be modified at runtime. Therefore, the address is a variable that allows the access to the data structure like arrays.
- Read the instruction to find the index register
- Read the index register to find the oprand address
- Read memory at the operand address to find the operand
There are three types of indexed addressing modes:
Offset Indexed
To load a value of a number from the memory or to store a value to memory, the data address must be loaded into a base register Rn, which can be one of the general purpose registers(including the PC, which allows PC-relative addressing for position-independent code). The destination address can be calculated by a base register and an offset value. The offset takes one of the three following formats:
Pre-indexed and Post-indexed addressing modes are generalizations of the Autodecrement and Autoincrement addressing modes, respectively,
Summary of ARM's Indexed Addressing Modes
Addressing Mode | Assembly Mnemonic | Effective Address | Final Value in R1 |
Indexed, base unchanged | LDR R0, [R1, #d] | R1 + d | R1 |
Pre-indexed, base updated | LDR R0, [R1, #d]! | R1 + d | R1 + d |
Post-indexed, base updated | LDR R0, [R1], #d | R1 | R1 + d |