# CS2208b Assignment 5

\$30.00

## Description

QUESTION 1 (100 marks)
Recursion is a method where the solution to a problem depends on solutions to smaller instances of the same problem. A
function is considered recursive if it calls itself.
The following function computes xn
recursively, where x is an integer number and n is a non-negative integer number.
int power(int x, unsigned int n)
{
int y;

if (n == 0)
return 1;

if (n & 1)
return x * power(x, n – 1);
else
{ y = power(x, n >> 1);
return y * y;
}
}
Draw a detailed flowchart and write an ARM assembly program to calculate xn using the above recursive function,
where n is passed-by-value through the stack to the function and the returned value is stored in the stack just above the
parameter. No other registers may be modified by the power function. Once the control is completely returned back from
the function (i.e., after calculating xn
), the returned value must be stored in a local variable (called result) in the main
function. Your code should be highly optimized, i.e., use as little number of instructions as possible.
You should utilize a big enough stack so that you can calculate xn
for various n values.
How many stack frames are needed to calculate xn
, when n = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12?