Assembly language program for factorial of number 0,1,2,3,4, 5,6,7 and 8

Program:

lxi h,0000H;
mov D,m;
INR D;
MVI B,1;
MVI C,0;
MVI E,0;
MVI L,1;
LOOP:     MVI A,0;
    LOOP1: ADD L;
        JNC NOCARRY;
        INR C;
       

NOCARRY: DCR B;
   
    JZ NEXT;
    JMP LOOP1;
    NEXT: MOV E,A;
        MOV A,L;
        INR A;
        CMP D;
        JZ END;
        INR L;
        MOV B,E;
        MOV H,C;       
        MVI A,0;
        PRECARRY: ADD L;
        DCR H;
        JNZ PRECARRY;
       
        MOV C,A;
        JMP LOOP;
     
END: MOV A,E;
    STA 0002;
MOV A,C;
STA 0001;
    HLT;

Observation:

6!=>  2001: 02    2002: 208
we know that value of  6!  is 720(i.e. 256+256+208), 8085 use 8bit memory so memory over flow become two times that represent carry 02 and remaining value is 208 
7!=>  2001: 19    2002: 176
we know that value of  7!  is 5040 (i.e.256*19+176), 8085 use 8bit memory so memory over flow become 19 times that represent carry 19 and remaining value is 176
8!=>  2001: 128    2002: 157
we know that value of  8!  is 40320 (i.e.256*157+128), 8085 use 8bit memory so memory over flow become 157 times that represent carry 157 and remaining value is 128

Result:



Input values at 0000 address
Output value at 2002
Carry at 2001
1
1
0
2
2
0
3
6
0
4
24
0
5
120
0
6
208
02
7
176
19
8
128
157
0
1
0





































Comments

Popular posts from this blog

Preamble to the Constitution of India:

Designing a calculator using HTML and JavaScript