Android Dalvik虚拟机实战

2、Dalvik 可执行文件体积更小。

3、Java虚拟机与Dalvik虚拟机架构不同。Dalvik 汇编语言

1、Dalvik指令

指令大多由三个字符组成,前两个是数字,最后一个是字母。

第一个数字是表示指令有多少个16位的字组成。

第二个数字是表示指令最多使用寄存器的个数。特殊标记‘r’标识使用一定范围内的寄存器。

第三个字母为类型吗,表示指令用到的额外的数据的类型。

例如:22x

第一个2表示 指令有两个16位字组成

第二个2表示 指令使用到2个寄存器

第三个x表示没有使用到额外的数据

2、DEX文件反汇编工具

目前DEX可执行文件主流的反汇编工具有BakSmali与Dedexer。

指令如:

java -jar baksmali.jar -o baksmaliout hello.dex

java -jar ddx.jar -d ddxout hello.dex

3、Dalvik 寄存器

Dalvik虚拟机是作用于特定架构的CPU上运行的,在设计之初采用了ARM架构,ARM架构的CPU本身继承了多个寄存器,Dalvik将部分寄存器映射到了ARM寄存器上,还有一个部分则通过调用栈进行模拟。.

Dalvik虚拟机又是如何虚拟地使用寄存器的呢?Dalvik虚拟机为每个进程维护一个调用栈,这个调用栈其中一个作用就是用来‘虚拟’寄存器。

4、两种不同的寄存器表示方法

v 命名法采用以小写字母 ‘v’开头的方式表示函数中用到的局部变量与参数,所有的寄存器命名从v0开始,依次递增。

p 命名法对函数的局部变量寄存器命名没有影响,它的命名规则:函数中引入的参数命名从p0开始,依次递增。Dalvik字节码的类型、方法与字段

1、类型

Dalvik字节码只有两种类型,基本类型与引用类型。

2、方法

Dalvik使用方法名、类型参数与返回值来详情描述一个方法。

方法格式如下: Lpackage/name/ObjectName;->MethodName(III)Z

3、字段

Dalvik虚拟机定位字段与字节码静态分析时会用到它。

格式如下:Lpackage/name/ObjectName;->FieldName:Ljava/lang/String;Dalvik指令集

1、空操作指令的助记符为nop。它的值为00,通常nop指令被用来作对齐代码只用。

2、数据操作指令为move。move 指令根据字节码的大小与类型不同,后面会跟上不同的后缀。

3、返回指令指的是函数结尾时运行的最后一条指令。return-void、return vAA、return-wide vAA、return-object vAA

4、数据定义指令用来定义程序中用到的常量、字符串、类等数据。它的基础字节码为const。如:const/4 vA,#+B

5、锁指令多用在多线程程序中对同一对象的操作。如:

monitor-enter vAA 锁住

monitor-exit vAA 释放

6、实例操作指令包括实例的类型转换、检查以及新建等。

7、数组操作指令包括获取数组长度、新建数组、数组赋值、数组元素取值与赋值等操作。

8、异常指令集中有一条指令用来抛出异常。如:throw vAA

9、跳转指令用于从当前地址跳转到指定的偏移处。如:goto、switch、if

10、比较指令用于对两个寄存器的值进行比较。如:cmpkind vAA,vBB

11、字段操作指令用来对对象实例的字段进行读写操作。如:iinstanceop vA,vB,field@CCCC 与sstaticop vAA,field@BBBB

12、方法调用指令负责调用类实例的方法。它的基础指令为invoke。如:invoke-kind/range {vCCCC…vNNNNN}

13、数据转换指令用于将一种类型的数值转换成另一种类型。如:unop vA,vB

14、数据运算指令包括算术运算指令与逻辑运算指令。如:binop vAA,vBB,vCC 、add-type、sub-type……。Dalvik指令练习

写一个Dalvik版的Hello World。

1、编写smali文件

.class public LHelloWorld;

.super Ljava/lang/Object;

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

.registers 4

.parameter

.prologue

#空指令

nop

nop

nop

nop

#数据定义指令

const/16 v0, 0x8

const/4 v1, 0x5

const/4 v2, 0x3

#数据操作指令

move v1, v2

#数组操作指令

new-array v0, v0, [I

array-length v1, v0

#实例操作指令

new-instance v1, Ljava/lang/StringBuilder;

#方法调用指令

invoke-direct {v1}, Ljava/lang/StringBuilder;->()V

#跳转指令

分享读者

作者2013年java转到Android开发,在小厂待过,也去过华为,OPPO等大厂待过,18年四月份进了阿里一直到现在。

被人面试过,也面试过很多人。深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长,而且极易碰到天花板技术停滞不前!

我们整理了一份阿里P7级别的Android架构师全套学习资料,特别适合有3-5年以上经验的小伙伴深入学习提升。

主要包括阿里,以及字节跳动,腾讯,华为,小米,等一线互联网公司主流架构技术。如果你有需要,尽管拿走好了。

35岁中年危机大多是因为被短期的利益牵着走,过早压榨掉了价值,如果能一开始就树立一个正确的长远的职业规划。35岁后的你只会比周围的人更值钱。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
197689)]

35岁中年危机大多是因为被短期的利益牵着走,过早压榨掉了价值,如果能一开始就树立一个正确的长远的职业规划。35岁后的你只会比周围的人更值钱。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 23
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android虚拟Dalvik完整源码,宝贵资源,欢迎下载! This directory contains the Dalvik virtual machine and core class library, as well as related tools, libraries, and tests. A note about the licenses and header comments --------------------------------------------- Much of the code under this directory originally came from the Apache Harmony project, and as such contains the standard Apache header comment. Some of the code was written originally for the Android project, and as such contains the standard Android header comment. Some files contain code from both projects. In these cases, the header comment is a combination of the other two, and the portions of the code from Harmony are identified as indicated in the comment. Here is the combined header comment: /* * Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ---------- * * Portions of the code surrounded by "// BEGIN Harmony code" and * "// END Harmony code" are copyrighted and licensed separately, as * follows: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Native SH call bridge --------------------- Native SH call bridge is written by Shin-ichiro KAWASAKI and Contributed to Android by Hitachi, Ltd. and Renesas Solutions Corp.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值