Instruction Set
What is an instruction set?
Notes
- Low registers = 0-7; High registers = 9-10
MOVwithin the CPU;LDR(copy from memory) andSTR(paste into memory) interacting with memoryLDRvs.POP?- Pop uses stack pointer to get data from the stack (goes down the stack, incrementing the stack pointer)
- Load uses a register in the CPU that is storing the target address of the memory to get data from the stack
STRvs.PUSH?- Push uses stack pointer to give data to the stack (goes up the stack, decrementing the stack pointer)
- Store uses a register in the CPU that is storing the target address of the memory to give data to the stack
- Return from a subroutine
MUL with immediate values?In short: No, not directly in standard ARM assembly.
The MUL (Multiply) instruction is a register-to-register operation. Unlike ADD or MOV, which allow you to use a "shifter operand" (like #5), the MUL hardware on the ARM processor is designed to take its inputs strictly from the register bank.
1. The Syntax Rule
The standard syntax for MUL is:
MUL Rd, Rm, Rs
-
: Destination register. -
: First factor (multiplier). -
: Second factor (multiplicand).
If you try to write MUL R0, R1, #5, the assembler will throw an error because the instruction doesn't have enough "bits" in its machine code to store a constant value.
2. How to Multiply by a Constant
If you need to multiply a register by a specific number (like 5), you have two main options:
Option A: The "Load then Multiply" Way
This is the most general method. You move the constant into a temporary register first.
Code snippet
MOV R2, #5 @ Load the constant into R2
MUL R0, R1, R2 @ R0 = R1 * R2 (which is 5)
Option B: The "Logical Shift" Way (Faster)
If your constant is a power of 2 (like 2, 4, 8, 16), or a combination of them, you can use the Barrel Shifter with an ADD or RSB instruction. This is often faster than using the MUL hardware.
-
To multiply by 4:
LSL R0, R1, #2(Logical Shift Left by 2) -
To multiply by 5 (which is
): ADD R0, R1, R1, LSL #2@
3. Why is MUL restricted?
Multiplication is a mathematically "heavy" operation for a CPU compared to addition. To keep the ARM chip small, fast, and power-efficient, the designers simplified the MUL instruction so it only looks at the register file. This avoids the extra wiring needed to pull "immediate" values from the instruction stream into the multiplier unit.
Summary
| Can you use #constant? | Instruction |
|---|---|
| YES | ADD, SUB, MOV, CMP, AND, ORR |
| NO | MUL, SDIV, UDIV |
Do you have a specific constant in mind? I can show you the most efficient "Shift and Add" sequence to replace the MUL for that specific number.