What is SUB Instruction in ARM: Explanation and Example
SUB instruction in ARM is used to subtract one value from another and store the result in a register. It performs simple arithmetic subtraction between registers or between a register and an immediate value.How It Works
The SUB instruction in ARM works like a basic calculator that takes two numbers and finds their difference. Imagine you have two boxes with numbers, and you want to find out how much smaller one box's number is compared to the other. The SUB instruction takes the number from the second box and subtracts it from the first box's number.
In ARM, these "boxes" are called registers, which are small storage areas inside the processor. The instruction subtracts the value in one register or an immediate number (a fixed number written in the instruction) from another register's value, then saves the result back into a register. This is useful for many tasks like counting down, calculating differences, or adjusting values.
Example
This example shows how to subtract the value 5 from the value in register R1 and store the result in register R0.
SUB R0, R1, #5When to Use
You use the SUB instruction whenever you need to find the difference between two numbers in your program. For example, it is common in loops where you count down a number until it reaches zero, or when calculating how far one value is from another.
In real-world applications, SUB helps in tasks like adjusting memory addresses, calculating time differences, or managing counters in embedded systems and low-level programming.
Key Points
- SUB subtracts one value from another in ARM registers.
- It can use a register or an immediate number as the second value.
- The result is stored in a register for further use.
- Commonly used in loops, counters, and arithmetic calculations.