# Registers

```java

public int addNumbers(int a, int b) {
    int sum = 0;
    sum = a + b;
    return sum;
}
```

* The function **`addNumbers`** is written in a high-level programming language (Java).
* High-level languages are designed to be human-readable, but the CPU cannot directly execute code written in such languages.
* To make the function executable by the CPU, a **compiler** translates it into low-level machine code (assembly or binary instructions) that the CPU can understand and process.
* The CPU uses **registers**, which are small, fast memory locations built into the processor, to store:
  * The values of variables `a` and `b`.
  * Intermediate results like the **sum** of `a` and `b`.
* Registers are critical for efficient computation because they provide rapid access to data during program execution.

## 📌What Are Registers in SMALI?

* Registers in SMALI are temporary storage locations used by the Dalvik Virtual Machine (DVM) or Android Runtime (ART). They store:
  1. **Local variables**
  2. **Parameters passed to methods**
  3. **Intermediate results of operations**
  4. **Object references**
* Think of registers as slots for holding data during a method’s execution.
* When we decompile an APK file using APKtool, the registers are split into two parts:
  * **Local registers (`vN`).**
  * **Parameter registers (`pN`).**

### 📍Types of Registers

1. **Parameter Registers (`pN`)**:
   * Hold the values passed as parameters to the method.
   * In instance methods:
     * `p0` is reserved for the `this` reference (current object instance).
     * `p1`, `p2`, etc., hold the actual method arguments.
   * In static methods:
     * There is no `this` reference, so `p0` starts with the first parameter.
2. **Local Registers (`vN`)**:
   * Used for variables declared and used within a method.
   * Store temporary data like intermediate computation results.
   * `v0`, `v1`, etc., represent these registers.
3. **Shared Pool**:
   * The total number of registers (parameter + local) available to a method is declared with `.registers`.
   * Alternatively, `.locals` can be used to declare the number of **non-parameter** registers.

> **These registers, if initialized at the beginning, do not necessarily hold the same values or variables by the end.**

### 📍Declaring Registers

* **`.locals`:** Specifies the number of registers used **excluding method parameters**.

  ```
  .locals 3   # This method uses 3 local registers (v0, v1, v2).
  ```
* **`.registers`:** Specifies the total number of registers, including parameters.

  ```
  .registers 5   # This method has 5 registers total (parameters + locals).
  ```

### 📍TypeDescriptor Semantics

<figure><img src="https://4027909796-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fwb35XsfjdYzmAOhXx0Kp%2Fuploads%2FN3UWOUKLCmjbqBgj3xoH%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

### ��***Dalvik opcodes***

* Dalvik opcodes are low-level instructions for the **DVM**, used to execute Android applications. They function like machine code, optimized for mobile devices, and are based on a register architecture for efficient operation.
* ***Common Dalvik Opcodes:***

  | **Opcode**           | **Description**                              | **Example**                                              |
  | -------------------- | -------------------------------------------- | -------------------------------------------------------- |
  | **`move`**           | Copies a value from one register to another. | `move v1, v2`                                            |
  | **`const`**          | Loads a constant into a register.            | `const v0, 10`                                           |
  | **`add-int`**        | Adds two integer values in registers.        | `add-int v0, v1, v2`                                     |
  | **`sub-int`**        | Subtracts two integer values.                | `sub-int v0, v1, v2`                                     |
  | **`mul-int`**        | Multiplies two integer values.               | `mul-int v0, v1, v2`                                     |
  | **`if-eq`**          | Jumps if two values are equal.               | `if-eq v1, v2, :label`                                   |
  | **`goto`**           | Jumps to a specific label.                   | `goto :label`                                            |
  | **`invoke-virtual`** | Calls a method on an object.                 | `invoke-virtual {v0}, Ljava/io/PrintStream;->println()V` |
  | **`return`**         | Returns from a method.                       | `return v0`                                              |
* ***Reading - Directions***

<div><figure><img src="https://4027909796-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fwb35XsfjdYzmAOhXx0Kp%2Fuploads%2FWlthGQr6G2tbjdzLWtnf%2Fccc23cb5%206265%204835%20868b%20a1f0266226c9.png?alt=media" alt="" width="467"><figcaption></figcaption></figure> <figure><img src="https://4027909796-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fwb35XsfjdYzmAOhXx0Kp%2Fuploads%2FJHb2KfD87RKWEgz10p07%2Fimage%201.png?alt=media" alt="" width="458"><figcaption></figcaption></figure></div>

* [***For more***](http://pallergabor.uw.hu/androidblog/dalvik_opcodes.html)
