0% found this document useful (0 votes)
262 views1 page

Factorial

This program uses an 8-bit accumulator and loop to calculate 5 factorial (5!) in an 8051 microcontroller. It employs a loop counter stored at data memory address 0, labeled as LoopControl, to repeatedly multiply the accumulator by the loop counter. The loop iterates from 5 to 1 to calculate the factorial of 5, which is 120, the maximum value that can be stored in an 8-bit accumulator.

Uploaded by

vsalaiselvam3553
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
262 views1 page

Factorial

This program uses an 8-bit accumulator and loop to calculate 5 factorial (5!) in an 8051 microcontroller. It employs a loop counter stored at data memory address 0, labeled as LoopControl, to repeatedly multiply the accumulator by the loop counter. The loop iterates from 5 to 1 to calculate the factorial of 5, which is 120, the maximum value that can be stored in an 8-bit accumulator.

Uploaded by

vsalaiselvam3553
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

;

;
;
;
;

This 8051 program computes 5! (that is, 5 factorial).


We can't use the 8051 to compute anything larger
than 5! = 120 since the ACC in the 8051 can only
hold 8 bit numbers. An 8 bit number has a max
value of 255 while 6! = 720.

;
;
;
;
;
;
;

Since computing a factorial involves repeated


multiplication, we want to employ a loop. We choose
to employ data memory address 0 for our loop control
variable. In order that we don't have to refer to
this location as "data memory address 0" we employ
the EQU assembler directive to assign a nice symbolic
label to this address.

LoopControl
;
;
;
;
;

EQU

It is a peculiarity of the factorial algorithm


that the loop control variable is employed not just
to control how many times the loop repeats, but
also for one of the operands in the multiplication
that takes place within the loop.
MOV A,#5
MOV LoopControl,A
DEC LoopControl

;
;
;
;
;

we want to compute 5! so
load the ACC in preparation
for an upcoming multiplication
at this point LoopControl = 5
LoopControl now holds 4

LoopBack:
MOV B,LoopControl

; load B reg in preparation


; for upcoming multiplication
MUL AB
; A <- A * B
DJNZ LoopControl,LoopBack
; decrements the data memory location
; and loops until it reaches 0
Done:

SJMP Done

You might also like