You are on page 1of 1

/*Factorial of an 8-bit number (Max 8-bit number)*/

ORG 0000H

MOV DPTR, #8300H ; get the input memory address

MOVX A, @DPTR ; get the input number to accumulator

MOV B, #00H ; clear register B

CJNE A, #00H, NEXT ; if input number is! = 00 jump to label “NEXT”

MOV A, #01H ; if input number is = 00 store factorial as 01 in accumulator

SJMP L2 ; jump to label “L2”

NEXT: CJNE A, #01H, FACTO ; if input number is! = 01 jump to label “FACTO”

SJMP L2 ; jump to label “L2”

FACTO: MOV R1, #01H ; Initialize register R1 with 01

MOV R2, #01H ; Initialize register R2 with 01

MOV R0, A ; copy the input data to register R0

REPEAT: MOV A, R2 ; get the R2 register content to accumulator

INC R1 ; increment the register R1 content

MOV B, R1 ; get the R1 register content to register B

MUL AB ; multiply the accumulator and B register content

MOV R2, A ; store the lower byte of result to register R2

MOV A, R0 ; get the input number to accumulator

CJNE A, 01H, REPEAT ; if input number! = register R1 content, jump to “REPEAT”

MOV A, R2 ; if equal, get lower byte of factorial output to accumulator

L2: INC DPTR ; get the result+1 memory address

MOVX @DPTR, A ; store the lower byte of result in result memory

SJMP $

END

You might also like