Smali grammar analysis, necessary for interview

Let's first look at the generated Hello.smali file:

.class public LHello;
.super Ljava/lang/Object;
.source "Hello.java"

# static fields
.field private static HELLO_WORLD:Ljava/lang/String;

# direct methods
.method static constructor <clinit>()V
    .registers 1

    .prologue
    .line 3
    const-string v0, "Hello World!"

    sput-object v0, LHello;->HELLO_WORLD:Ljava/lang/String;

    return-void
.end method

.method public constructor <init>()V
    .registers 1

    .prologue
    .line 1
    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    return-void
.end method

.method public static main([Ljava/lang/String;)V
    .registers 3

    .prologue
    .line 6
    sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;

    sget-object v1, LHello;->HELLO_WORLD:Ljava/lang/String;

    invoke-virtual {v0, v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V

    .line 7
    return-void
.end method 

File header

First look at the file header:

.class public LHello;       // Class name
.super Ljava/lang/Object;   // Parent class name
.source "Hello.java"        // Source file name 

. Class is followed by the access modifier and the current Class, where the Class name is represented by LHello. So what does this l stand for? In fact, this representation method has also appeared in previous Class files. The bytecode instructions of JVM and DalviK are similar in many places. Java is divided into basic types and reference types. DalviK has different description methods for these two types. Both basic and Void types are represented by an uppercase letter. For reference types, use the letter L plus the fully qualified name of the object type. The specific rules are shown in the table below:

Java typetype descriptor
charC
byteB
shortS
intI
longJ
floatF
doubleD
booleanZ
voidV
objectL
array[

The representation of basic types is very simple. int can be represented by I. Object representation, as shown in the above figure, the parent class object representation method Ljava/lang/Object;, Another example is String type, which is represented by Ljava/lang/String.

For arrays, DalviK has a special representation [followed by the type of array elements. The representation of int [] is [I, and the representation of string [] is [Ljava/lang/String;. Two dimensional arrays are represented by [[, [[Ljava/lang/String refers to string [] [], and so on.

Field representation

# static fields
.field private static HELLO_WORLD:Ljava/lang/String; 

Fields in smali begin with. Field and are annotated with # static field or # instance field Field is followed by access modifier, field name, colon and field type descriptor. This sentence smali declares a String type named hello_ Private static field of world.

Method representation

Methods in smali begin with. Method. Hello.smali contains three methods, clinit, init and main. The main method is written by ourselves, while the clinit and init methods are generated during javac compilation. The following are analyzed one by one:

clinit

.method static constructor <clinit>()V
    .registers 1

    .prologue
    .line 3
    const-string v0, "Hello World!"

    sput-object v0, LHello;->HELLO_WORLD:Ljava/lang/String;

    return-void
.end method 

clinit method will initialize static variables and execute static code blocks. This method is called when the class is loaded. Analyze the execution logic of the method line by line:

  • . registers 1: the number of registers that this method needs to use. As mentioned earlier, DalviK VM is register based. The number of virtual registers that can be used for bytecode can reach 65536. Each register is 32 bits, and 64 bit data is represented by two adjacent registers. Finally, all virtual registers are mapped to real physical registers. In general, we use the letter v to represent the registers used by local variables and the letter p to represent the registers used by parameters. The registers used by local variables are arranged in the front and the registers used by parameters are arranged in the back. This means that the clinit method uses only one register.

  • . prologue: indicates the beginning of the logical code

  • . line 3: indicates the number of lines in the java source file

  • const-string v0, "Hello World!" : The string Hello world! The reference to is moved to register v0.

  • sput-object v0, LHello;-> HELLO_WORLD:Ljava/lang/String; : Sput and sget instructions with prefix s are used for reading and writing static fields. Assign the string reference stored in register v0 to hello_ The world field, combined with the bytecode of the previous sentence, completes the static variable hello_ The assignment of world also verifies that the clinit method does initialize static variables.

  • Return void: indicates that the method has no return value

  • . end method: indicates the end of method execution

At this point, the clinit method is finished. The init method is analyzed below.

init

.method public constructor <init>()V
    .registers 1

    .prologue
    .line 1
    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    return-void
.end method 

The other items are the same as the clinit method. Let's look directly at the executed code logic:

invoke-direct {p0}, Ljava/lang/Object;-><init>()V 

Invoke direct is used to call non static direct methods (that is, instance methods that are essentially non overridable, that is, private instance methods or constructors). Obviously, the default constructor is called here.

main

.method public static main([Ljava/lang/String;)V
    .registers 3

    .prologue
    .line 6
    sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;

    sget-object v1, LHello;->HELLO_WORLD:Ljava/lang/String;

    invoke-virtual {v0, v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V

    .line 7
    return-void
.end method 

Finally, the main method. From the above smali code, we can see that the main method uses three registers and has no return value (that's for sure). The specific code executed is the following three lines:

sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;

sget-object v1, LHello;->HELLO_WORLD:Ljava/lang/String;

# summary

Android Advanced architecture learning is a long and arduous road. It can't be learned by passion or by staying up for a few days and nights. We must develop the habit of studying hard at ordinary times.**So: insist!**

The company's 2021 interview real question analysis collection shared above, and the author also sorted out the main interview technical points of front-line Internet enterprises into videos and PDF(In fact, it takes a lot more energy than expected), including the context of knowledge + Many details.
![](https://img-blog.csdnimg.cn/img_convert/00d748fb27f23d73a0a9395a913556d2.png)

**[CodeChina Open source projects:< Android Summary of study notes+Mobile architecture video+Real interview questions for large factories+Project practice source code](https://codechina.csdn.net/m0_60958482/android_p7)**

**[Android Advanced architecture video learning resources]**

 Many details.
[External chain picture transfer...(img-MYLs318M-1630940389273)]

**[CodeChina Open source projects:< Android Summary of study notes+Mobile architecture video+Real interview questions for large factories+Project practice source code](https://codechina.csdn.net/m0_60958482/android_p7)**

**[Android Advanced architecture video learning resources]**

Android Some excellent videos are even more powerful after learning! March BATJ Dachang, etc. (prepare for war)! Now it is said that in the cold winter of the Internet, in fact, it is nothing more than that you get on the wrong car and wear less (skills). If you get on the right car, your own technical ability is strong enough and the cost of replacing the company is high, how can you be cut off? It is all to eliminate the end business Curd nothing more! Nowadays, the market is flooded with junior programmers. This tutorial is aimed at Android Development Engineer 1-6 The staff in is in a bottleneck period and wants to break through their salary increase and advance in the next year Android Middle and senior architects are like fish in water for you. Get it quickly!

Tags: Java Android Design Pattern Interview

Posted on Mon, 06 Sep 2021 13:58:28 -0400 by hellrising