Android-smali syntax learning and android-smali syntax

Source: Internet
Author: User

Android-smali syntax learning and android-smali syntax

Reprinted please indicate the source: http://blog.csdn.net/goldenfish1919/article/details/40821415

The following content comes from: http://bbs.pediy.com/showthread.php? T = 151769

Dalvik bytecode can be of the original type or reference type. Objects and arrays are reference types, while others are original types.
V void, which can only be used for the return value type
Z boolean
B byte
S short
C char
I int
J long (64-bit)
F float
D double (64-bit)
The object is represented in the form of Lpackage/name/ObjectName.
The preceding L indicates that this is an object type. package/name/indicates the package where the object is located, ObjectName indicates the object name, and ";" indicates the end of the Object name.
It is equivalent to package. name. ObjectName in java. For example: Ljava/lang/String; equivalent to java. lang. String

Array Representation
Struct [I -- represents an integer one-dimensional array, which is equivalent to int [] in java.
For multi-dimensional arrays, just add. [[I is equivalent to int [] [], and [[I is equivalent to int [] [] []. Note that a maximum of 255 entries per dimension can be entered.
Representation of the struct object array: [Ljava/lang/String; represents a String object array.
Method
Representation: Lpackage/name/ObjectName;-> MethodName (III) Z
 Lpackage/name/ObjectName; indicates the type, and MethodName indicates the method name. III is the parameter (three Integer Parameters here), and Z is the return type (bool type ).
The parameters of the callback method are one by one and are not separated between them.
A more complex example:
Method (I [[IILjava/lang/String; [Ljava/lang/Object;) Ljava/lang/String;
In java:
String method (int, int [] [], int, String, Object [])
Field
Representation:
Lpackage/name/ObjectName;-> FieldName: Ljava/lang/String;
That is, the package name, field name, and field type.
Register
In dalvik bytecode, registers are 32-bit and support any type. The 64-bit type (Long and Double type) is represented by two registers.
There are two ways to specify how many registers are available in a method .. The registers command specifies the total number of registers in the method .. The locals instruction lists the number of non-parameter registers in the method.
Passing PARAMETERS OF THE METHOD
When a method is called, The method parameters are placed in the last N registers. If a method has 2 parameters, 5 registers (v0-v4), the parameters are placed in the last 2 registers-v3 and v4.
The first parameter in a non-static method always calls the object of this method.
For example, the non-static method LMyObject;-> callMe (II) V has two Integer Parameters and an implicit LMyObject parameter. Therefore, there are three parameters in total.
If five registers (v0-v4) are specified in this method. the registers method specifies five or. specify two local registers and three parameter registers in locals mode ).
When this method is called, the object (that is, this reference) that calls this method is stored in v2, and the first integer parameter is stored in v3, the second integer parameter is stored in v4.
Static methods are the same except for the implicit this parameter.
Register naming
There are two methods: V and P. The first register in the P naming method is the first parameter register in the method.
In the following table, we use these two naming methods to represent five registers and three parameters in the previous example.
The first local register in v0
V1 Second local register
V2 p0 first parameter register
V3 p1 second parameter register
V4 p2 third parameter register
You can reference parameter registers in any way-they have no difference.
Note: baksmali uses the P naming method for parameter registers by default. If you want to use the V naming method, you can use the-pl-no-parameter-registers option.
The P naming method is used to prevent the disadvantage of re-numbering the parameter register if you want to add registers in the method in the future.
Long/Double value
The Long and double types are 64-bit, and two registers are required (remember ).
For example, for non-static method LMyObject;-> MyMethod (IJZ) V, the parameters are LMyObject;, int, long, bool. Therefore, this method requires five registers to store parameters.

P0 this
P1 I
P2, p3 J
P4 Z

Supplement:
# Static fields defining static variable tags
# Instance fields defines the tag of instance variables
# Direct methods defining static method tags
# Virtual methods defining non-static method tags
The return type of the constructor is V and its name is <init>.
 
If-eq p1, v0,: cond_8 indicates that if p1 and v0 are equal, the cond_8 process is executed:
: Cond_8
Invoke-direct {p0}, Lcom/paul/test/a;-> d () V
Call the d () method of com. paul. test..
If-ne p1, v0,: cond_ B indicates not equal, then run the cond_ B process:
: Cond_ B
Const/4 v0, 0x0
Invoke-virtual {p0, v0}, Lcom/paul/test/a;-> setPressed (Z) V
Invoke-super {p0, p1, p2}, Landroid/view/View;-> onKeyUp (ILandroid/view/KeyEvent;) Z
Move-result v0
 
The general meaning is to call the setPressed method of com. paul. test. a, then call the onKeyUp method of the parent class View, and finally return v0.

Here are two examples:

Sget-object v5, Lcom/google/youngandroid/runtime;-> Lit227: Lgnu/mapping/SimpleSymbol;
Get the Lit227 field in com. google. youngandroid. runtime and store it in the v5 register, which is equivalent
Gnu. mapping. SimpleSymbol localVariable = com. google. youngandroid. runtime. Lit227;

Sput-object v0, Lcom/google/youngandroid/runtime;-> Lit78: Lkawa/lang/SyntaxTemplate;
Likewise, this is setting the value of a static field. I. e.
Set com. google. youngandroid. runtime. Lit78 to the value of the kawa. lang. SyntaxTemplate type variable in the v0 register. Equivalent to com. google. youngandroid. runtime. Lit78 = kawa. lang. SyntaxTemplate localVariable;
The rest is relatively simple and you should understand it.

Here is a simple example:

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView textview1 = (TextView) this.findViewById(R.id.text);textview1.setText(R.string.hello_world);}}
Generated smali:

.class public Lcom/example/hello/MainActivity;.super Landroid/app/Activity;.source "MainActivity.java"# direct methods.method public constructor <init>()V    .locals 0    .prologue    .line 14    invoke-direct {p0}, Landroid/app/Activity;-><init>()V    return-void.end method# virtual methods.method protected onCreate(Landroid/os/Bundle;)V    .locals 2    .parameter "savedInstanceState"    .prologue    .line 18    invoke-super {p0, p1}, Landroid/app/Activity;->onCreate(Landroid/os/Bundle;)V    .line 19    const/high16 v1, 0x7f03    invoke-virtual {p0, v1}, Lcom/example/hello/MainActivity;->setContentView(I)V    .line 20    const/high16 v1, 0x7f0a    invoke-virtual {p0, v1}, Lcom/example/hello/MainActivity;->findViewById(I)Landroid/view/View;    move-result-object v0    check-cast v0, Landroid/widget/TextView;    .line 21    .local v0, textview1:Landroid/widget/TextView;    const v1, 0x7f070002    invoke-virtual {v0, v1}, Landroid/widget/TextView;->setText(I)V    .line 22    return-void.end method

Let's focus on the onCreate () method.

. Method protected onCreate (Landroid/OS/Bundle;) V
. Locals 2
. Parameter "savedInstanceState"

. Prologue
. Line 18
Invoke-super {p0, p1}, Landroid/app/Activity;-> onCreate (Landroid/OS/Bundle;) V // This calls super, that is, Activity. onCreate (Bundle) p0: this, p1: Bundle

. Line 19
Const/high16 v1, 0x7f03 // assign the 0x7f03 constant to v1. 0x7f03 is R. layout. activity_main

Invoke-virtual {p0, v1}, Lcom/example/hello/MainActivity;-> setContentView (I) V // call MainActivity. setContentView (v1), p0: this, v1 is R. layout. activity_main

. Line 20
Const/high16 v1, 0x7f0a // assign the constant 0x7f0a to v1, and 0x7f0a is R. id. text.

Invoke-virtual {p0, v1}, Lcom/example/hello/MainActivity;-> findViewById (I) Landroid/view/View; // call View = MainActivity. findViewById (v1), p0: this, v1: R. id. text

Move-result-object v0 // move the output result of the previous command to v0, which is to assign a value to textview1.

Check-cast v0, Landroid/widget/TextView; // convert v0 to TextView

. Line 21
. Local v0, textview1: Landroid/widget/TextView;
Const v1, 0x7f070002 // put int constant 0x7f070002 into v1, that is, read the R. string. hello_world

Invoke-virtual {v0, v1}, Landroid/widget/TextView;-> setText (I) V // call TextView. setText (v1) TextView is v0, and the parameter is v1

. Line 22
Return-void
. End method

This is a simple example. Very similar to the class bytecode. Let's take a look at the differences between the same code in class and smali.

public void multiply(int a, int b){int result = a * b;System.out.println(result);}
Smali:

# Virtual methods
. Method public multiply (II) V
. Locals 2
. Parameter ""
. Parameter "B"

. Prologue
. Line 26
Mul-int v0, p1, p2 // multiply p1 and p2 and put the result in v0

. Line 27
. Local v0, result: I
Sget-object v1, Ljava/lang/System;-> out: Ljava/io/PrintStream; // obtain the reference of System. out and put it in v1.

Invoke-virtual {v1, v0}, Ljava/io/PrintStream;-> println (I) V // call PrintStream. println (v0)

. Line 28
Return-void
. End method

Class:

Public void multiply (int, int );
Code:
Stack = 2, Locals = 4, Args_size = 3
0: iload_1 // pressure stack of local variable 1
1: iload_2 // local variable 2 pressure Stack
2: imul // multiply the two above in the stack
3: istore_3 // result output stack, put in local variable 3
4: getstatic #5; // Field java/lang/System. out: Ljava/io/PrintStream; // System. out pressure Stack
7: iload_3 // local variable 3 pressure Stack
8: invokevirtual #6; // Method java/io/PrintStream. println :( I) V // call PrintStream. println
11: return
LineNumberTable:
Line 6: 0
Line 7: 4
Line 8: 11

The above clearly shows that the implementation of the same function is less based on registers than the number of stack-based commands. Int result = a * B; there is one command in smali, and four commands are required in class.

For the format and meaning of all smali commands, refer to: http://pallergabor.uw.hu/androidblog/dalvik_opcodes.html



How do I learn android applications? Best details

1. Android Application development is based on the Java language, so there is no solid basic Java knowledge, just mechanically copying others' Code, there is no significance. Therefore, we need to learn basic Java syntax and design patterns.
2. The Android system is based on the Linux operating system. In the development process, we also need to use some Linux commands. Therefore, some basic Linux knowledge is required.
3. Basic Database knowledge.
4. At least two basic protocols are required for network protocols: HTTP protocol and Socket protocol.
5. The basic knowledge of Android has been paved with the above. Let's look at Android again. Does it feel much easier? The best way to learn the basic knowledge is to follow the order in the Dev Guide in the Android SDK help document.
6. server-side development knowledge many Android applications require server-side support, so it is necessary to master some server-side development knowledge.
Reference: www.miui.com/thread-165925-1-1.html

Getting started with Android

1. Android is java, and there are basically no obstacles to the syntax.

2. You can go to pai.android.com to view the android sdk documentation. In dev guide, the content about application foundation can be used for interviews. If you have time, you 'd better read all the dev guides.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.