1
        .file   "calcnew.c"
        .text
.globl calcnew
        .type   calcnew, @function
calcnew:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %edx
        movl    12(%ebp), %ecx
        leal    0(,%ecx,8), %eax
        subl    %ecx, %eax
        leal    (%edx,%edx,2), %edx
        addl    %edx, %eax
        imull   $14, 16(%ebp), %edx
        addl    %edx, %eax
        popl    %ebp
        ret
        .size   calcnew, .-calcnew
        .ident  "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
        .section        .note.GNU-stack,"",@progbits

我想删除此汇编代码中的所有lealimull操作,并仅用sall addlsubl操作替换它们的功能。这是我的尝试:

    .file   "calcnew.c"
        .text
.globl calcnew
        .type   calcnew, @function
calcnew:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %edx
        movl    12(%ebp), %ecx
        sall    $3, %ecx
        movl    %ecx, %eax
        ;leal   0(,%ecx,8), %eax
        subl    %ecx, %eax
        movl    %edx, %ecx
        sall    $1, %edx
        addl    %ecx, %edx
        ;leal   (%edx,%edx,2), %edx
        addl    %edx, %eax
        movl    16(%ebp), %edx
        movl    %edx, %ecx
        sall    $4, %edx
        sall    $1, %ecx
        subl    %ecx, %edx
        ;imull  $14, 16(%ebp), %edx
        addl    %edx, %eax
        popl    %ebp
        ret
        .size   calcnew, .-calcnew
        .ident  "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
        .section        .note.GNU-stack,"",@progbits

使用新的程序集文件编译我的 c 代码时的输出应该与使用原始程序集文件编译(使用lealmull操作)相同,但是,我的输出是错误的。我做错了什么?

下面是调用汇编文件的 C 代码:

#include <stdio.h>

int calcnew(int x, int y, int z);

int main()
{
        int x = 2;
        int y = 6;
        int z = 11;
        int result;

        result = calcnew(x,y,z);

        printf("x=%d, y=%d, z=%d, result=%d\n",x,y,z,result);
}
4

1 回答 1

0

你替换

    leal    0(,%ecx,8), %eax

    sall    $3, %ecx
    movl    %ecx, %eax

但这破坏了 的值ecx,它是活出这些指令的。反而:

    movl    %ecx, %eax
    sall    $3, %eax
于 2013-11-04T06:16:04.433 回答