用Dalvik指令集寫個java類

來源:互聯網
上載者:User

標籤:android java dalvik指

Dalvik指令集

.class public LCalculate;#定義類名

.super Ljava/lang/Object;#定義父類
.method public static main([Ljava/lang/String;)V#申明靜態main()方法,L表示這是一個類
.registers 5   #方法中使用5個寄存器
.prologue  #代碼起始指令
nop #空指令
nop
nop
nop
new-instance v0, LCalculate;   #構造一個Calculate執行個體
invoke-direct{v0},LCalculate;-><init>()V      #調用建構函式
sget-object v1,Ljava/lang/System;->out:Ljava/io/PrintStream;  #靜態欄位操作指令
const/4 v2,0x5      #將5存入寄存器V2
const/4 v3,0x3     #將3存入寄存器V3
invoke-virtual{v0,v2,v3},LCalculate;->foo(II)I  #調用LCalculate執行個體foo虛函數(V0為隱式的LCalculate對象引用,後面二個是參數)
move-result v0  #擷取foo函數執行後返回的結果
invoke-virtual{v1,v0},Ljava/io/PrintStream;->println(I)V  #方法調用指令
return-void  #函數從一個void方法返回
.end method  #方法結束符號


# direct methods     #表示這是一個直接方法
.method public constructor <init>()V    #建構函式
    .registers 1   #一個寄存器


    .prologue   #代碼起始指令
    invoke-direct {p0}, Ljava/lang/Object;-><init>()V #調用執行個體的直接方法


    return-void #函數從一個void方法返回

.end method  #方法結束符號


# virtual methods  #表示這是一個虛方法
.method public foo(II)I  #方法名為foo,二個參數且都是int類型,並且返回值為int
.registers 5  #方法中使用5個寄存器
.prologue #代碼起始指令
add-int v0,p1,p2 #p1,p2兩個int類型相加,結果儲存在V0
sub-int v1,p1,p2#p1,p2兩個int類型相減,結果儲存在V1

mul-int/2addr v0,v1  #v0,v1兩個寄存器進行乘法運算,結果儲存在v0寄存器
return v0   #返回寄存器V0的值
.end method #方法結束符號

java源碼:

public class Calculate
{
public int foo(int a,int b){
return (a+b)*(a-b);
}
public static void main(String[] argc) 
{
Hello hello=new Hello();
System.out.println(hello.foo(5,3));
}
}

用Dalvik指令集寫個java類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.