Program that calculates the factorial on an integer number.

UNIVERSITY OF THE PUNJAB

Third Semester 2015

Examination: B.S. 4 Years Programme 

Paper: Computer Organization and Assembly Language

Course Code: IT-203 / 21402

Question # 4:

Write an assembly language program that calculates the factorial on an integer number.

Solution:

INCLUDE Irvine32.inc
.data
msg BYTE "Factorial : ",0
.code
main PROC
    push 5                ; calc 5!
    call Factorial        ; calculate factorial (EAX)

    mov edx, offset msg
    call WriteString
    call WriteDec        ; display it
    call Crlf
    call WaitMsg
    exit
main ENDP

Factorial PROC  
    ;Calculates a factorial.
    ;Receives: [ebp+8] = n, the number to calculate
    ;Returns: eax = the factorial of n
    push ebp
    mov ebp,esp
    mov eax,[ebp+8]        ; get n
    cmp eax,0            ; n > 0?
    ja L1                ; yes: continue
    mov eax,1            ; no: return 1 as the value of 0!
    jmp L2                ; and return to the caller
    L1:
    dec eax
    push eax            ; Factorial(n - 1)
    call Factorial        ; Instructions from this point on execute when each recursive call returns.
    ReturnFact:
    mov ebx,[ebp+8]       ; get n
    mul ebx                ; EDX:EAX = EAX * EBX
    L2:
    pop ebp                ; return EAX
    ret 4                ; clean up stack
Factorial ENDP
END main

Comments