# Factorial using FAR PROCEDURE (Assembly Language)

; Program to compute factorial of a number using FAR Procedure

DATA SEGMENT ;INITIALIZE DATA SEGMENT
NUM1 DW 05H ;INITIALIZE NUM1 ANY DATA
RESULT DW ? ;INITIALIZE RESULT
DATA ENDS ;END OF DATA SEGMENT

MY SEGMENT ;DEFINING OUR OWN SEGMENT
;SYNTAX – XX(SEGMENT NAME) SEGMENT
FACT PROC FAR ;DEFINING FAR PROCEDURE
;SYNTAX – XXX(PROCEDURE NAME) PROC FAR(METHOD)
MUL CX ;MULTIPLY THE CONTENT OF CX REGISTER
RET ;RETURN BACK TO PROCEDURE
ENDP ;END OF PROCEDURE
MY ENDS ;END OF MY SEGMENT

CODE SEGMENT ;INITIALIZE CODE SEGMENT
START: ;START THE CODE
ASSUME CS:CODE , DS:DATA ;ASSUMPTION OF CODE AND DATA
MOV AX,DATA ;MOV DATA INTO ACCUMULATOR AX
MOV DS,AX ;MOV DATA FROM ACCUMULATOR INTO DATA SEGMENT REGISTER
MOV CX,NUM1 ;MOV NUM1 INTO THE CODE SEGMENT CX REGISTER
;WE MOVE THE NUMBER INTO CS REGISTER BECAUSE CS REGISTER ACT AS COUNT
MOV AX,0001H ;MOV CONSTANT 0001H INTO AX
UP : CALL FACT ;USING A LABEL UP TO MARK IT AS A FLAG
;CALLING THE FACT PROCEDURE
LOOP UP ;LOOPING THE FLAG IN UP
;LOOP DECREMENTS THE CX REGISTER BY ONE
MOV RESULT,AX ;MOVE THE RESULT FROM AX INTO RESULT VARIABLE
MOV AH,4CH ;END OF THE CODE
INT 21H ;END OF THE CODE
CODE ENDS ;END OF CODE SEGMENT
END START ;END OF START

; INPUT :
; NUM1 – 05H

; OUTPUT :
; RESULT – 078H

This site uses Akismet to reduce spam. Learn how your comment data is processed.