do_something(int):
push rbp
mov rbp, rsp
push rbx
sub rsp, 24
mov DWORD PTR [rbp-20], edi
cmp DWORD PTR [rbp-20], 1
jg .L2
mov eax, DWORD PTR [rbp-20]
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-20]
sub eax, 1
mov edi, eax
call do_something(int)
mov ebx, eax
mov eax, DWORD PTR [rbp-20]
sub eax, 2
mov edi, eax
call do_something(int)
add eax, ebx
.L3:
add rsp, 24
pop rbx
pop rbp
ret
Asked
Active
Viewed 432 times
-3

Peter Cordes
- 328,167
- 45
- 605
- 847

BL L
- 11
- 2
-
Everything in the code is hidden 100% of the code is calling another functions and offsets you havent provided no anyone can help you – coderx64 May 03 '21 at 04:12
-
4@coderx64: `call do_something(int)` is calling *the same* function; the problem with this question is just lack of effort / attempt. This is pretty clearly un-optimized `g++` output copy/pasted from https://godbolt.org/. Changing the demangled `do_something(int)` to a valid asm symbol name like `do_something`, you could write a caller, and assemble this and run it so you could single-step. It is a self-contained recursive function. – Peter Cordes May 03 '21 at 06:24
-
@PeterCordes but there alot of [rbp-20] which we dont know what it is. – coderx64 May 03 '21 at 06:27
-
2@coderx64: Note the `push rbp` / `mov rbp, rsp` stack-frame setup, then `mov [rbp-20], edi` to dump the incoming integer arg to the stack. Accessing locals relative to the frame pointer (and spill/reload between every C statement) is 100% standard for un-optimized code ("debug" builds, like `gcc -O0`, the default for gcc/clang.) [Why does clang produce inefficient asm with -O0 (for this simple floating point sum)?](https://stackoverflow.com/q/53366394) – Peter Cordes May 03 '21 at 07:49
-
Ok thanks for info. it was just confusing to me because im not so much working with stack-frame setup – coderx64 May 03 '21 at 07:54
-
1This question will help nobody who stumbles upon it. "Can somebody explain what this does" is not even a question, so it does not provide value for the community. Just yourself. – justGoscha May 03 '21 at 16:57
1 Answers
2
It is an unoptimised recursive function in 64bit SystemV calling convention with one integer argument which returns integer value. It internally uses RBX
plus a 4-byte (DWORD) local variable at [rbp-20]
.
Called with negative argument, say -1, it returns RAX=0x00000000_FFFFFFFF
.
Called with zero argument it returns 0.
Going thru it step by step it gives
do_something(0)=0;
do_something(1)=1;
do_something(2)=1;
do_something(3)=2;
do_something(4)=3;
do_something(5)=5;
do_something(6)=8;
do_something(7)=13;
...
Generally, for positive number n this function returns the sum of its two predecessors: do_somenging(n)=do_something(n-1)+do_something(n-2)

Peter Cordes
- 328,167
- 45
- 605
- 847

vitsoft
- 5,515
- 1
- 18
- 31
-
1It's not a 24-byte local variable, that's was a misleading unhelpful description so I fixed it for you. The extra 16 bytes GCC reserves but doesn't use (beyond the 8 below the saved RBX needed for alignment to follow the ABI) are just a missed optimization. [Why does GCC allocate more space than necessary on the stack, beyond what's needed for alignment?](https://stackoverflow.com/q/63009070) – Peter Cordes May 03 '21 at 07:54
-
-
@phuclv: Deleted my comment, too, so future readers will have to recognize the sequence from the numbers. – Peter Cordes May 06 '21 at 05:10