Recusive Functions in x86 Assembly

Scott Cosentino
5 min readDec 22, 2019

Using functions, it is also possible to implement recursion in x86 assembly. The idea of recursion is very similar to high-level languages; however, we need to still account for the typical calling conventions of x86 in our recursive calls.

Suppose we want to implement a factorial function, which calculates the factorial of a single parameter passed to it. To start, we are going to set up our function call the same as any other function. We will push the value we want to find the factorial of onto the stack, then call the factorial function. When it returns, we will move the result from eax to ebx, move 1 into eax, and interrupt the system.

Inside the factorial function is where things get a little more interesting. We start off by pushing ebp to the stack to set up our stack pointer and reference point. From here, we move 8(%ebp) into eax to get our parameter off the stack. The end condition of our recursion is that the parameter we have is equal to 1, so we will check this first. We compare one to the current parameter and jump to the end_factorial method if they are equal. The end_factorial method completes all the typical cleanup of a function and returns to the previous eip.

If the parameter doesn’t equal 1, we decrement it, then push it to the stack as a parameter, and call factorial again. The tricky part of assembly recursion is tracking where each eip is pointing. When we called the first time, eip is pointing to the next line in ­_start. The second time we call it, the eip will be pointing…

--

--