Sum of 8-bit Numbers Stored in Memory
Here we will discuss about 8051 programs but we can't discuss about all of the 8051 instructions. For programming 8051 we should know about assembler directives as well as instruction set. Click here to download Atmel c51 user guide that will discuss about 8051(c51 family microcontroller) instruction set, assembler directives, c51 cross assembler from Atmel.Program
MOV R0,#50H ;get memory location in memory pointer R0
MOV R1,#51H ;get memory location on memory pointer register R1
MOV A,@R0 ;get content of memory location 50H to accumulator
ADD A,@R1 ;add content of A with content of memory location 51H and store result in A
MOV R0,#52H ;get 52H to memory pointer R0
MOV@R0,A ;copy content of A to memory location 52H
END
Add 16-bit Numbers
Program
ORG 00H
MOV DPTR,#2040H ; get 2040H into DPTR
MOV A,#2BH ;get lower byte of second 16-bit number on accumulator
MOV R0,#20H ;get higher byte of second 16-bit number on accumulator
ADD A,82H ;[A]+[DPL]
MOV 82H,A ;save result of lower byte addition
MOV A,R0 ;get higher byte of second number in A
ADDC A,83H ;[A]+[DPH]
MOV 83H,A ;Save result of higher byte addition
END
Unpack Packed Numbers
Program
ORG 00H
MOV R0,50H ;get packed number from memory location 50H
MOV A,R0 ;copy packed number to accumulator
ANL A,#0FH ; mask upper nibble of packed number
MOV 51H,A ;save lower digit to 51H
MOV A,R0 ;get packed number again
ANL A,#0F0H ;mask lower nibble of packed number
SWAP A ;exchange lower and upper nibbles
MOV 52H,A ;save upper digit
END
Unpack the Packed BCD Numbers
Program
MOV A,5OH ;get the packed BCD number from memory location 50H
MOV 0F0H,A ;save the packed BCD number to register B
ANL A,#0FH ;mask upper nibble of BCD number
MOV R0,A ;save the lower digit to R0
MOV A,0F0H ;get the packed BCD number from register B to A
ANL A,#0F0H ;mask lower nibble of BCD number
SWAP A ;exchange lower and upper nibbles
MOV R1,A ;save the upper digit
END
Multiplication and Division
Program
MOV A,51H ;get content of memory location 51H to accumulator
MOV 0F0H,52H;get content of memory location 52H to B register
MUL AB ;multiply content of A with content of B
MOV 53H,A ;get lower order byte of product in memory location 53H
MOV54H,0F0H ;get higher order byte of product in memory location in 54H
MOV A,51H ;get content of memory location 51H to accumulator
MOV 0F0H,52H ;get content of memory location 52H to register B
DIV AB ;divide content of register A with register B
MOV 55H,A ;Copy quotient of result to memory location 55H
MOV 56H,0F0H ;copy remainder of result to memory location 56H
END
Note:After executing MUL AB Instruction the lower byte of 16-bit product is left in Accumulator and the high order byte in register B. If the product is greater than 255(FFH) the overflow(OV) flag is set; otherwise it is cleared.
For example, the largest possible product is FE01H when both A and B contain FFH. That is A contains 01H and register B contains FEh after multiplication of FFH by FFH. The OV flag is set to 1 to indicate that register B contains the high-order byte of the product; carry flag is zero.
Find Largest Number
Program
MOV DPTR,#2000H;initialize pointer to memory where numbers are stored
MOV R0,#0AH ; initialize counter
MOV R3,#00H ;maximum=0
AGAIN: MOV A,@DPTR ;get the number from memory
CJNE A,R3,NE ;compare number wi maximum number
AJMP SKIP ;if equal go to SKIP
NE: JC SKIP ;if not equal check for carry, if carry go to skip
MOV R3,A ;otherwise maximum=[[DPTR]]
SKIP: INC DPTR ; Increment memory pointer
DJNZ R0,AGAIN ; Decrement count, if count=0 stop otherwise go to AGAIN
END
Sort n Numbers in Ascending Order
Program
MOV R2,50H ;get n in R2
DEC R2 ;get actual count on register R2
L3:MOV 03,O2 ;get actual count from R2 to R3
MOV R0,#60H ;get memory location to be pointed on pointer register R0
L4:MOV A,@R0 ;copy [[R0]] into Accumulator
INC R0 ;Increment R0 to access next memory location
MOV 05,@R0 ; copy [[R0]] to register R5
CJNE A,05,L1 ;Compare[A] with [R5] and jump if not equal to L1
L1:JC L2 ;Is [A]<[R5]? Yes jump to L2. If there is carry go to next step.
MOV 04,@R0 ;copy [[R0]] to R4. Now small number is in R4. Since [[R0]]=[R5]
MOV @R0,A ;copy large number from A to current memory location pointed by R0.
DEC R0 ;accessing previous memory location
MOV @R0,04 ;copy smallest number to [[R0]]
INC R0 ;comeback to current memory location
L2:DJNZ R2,L4
DJNZ R2,L3 ;compare nc2 times using these two loops
END
Sort n Numbers in Descending Order
Program
MOV R2,50H ;get n in R2
DEC R2 ;get actual count on register R2
L3:MOV 03,O2 ;get actual count from R2 to R3
MOV R0,#60H ;get memory location to be pointed on pointer register R0
L4:MOV A,@R0 ;copy [[R0]] into Accumulator
INC R0 ;Increment R0 to access next memory location
MOV 05,@R0 ; copy [[R0]] to register R5
CJNE A,05,L1 ;Compare[A] with [R5] and jump if not equal to L1
L1:JNC L2 ;Is [A]>[R5]? Yes jump to L2. If there is no carry go to next step.
MOV 04,@R0 ;copy [[R0]] to R4. Now large number is in R4. Since [[R0]]=[R5]
MOV @R0,A ;copy small number from A to current memory location pointed by R0.
DEC R0 ;accessing previous memory location
MOV @R0,04 ;copy largest number to [[R0]]
INC R0 ;comeback to current memory location
L2:DJNZ R2,L4
DJNZ R2,L3 ;compare nC2 times using these two loops
END
Largest of n Number
Program
MOV R2,50H ;get n from memory location 50H to register R2
DEC R2 ;get actual count on R2 since 2 numbers are compared
MOV R0,#60H ;get memory location to be pointed on pointer register R0
MOV A,@R0 ;get [[R0]] in Accumulator
MOV R1,#61H ;get memory location to be pointed on pointer register R1
L1:MOV 00,@R1 ;get[[R1]] in register R0
CJNE A,00,L2 ;compare [A] with [R0] and jump to label L2
L2:JNC L3 ;is carry is not generated? Yes; jump to L3.If carry generated go to next step
MOV A,@R1 ;if there is no carry generated then [A]>[R0]. So move [[R1]]=[R0] which is largest number into Accumulator
L3:INC R1 ;access next memory location.
DJNZ R2,L1 ;[R0]-1: is [R0]=0? No, jump to L1
MOV 51H,A ;save largest number
END
Smallest of n Numbers
Program
MOV R2,50H ;get n from memory location 50H to register R2
DEC R2 ;get actual count on R2 since 2 numbers are compared
MOV R0,#60H ;get memory location to be pointed on pointer register R0
MOV A,@R0 ;get [[R0]] in Accumulator
MOV R1,#61H ;get memory location to be pointed on pointer register R1
L1:MOV 00,@R1 ;get[[R1]] in register R0
CJNE A,00,L2 ;compare [A] with [R0] and jump to label L2
L2:JC L3 ;is carry generated? Yes; jump to L3.If no carry generated go to next step
MOV A,@R1 ;if there is no carry generated then [A]>[R0]. So move [[R1]]=[R0] which is smallest number into Accumulator
L3:INC R1 ;access next memory location.
DJNZ R2,L1 ;[R0]-1: is [R0]=0? No, jump to L1
MOV 51H,A ;save smallest number
END
Hexadecimal to Decimal Conversion
Program
Fortunately here we have instructions like MUL AB and DIV AB unlike 8085 microprocessor.MOV A,52H ;get hexadecimal number from memory location 52H
MOV 0F0H,#64H;load register B with 64H= 100 in decimal
DIV AB ;divide number with 100
MOV 60H,A ;save number of hundreds(quotient of previous division)
MOV A,0F0H ;Get remainder on A
MOV 0F0H,#0AH;load register B with 0AH=10 in decimal
DIV AB ;divide number with 10
MOV 61H,A ;save tens of the number
MOV 62H,0F0H ; save number of ones of the number
END
Write 8051 assembly language program to add 10 BCD numbers and store the result in BCD in register R1 pls do give answer to this question
ReplyDeleteWrite an ALP to check the given array is sorted in ascending order if not then sort the given number in ascending order.
ReplyDeleteMay I know the memory address for every programs?????
ReplyDeleteWhen it comes to programming the 8051 microcontroller, there are several simple programs that you can write to get started. These programs are typically used for learning purposes and can help you understand the basics of the 8051 architecture and instruction set.
ReplyDeletebest cheap camera for photography