从装配中反向设计优化的c代码

时间:2015-02-19 16:18:15

标签: c assembly x86 reverse-engineering compiler-optimization

这个问题的关键是对使用2级优化运行编译器后生成的c代码进行反向工程。原始c代码如下(计算最大公约数):

int gcd(int a, int b){
    int returnValue = 0;
    if (a != 0 &&  b != 0){
        int r;
        int flag = 0;
        while (flag == 0){
            r = a % b;
            if (r ==0){
                flag = 1;
            } else {
                a = b;
                b = r;
            }
        }
        returnValue = b;
    }
    return(returnValue);
}

当我运行优化编译时,我从命令行运行它:

gcc -O2 -S Problem04b.c

获取此优化代码的汇编文件

.gcd:
    .LFB12:
        .cfi_startproc
        testl   %esi, %esi
        je  .L2
        testl   %edi, %edi
        je  .L2
    .L7:
        movl    %edi, %edx
        movl    %edi, %eax
        movl    %esi, %edi
        sarl    $31, %edx
        idivl   %esi
        testl   %edx, %edx
        jne .L9
        movl    %esi, %eax
        ret
        .p2align 4,,10
        .p2align 3
    .L2:
        xorl    %esi, %esi
        movl    %esi, %eax
        ret
        .p2align 4,,10
        .p2align 3
    .L9:
        movl    %edx, %esi
        jmp .L7
        .cfi_endproc

我需要将此汇编代码转换回c代码,这就是我现在所处的位置:

int gcd(int a int b){
    /*
       testl %esi %esi
       sets zero flag if a is 0 (ZF) but doesn't store anything
       */
    if (a == 0){
        /*
           xorl %esi %esi
           sets the value of a variable to 0. More compact than movl
           */
        int returnValue = 0;
        /*
           movl %esi %eax
           ret

           return the value just assigned
           */
        return(returnValue);
    }
    /*
       testl %edi %edi
       sets zero flag if b is 0 (ZF) but doesn't store anything
       */
    if (b == 0){
        /*
           xorl %esi %esi
           sets the value of a variable to 0. More compact than movl
           */
        int returnValue = 0;
        /*
           movl %esi %eax
           ret

           return the value just assigned
           */
        return(returnValue);
    }

    do{
        int r = b;
        int returnValue = b;

    }while();


}

任何人都可以帮我写回c代码吗?我很丢失。

1 个答案:

答案 0 :(得分:8)

首先,您在代码中混合了值。 %esi以值b开头,%edi以值a开头。

您可以从testl %edx, %edx行推断%edx用作以.L7开头的循环的条件变量(如果%edx与0不同则控制为转移到.L9块,然后返回.L7)。我们在反向工程代码中将%edx称为remainder


让我们开始对主循环进行逆向工程:

movl    %edi, %edx

由于%edi存储a,这相当于使用remainder初始化a的值:int remainder = a;

movl    %edi, %eax

存储int temp = a;

movl    %esi, %edi

执行int a = b;(请记住%edia%esib)。

sarl $31, %edx

此算术移位指令将remainder变量向右移位31位,同时保持数字的符号。通过移动31位,如果它是正(或零),则将remainder设置为0,如果它为负,则设置为-1。所以它等同于remainder = (remainder < 0) ? -1 : 0

idivl %esi

%edx:%eax除以%esi,或者在我们的情况下,将remainder * temp除以b(变量)。 余额将存储在%edx或我们的代码remainder中。将此与前一条指令相结合时:remainder < 0然后remainder = -1 * temp % b,以及remainder = temp % b

testl   %edx, %edx
jne .L9

检查remainder是否等于0 - 如果不是,请跳至.L9。那里的代码只是在返回b = remainder;之前设置.L7。为了在C中实现这一点,我们将保留一个count变量,该变量将存储循环迭代的次数。我们将在循环开始时执行b = remainder,但仅在第一次迭代后执行count != 0

我们现在已准备好构建完整的C循环:

int count = 0;
do {
    if (count != 0)
        b = remainder;
    remainder = a;
    temp = a;
    a = b;
    if (remainder < 0){
        remainder = -1 * temp % b;
    } else {
        remainder = temp % b;
    }

    count++;
} while (remainder != 0)

循环结束后,

movl    %esi, %eax
ret

将返回程序计算出的GCD(在我们的代码中它将被存储在b变量中)。