ARM除法逆向分析

背景介绍

arm指令分析学习环境ndk build + IDA6.8 + android 6.0手机

有符号除法

模板 argc/2^n

c代码

  int main(int argc,char **argv){

        printf("hello world");
        //有符号除以2的幂
        printf("\n%d",argc/2);
        printf("\n%d",argc/4);
        printf("\n%d",argc/8);
        return 0;
        }

汇编代码


        .text:00000434                 CODE16
        .text:00000434                 PUSH    {R4-R6,LR}      ; DATA XREF: .text:000004D0o
        .text:00000434                                         ; .got:off_2FB4o
        .text:00000436                 MOVS    R4, R0            ;R4 = argc
        .text:00000438                 LDR     R0, =(aHelloWorld - 0x442)
        .text:0000043A                 LDR     R5, =(aD - 0x44A)
        .text:0000043C                 ASRS    R6, R4, #0x1F
        .text:0000043E                 ADD     R0, PC          ; "hello world"
        .text:00000440                 BL      j_j_printf

        .text:00000444                 LSRS    R1, R4, #0x1F
        .text:00000446                 ADD     R5, PC          ; "\n%d"
        .text:00000448                 ADDS    R1, R1, R4
        .text:0000044A                 MOVS    R0, R5
        .text:0000044C                 ASRS    R1, R1, #1
        .text:0000044E                 BL      j_j_printf

        .text:00000452                 LSRS    R1, R6, #0x1E
        .text:00000454                 ADDS    R1, R1, R4
        .text:00000456                 MOVS    R0, R5
        .text:00000458                 LSRS    R6, R6, #0x1D
        .text:0000045A                 ASRS    R1, R1, #2
        .text:0000045C                 BL      j_j_printf

        .text:00000460                 ADDS    R1, R6, R4
        .text:00000462                 MOVS    R0, R5
        .text:00000464                 ASRS    R1, R1, #3
        .text:00000466                 BL      j_j_printf
        .text:0000046A                 MOVS    R0, #0
        .text:0000046C                 POP     {R4-R6,PC}
回顾一下涉及到的指令
LSR语法
LSR Logical shift right by immediate
      <Rm>, LSR #<shift_imm>
      if shift_imm == 0 then
          shifter_operand = 0
          shifter_carry_out = Rm[31]
      else /* shift_imm > 0 */
          shifter_operand = Rm Logical_Shift_Right shift_imm
          shifter_carry_out = Rm[shift_imm - 1]
ASR语法          
ASR Arithmetic shift right by immediate
    if shift_imm == 0 then
        if Rm[31] == 0 then
            shifter_operand = 0
            shifter_carry_out = Rm[31]
        else /* Rm[31] == 1 */
            shifter_operand = 0xFFFFFFFF
            shifter_carry_out = Rm[31]
    else /* shift_imm > 0 */
        shifter_operand = Rm Arithmetic_Shift_Right <shift_imm>
        shifter_carry_out = Rm[shift_imm - 1]

两个指令的区别:LSR指令不用理会符号位,而ASR需要操作符号位

举例
    r0 = 0x3f000000  要变成--->0x7e000000
    MOVS R2,R0,LSL#1  会影响哪些标志位?为什么会影响C标志位?
    会影响N 标志位,C标志位,Z标志位
    //对于包含移位操作的非加法/减法,C被设置为由移位器移出值的最后一位
    r2 = 0x7e000000 其中
         被移除来的是0,所以C被设置为0
         结果0X7E000000 是正数,所以N标志位为1
         结果不等于0,所以Z标志位为0

模板 argc/2^n + 1 c代码

  int main(int argc,char **argv){
        printf("\n%d",argc/3);
        return 0;
        }

汇编代码

.text:AB1C3464                 PUSH    {R3,LR}
.text:AB1C3466                 MOVS    R1, #3    ;除数 3
.text:AB1C3468                 BL      sub_AB1C3528
.text:AB1C346C                 MOVS    R1, R0
.text:AB1C346E                 LDR     R0, =(unk_AB1C4760 - 0xAB1C3474)
.text:AB1C3470                 ADD     R0, PC ; unk_AB1C4760 ; char *
.text:AB1C3472                 BL      j_j_printf
.text:AB1C3476                 MOVS    R0, #0
.text:AB1C3478                 POP     {R3,PC}
---------------------------------------------------------------
.text:000005D0 sub_5D0                                 ; CODE XREF: sub_464+1Ap
 .text:000005D0                 CMP     R1, #0
 .text:000005D2                 BEQ     sub_658
 .text:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值