Hello Friends!! Over the years, the students have always faced a problem of learning Assembly level programming in a systematic manner. To overcome the same, Elex Tutorials has come up with this blog. We are here to share our knowledge of assembly programming on 8051 Microcontrollers which we have gained over the years.
To begin with, we shall start with our programming, using the very basic instructions and then understanding the concepts behind using every instruction.
Let us start with our first program.
- Write a program to add two 8-bit numbers.
ORG 0000H
MOV A,#55H
MOV B,#54H
ADD A,B
MOV R1,A
END
Explanation:
- The program asks us to add two 8-bit numbers for which we store two 8-bit numbers in any of the registers and then add the numbers only to store them in Accumulator. We shall understand the program line by line in subsequent points.
- The first opcode ORG is used to begin with 8051 assembly program. The program memory starts from the memory location 0000H. Hence, the first line of the program follows.
- In the second line, we use MOV opcode to perform a transfer function. Here A denotes Accumulator. The operation being performed is the transfer of hexadecimal number 55 in the accumulator. The ‘#’ before a number indicates that the number itself is a operand.
- The third line again is intended to perform transfer function to the register B.
- The fourth line of the program is intended to perform the addition operation on the contents of the registers A and B storing the result in Accumulator.
- In fifth line of the program, we transfer the result of the addition in Accumulator to register R1.
- In 8051, the program is ended with a END opcode.
- Write a program to display the contents of stack pointer(SP) on the port P0.
ORG 0000H
MOV A,#255
MOV B,#10
ADD A,B
MOV P0,SP
END
Explanation:
- The first opcode ORG is used to begin with 8051 assembly program. The program memory starts from the memory location 0000H. Hence, the first line of the program follows.
- The second line of the program is intended to perform transfer function in the accumulator.
- The third line again is intended to perform transfer function to the register B.
- The fourth instruction performs the addition operation on the contents of the registers A and B storing the result in Accumulator.
- The fifth instruction demonstrates the transfer of contents of stack pointer on the port P0.
- In 8051, the program is ended with a END opcode.
- You must be wondering as to what the whole code till 4th line has to do with the Stack Pointer contents. Well, no problem. I have written a random code. We are concerned about the contents of SP.
- Write a program to display a binary counter on port P0.
ORG 0000
MOV A,#0
MOV B,#1H
MOV R1,#AH
MOV P0,#0
BINCOUNT:
ADD A,B
MOV P0,A
DJNZ R1,BINCOUNT
END
Explanation:
- The first opcode ORG is used to begin with 8051 assembly program. The program memory starts from the memory location 0000H. Hence, the first line of the program follows.
- Clear Accumulator. Alternatively, we can use the opcode CLC to perform ‘clear’ operation.
- Transfer 1H to B.
- Set the counter by transferring AH into register R1 i.e the counter is set to 10. We will study how will the register work like a counter in subsequent steps.
- Clear port P0. Alternatively, we can use the opcode CLC to perform ‘clear’ operation.
- Let us now create a loop with some label, let us suppose, BINCOUNT in which we add the contents of A and B and then transfer the result to P0 so that the contents can be displayed on the port.
- Let us now get introduced to another instruction called DJNZ which when expanded comes out to be ‘Decrease and Jump till Not Zero’.
- We have two operands here. Let us study their significance in detail. The contents of the first operand register R1 is decreased by unit 01H and as soon as it is decreased, the Program Counter points to the first line of label BINCOUNT and again goes through subsequent lines until again the register R1 is decreased by 01H and the PC is again stored with the address of the first line of the label BINCOUNT.
- The procedure is repeated till the contents of register R1 is decreased to 00H.
- As soon as, it reaches 00H the Program Counter falls out of the loop and points to the next instruction to be executed.
- We end the program with END instruction.
- Write a program for blinking LEDs on port P0.
ORG 0H
BLINK:
MOV A,#00000000B
MOV P0,A
MOV B,#11111111B
MOV P0,B
SJMP BLINK
END
Explanation:
- We have been asked to display the blinking of LEDs on port P0.
- The first opcode ORG is used to begin with 8051 assembly program. The program memory starts from the memory location 0000H.
- We first make a label BLINK in which we write our program to blink the LEDs.
- We then transfer the binary digits 00000000 to the accumulator which is further transferred to port P0 for display
- We then transfer the binary digits 11111111 to the register B which is further transferred to port P0 for display.
- The next opcode used in the program is SJMP- Short Jump. The
SJMP BLINK allows us to run an infinite loop wherein we observe the infinite blinking of LEDs. With this we come to the end of our program.
- WAP to demonstrate data transfer and halt the PC.
ORG 0000H
MOV P0,#23
MOV P1,#34
MOV P2,#76
MOV P3,#25
SJMP $
END
Explanation:
- We have been asked to write a program to demonstrate data transfer operations for which we use MOV instruction.
- We start with our program using opcode ORG . Since the starting memory location is 0000H, we shall write the first line of our program as ORG 0000H.
- In the subsequent lines, we use the opcode MOV to transfer the random data to the ports.
- The only new instruction we come across in this program is SJMP $ which is used to halt/stop the Program Counter.
NOTE: 1. #23 implies decimal count 23(0001 0111)
2. #23H implies hexadecimal count 23.(0010 0011)
3. # 23B implies binary pattern 0001 0111.
Annalisa Cattladge
I found a great…
Appreciating the dedication you put into your site and detailed information you offer. It’s awesome to come across a blog every once in a while that isn’t the same out of date rehashed information. Great read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.