STACK EQU 1FH
BOTTOM EQU 00H
M EQU 200
TOP DATA 3EH
N DATA 30H
NFACT EQU 31H
TST:
MOV SP,#5FH
LCALL SETNULL
MOV N,#5
LCALL FACT0
MOV N,#8
LCALL FACT0
MOV N,#10
LCALL FACT0
STOP: LJMP STOP
FACT0:
MOV A,N
LCALL DPUSH
LCALL FACT
LCALL DPOP
MOV R5,A
LCALL DPOP
MOV R6,A
LCALL DPOP
MOV R7,A
LCALL HBCD
MOV R0,#NFACT
MOV A,R1
MOV @R0,A
INC R0
MOV A,R2
MOV @R0,A
INC R0
MOV A,R3
MOV @R0,A
INC R0
MOV A,R4
MOV @R0,A
RET
FACT:
LCALL DPOP
CJNE A,#1,FACT1
LCALL DPUSH
CLR A
LCALL DPUSH
LCALL DPUSH
RET
FACT1:
LCALL DPUSH
DEC A
LCALL DPUSH
LCALL FACT
LCALL DPOP
MOV R5,A
LCALL DPOP
MOV R6,A
LCALL DPOP
MOV R7,A
LCALL DPOP
MOV R3,A
MOV A,R7
MOV B,R3
MUL AB
MOV R7,A
MOV A,B
XCH A,R6
MOV B,R3
MUL AB
ADD A,R6
MOV R6,A
CLR A
ADDC A,B
XCH A,R5
MOV B,R3
MUL AB
ADD A,R5
MOV R5,A
MOV A,R7
LCALL DPUSH
MOV A,R6
LCALL DPUSH
MOV A,R5
LCALL DPUSH
RET
HBCD:
CLR A
MOV R1,A
MOV R2,A
MOV R3,A
MOV R4,A
MOV R0,#18H
HB3:
MOV A,R7
RLC A
MOV R7,A
MOV A,R6
RLC A
MOV R6,A
MOV A,R5
RLC A
MOV R5,A
MOV A,R4
ADDC A,R4
DA A
MOV R4,A
MOV A,R3
ADDC A,R3
DA A
MOV R3,A
MOV A,R2
ADDC A,R2
DA A
MOV R2,A
MOV A,R1
ADDC A,R1
DA A
MOV R1,A
DJNZ R0,HB3
RET
SETNULL:
MOV A,#BOTTOM
MOV TOP,A
RET
DPUSH:
MOV B,A
MOV A,TOP
INC A
CJNE A,#M,PUSH1
SETB C
SJMP PUSHE
PUSH1:
MOV TOP,A
MOV DPH,#STACK
MOV DPL,TOP
MOV A,B
MOVX @DPTR,A
CLR C
PUSHE:
RET
DPOP:
MOV A,TOP
CJNE A,#BOTTOM,POP1
SETB C
SJMP POPE
POP1:
MOV DPH,#STACK
MOV DPL,TOP
MOVX A,@DPTR
DEC TOP
CLR C
POPE:
RET
END