Factorial using MACRO (Assembly Language)

; Program to compute factorial of a number using macro

FACT MACRO F ;DEFINING FACT MACRO
;SYNTAX – XX(NAME OF MARCO) MACRO XX(OBJECT)
UP: ;KIND OF LABEL TO LOOP BACK TO THE DESIRED LOCATION
MUL F ;MULTIPLY THE NUMBER IN THE F REGISTER
DEC F ; DECREMENT THE NUMBER
JNZ UP ; JUMP IF NOT ZERO TO UP BACK
ENDM ; END OF MACRO

DATA SEGMENT ;INITIALIZE DATA SEGMENT
NUM1 DW 05H ;INITIALIZE NUM1 ANY DATA
RESULT DW ? ;INITIALIZE RESULT
DATA ENDS ;END OF DATA 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
FACT NUM1 ;CALL THE MACRO FUNCTION BY
;SPECIFYING NAME OF MACRO AND PARAMETERS (OBJECT) TO PASS
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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s