This article is a small exercise in Dalvik instruction set to create a new text file renamed Helloworld.smali, and then write out the HelloWorld class program code as follows
[Java]View PlainCopyprint?
- . class public Lhelloworld; #定义类名
- . Super Ljava/lang/object; #定义父类
- . method Public static main ([ljava/lang/string;) V #声明静态main () methods
- . registers 4 #程序中使用v0, V1, v2 register and a parameter register
- . Parameter #一个参数
- . Prologue #代码起始指令
- #空指令
- Nop
- Nop
- Nop
- Nop
- #数据定义指令 V0 = 8; v1 = 5; v2 = 3;
- const/v0, 0x8
- const/4 v1, 0x5
- const/4 v2, 0x3
- #数据操作指令 V1 = v2;
- Move V1, v2
- #数组操作指令 V0 = new int[v0]; v1 = v0
- New-array V0, V0, [I
- Array-length v1, V0
- #实例操作指令 v1 = new StringBuilder ();
- new-instance v1, Ljava/lang/stringbuilder;
- #方法调用指令 V1.init ();
- Invoke-direct {v1}, ljava/lang/stringbuilder;-><init> () V
- #跳转指令if (v0! = 0)
- If-nez V0,: Cond_0
- goto:goto_0
- : Cond_0
- #数据运算指令 v2 = v2 << v2; (v2 = = )
- shl-int v2, v2, v2
- #数据转换指令 v2 = (float) v2;
- int-to-Float v2, v2
- #数据运算指令 v2 + = v2; (v2 = = 48.0)
- add-Float v2, v2, v2
- #比较指令 V0 = V2 < v2; (V0 = = 0)
- cmpl-float V0, v2, v2
- #字段操作指令 PrintStream v0 = System.out; V1 = "Hello World"
- Sget-object V0, ljava/lang/system;->out:ljava/io/printstream;
- const-string v1, "Hello World" #构造字符串
- #方法调用指令 v0.println (v1); V0.println (v2);
- invoke-virtual {v0, v1}, Ljava/io/printstream;->println (ljava/lang/string;) V
- invoke-virtual {v0, v2}, Ljava/io/printstream;->println (F) V
- #返回指令
- : goto_0
- return-void #返回空
- . End method
Next compile the Smali file, you need to use the Smali.jar. In cmd Enter the following command (Smali.jar and Helloworld.smali in the same directory): Java-jar smali.jar-o classes.dex Helloworld.smali will be generated Classes.dex Compress. zip format to connect an Android device or emulator, enter the following command in cmd input adb push classes.zip/data/localadb shell Dalvikvm-cp/data/local/classes.zip HelloWorld effect
Dalvik version of Hello World (RPM)