an 8085 assembly language program to find the factorial of a number
Question
i want to find factorial a number which i take memory first.(intel 8085)
edit: i'm beginner. i don't know how to write it's assembly codes.
pseudo code:
input n
fact = 1
loop:
..multiply fact by n
..decrement n
..test n
..jump if not zero to loop
output fact
Solution
In the first place, you'd better learn how to write 8085 if you expect to use it. Assembler, especially for old 8-bit microprocessors, is not something you can just take canned software and patch in.
In the second place, exactly what are you using for numbers? The 8085 has an 8-bit bus and can use registers as 16 bits. If you're limited to 16-bit numbers, you can use a lookup table, as you can't represent 9! anyway.
In the third place, if you're doing it the algorithmic way, you might want to first look into what you're using for multiplication. The 8085 does not have on-chip multiplication. (I once won a contest for multiple-precision multiplication and division using the Z80, which had some features the 8085 didn't have. In particular, I was able to use the alternate register bank to do some useful stuff.)
OTHER TIPS
with an intel 8085 processor, taking an 8 bit number, in this case 5, i can write this:
mvi b,05
mov c,b
dcr b
l1: mov d,b
mvi a,00
l2: add c
dcr d
jnz l2
mov c,a
dcr b
jnz l1
hlt
the factorial of 5 would be then 78 in hex. the logic is.. 5!= 5x4x3x2x1
so add 5 four times, the result (i.e 20) add three times and so on... (5+5+5+5), (20+20+20), (60+60) and convert 120 to hex ...
With this program, you can find factorial of an 8 bit number whose answer doesn't exceed 24bits!
The input is given in the address location #2070
and output is obtained is in 2 memory locations in the order #2074
#2073
#2072
LHLD 2070
ANI 00
MOV C,L
MOV D,A
MOV E,A
XCHG
DCR C
JZ EXPT
JM EXPT
MOV B,C
L1: DAD D
JNC BAK1
INR A
BAK1: DCR C
JNZ L1
L3: DCR B
JZ STOP
MOV C,B
XCHG
LXI H,0000
L2: DAD D
JNC BAK2
INR A
BAK2: DCR C
JNZ L2
JMP L3
EXPT: MVI A,01
STA 2072
JMP END
STOP: SHLD 2072
STA 2074
END: HLT
MVI B, 07h
LXI H, 0007h
LXI D, 0007h
DCR B
LOOP1:
MOV C, B
LXI H, 0
LOOP:
DAD D
DCR C
JNZ LOOP
MOV E, L
MOV D, H
DCR B
JNZ LOOP1
HLT
This may help you. This is for 7!.
This code will find the factorial of 9. You can find factorial of any number upto 9 by changing the value in DB.
LHLD 2090
MOV C,L
MOV D,A
MOV E,A
XCHG
DCR C
MOV B,C
L1:DAD D
JNC B1
INR A
B1: DCR C
JNZ L1
L2 : DCR B
JZ END
MOV C,B
XCHG
LXI H,0000
JMP L1
END:SHLD 2092
STA 2094
HLT
#ORG 2090H
#DB 09H
Org 0000h
Mov dptr,#8000h
Mov a,@dptr
Mov r0,a
Mov r1,#01h
Mov b,r1
L1:mul ab
Dec r0
Mov b,r0
Cjne r0,#00h,l1
Mov r2,a
H:sjmp h
End