an 8085 assembly language program to find the factorial of a number
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
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.)
With an intel 8085 processor, taking an 8 bit number as the input one can use this. In this case we used 5 (assigning it on the first line).
Code
mvi b,05 ; Put the value of 5 in the b regsiter
mov c,b ; Copy b into c
dcr b ; Decrement the b register
l1: mov d,b ; Create the l1 section and move b into d
mvi a,00 ; Put 0 in the accumulator
l2: add c ; Create the l2 section and move add c to the accumulator
dcr d ; Decrease d
jnz l2 ; Jump back to l2
mov c,a ; Move a into c
dcr b ; Decrease b by 1
jnz l1 ; Jump to l1
hlt ; Stop the program
Explanation
Firstly, we will use two registers, both starting off in our case with 5 in them. We will use the c register to count our ongoing calculation, while we use the b value to see what is left to do. Since c already has the first value (in our case 5) in it, we can decrement the b value by 1. After the first three lines we end up with:
B: 4 ; What to multiply buy next
C: 5 ; Final Answer
In the next line we create the l1 section. This will be used to trigger the multiplication. We will do multiplication here through repeatedly adding the current c value to the overall c, and doing this b amounts of time. We do this through the following:
l1: mov d,b ; Move the b value into d (this is what we will decrease in the repeated addition)
mvi a,00 ; Clear out the accumulator
l2: add c ; Add what we have done so far to the accumulator
dcr d ; Decrease d by 1
jnz l2 ; Jump back to l2
mov c,a ; Move a into c
dcr b ; Decrease b by 1
jnz l1 ; Jump to l1
Example Walk Through
By the end of line 3:
A: 0
B: 4
C: 5
D: 0
By line 7, before we jump back:
A: 5
B: 4
C: 5
D: 3
After jumping back to l2 until D is 0
A: 14
B: 04
C: 05
D: 00
After moving this into c and decreasing B, before we start working with the x3:
A: 14
B: 03
C: 14
D: 00
After repeating the process above, by adding c to itself twice, and then assigning that to the value c
A: 3C
B: 02
C: 3C
D: 00
After the program completes, before the `hlt` function
A: 78
B: 00
C: 78
D: 00
Logic
5!= 5x4x3x2x1 = 120
The program outputs this as 78 in the C register, and 78 in hex = 120 in denary.
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
精彩评论