8086 program to find the factorial of a number - GeeksforGeeks

22 May, 2018
Prerequisite – 8085 program to find the factorial of a numberProblem – Write an assembly language program for calculating the factorial of a number using 8086 microprocessor
Examples –
Input : 04H
Output : 18H
as In Decimal : 4*3*2*1 = 24
   In Hexadecimal : 24 = 18H

Input : 06H
Output : 02D0H
as In Decimal : 6*5*4*3*2*1 = 720
   In Hexadecimal : 720 = 02D0H
Assumptions –Starting address of program: 0400Input memory location: 0500Output memory location: 0600 and 0601
Important –If the Given Number is a 16-bit number, the AX register is automatically used as the second parameter and the product is stored in the DX:AX register pair. This means that the DX register holds the high part and the AX register holds the low part of a 32-bit number.
In 8086 microprocessor, user have direct instruction (MUL) to multiply two numbers, so we don’t have to add Multiplicand by Multiplier times like in 8085
Advantage of 8086 over 8085 (In case of Multiply):
Don’t have to write a bulky code as 8086 has a small code
Easy to remember
Already have multiplication Instruction
Algorithm –
Input the Number whose factorial is to be find and Store that Number in CX Register (Condition for LOOP Instruction)Insert 0001 in AX(Condition for MUL Instruction) and 0000 in DXMultiply CX with AX until CX become Zero(0) using LOOP InstructionCopy the content of AX to memory location 0600Copy the content of DX to memory location 0601Stop Execution
Input the Number whose factorial is to be find and Store that Number in CX Register (Condition for LOOP Instruction)
Insert 0001 in AX(Condition for MUL Instruction) and 0000 in DX
Multiply CX with AX until CX become Zero(0) using LOOP Instruction
Copy the content of AX to memory location 0600
Copy the content of DX to memory location 0601
Stop Execution
Program –
Explanation –
MOV CX, [0500] loads 0500 Memory location content to CX RegisterMOV AX, 0001 loads AX register with 0001MOV DX, 0000 loads DX register with 0000MUL CX multiply AX with CX and store result in DX:AX pairLOOP 040A runs loop till CX not equal to ZeroMOV [0600], AX store AX register content to memory location 0600MOV [0601], DX store DX register content to memory location 0601HLT stops the execution of program
MOV CX, [0500] loads 0500 Memory location content to CX Register
MOV AX, 0001 loads AX register with 0001
MOV DX, 0000 loads DX register with 0000
MUL CX multiply AX with CX and store result in DX:AX pair
LOOP 040A runs loop till CX not equal to Zero
MOV [0600], AX store AX register content to memory location 0600
MOV [0601], DX store DX register content to memory location 0601
HLT stops the execution of program
microprocessor
system-programming
Computer Organization & Architecture
microprocessor
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Architecture of 8086
Logical and Physical Address in Operating System
Computer Organization and Architecture | Pipelining | Set 1 (Execution, Stages and Throughput)
Computer Organization | RISC and CISC
Architecture of 8085 microprocessor
Interrupts
Memory Hierarchy Design and its Characteristics
Computer Organization | Von Neumann architecture
Difference between Von Neumann and Harvard Architecture
Programmable peripheral interface 8255