Registers
public int addNumbers(int a, int b) {
int sum = 0;
sum = a + b;
return sum;
}The function
addNumbersis 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
aandb.Intermediate results like the sum of
aandb.
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:
Local variables
Parameters passed to methods
Intermediate results of operations
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
Parameter Registers (
pN):Hold the values passed as parameters to the method.
In instance methods:
p0is reserved for thethisreference (current object instance).p1,p2, etc., hold the actual method arguments.
In static methods:
There is no
thisreference, sop0starts with the first parameter.
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.
Shared Pool:
The total number of registers (parameter + local) available to a method is declared with
.registers.Alternatively,
.localscan 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..registers: Specifies the total number of registers, including parameters.
📍TypeDescriptor Semantics

📍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
moveCopies a value from one register to another.
move v1, v2constLoads a constant into a register.
const v0, 10add-intAdds two integer values in registers.
add-int v0, v1, v2sub-intSubtracts two integer values.
sub-int v0, v1, v2mul-intMultiplies two integer values.
mul-int v0, v1, v2if-eqJumps if two values are equal.
if-eq v1, v2, :labelgotoJumps to a specific label.
goto :labelinvoke-virtualCalls a method on an object.
invoke-virtual {v0}, Ljava/io/PrintStream;->println()VreturnReturns from a method.
return v0Reading - Directions


Last updated