Migrating from Assembly code to C

This is AT & T syntax

.global bar
.type bar, @function

bar:

pushl %ebp
movl %esp, %ebp
pushl %ebx
subl $20, %esp
movl 8($ebp), %ebx
movl $1, %eax
cmpl $1, %ebx
jle .L3
leal -1(%ebx), %eax  //subtracts 1 from ebx and stores into eax
movl %eax, (%esp)    //putting on stack frame
call bar             //recursive call
addl %ebx, %eax      // adds %ebx and %eax


.L3                  //returns %eax
addl $20, %esp
popl %ebx
popl %ebp
ret                  //end of bar

So, I think what happens here, basically, it checks if% ebx <= 1 is equal, and if it is, it returns one. otherwise, it calls bar with x -;

So my C code:

int bar (int x)
{
if (x<= 1)
return 1;
else
return x + bar(x-1);
}

The recursive challenge really deceives me. I understand that it calls a bar with the new% eax register (which basically becomes x-1). So does it just return the sum of numbers?

+4
source share
2 answers

I would put in the annotation like this:

bar:                     // bar() {
    pushl %ebp           //   function prologue
    movl %esp, %ebp      //
    pushl %ebx           //
    subl $20, %esp       //
    movl 8($ebp), %ebx   //   %ebx = x
    movl $1, %eax        //   %eax = 1
    cmpl $1, %ebx        //   if (x > 1)
    jle .L3              //   {
    leal -1(%ebx), %eax  //     %eax = x - 1
    movl %eax, (%esp)    //     put (x - 1) on stack
    call bar             //     %eax = bar(x - 1)
    addl %ebx, %eax      //     %eax += x
.L3                      //   }
    addl $20, %esp       //   function epilogue
    popl %ebx            //
    popl %ebp            //
    ret                  //   return %eax
                         // }

So, C looks quite equivalent to what you posted:

int bar (int x)
{
  if (x > 1)
    return bar(x - 1) + x;

  return 1;
}

: () C clang -m32 -S "" , store/load, - , , . .

+4
int bar(int x)
{
if (x<= 1)
return 1;
else
return x+bar(x-1);
}

x 1

+1

Source: https://habr.com/ru/post/1620022/


All Articles