In our previous article, we discussed how to construct C code from x86 assembly code for functions. We also saw how parameters are passed in a function and how the returned value is obtained. We also learned about stack frames and where in the stack frame the local variables and parameters of a function are stored. In this article, we will learn how to construct C code from x86 assembly code for recursive functions.
This article is divided into two parts. In the first part, we will write C code that uses recursive functions and analyze the corresponding x86 assembly code. In the second part of the article, we will look into the x86 assembly code and try to construct the corresponding C code that uses recursion.
Let’s first write a small piece of C code that uses recursion.
#include <stdio.h> int func(int n) { if(n == 0) return 0; n = n + func(n - 1); return n; } int main() { int r; r = func(10); return 0; }
Now, let’s compile the program using the GCC compiler and see the corresponding assembly code of the executable file. Please note that the “-M intel” option will generate the assembly code in the Intel syntax.
$ gcc recursion1.c $ objdump -M intel -d a.out
Please note that a.out is an executable file. We will look into the corresponding assembly code for main() and func() only. …
0 Comments