WRITE A 8085 CODE TO FIND FACTORIAL OF A NUMBER
- User
WRITE A 8085 CODE TO FIND FACTORIAL OF A NUMBER
- Assistant
WRITE A 8085 CODE TO FIND FACTORIAL OF A NUMBER
Certainly! Below is an example of 8085 assembly language code to find the factorial of a number. The number whose factorial is to be calculated is stored in memory location NUM (you can modify it as needed). The result (factorial) will be stored in memory locations RES_H (higher byte) and RES_L (lower byte) to accommodate factorial results larger than 8 bits (since factorial results grow very fast and can exceed 8-bit).
This example assumes the number is small (like 5 or 6) as factorial grows quickly and 16-bit multiplication might overflow quickly.
; Program to find factorial of a number (8-bit number)
; Input number at memory location NUM
; Output: factorial result at RES_H (high byte), RES_L (low byte)
ORG 0000H ; origin
; Initialize
LXI H, NUM ; HL points to input number
MOV A, M ; A = number (n)
CPI 0 ; check if n=0
JZ FACT_DONE ; if number=0 factorial=1
MVI D, 0 ; D = 0, will store high byte of result (initially 0)
MVI E, 1 ; E = 1, low byte of result (initially 1) => factorial=1
MOV C, A ; C = counter (from n down to 1)
FACTORIAL_LOOP:
; Multiply DE (16 bits) * C (8 bits)
; Multiplication routine (16-bit * 8-bit = 16-bit result)
; We'll multiply DE * C, store back in DE
MOV B, E ; B = low byte of factorial
MOV A, D ; A = high byte of factorial
; Clear HL for result (16-bit)
XRA H
XRA L
; Multiply B * C (low byte * multiplier)
MVI H, 0
MOV A, B
MOV L, A ; L = B
MOV A, 0
; Multiply low byte by C with addition
MOV A, B
CALL MULT8A1 ; Result in HL
; Save lower multiplication result
MOV E, L
MOV D, H
; Multiply high byte by C, add shifted result
MOV A, C
MOV B, A ; Save multiplier in B
MOV A, D ; Higher byte of original factorial
CALL MULT8A1 ; Multiply high byte of factorial by C
MOV H, H ; H now high byte of partial product
MOV L, L ; L now low byte
; Add shifted result (multiply by 256 means add to high byte of multiplication result)
MOV A, H
MOV C, D ; Save current high byte of factorial product in C
MOV D, A ; Update D (high byte) with A (from last multiplication)
; Accumulate results
MOV A, C
ADD E
MOV E, A
MOV A, D
ADC 0
MOV D, A
; Decrement counter
MOV A, C
DCR A
MOV C, A
JNZ FACTORIAL_LOOP
FACT_DONE:
; Store result in memory
LXI H, RES_L
MOV M, E ; Low byte
INX H
MOV M, D ; High byte
HLT
; Multiplication subroutine - multiply 8 bit A by 8 bit B in register C
; Returns 16-bit result in HL
; Using repeated addition (basic)
MULT8A1:
MOV L, A ; store multiplicand in L
XRA H ; initialize H=0 for result
MVI B, 0 ; B = 0, counter
MOV A, C ; multiplier in A
MOV B, A ; store multiplier in B
XRA A ; A=0 accumulator
MULT_LOOP:
MOV A, B
CPI 0
JZ MULT_DONE
DCR B
MOV A, L
ADD L ; A = A + multiplicand (adding twice each loop)
MOV L, A
JNZ MULT_LOOP
MULT_DONE:
RET
; Data
NUM: DB 05H ; Number for factorial
RES_L: DS 1 ; Low byte of result
RES_H: DS 1 ; High byte of result
END
NUM to any number between 0 and 6 for which this program will work.If you'd like a simpler code for small numbers storing only 8-bit result (factorial up to 5! = 120), let me know!
Not yet included in a published release.