#算数运算 Java代码:
public void num(int b){
int a = b;
boolean b1 = true;
Log.d("MainActivity","a + b ="+(a+b));
Log.d("MainActivity","a - b ="+(a-b));
Log.d("MainActivity","a * b ="+(a*b));
Log.d("MainActivity","a / b ="+(a/b));
Log.d("MainActivity","a % b ="+(a%b));
}
与之对应的smali代码:
.method public num(I)V
.locals 5 #定义局部变量寄存器
.param p1,"b" #声明参数
.prologue #代码开始
.line 30
move v0, p1 #数据变量赋值
.line 31
.local v0, "a":I #定义局部变量
const/4 v1, 0x1 #数据常量赋值
.line 32
.local v1, "b1":Z #定义局部变量
const-string v2, "MainActivity" #定义字符串常量
new-instance v3,Ljava/lang/StringBuilder; #创建StringBuilder对象
invoke-direct {v3},Ljava/lang/StringBuilder;-><init>()V #调用构造
const-string v4,"a+b=" #调用虚方法 v3.append(v4)
invoke-virtual {v3,v4}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
move-result-object v3 #将返回值放入v3
add-int v4,v0,p1 #v4=v0+v1
invoke-virtual {v3,v4}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder; # v3.append(v4)
move-result-object v3
invoke-virtual v3, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
move-result-object v3
invoke-static {v2,v3},Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String) #调用静态方法
.line 33
const-string v2,"MainActivity"
new-instance v3,Ljava/lang/StringBuilder;-><init>V
const-string v4,"a-b="
invoke-virtual {v3,v4}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
move-result-object v3
sub-int v4,v0,p1 #v4 = v0 - p1
invoke-virtual {v3,v4}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
move-result-object v3
invoke-virtual v3, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
move-result-object v3
invoke-static {v2,v3},Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String)
.line 34
const-string v2,"MainActivity"
new-instance v3,Ljava/lang/StringBuilder;-><init>V
const-string v4,"a/b="
invoke-virtual {v3,v4}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
move-result-object v3
div-int v4,v0,p1 #v4 = v0 / p1
invoke-virtual {v3,v4}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
move-result-object v3
invoke-virtual v3, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
move-result-object v3
invoke-static {v2,v3},Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String)
.line35
const-string v2,"MainActivity"
new-instance v3,Ljava/lang/StringBuilder;-><init>V
const-string v4,"a*b="
invoke-virtual {v3,v4}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
move-result-object v3
mul-int v4,v0,p1 #v4 = v0 * p1
invoke-virtual {v3,v4}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
move-result-object v3
invoke-virtual v3, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
move-result-object v3
invoke-static {v2,v3},Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String)
.line 36
const-string v2,"MainActivity"
new-instance v3,Ljava/lang/StringBuilder;-><init>V
const-string v4,"a/b="
invoke-virtual {v3,v4}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
move-result-object v3
rem-int v4,v0,p1 #v4 = v0 % p1
invoke-virtual {v3,v4}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;
move-result-object v3
invoke-virtual v3, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
move-result-object v3
invoke-static {v2,v3},Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String)
.line 37
return-void
.end method
可以看到关于算数运算的代码如下:
add-int v4, v0 ,p1 加法,v4 = v0 + p1
sub-int v4, v0 ,p1 减法,v4 = v0 - p1
mul-int v4, v0 ,p1 乘法,v4 = v0 * p1
div-int v4, v0 ,p1 除法,v4 = v0 / p1
rem-int v4, v0 ,p1 取余,v4 = v0 % p1