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?
source
share