Some 8051 micro controller programms
FINDING SUM OF NATURAL NUMBERS
CLR A
MOV R1,#0
MOV R2,#20
AGAIN: INC R1
ADD A,R1
DJNZ R2,AGAIN
MOV R5,A
END
FINDING NUMBER OF ZERO’S AND ONE’S IN A NUMBER
MOV R2,#0;INITIALIZE ONE'S COUNTER=0
MOV R3,#0;INITIALIZE ZERO'S COUNTER=0
MOV R1,#08;INITIALIZE THE LOOP COUNT
MOV R0,#56;NUMBER
MOV A,R0
BACK:RRC A
JC SKIP;IF CARRY IS PRESENT GO TO SKIP
INC R3;INCREMENT ZERO'S COUNT
AJMP LOOP1
SKIP:INC R2;INCREMENT ONE'S COUNT
LOOP1:DJNZ R1,BACK
END
FINDING VARIANCE AND MEAN OF NUMMBERS
MOV R7,#6H
MOV R0,#30H
MOV @R0,#5H
INC R0
MOV @R0,#2H
INC R0
MOV @R0,#7H
INC R0
MOV @R0,#4H
INC R0
MOV @R0,#3H
INC R0
MOV @R0,#3H
MOV R0,#30H
LJMP LOOP
BACK: MOV R0,#30H
MOV R3,#00H
MOV R2,#6H
SJMP LOOP2
LOOP2: MOV A,10H
MOV B,@R0
LOOP6: SUBB A,B
JC LOOP4
MOV R4,A
MOV B,R4
MUL AB
MOV R5,A
MOV R6,B
MOV A,#0FH
CLR C
ANL A,R5
MOV R5,A
MOV A,#0F0H
ANL A,R6
ORL A,R5
ADD A,R3
MOV R3,A
INC R0
DJNZ R2,LOOP2
MOV B,#6
DIV AB
MOV 18H,A
MOV 19H,B
SJMP SKIP
LOOP4:MOV A,@R0
MOV B,10H
CLR C
AJMP LOOP6
LOOP:CLR A
LOOP1: ADD A,@R0
INC R0
DJNZ R7,LOOP1
ACALL AVG
AJMP BACK
AVG: MOV B,#6H
DIV AB
MOV 10H,A
MOV 11H,B
RET
SKIP:NOP
END
SORTING AND FINDING AVERAGE
MAX SET R3 ;SET R3 FOR STORING MAXIMUM NUMBER
MIN SET R2 ;SET R2 FOR STORING MINIMUM NUMBER
MOV R0,#30H ;INITIALIZE THE REGISTER R1
MOV @R0,#7 ;INPUT NUMBERS TO STORE FROM 30H
INC R0 ;
MOV @R0,#4 ;
INC R0 ;
MOV @R0,#1 ;
INC R0 ;
MOV @R0,#8 ;
INC R0 ;
MOV @R0,#3 ;
MOV R3,#8 ;ASIGN THE LOOP COUNT FOR OUTER LOOP (AGAIN)
AGAIN:MOV R0,#30H ;STARTING THE PROCESS OF SORTING
MOV R4,#4 ; ASIGN INNER LOOP COUNT(BACK)
BACK:MOV B,@R0 ;
MOV R5,B ;
INC R0 ;
MOV A,@R0 ;
MOV R6,A ;
CJNE A,B,LOOP ;COMPARE THE TWO NUMBERS IF IT IS NOT EQUAL JUMP TO LOOP
AJMP LOOP1 ;IF TWO NUMBERS ARE EQUAL THEN JUMP TO LOOP1
LOOP:JNC LOOP1 ;IF A IS GREATER THAN B JUMP TO LOOP1
MOV B,R5 ;IF A<B EXCHANGE PROCESS STARTS ACORDING TO BUBBLE SORT
MOV @R0,B ;
DEC R0 ;
MOV A,R6 ;
MOV @R0,A ;
INC R0 ;
DJNZ R4,BACK ;REPEAT LOOP UNTIL R4=0
DJNZ R3,AGAIN ;REPEAT LOOP UNTIL R3=0
ACALL NEXT ;CALLS SUBROUTINE NEXT
AJMP SKIP ;JUMP TO SKIP AT THE END OF PROGRAM
LOOP1:DJNZ R4,BACK
DJNZ R3,AGAIN
ACALL NEXT
AJMP SKIP
NEXT:MOV A,@R0
MOV MAX,A ;STORES THE MAXIMUM VALUE IN MAX
MOV R0,#30H
MOV A,@R0
MOV MIN,A ;STORES THE MINIMUM VALUE IN MIN
ACALL LOOP3 ;CALLS THE SUBROUTINE LOOP3 FOR FINDING AVERAGE
RET
LOOP3: MOV R5,#5
MOV R0,#30H
CLR A
LOOP4:ADD A,@R0
INC R0
DJNZ R5,LOOP4
MOV B,#5
DIV AB
MOV 40H,A;MOVES THE QUOTIENT OF AVERAGE TO 40H
MOV 41H,B ;MOVES THE REMAINDER OF AVERAGE TO 41H
RET
SKIP:NOP
END
FIBBONACI SERIES
MOV R4,#8
MOV A,R4
MOV R3,A
SUBB A,#2
MOV R5,A
MOV R1,#30H
MOV @R1,#0
CLR C
MOV A,#1
MOV B,R3
MOV R4,B
SUBB A,R4
JZ NEXT
INC R1
MOV @R1,#1
CLR C
MOV A,#2
MOV B,R3
MOV R4,B
SUBB A,R4
JZ NEXT
MOV A,@R1
AGAIN:DEC R1
MOV B,@R1
ADD A,B
INC R1
INC R1
MOV @R1,A
DJNZ R5,AGAIN
NEXT :NOP
END
FINDING BIGGER NUMBER IN ARRAY
MOV R0,#60H
MOV R5,#1
MOV R2,#10
MOV R3,#10
AGAIN:MOV A,R5
MOV @R0,A
INC R5
INC R0
DJNZ R2,AGAIN
CLR A
MOV R0,#60H
MOV B,#0
SUM: MOV A,@R0
CJNE A,B,LOOP
LOOP: JC LOOP1
MOV B,A
INC R0
DJNZ R3,SUM
SJMP NEXT
LOOP1:INC R0
DJNZ R3,SUM
SJMP NEXT
NEXT:MOV A,B
END
BINARY TO GRAY
MOV A,#52H
MOV R0,A
CLR C
RRC A
XRL A,R0
END
BINARY TO BCD
MOV A,#76H
MOV B,#100
DIV AB
MOV R0,A
MOV A,B
MOV B,#10H
DIV AB
MOV R1,A
MOV R3,B
END
PROGRAM TO CHECK WHETHER CONTENT OF A>20H OR NOT
MOV A,#89H
MOV 20H,#10H
SUBB A,20H
JZ EQUAL
JNB ACC.7,POS
CLR P2.7
SJMP NEXT
POS:CLR P1.1
CLR P1.2
EQUAL:CLR P3.2
NEXT:NOP
END
FINDING GCD AND LCM OF NUMBERS <=22
NUM1 SET R2;SETS THE R2 FOR NUMBER1
NUM2 SET R3;SETS THE R3 FOR NUMBER2
GCD SET R6;SETS THE R6 FOR STORE GCD
LCM SET R7;SETS THE R7 FOR LCM
MOV NUM1,#6;NUMBER1
MOV NUM2,#10;NUMBER2
MOV A,NUM1;
MOV B,NUM2;
MOV R4,A;TEMPORERLY STORE NUM1 IN R4
MOV R5,B;TEMPORERLY STORE NUM2 IN R5
AGAIN:MOV A,R4;MOVES R4 TO A
MOV B,R5;MOVES R5 TO B
DIV AB;DIVIDE NUMBER1 BY NUMBER2
MOV A,R5;MOVE R5 TO A
MOV R4,A;MOV A TO R4
MOV R5,B;MOV REMAINDER TO R5
MOV A,B;MOV REMAINDER TO A
JZ NEXT;IF REMAINDER IS ZERO JUMP TO NEXT
AJMP AGAIN;REPEAT LOOP
NEXT:MOV A,R4;
MOV GCD,A;MOVES THE GCD OF NUMBERS TO GCD
MOV A,NUM1;
MOV B,NUM2;
MUL AB;MULTIPLIES NUM1 AND NUM2
MOV R4,A
MOV R5,B
CLR C
MOV B,GCD
DIV AB
MOV LCM,A
END
DA A OPERATION
MOV 31H,#00H
MOV 25H,#63H
MOV 26H,#63H
MOV 27H,#63H
MOV A,25H
ADD A,26H
DA A
MOV R0,A
MOV A,#00H
ADDC A,31H
MOV 31H,A
MOV A,R0
ADD A,27H
DA A
MOV 30H,A
MOV A,#00H
ADDC A,31H
MOV 31H,A
END
BINARY TO DECIMAL AND ASCII
MOV R0,#32H
MOV R1,#60H
MOV R3,#83H
MOV R4,#3
MOV B,#10
MOV A,R3
DIV AB
MOV @R0,B
DEC R0
MOV B,#10
DIV AB
MOV @R0,B
DEC R0
MOV @R0,A
MOV R0,#30H
AGAIN:MOV A,@R0
ORL A,#30H
MOV @R1,A
INC R0
INC R1
DJNZ R4,AGAIN
END
PACKED BCD TO ASCII
MOV R0,#38H
MOV A,R0
ANL A,#0FH
ADD A,#30H
MOV R1,A; ASCII(FIRST DGT)
MOV A,R0
ANL A,#0F0H
SWAP A
ADD A,#30H
MOV R2,A ;ASCII(SECOND DGT)
END
FINDING SMALLEST NUMBER IN ARRAY OF NUMBERS
MOV R0,#60H
MOV R5,#10
MOV R2,#8
MOV R3,#10
AGAIN:MOV A,R5
MOV @R0,A
DEC R5
INC R0
DJNZ R2,AGAIN
CLR A
MOV R0,#60H
MOV B,@R0
SUM: MOV A,@R0
CJNE A,B,LOOP
LOOP: JNC LOOP1
MOV B,A
INC R0
DJNZ R3,SUM
SJMP NEXT
LOOP1:INC R0
DJNZ R3,SUM
SJMP NEXT
NEXT:MOV A,B
MOV R7,A;SAVE SMALLEST NUMBER IN R7
END
ASCII TO DECIMAL
MOV R1,#42H
MOV A,R1
CLR C
SUBB A,#41
MOV A,R1
JC DGT
CLR C
SUBB A,#37H ;ASCII?CHAR
SJMP NEXT
DGT: CLR C
SUBB A,#30H ;ASCII?NUMBER
NEXT:
END
FINDING SUM OF NATURAL NUMBERS
CLR A
MOV R1,#0
MOV R2,#20
AGAIN: INC R1
ADD A,R1
DJNZ R2,AGAIN
MOV R5,A
END
FINDING NUMBER OF ZERO’S AND ONE’S IN A NUMBER
MOV R2,#0;INITIALIZE ONE'S COUNTER=0
MOV R3,#0;INITIALIZE ZERO'S COUNTER=0
MOV R1,#08;INITIALIZE THE LOOP COUNT
MOV R0,#56;NUMBER
MOV A,R0
BACK:RRC A
JC SKIP;IF CARRY IS PRESENT GO TO SKIP
INC R3;INCREMENT ZERO'S COUNT
AJMP LOOP1
SKIP:INC R2;INCREMENT ONE'S COUNT
LOOP1:DJNZ R1,BACK
END
FINDING VARIANCE AND MEAN OF NUMMBERS
MOV R7,#6H
MOV R0,#30H
MOV @R0,#5H
INC R0
MOV @R0,#2H
INC R0
MOV @R0,#7H
INC R0
MOV @R0,#4H
INC R0
MOV @R0,#3H
INC R0
MOV @R0,#3H
MOV R0,#30H
LJMP LOOP
BACK: MOV R0,#30H
MOV R3,#00H
MOV R2,#6H
SJMP LOOP2
LOOP2: MOV A,10H
MOV B,@R0
LOOP6: SUBB A,B
JC LOOP4
MOV R4,A
MOV B,R4
MUL AB
MOV R5,A
MOV R6,B
MOV A,#0FH
CLR C
ANL A,R5
MOV R5,A
MOV A,#0F0H
ANL A,R6
ORL A,R5
ADD A,R3
MOV R3,A
INC R0
DJNZ R2,LOOP2
MOV B,#6
DIV AB
MOV 18H,A
MOV 19H,B
SJMP SKIP
LOOP4:MOV A,@R0
MOV B,10H
CLR C
AJMP LOOP6
LOOP:CLR A
LOOP1: ADD A,@R0
INC R0
DJNZ R7,LOOP1
ACALL AVG
AJMP BACK
AVG: MOV B,#6H
DIV AB
MOV 10H,A
MOV 11H,B
RET
SKIP:NOP
END
SORTING AND FINDING AVERAGE
MAX SET R3 ;SET R3 FOR STORING MAXIMUM NUMBER
MIN SET R2 ;SET R2 FOR STORING MINIMUM NUMBER
MOV R0,#30H ;INITIALIZE THE REGISTER R1
MOV @R0,#7 ;INPUT NUMBERS TO STORE FROM 30H
INC R0 ;
MOV @R0,#4 ;
INC R0 ;
MOV @R0,#1 ;
INC R0 ;
MOV @R0,#8 ;
INC R0 ;
MOV @R0,#3 ;
MOV R3,#8 ;ASIGN THE LOOP COUNT FOR OUTER LOOP (AGAIN)
AGAIN:MOV R0,#30H ;STARTING THE PROCESS OF SORTING
MOV R4,#4 ; ASIGN INNER LOOP COUNT(BACK)
BACK:MOV B,@R0 ;
MOV R5,B ;
INC R0 ;
MOV A,@R0 ;
MOV R6,A ;
CJNE A,B,LOOP ;COMPARE THE TWO NUMBERS IF IT IS NOT EQUAL JUMP TO LOOP
AJMP LOOP1 ;IF TWO NUMBERS ARE EQUAL THEN JUMP TO LOOP1
LOOP:JNC LOOP1 ;IF A IS GREATER THAN B JUMP TO LOOP1
MOV B,R5 ;IF A<B EXCHANGE PROCESS STARTS ACORDING TO BUBBLE SORT
MOV @R0,B ;
DEC R0 ;
MOV A,R6 ;
MOV @R0,A ;
INC R0 ;
DJNZ R4,BACK ;REPEAT LOOP UNTIL R4=0
DJNZ R3,AGAIN ;REPEAT LOOP UNTIL R3=0
ACALL NEXT ;CALLS SUBROUTINE NEXT
AJMP SKIP ;JUMP TO SKIP AT THE END OF PROGRAM
LOOP1:DJNZ R4,BACK
DJNZ R3,AGAIN
ACALL NEXT
AJMP SKIP
NEXT:MOV A,@R0
MOV MAX,A ;STORES THE MAXIMUM VALUE IN MAX
MOV R0,#30H
MOV A,@R0
MOV MIN,A ;STORES THE MINIMUM VALUE IN MIN
ACALL LOOP3 ;CALLS THE SUBROUTINE LOOP3 FOR FINDING AVERAGE
RET
LOOP3: MOV R5,#5
MOV R0,#30H
CLR A
LOOP4:ADD A,@R0
INC R0
DJNZ R5,LOOP4
MOV B,#5
DIV AB
MOV 40H,A;MOVES THE QUOTIENT OF AVERAGE TO 40H
MOV 41H,B ;MOVES THE REMAINDER OF AVERAGE TO 41H
RET
SKIP:NOP
END
FIBBONACI SERIES
MOV R4,#8
MOV A,R4
MOV R3,A
SUBB A,#2
MOV R5,A
MOV R1,#30H
MOV @R1,#0
CLR C
MOV A,#1
MOV B,R3
MOV R4,B
SUBB A,R4
JZ NEXT
INC R1
MOV @R1,#1
CLR C
MOV A,#2
MOV B,R3
MOV R4,B
SUBB A,R4
JZ NEXT
MOV A,@R1
AGAIN:DEC R1
MOV B,@R1
ADD A,B
INC R1
INC R1
MOV @R1,A
DJNZ R5,AGAIN
NEXT :NOP
END
FINDING BIGGER NUMBER IN ARRAY
MOV R0,#60H
MOV R5,#1
MOV R2,#10
MOV R3,#10
AGAIN:MOV A,R5
MOV @R0,A
INC R5
INC R0
DJNZ R2,AGAIN
CLR A
MOV R0,#60H
MOV B,#0
SUM: MOV A,@R0
CJNE A,B,LOOP
LOOP: JC LOOP1
MOV B,A
INC R0
DJNZ R3,SUM
SJMP NEXT
LOOP1:INC R0
DJNZ R3,SUM
SJMP NEXT
NEXT:MOV A,B
END
BINARY TO GRAY
MOV A,#52H
MOV R0,A
CLR C
RRC A
XRL A,R0
END
BINARY TO BCD
MOV A,#76H
MOV B,#100
DIV AB
MOV R0,A
MOV A,B
MOV B,#10H
DIV AB
MOV R1,A
MOV R3,B
END
PROGRAM TO CHECK WHETHER CONTENT OF A>20H OR NOT
MOV A,#89H
MOV 20H,#10H
SUBB A,20H
JZ EQUAL
JNB ACC.7,POS
CLR P2.7
SJMP NEXT
POS:CLR P1.1
CLR P1.2
EQUAL:CLR P3.2
NEXT:NOP
END
FINDING GCD AND LCM OF NUMBERS <=22
NUM1 SET R2;SETS THE R2 FOR NUMBER1
NUM2 SET R3;SETS THE R3 FOR NUMBER2
GCD SET R6;SETS THE R6 FOR STORE GCD
LCM SET R7;SETS THE R7 FOR LCM
MOV NUM1,#6;NUMBER1
MOV NUM2,#10;NUMBER2
MOV A,NUM1;
MOV B,NUM2;
MOV R4,A;TEMPORERLY STORE NUM1 IN R4
MOV R5,B;TEMPORERLY STORE NUM2 IN R5
AGAIN:MOV A,R4;MOVES R4 TO A
MOV B,R5;MOVES R5 TO B
DIV AB;DIVIDE NUMBER1 BY NUMBER2
MOV A,R5;MOVE R5 TO A
MOV R4,A;MOV A TO R4
MOV R5,B;MOV REMAINDER TO R5
MOV A,B;MOV REMAINDER TO A
JZ NEXT;IF REMAINDER IS ZERO JUMP TO NEXT
AJMP AGAIN;REPEAT LOOP
NEXT:MOV A,R4;
MOV GCD,A;MOVES THE GCD OF NUMBERS TO GCD
MOV A,NUM1;
MOV B,NUM2;
MUL AB;MULTIPLIES NUM1 AND NUM2
MOV R4,A
MOV R5,B
CLR C
MOV B,GCD
DIV AB
MOV LCM,A
END
DA A OPERATION
MOV 31H,#00H
MOV 25H,#63H
MOV 26H,#63H
MOV 27H,#63H
MOV A,25H
ADD A,26H
DA A
MOV R0,A
MOV A,#00H
ADDC A,31H
MOV 31H,A
MOV A,R0
ADD A,27H
DA A
MOV 30H,A
MOV A,#00H
ADDC A,31H
MOV 31H,A
END
BINARY TO DECIMAL AND ASCII
MOV R0,#32H
MOV R1,#60H
MOV R3,#83H
MOV R4,#3
MOV B,#10
MOV A,R3
DIV AB
MOV @R0,B
DEC R0
MOV B,#10
DIV AB
MOV @R0,B
DEC R0
MOV @R0,A
MOV R0,#30H
AGAIN:MOV A,@R0
ORL A,#30H
MOV @R1,A
INC R0
INC R1
DJNZ R4,AGAIN
END
PACKED BCD TO ASCII
MOV R0,#38H
MOV A,R0
ANL A,#0FH
ADD A,#30H
MOV R1,A; ASCII(FIRST DGT)
MOV A,R0
ANL A,#0F0H
SWAP A
ADD A,#30H
MOV R2,A ;ASCII(SECOND DGT)
END
FINDING SMALLEST NUMBER IN ARRAY OF NUMBERS
MOV R0,#60H
MOV R5,#10
MOV R2,#8
MOV R3,#10
AGAIN:MOV A,R5
MOV @R0,A
DEC R5
INC R0
DJNZ R2,AGAIN
CLR A
MOV R0,#60H
MOV B,@R0
SUM: MOV A,@R0
CJNE A,B,LOOP
LOOP: JNC LOOP1
MOV B,A
INC R0
DJNZ R3,SUM
SJMP NEXT
LOOP1:INC R0
DJNZ R3,SUM
SJMP NEXT
NEXT:MOV A,B
MOV R7,A;SAVE SMALLEST NUMBER IN R7
END
ASCII TO DECIMAL
MOV R1,#42H
MOV A,R1
CLR C
SUBB A,#41
MOV A,R1
JC DGT
CLR C
SUBB A,#37H ;ASCII?CHAR
SJMP NEXT
DGT: CLR C
SUBB A,#30H ;ASCII?NUMBER
NEXT:
END
No comments:
Post a Comment