Dalvik version of Hello World

Source: Internet
Author: User

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?
  1. .    class public Lhelloworld; #定义类名
  2. .    Super Ljava/lang/object; #定义父类
  3. . method Public static main ([ljava/lang/string;) V #声明静态main () methods
  4. . registers 4 #程序中使用v0, V1, v2 register and a parameter register
  5. . Parameter #一个参数
  6. . Prologue #代码起始指令
  7. #空指令
  8. Nop
  9. Nop
  10. Nop
  11. Nop
  12. #数据定义指令 V0 = 8; v1 = 5; v2 = 3;
  13. const/v0, 0x8
  14. const/4 v1, 0x5
  15. const/4 v2, 0x3
  16. #数据操作指令 V1 = v2;
  17. Move V1, v2
  18. #数组操作指令 V0 = new int[v0]; v1 = v0
  19. New-array V0, V0, [I
  20. Array-length v1, V0
  21. #实例操作指令 v1 = new StringBuilder ();
  22. new-instance v1, Ljava/lang/stringbuilder;
  23. #方法调用指令 V1.init ();
  24. Invoke-direct {v1}, ljava/lang/stringbuilder;-><init> () V
  25. #跳转指令if (v0! = 0)
  26. If-nez V0,: Cond_0
  27. goto:goto_0
  28. : Cond_0
  29. #数据运算指令 v2 = v2 << v2; (v2 = = )
  30. shl-int v2, v2, v2
  31. #数据转换指令 v2 = (float) v2;
  32. int-to-Float v2, v2
  33. #数据运算指令 v2 + = v2; (v2 = = 48.0)
  34. add-Float v2, v2, v2
  35. #比较指令 V0 = V2 < v2; (V0 = = 0)
  36. cmpl-float V0, v2, v2
  37. #字段操作指令 PrintStream v0 = System.out; V1 = "Hello World"
  38. Sget-object V0, ljava/lang/system;->out:ljava/io/printstream;
  39. const-string v1, "Hello World" #构造字符串
  40. #方法调用指令 v0.println (v1); V0.println (v2);
  41. invoke-virtual {v0, v1}, Ljava/io/printstream;->println (ljava/lang/string;) V
  42. invoke-virtual {v0, v2}, Ljava/io/printstream;->println (F) V
  43. #返回指令
  44. : goto_0
  45. return-void #返回空
  46. . 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)

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.