public
class
UnicornTest {
long
BASE
=
0x1000
;
@Test
public void test(){
/
/
先来定义我们的机器码
byte[] code
=
new byte[]{
0x03
,
0x20
,
0x02
,
0x21
,
0x08
,
0x44
};
/
/
创建 Unicorn 对象, 它就像一个CPU, 我们可以使用它的接口来操作这个CPU
/
/
参数一:架构
/
/
参数二:运行模式(在ARM、Thumb中无关紧要,最终还是靠运行时判断)
Unicorn unicorn
=
new Unicorn(UnicornConst.UC_ARCH_ARM,UnicornConst.UC_MODE_THUMB);
/
/
mem_map 进行内存映射,在使用Unicorn提供的内存时,必须先进行映射
/
/
参数一:起始地址
/
/
参数二:映射内存区域的大小
/
/
参数三:内存操作权限
unicorn.mem_map(BASE,
0x1000
,UnicornConst.UC_PROT_WRITE | UnicornConst.UC_PROT_READ | UnicornConst.UC_PROT_EXEC);
/
/
mem_write 可以进行对映射出的内存进行写入操作
/
/
参数一:写入的地址
/
/
参数二:写入的内容
unicorn.mem_write(BASE,code);
/
/
emu_start 开始执行CPU
/
/
参数一:开始执行的地址(Thumb指令地址
+
1
)
/
/
参数二:结束地址(当结束地址命中时,结束执行)
/
/
参数三:超时时间(ms),当该值为
0
时,Unicorn将在无限时间内模拟代码,直到模拟完成
unicorn.emu_start(BASE
+
1
,BASE
+
code.length,
0
,
0
);
/
/
此时三条Thumb指令已经模拟完成
/
/
reg_read 可以读取寄存器,相应的reg_write可以写入寄存器的值
/
/
参数:寄存器常量
Long
o
=
(
Long
) unicorn.reg_read(ArmConst.UC_ARM_REG_R0);
System.out.println(
"the emulate finished result is ==> "
+
o.intValue());
}
}