Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

assembly language factorial In this assignment, you will design and correctly im

ID: 3762365 • Letter: A

Question

assembly language factorial

In this assignment, you will design and correctly implement a factorial() function that calcu-
lates factorial recursively.
1. Function Call
You should have the following prototype for factorial():
word int factorial (word int number);
The basic structure of your code should be as follows:
main {
int num = getValueFromUser(); // value from switches
print(num); // print num to Hex A
int answer = factorial(num); // call the function
print(answer); // print answer to Hex X
}
word int factorial (word int num)
{
if(num==0)
return 1;
else
int value = num - 1;
print(value); // print the value to Hex X
return factorial(value)*num;
}
Note, you must pass and return ALL values to/from the function via the stack. No values can
be passed via registers.

Write the correct assembly code.

Explanation / Answer

A recursive procedure is one that calls itself. There are two kind of recursion: direct and indirect. In direct recursion, the procedure calls itself and in indirect recursion, the first procedure calls a second procedure, which in turn calls the first procedure.

Recursion could be observed in numerous mathematical algorithms. For example, consider the case of calculating the factorial of a number. Factorial of a number is given by the equation

For example: factorial of 5 is 1 x 2 x 3 x 4 x 5 = 5 x factorial of 4 and this can be a good example of showing a recursive procedure. Every recursive algorithm must have an ending condition, i.e., the recursive calling of the program should be stopped when a condition is fulfilled. In the case of factorial algorithm, the end condition is reached when n is 0.

Here is the poseudocode for the factorial

function factorial(n)
{
   if (n<=1)
      return 1;
   else
      return n * factorial(n-1);
}

section   .text
   global _start         ;must be declared for using gcc
  
_start:                  ;tell linker entry point

   mov bx, 3             ;for calculating factorial 3
   call proc_fact
   add   ax, 30h
   mov [fact], ax
  
   mov      edx,len        ;message length
   mov      ecx,msg        ;message to write
   mov      ebx,1          ;file descriptor (stdout)
   mov      eax,4          ;system call number (sys_write)
   int      0x80           ;call kernel

   mov   edx,1            ;message length
   mov      ecx,fact       ;message to write
   mov      ebx,1          ;file descriptor (stdout)
   mov      eax,4          ;system call number (sys_write)
   int      0x80           ;call kernel
  
   mov      eax,1          ;system call number (sys_exit)
   int      0x80           ;call kernel
  
proc_fact:
   cmp   bl, 1
   jg    do_calculation
   mov   ax, 1
   ret
  
do_calculation:
   dec   bl
   call proc_fact
   inc   bl
   mul   bl        ;ax = al * bl
   ret

section   .data
msg db 'Factorial 3 is:',0xa  
len equ $ - msg          

section .bss
fact resb 1