Sei sulla pagina 1di 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

Potrebbero piacerti anche