Experiment No:- 9
(Right Page)
Aim: To write an assembly language program to find the factorial of a number using procedure.
(Right Page)
System Requirement: IBM PC or Compatible System with DOS
(Right Page)
Software Requirement: MASM611 Or TASM on DOS Box
(Right Page)
Theory: Instruction used:1. MOV Destination Source:This instruction loads the destinations with the contents of thesource. Both cannot be memory location. 2. RET:RET instruction causes the control to return to the main programfrom the subroutine. 3. DEC destination:Subtracts 1 from the specified destination. The destination canbe register or memory location. 4. MUL source:This instruction multiplies the source with the accumulator andstores the result in accumulator. 5. JNZ LABEL:Jump to specified label till zero flag is not set. 6. PROC:A procedure is a group of instructions that perform a task. Eachtime we call the procedure in our program, the control istransferred to the procedure. For calling the procedure, thereturn address has to be stored on to the stack. Format: [Procedure name] PROC [attribute]; beginning of the Procedure Procedure to be Done [Procedure name] ENDP ; end of procedure
(Right Page)
Algorithm: Step 1: Initialize data segment. Step 2: Declare two variable NUM and RES and initialize them with somevalue Step 3: End of data segment. Step 4: Initialize code segment.
Step 5: Initialize CS and DS register Step 6: Load counter CX with the value of variable NUM. Step 7: Load AX with value 0001H Step 8: Call procedure FACT. Step 9: Multiply the contents of counter CX with accumulator. Step 10: Decrement CX Step 11: Repeat steps 11 to 13 till counter CX becomes zero Step 12: Move the contents of accumulator to variable RES Step 13: End of code segment. Step 14: End of program
(Right Page)
Example: LET A = 4H CX = 4 Ax = 1 AX=AX*CX=4 CX= 3(after decrement) AX=AX*BX=0C BX=2 (after decrement) AX=AX*BX=18 BX= 1 (after decrement) AX=AX*BX=18
(LEFT PAGE)
Flow Chart:
(Right Page)
Conclusion: The program to find factorial of a number was developed by calling a procedure recursively. The procedure was written to perform the [Link] segment was used to define the procedure.
(Print Out)
Program: DATA SEGMENT NUM DW 0005H RES DW ? DATA ENDS CODE SEGMENT ASSUME DS:DATA,CS:CODE START: MOV AX,DATA MOV DS,AX MOV AX,0001H MOV CX,NUM BACK:CALL FACT JNZ BACK MOV RES,AX MOV AH,4CH INT 21H FACT PROC NEAR MUL CX DEC CX RET FACT ENDP CODE ENDS END START END
(Print Out)
Output: NUM 0005H AX 0001H CX NUM CALL FACT AX 0078H RES 0078H