Smali disassembly language data types and methods

Smali disassembly language data types and methods

Introduction to Smali

Smali is a disassembly language for Android applications that converts the Dalvik bytecode of an Android application into an easy-to-read and edit text format. Using Smali, developers can view and modify the inner workings of Android applications for debugging and analysis. Smali code is often used to locate and fix problems in applications, or for reverse engineering.

Smali Data Types

In the Smali language, data types include the following:

  1. Basic data types: including integer, floating point, Boolean and character. For example, integers can be int, long, short, byte, etc.; floating point types can be float, double; Boolean is boolean; character is char.
  2. Reference data types: include class types, interface types, and array types. For example, a class type can be a custom class or a system-provided class; an interface type refers to a class that implements a certain interface; an array type can be an array of a basic data type or an array of a reference data type.

In the Smali language, each data type has corresponding instructions and operators to operate and process it.

  • Classes in Java correspond to .class files in Smali
  • Methods in Java correspond to .method instructions in Smali
  • A field in Java corresponds to a .field directive in Smali.
  • Control structures in Java (such as if statements, for loops, etc.) correspond to corresponding instruction sequences in Smali

Smali Data Types

Java Data Types

Z

boolean

B

byte

S

short

C

char

I

int

J

long

F

float

D

double

L

Object references

[

Arrays

Smali code instructions

Smali code can be obtained by compiling Java source code into Dalvik bytecode and then disassembling it.

Smali code example:

 .method public static main([Ljava/lang/String;)V .registers 5 const/4 v0, 0x3 const/4 v1, 0x5 add-int v2, v0, v1 return-void .end method

The above is a simple Smali method that implements the function of adding two constants and returning the result.

Some common Smali instruction examples:

  1. Load constants:
 const/4 v0, 0x0
  1. Jump instruction:
 if-eqz v0, :cond_0
  1. Method call:
 invoke-virtual {v0}, Ljava/lang/String;->length()I
  1. Field operations:
 iget-object v0, p0, Lcom/example/MyClass;->myField:Ljava/lang/String;
  1. Mathematical operations:
 add-int v0, v1, v2

These are just a few examples of Smali instructions. The Smali language covers various operations of the Dalvik virtual machine and is used to describe various behaviors and logic of Android applications.

Smali Examples

 .method public static main([Ljava/lang/String;)V .registers 2 .prologue const-string v0, "Hello, World!" sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream; invoke-virtual {v1, v0}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V return-void .end method

The above code represents a static method named main, which accepts a String array as a parameter and returns a void type. In the method, the string "Hello, World!" is first stored in register v0, then the System.out object is obtained and the println method is called with the string (Hello, World!) in v0 as a parameter for output. Finally, the void type is returned.

<<:  More modern image format WebP usage and conversion

>>:  Android uses JobScheduler to perform background tasks

Recommend

How to take advantage of hot topics to create popular advertising materials?

01. Why do selling points need to take advantage ...

What kind of H5 page will trigger chain transmission?

Countless people have quoted Dickens's words ...

The marketing logic of private domain traffic

Private domain traffic is a good thing for some p...

Will the ill-fated Tizen make a comeback in low-end phones?

At the beginning of last year, Samsung launched t...

Yuanfudao product analysis report!

On March 31, 2020, Yuanfudao Online Education off...

Why do sunflowers follow the sun? Scientists discover new mechanism →

Compiled by: Gong Zixin sunflower Known for its a...

Is new media operation easy to do? 4 Dimensions of Analysis

Taking a different approach, let everyone use fou...

Search advertising or social media advertising, which one is better?

When it comes to advertising, especially digital ...

Short video operation skills and case practice

With the continuous development and popularizatio...