0
0
Cnc-programmingConceptBeginner · 3 min read

What is SUB Instruction in ARM: Explanation and Example

The 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.

arm_assembly
SUB R0, R1, #5
Output
If R1 contains 10, after execution R0 will contain 5.
🎯

When 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.

Key Takeaways

The SUB instruction subtracts one register or immediate value from another and stores the result.
It is essential for arithmetic operations like counting down or calculating differences.
SUB works with registers and immediate values in ARM assembly.
Use SUB in loops, counters, and when adjusting values in low-level programming.