Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

introduce

The Dalviks bytecode registers are 32-bit and can represent any type, with two registers for 64-bit types (Long and Double).

role

Declared inside a method (must)

.method public getName(a)V
    .registers 6 

	return-void
.end method
Copy the code

. Registers and locals are basically different

There are two ways to specify how many registers are available in a method. Registers registers specify how many registers are available in the method.

The. Locals directive specifies the number of non-parameter registers in this method. However, the total number of registers also includes registers that hold method parameters.

How are parameters passed?

1. If the method is non-static

For example, you write a non-static method LMyObject; – > callMe (II) V. This method takes two ints, but there is a hidden LMyObject in front of them; This is a reference to the current object, so this method takes three arguments in total. Suppose a method contains five registers (v0-V4) as follows:

.method public callMe(II)V
 	const-string v0,"1"
    const-string v1,"1"
 
   return-void
.end method
Copy the code

Just specify 5 with the.register directive, or 2 with the.locals directive (2 local registers +3 parameter registers). As follows:

.method public callMe(II)V
    .registers 5
 	const-string v0,"1"
    const-string v1,"1"
    v3==>p0
    V4==>P1
    V5==>P2
   
   return-void.end method or.method public callMe(II)V
    .locals 2
 	const-string v0,"1"
    const-string v1,"1"
   return-void
.end method
Copy the code

When the method is called, the object calling the method (that is, the this reference) is stored in V2, the first argument in V3, and the second argument in V4.

2. If the method is static

Registers are 4, and locals is 2, as in non-static cases

About register naming rules

V nomenclature

In the example above we use v nomenclature, which is to add the parameter registers after the local registers,

But this way of naming a problem: if I later want to change the contents of the method body, involve the add or remove a register, given the limitations of v nomenclature need sorting, so will cause a lot of code changes, we only change registers or is there a way to make the value of the locals is ok, the answer is: yes

In addition to v nomenclature, there is another nomenclature called p nomenclature

P nomenclature

P nomenclature can only name method parameters, not local variables

Suppose we have a non-static method like this:

.method public print(Ljava/lang/String; Ljava/lang/String; I)VCopy the code

The following is the corresponding table of p nomenclature parameters:

p0 this
p1 The first argument is Ljava/lang/String;
p2 The second argument is Ljava/lang/String;
p3 The third parameter I

As mentioned earlier, long and double are both 64-bit and require two registers. Keep in mind when referencing arguments, for example, that you have a non-static method

LMyObject; ->MyMethod(IJZ)VCopy the code

Method parameters are int, long, bool. So all the parameters of this method need 5 registers.

p0 this
p1 I
p2, p3 J
p4 Z

In addition, when you call the method, you must specify in the register list, the call instruction, that two registers hold double-wide parameters.

Note: In the default Baksmali, parameter registers will use P naming. If for some reason you want to disable P naming and force V naming, the -p/–no-parameter-registers option should be used.

conclusion

  • Registers can be either locals or registers. Locals specifies the number of local variable registers. Registers are the total number of locals and parameter registers
  • At the same time, there are two register naming methods, one is V naming method, the other is P naming method
v0 the first local register
v1 the second local register
v2 p0 the first parameter register
v3 p1 the second parameter register
v4 p2 the third parameter register