Unit-3
Instruction Set and Programming
3.1.1 Introduction
The addressing mode is the method to specify the operand of an instruction. The operations require the following:
- The operator or opcode which determines what will be done.
- The operands which define data to be used in the operation.
3.1.2 Instruction syntax
An 8051 Instruction consists of an Opcode followed by Operand(s) of size Zero Byte, One Byte or Two Bytes.
The Op-Code part contains the mnemonic, which specifies the type of operation to be performed. All Mnemonics or the Opcode part of the instruction are of One Byte size.
The Operand part of the instruction defines the data being processed by the instructions. There can multiple operands and the format of instruction is as follows:
Mnemonic Destination Operand, Source Operand
A simple instruction consists of just the opcode. Other instructions may include one or more operands. Instruction can be one-byte instruction, which contains only opcode, or two-byte instructions, where the second byte is the operand or three- byte instructions, where the operand makes up the second and third byte.
3.1.3 Data Types
DB (define byte)
- It is used to define the 8-bit data.
- When DB is used to define data, the numbers can be in decimal, binary, hex, or ASCII formats.
- “B” (binary) and “H” (hexadecimal) is required.
- To indicate ASCII, simply place the characters in quotation marks
- The DB directive is the only directive that can be used to define ASCII strings larger than two characters; therefore, it should be used for all ASCII data definitions.
Following are some DB examples:
ORG 2000H
DATA1: DB 28 ;DECIMAL NUMBER 28
DATA2: DB 1234h ;HEXADECIMAL
DATA3: DB "ABCD" ;ASCII CHARACTER
3.1.4 Subroutines
Subroutine is a group of instructions written separately from the main program to perform function that occurs repeatedly in the main program. When you call a subroutine implementation the current program is stopped and PC is loaded with the memory location of the subroutine running upto RET instruction which marks the end of the subroutine where we produce a return to the main program.
The functions of the subroutines are:
- Perform specific functions which are not operating on their own.
- Always linked to the major program or other subroutines.
- Can be called as many times as necessary to reduce the code of the program.
- Allow division of program blocks as per the function of the structure.
3.1.5 Addressing modes
3.1.5.1 Immediate Addressing
In this addressing mode the data is provided as a part of instruction itself.
That is the data immediately follows the instruction.
Eg. MOV A,#30H ;
ADD A, #83 ;
Here # Symbol indicates that data is immediate.
3.1.5.2 Register addressing.
In this addressing mode the register will hold the data.
One of the eight general registers (R0 to R7) is used and specified as the operand.
Eg. MOV A,R0 ;
ADD A,R6
R0 – R7 will be selected from the current selection of register bank. The default register bank will be bank 0.
3.1.5.3 Direct addressing
There are two ways to access the internal memory that is by using direct address and indirect address.
In direct addressing mode we not only address the internal memory but SFRs also.
In direct addressing, an 8 bit internal data memory address is specified as part of the instruction and hence, it can specify the address only in the range of 00H to FFH.
In this addressing mode, data is obtained directly from the memory.
Eg. MOV A,60h
ADD A,30h
3.1.5.4 Indirect addressing
The indirect addressing mode uses a register to hold the actual address that is used in data movement.
Registers R0 and R1 and DPTR are the only registers that can be used as data pointers.
Indirect addressing cannot be used to refer to SFR registers.
Both R0 and R1 can hold 8 bit address and DPTR can hold 16 bit address.
Eg. MOV A,@R0
ADD A,@R1
MOVX A,@DPTR
3.1.5.5 Relative Addressing
Relative addressing is used only with conditional jump instructions. The relative address, (offset), is an 8 bit signed number, which is automatically added to the PC to make the address of the next instruction.
The 8 bit signed offset value gives an address range of +127 to —128 locations.
The jump destination is usually specified using label and the assembler calculates the jump offset accordingly.
Eg. SJMP LOOP1 JC BACK
3.1.5.6 Indexed addressing
In indexed addressing, either the program counter (PC), or the data pointer (DTPR)—is used to hold the base address, and A is used to hold the offset address.
Adding the value of the base address to the value of the offset address forms the effective address.
Effective address = Base address + value of offset address.
Indexed addressing is used with JMP or MOVC instructions.
Look up tables are easily implemented with the help of index addressing.
Eg. MOVC A, @A+DPTR // copies the contents of memory location pointed by the sum of the accumulator A and the DPTR into accumulator A.
MOVC A, @A+PC // copies the contents of memory location pointed by the sum of the accumulator A and the program counter into accumulator A.
3.1.5.7 Bit Inherent Addressing
In this addressing, the address of the flag which contains the operand, is implied in the opcode of the instruction. Eg. CLR C ; Clears the carry flag to 0
3.1.5.8 Bit Direct Addressing
In this addressing mode the direct address of the bit is specified in the instruction. The RAM space 20H to 2FH and most of the special function registers are bit addressable. Bit address values are between 00H to 7FH.
Eg. CLR 07h ; Clears the bit 7 of 20h RAM space
SETB 07H ; Sets the bit 7 of 20H RAM space.
Key Take Aways:
Addressing mode is a way to address an operand. Operand means the data we are operating upon.
3.2.1 Instruction Timings
The time taken to complete any instruction is called as machine cycle or instruction cycle. In 8051 one instruction cycle consists of 6 states or 12 clock cycles, instruction cycle is also referred as Machine cycle.
Figure 1. Instruction cycle of 8051 (Instruction cycle has six states (S 1 - S 6). Each state has two pulses (P1 and P2))
3.2.2 Data Transfer Instructions
In this group, the instructions perform data transfer operations of the following types.
a. Move the contents of a register Rn to A
- MOV A,R2
- MOV A,R7
b. Move the contents of a register A to Rn
i. MOV R4,A
ii. MOV R1,A
c. Move an immediate 8 bit data to register A or to Rn or to a memory location(direct or indirect)
i. MOV A, #45H
ii. MOV R6, #51H i
iii.MOV 30H, #44H
iv. MOV @R0, #0E8H
v. MOV DPTR, #0F5A2H
vi. MOV DPTR, #5467H
d. Move the contents of a memory location to A or A to a memory location using direct and indirect addressing
i. MOV A, 65H
ii. MOV A, @R0
iii.MOV 45H, A
iv. MOV @R1, A
e. Move the contents of a memory location to Rn or Rn to a memory location using direct addressing
i. MOV R3, 65H
ii. MOV 45H, R2
f. Move the contents of memory location to another memory location using direct and indirect addressing
i. MOV 47H, 65H
ii. MOV 45H, @R0
g. Move the contents of an external memory to A or A to an external memory
i. MOVX A,@R1
ii. MOVX @R0,A
iii. MOVX A,@DPTR iv. MOVX@DPTR,A
h. Move the contents of program memory to A
i. MOVC A, @A+PC
ii. MOVC A, @A+DPTR
Program
Write 8085 Assembly language program to swap two 8-bit number stored at location 8000H and 8001H using direct addressing mode.
MOV B,M ; Load the first number to B
INX H ; Point to the next number
MOV A,M; Load the second number into A
MOV M,B; Store first number to second position
DCX H ; Point to previous location
MOV M,A; Store second number to first position.
HLT.
3.2.3 Arithmetic Instructions
The 8051 can perform addition, subtraction. Multiplication and division operations on 8- bit numbers.
Addition
In this group, we have instructions to
i. Add the contents of A with immediate data with or without carry.
- ADD A, #45H
- ADDC A, #OB4H
ii. Add the contents of A with register Rn with or without carry.
- ADD A, R5
- ADDC A, R2
- Add the contents of A with contents of memory with or without carry using direct and indirect addressing
- ADD A, 51H
- ADDC A, 75H
- ADD A, @R1
- ADDC A, @R0 CY AC and OV flags will be affected by this operation.
Subtraction
In this group, we have instructions to
i. Subtract the contents of A with immediate data with or without carry.
i. SUBB A, #45H
ii. SUBB A, #OB4H
ii. Subtract the contents of A with register Rn with or without carry.
- SUBB A, R5
- SUBB A, R2
- Subtract the contents of A with contents of memory with or without carry using direct and indirect addressing
- SUBB A, 51H
- SUBB A, 75H
- SUBB A, @R1
- SUBB A, @R0 CY AC and OV flags will be affected by this operation.
Multiplication
MUL AB.
This instruction multiplies two 8 bit unsigned numbers which are stored in A and B register. After multiplication the lower byte of the result will be stored in accumulator and higher byte of result will be stored in B register.
Eg. MOV A,#45H ;[A]=45H
MOV B,#0F5H ;[B]=F5H
MUL AB ;[A] x [B] = 45 x F5 = 4209 ;[A]=09H, [B]=42H
Division
DIV AB.
This instruction divides the 8 bit unsigned number which is stored in A by the 8 bit unsigned number which is stored in B register. After division the result will be stored in accumulator and remainder will be stored in B register.
Eg. MOV A,#45H ;[A]=0E8H
MOV B,#0F5H ;[B]=1BH
DIV AB ;[A] / [B] = E8 /1B = 08 H with remainder 10H ;[A] = 08H, [B]=10H
DA A (Decimal Adjust After Addition).
When two BCD numbers are added, the answer is a non-BCD number. To get the result in BCD, we use DA A instruction after the addition.
DA A works as follows.
If lower nibble is greater than 9 or auxiliary carry is 1, 6 is added to lower nibble.
If upper nibble is greater than 9 or carry is 1, 6 is added to upper nibble.
Eg 1: MOV A,#23H
MOV R1,#55H
ADD A,R1 // [A]=78
DA A // [A]=78 no changes in the accumulator after da a
Eg 2: MOV A,#53H
MOV R1,#58H
ADD A,R1 // [A]=ABh
DA A // [A]=11, C=1 .
ANSWER IS 111. Accumulator data is changed after DA A
3.2.4 Logical Instructions
LOGICAL AND
a. ANL C,BIT(BIT ADDRESS) ;
Logically and carry content of Bit address and store the result in carry.
b. ANL C, /BIT;
Logically and carry and complement content of bit address and store the result in carry.
Examples:
ANL A, #n ;
AND each bit of A with the same bit of immediate number n, Result is placed in A.
ANL A, add ;
AND each bit of A with the same bit of the direct RAM address, result is placed in A
ANL A, Rr ;
AND each bit of A with the same bit of register Rr, result is placed In A.
ANL A, @Rr ;
AND each bit of A with the same bit of the contents of RAM Address in Rp, result is placed in A.
ANL add,A ;
AND each bit of A with the same bit of the direct RAM address, result is placed in direct RAM add.
ANL add, #n ;
AND each bit of A with the same bit of the immediate number, result is placed in direct RAM add.
Find the result of the following code:
MOV A,#57H ; A = 57H
ANL A,#0F0H ; A = A and F0 H
57 H = 0101 0111
F0 H = 1111 0000
0101 0000 = 50H
A = 50H
LOGICAL OR
a. ORL C,BIT(BIT ADDRESS) ;
Logically OR carry the content of bit address and store the result in carry.
b. ORL C, /BIT;
Logically OR carry and Complement the content of bit address and the store the result in carry.
Examples
ORL A, #n ;
OR each bit of A with the same bit of immediate number n, Result is placed in A.
ORL A, add ;
OR each bit of A with the same bit of the direct RAM address, result is placed in A
ORL A, Rr ;
OR each bit of A with the same bit of register Rr, result is placed in A.
ORL A, @Rr ;
OR each bit of A with the same bit of the contents of RAM Address in Rp, result is placed in A.
ORL add,A ;
OR each bit of A with the same bit of the direct RAM address, result is placed in direct RAM add.
ORL add, #n ;
OR each bit of A with the same bit of the immediate number, result is placed in direct RAM add.
Find the result of the following code:
MOV A,#15H ; A =15H
ORL A,#70H ; A=A OR 70H
Solution:
15 H 0001 0101
70H 0111 0000
0 111 0101 = 75H
A= 75H
LOGICAL XOR
a. XRL C,BIT(BIT ADDRESS) ;
Logically XOR carry the content of bit address and store the result in carry.
b. XRL C, /BIT;
Logically XOR carry and Complement the content of bit address and the store the result in carry.
Examples
XRL A, #n ;
XOR each bit of A with the same bit of immediate number n, Result is placed in A.
XRL A, add
XOR each bit of A with the same bit of the direct RAM address, result is placed in A
XRL A, Rr ;
XOR each bit of A with the same bit of register Rr, result is placed in A.
XRL A, @Rr ;
XOR each bit of A with the same bit of the contents of RAM Address in Rp, result is placed in A.
XRL add,A ;
XOR each bit of A with the same bit of the direct RAM address, result is placed in direct RAM add.
XRL add, #n ;
XOR each bit of A with the same bit of the immediate number, result is placed in direct RAM add.
Find the result of the following code:
MOV A,#55H ; A = 55H
XRL A,#F0H ; A= A OR F0H
Solution
55 H = 0101 0101
F0 H = 1111 0000
1010 0101 = A5H
A = A5H
Rotate Instructions
RR A
This instruction is rotate right the accumulator.
Its operation is illustrated below.
Figure 2. RR A
Each bit is shifted one location to the right, with bit 0 going to bit 7.
RL A Rotate left the accumulator.
Figure 3. RLA
Each bit is shifted one location to the left, with bit 7 going to bit 0
RRC A Rotate right through the carry
Each bit is shifted one location to the right, with bit 0 going into the carry bit in the PSW, while the carry was at goes into bit 7
|
Figure 4. RRC
RLC A Rotate left through the carry.
Each bit is shifted one location to the left, with bit 7 going into the carry bit in the PSW, while the carry goes into bit 0.
Figure 5.RLC
SWAP
The SWAP instruction can be thought of as a rotation of nibbles in the A register.
|
Figure 6. SWAP
Example:
OY A,#OA5h | A = I0l00I0lb = A5h |
RR A | A = I I0I00I0b = D2h |
RR A | A = 0l I0l00lb = 69h |
RR A | A = I0I l0I00b = B4h |
RR A | A = 0I0l I0I0b = 5Ah |
SWAP A | A= I0I00I0lb = A5h |
CLR C | C = 0; A = I0I00I0lb = A5h |
RRC A | C = I; A= 0I0I00I0b = 52h |
RRC A | C = 0; A = l0I0J00lb = A9h |
RL A | A= 0I0I00llb = 53h |
RL A | A = I0I00l I0b = A6h |
SWAP A | C = 0; A = 0l 101010b = 6Ah |
RLC A | C = 0; A = 1 I0I0l00b = D4h |
RLC A | C = l; A= I0I0l000b = A8h |
SWAP A | C = l; A= l000I0I0b = 8Ah |
3.2.5 Branch Instructions
Jump and Call Program Range There are 3 types of jump instructions.
They are:-
1. Relative Jump
2. Short Absolute Jump
3. Long Absolute Jump
Relative Jump
Jump that replaces the PC (program counter) content with a new address that is greater than the address following the jump instruction by 127 or less than the address following the jump by 128 or less is called a relative jump. Schematically, the relative jump can be shown as follows: -
Figure 7. Swap
The advantages of the relative jump are as follows: -
1. Only 1 byte of jump address needs to be specified in the 2's complement form, ie. For jumping ahead, the range is 0 to 127 and for jumping back, the range is -1 to -128.
2. Specifying only one byte reduces the size of the instruction and speeds up program execution.
3. The program with relative jumps can be relocated without reassembling to generate absolute jump addresses.
Disadvantages of the absolute jump: -
- Short jump range (-128 to 127 from the instruction following the jump instruction)
Relative Jump
SJMP <relative address> ; this is unconditional jump
The remaining relative jumps are conditional jumps
JC <relative address>
JNC <relative address>
JB bit,<relative address>
JNB bit, <relative address>
JBC bit, <relative address>
CJNE , <relative address>
DJNZ ,<relative address>
JZ <relative address>
JNZ <relative address>
Short Absolute Jump
In this case only 11bits of the absolute jump address are needed. The absolute jump address is calculated in the following manner.
In 8051, 64 kbyte of program memory space is divided into 32 pages of 2 kbyte each.
The hexadecimal addresses of the pages are given as follows:-
Page(Hex) | Address(Hex) |
00 | 0000-07FF |
01 | 0800-0FFF |
02 | 1000-17FF |
03 | 1800-1FFF |
.. | …. |
.. | …. |
1E | F000-F7FF |
1F | F800-FFFF |
Thus, an absolute address is formed by taking page numbers of the instruction (from the program counter) following the jump and attaching the specified 11bits to it to form the 16-bit address.
Advantage: The instruction length becomes 2 bytes.
Example of short absolute jump: -
ACALL <address11>
AJMP <address 11>
Long Absolute Jump/Call
Applications that need to access the entire program memory from 0000H to FFFFH use long absolute jump. Since the absolute address has to be specified in the op-code, the instruction length is 3 bytes (except for JMP @ A+DPTR). This jump is not re-locatable.
Example: - LCALL <address16>
LJMP <address16>
JMP @A+DPTR
Another classification of jump instructions is
1. Unconditional Jump
2. Conditional Jump
1. The unconditional jump is a jump in which control is transferred unconditionally to the target location. a. LJMP (long jump). This is a 3-byte instruction. First byte is the op-code and second and third bytes represent the 16-bit target address which is any memory location from 0000 to FFFFH
eg: LJMP 3000H
b. AJMP: this causes unconditional branch to the indicated address, by loading the 11 bit address to 0 -10 bits of the program counter. The destination must be therefore within the same 2K blocks.
c. SJMP (short jump).
This is a 2-byte instruction. First byte is the op-code and second byte is the relative target address, 00 to FFH (forward +127 and backward -128 bytes from the current PC value). To calculate the target address of a short jump, the second byte is added to the PC value which is address of the instruction immediately below the jump.
Conditional Jump instructions.
JBC Jump if bit = 1 and clear bit
JNB Jump if bit = 0 JB
Jump if bit = 1
JNC Jump if CY = 0
JC Jump if CY = 1
CJNE reg,#data Jump if byte ≠ #data
CJNE A,byte Jump if A ≠ byte
DJNZ Decrement and Jump if A ≠ 0 JNZ
Jump if A ≠ 0 JZ
Jump if A = 0
All conditional jumps are short jumps.
Bit level jump instructions:
Bit level JUMP
The instructions will check the conditions of the bit and if condition is true, it jumps to the address specified in the instruction. All the bit jumps are relative jumps.
JB bit, rel ; jump if the direct bit is set to the relative address specified.
JNB bit, rel ; jump if the direct bit is clear to the relative address specified.
JBC bit, rel ; jump if the direct bit is set to the relative address specified and then clear the bit.
3.2.6 Subroutine Instructions
CALL And RETURN Instructions
Subroutines are handled by CALL and RET instructions There are two types of CALL instructions
1. LCALL address(16 bit) This is long call instruction which unconditionally calls the subroutine located at the indicated 16 bit address. This is a 3 byte instruction. The LCALL instruction works as follows.
a. During execution of LCALL, [PC] = [PC]+3; (if address where LCALL resides is say, 0x3254; during execution of this instruction [PC] = 3254h + 3h = 3257h
b. [SP]=[SP]+1; (if SP contains default value 07, then SP increments and [SP]=08
c. [[SP]] = [PC7-0]; (lower byte of PC content ie., 57 will be stored in memory location 08.
d. [SP]=[SP]+1; (SP increments again and [SP]=09)
e. [[SP]] = [PC15-8]; (higher byte of PC content ie., 32 will be stored in memory location 09. With these the address (0x3254) which was in PC is stored in stack.
f. [PC]= address (16 bit);the new address of subroutine is loaded to PC. No flags are affected.
2. ACALL address(11 bit) This is absolute call instruction which unconditionally calls the subroutine located at the indicated 11 bit address. This is a 2 byte instruction.
The ACALL instruction works as follows.
During execution of SCALL, [PC] = [PC]+2; if address where LCALL resides is say, 0x8549;
During execution of this instruction [PC] = 8549h + 2h = 854Bh
[SP]=[SP]+1; (if SP contains default value 07, then SP increments and
[SP]=08
[[SP]] = [PC7-0]; (lower byte of PC content ie., 4B will be stored in
memory location 08.
[SP]=[SP]+1; (SP increments again and [SP]=09)
[[SP]] = [PC15-8]; (higher byte of PC content ie., 85 will be stored in memory location 09. With these the address (0x854B) which was in PC is stored in stack.
[PC10-0]= address (11 bit); the new address of subroutine is loaded to PC. No flags are affected.
RET instruction
RET instruction pops top two contents from the stack and load it to PC.
[PC15-8] = [[SP]] ;content of current top of the stack will be moved to higher byte of PC.
[SP]=[SP]-1; (SP decrements)
[PC7-0] = [[SP]] ;content of bottom of the stack will be moved to lower byte of PC. j. [SP]=[SP]-1; (SP decrements again)
3.2.7 Bit Manipulation Instructions
CLR bit
a. CLR bit ; Content of Bit Address specified will be cleared.
b. CLR C ; Content of Carry will be cleared.
CPL bit
a. CPL bit ; Content of Bit address specified will be complemented.
b. CPL C ; Content of carry will be complemented.
SET
a. SET B C
b. SET Address
Key Take Aways:
Writing a Program for any Microcontroller consists of giving commands to the Microcontroller in a particular order in which they must be executed to perform a specific task. The commands to the Microcontroller are known as a Microcontroller’s Instruction Set.
Write a program to store data FFH into RAM memory locations 50H to 58H using direct addressing mode
ORG 0000H ; Set program counter 0000H
MOV A, #0FFH ; Load FFH into A
MOV 50H, A ; Store contents of A in location 50H
MOV 51H, A ; Store contents of A in location 5IH
MOV 52H, A ; Store contents of A in location 52H
MOV 53H, A ; Store contents of A in location 53H
MOV 54H, A ; Store contents of A in location 54H
MOV 55H, A ; Store contents of A in location 55H
MOV 56H, A ; Store contents of A in location 56H
MOV 57H, A ; Store contents of A in location 57H
MOV 58H, A ; Store contents of A in location 58H
END
Write a program to add two 16 bit numbers stored at locations 51H-52H and 55H-56H and store the result in locations 40H, 41H and 42H. Assume that the least significant byte of data and the result is stored in low address and the most significant byte of data or the result is stored in high address.
ORG 0000H ; Set program counter 0000H
MOV A,51H ; Load the contents of memory location 51H into A
ADD A,55H ; Add the contents of 55H with contents of A
MOV 40H,A ; Save the LS byte of the result in location 40H
MOV A,52H ; Load the contents of 52H into A
ADDC A,56H ; Add the contents of 56H and CY flag with A
MOV 41H,A ; Save the second byte of the result in 41H
MOV A,#00 ; Load 00H into A
ADDC A,#00 ; Add the immediate data 00H and CY to A
MOV 42H,A ; Save the MS byte of the result in location 42H
END
Write a program to multiply two 8 bit numbers stored at locations 70H and 71H and store the result at memory locations 52H and 53H. Assume that the least significant byte of the result is stored in low address.
ORG 0000H ; Set program counter 00 OH
MOV A, 70H ; Load the contents of memory location 70h into A
MOV B, 71H ; Load the contents of memory location 71H into B
MUL AB ; Perform multiplication
MOV 52H,A ; Save the least significant byte of the result in location 52H
MOV 53H,B ; Save the most significant byte of the result in location 53H
END
Write a program to compute 1 + 2 + 3 + N (say N=15) and save the sum at70H
ORG 0000H ; Set program counter 0000H
N EQU 15
MOV R0,#00 ; Clear R0
CLR A ; Clear A
again:INC R0 ; Increment R0
ADD A, R0 ; Add the contents of R0 with A
CJNE R0,#N,again ; Loop until counter, R0, N
MOV 70H,A ; Save the result in location 70H
END
Write a program to find the average of five 8 bit numbers. Store the result in 55H. (Assume that after adding five 8 bit numbers, the result is 8 bit only).
ORG 0000H
MOV 40H,#05H
MOV 41H,#55H
MOV 42H,#06H
MOV 43H,#1AH
MOV 44H,#09H
MOV R0,#40H
MOV R5,#05H
MOV B,R5
CLR A
Loop: ADD A,@RO
INC RO DJNZ R5,Loop
DIV AB
MOV 55H,A
END
Write a program to find the cube of an 8 bit number program is as follows
ORG 0000H
MOV R1,#N
MOV A,R1
MOV B,R1
MUL AB //SQUARE IS COMPUTED
MOV R2, B
MOV B, R1
MUL AB
MOV 50,A
MOV 51,B
MOV A,R2
MOV B, R1
MUL AB
ADD A, 51H
MOV 51H, A
MOV 52H, B
MOV A, # 00H
ADDC A, 52H
MOV 52H, A //CUBE IS STORED IN 52H,51H,50H
END
Write a program to count the number of and o's of 8 bit data stored in location 6000H.
ORG 00008 ; Set program counter 00008
MOV DPTR, #6000h ; Copy address 6000H to DPTR
MOVX A, @DPTR ; C o p y n u m b e r t o A
MOV R0,#08 ; C o py 0 8 i n R O
MOV R2,#00 ; C o p y 0 0 i n R 2
MOV R3,#00 ; C o p y 0 0 i n R 3
CLR C ; Clear carry flag
BACK: RLC A ; R o t a t e A t h r o u g h c a r r y f l a g
JC NEXT ; I f C F = 1 , b r a n c h t o n e x t
INC R2 ; I f C F = 0 , i n c r e m e n t R 2
AJMP NEXT2
NEXT: INC R3 ; I f C F = 1 , i n c r e m e n t R 3
NEXT2: DJNZ RO,BACK ; R e p e a t u n t i l R O i s z e r o
END
Key Take Aways:
- The Programs written in Assembly gets executed faster and they occupy less memory.
- With the help of Assembly Language, you can directly exploit all the features of a Microcontroller.
Write an 8051 C program to get a byte of data from PI, wait 1/2 second, and then send it to P2.
Solution:
# include<reg51.h>
void MSDelay(unsigned int);
void main(void)
{
unsigned char mybyte;
P1=0xFF; // make P1 an input port
while(1)
{
mybyte=P1; // get a byte from P1
MS Delay(500);
P2=mybyte; // send it to P2
}
void MSdelay (unsigned int itume)
{
unsigned int i,j;
for i=0;i<time;i++)
for(j=0;j<1275;j++)
}
Write an 8051 C program to get a byte of data from PO. If it is less than 100, send it to
PI; otherwise, send it to P2.
Solution:
# include<reg51.h>
void main(void)
{
unsigned char mybyte;
P0=0xFF; // make P1 an input port
while(1)
{
mybyte=P0; // get a byte from P1
if (mybyte< 100)
P1=mybyte; // send it to P2
else
P2=mybyte;
}
}
Key Take Aways:
Embedded C language is used commonly to program microcontrollers.
- Assembler
The assembly language is a low-level programming language which writes program code in terms of mnemonics. It can be used for direct hardware manipulations. It is used to write 8051 programming code efficiently with less number of clock cycles by consuming less memory compared to other high-level languages.
8051 Programming
8051 Programming in Assembly Language
The assembly language is a completely hardware related programming language. It is essential that the embedded designers have sufficient knowledge on hardware of particular processor or controllers before writing the program. The assembly language is developed on mnemonics.
8051 Programming in Assembly Language
- It is developed by various compilers and the “keiluvison” is best suitable for microcontroller programming development.
- Microcontrollers or processors can understand only binary language in the form of ‘0s or 1s’; An assembler converts the assembly language to binary language, and then stores in microcontroller memory to perform the specific task.
- Compilers
- The name High-level language means the words and statements that are easily understood by humans.
- Few examples of High-level Languages are BASIC, C Pascal, C++ and Java. A program called Compiler will convert the Programs written in High-level languages to Machine Code.
Key Take Aways:
The difference between compiler and assembler is that a compiler is used to convert high-level programming language code into machine language code. On the other hand, an assembler converts assembly level language code into machine language code.
ET-Z8051 Debugger and Programmer.
This unit plugs directly into the target board for controlling and programming your Zilog Microcontroller. This works with Zilog® Z8051 OCD Software and allows for single stepping through the program, viewing register values as well as for programming the microcontroller. Find program errors quickly and easily, with the easy to use debugging tool.
Programs can be downloaded directly through the interface to the target board or microcontroller. Program execution can be initiated from the program and the various register values read and monitored through the software. Control program execution through the easy to use, user interface, with options for Breakpoints and Watch Values.
The ET-Z8051 Debugger and Programmer connects directly to computers USB Port. A 10 pin connector then connects to target board. The power source is taken from the target board and no separate power supply is required.
The ET-Z8051 Programming tool is the ideal debugging tool for developing your Z8051 applications.
Key Take Aways:
Debugging is the process of detecting and removing of existing and potential errors called as 'bugs' in a software code that can cause it to behave unexpectedly or crash.
References:
- The 8051 Microcontroller and Embedded Systems using Assembly and C by Muhammad Ali Mazidi.
- The 8051 Microcontroller by I. Scott Mackenzie, Raphael C.W Phan
8051 Microcontrollers: Internals, Instructions, Programming, and Interfacing Book by Subrata Ghoshal