MICROCONTROLLER & EMBEDDED SYSTEMS UNIT-3 ASSEMBLY LANGUAGE PROGRAMMING • The assembly language is a low-level programming language used to write program code in terms of mnemonics. • It can be used for direct hardware manipulations. • It is also used to write the 8051 programming code efficiently with less number of clock cycles by consuming less memory compared to the other high-level languages. Assembly Language Programming • The assembly language is made up of elements which all are used to write the program in sequential manner. • The assembly language mnemonics are in the form of op-code, such as MOV, ADD, JMP, and so on, which are used to perform the operations. Basic Elements of Assembly Language Programming There are three basic elements of ALP: - Assembler Directives - Instruction Set - Addressing Modes ASSEMBLER DIRECTIVES: The assembling directives give the directions to the CPU. The 8051 microcontroller consists of various kinds of assembly directives to give the direction to the control unit. The most useful directives are 8051 programming, such as: • ORG • DB • EQU • END
12
Embed
MICROCONTROLLER & EMBEDDED SYSTEMS UNIT-3 ASSEMBLY ... · MICROCONTROLLER & EMBEDDED SYSTEMS UNIT-3 ASSEMBLY LANGUAGE PROGRAMMING • The assembly language is a low-level programming
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
MICROCONTROLLER & EMBEDDED SYSTEMS UNIT-3
ASSEMBLY LANGUAGE PROGRAMMING • The assembly language is a low-level programming language used to write
program code in terms of mnemonics. • It can be used for direct hardware manipulations. • It is also used to write the 8051 programming code efficiently with less number of
clock cycles by consuming less memory compared to the other high-level languages.
Assembly Language Programming • The assembly language is made up of elements which all are used to write the
program in sequential manner. • The assembly language mnemonics are in the form of op-code, such as MOV,
ADD, JMP, and so on, which are used to perform the operations.
Basic Elements of Assembly Language Programming
There are three basic elements of ALP: - Assembler Directives - Instruction Set - Addressing Modes ASSEMBLER DIRECTIVES: The assembling directives give the directions to the CPU. The 8051 microcontroller consists of various kinds of assembly directives to give the direction to the control unit. The most useful directives are 8051 programming, such as: • ORG • DB • EQU • END
1. ORG(origin): - This directive indicates the start of the program. - This is used to set the register address during assembly. - For example; ORG 0000h tells the compiler all subsequent code starting at
address 0000h. Syntax: ORG 0000h
2. DB(define byte): - The define byte is used to allow a string of bytes. - For example, print the “EDGEFX” wherein each character is taken by the
address and finally prints the “string” by the DB directly with double quotes. Syntax: ORG 0000h MOV A, #00h ————- ————- DB “EDGEFX”
3. EQU (equivalent): - The equivalent directive is used to equate address of the variable.
Syntax: reg equ,09h —————– —————– MOV reg,#2h
4. END: - The END directive is used to indicate the end of the program.
Syntax: reg equ,09h —————– —————– MOV reg,#2h END
8051 ASSEMBLY LANGUAGE PROGRAMS
Write an ALP for 8051 microcontroller to add two 8-bit numbers stored at memory location 20H and 21H and after addition store the result at 30H and 31H Let us suppose two 8-bit numbers 5FH and D8H are stored at memory locations 20H and 21H respectively.
Program:
Result: On adding 5FH and D8H, we get 37 H as the sum and 01 H as the carry. So, as the output of the above program, the sum 37H is stored at the memory address 30H and the carry 01H is stored at the memory address 31 H.
MEMORY ADDRESS
MACHINE CODE LABEL OPCODE OPERAND COMMENTS
ORG 0000 H Assembler Directive
0000 H 78, 20 MOV R0,#20H Set source address 20H to R0
0002 H 79, 30 MOV R1,#30H Set destination address 30H to R1
0004 H E6 MOV A,@R0 Move the value from source address to register A (Acc)
0005 H FD MOV R5,A Move the value from Accumulator to R5
0006 H 7C, 00 MOV R4,#00H Clear register R4 to store carry
0008 H 08 INC R0 Increment R0 to point to the next address 21H
0009 H E6 MOV A,@R0 Move the value from source address to register A
000A H 2D ADD A,R5 Add R5 with A and store the result in register A
000B H 50, 0E JNC SAVE Jump if carry is not generated to memory address 000E H
000D H 0C INC R4 Increment R4 to get carry
000E H F7 SAVE: MOV @R1,A Store the result from A to memory address 30H
000F H EC MOV A,R4 Get carry to register A
0010 H 09 INC R1 Increase R1 to point to the next address 31H
0011 H F7 MOV @R1,A Store the carry at memory address 31H
0012 H 80, 12 HALT: SJMP HALT Stop the program
0014 H END Assembler Directive
Write an ALP for 8051 microcontroller to add two 16-bit numbers and store the result at 30 H and 32 H. Let us suppose two 16-bit numbers are AB20 H and 65DE H. Treat R4-R5 and R6-R7 as two 16-bit registers where R4 and R6 carry the higher bytes and R5 and R7 carry the lower bytes. Input:
Program:
FIRST OPERAND (16-bit number) SECOND OPERAND (16-bit number)
Higher Byte Lower Byte Higher Byte Lower Byte
R4 R5 R6 R7
AB 20 65 DE
MEMORY ADDRESS
MACHINE CODE LABEL OPCODE OPERAND COMMENTS
ORG 0000 H Assembler Directive
0000 H C3 CLR C Make CY=0
0001 H 7C,AB MOV R4,#AB Load the higher byte of AB20 to register R4
0003 H 7D,20 MOV R5,#20 Load the lower byte of AB20 to register R5
0005 H 7E,65 MOV R6,#65 Load the higher byte of 65DE to register R6
0007 H 7F,DE MOV R7,#DE Load the lower byte of 65DE to register R7
0009 H 78, 32 MOV R0,#32 Set destination address 32H to R0
000B H ED MOV A,R5 Move the value from register R5 to Accumulator A
000C H 2F ADD A,R7 Add the lower bytes of both the numbers and store the result in A
000D H F6 MOV @R0,A Store the lower byte of the result at memory address 32H
000E H 18 DEC R0 Decrement R0 to point to the memory address 31H
000F H EE MOV A,R6 Move the value from register R6 to Accumulator A
0010 H 3C ADDC A,R4 Add the higher bytes of both the numbers along with carry and store the result in A
0011 H F6 MOV @R0,A Store the higher byte of the result at memory address 31H
Output:
Result: On adding AB20 H and 65DE H, we get 10FE H as the sum and 01 H as the carry. So, as the output of the above program, the higher byte of the sum 10H is stored at the memory address 31H, the lower byte of the sum, FE H is stored at the memory address 32 H and the carry 01H is stored at the memory address 30 H.
0012 H 50, 16 JNC HALT Jump if carry is not generated to memory address 0016H
0013 H 18 DEC R0 Decrement R0 to point to the memory address 30H
0014 H 76, 01 MOV @R0,#01 Store the carry generated at memory address 30H
0016 H 80, 16 HALT: SJMP HALT Stop the program
0018 H END Assembler Directive
MEMORY ADDRESS
MACHINE CODE LABEL OPCODE OPERAND COMMENTS
Memory Address 30H 31H 32H
ResultCarry Higher Byte Lower Byte
01 10 FE
Write an ALP for 8051 microcontroller to subtract two 8-bit numbers stored at memory location 20H and 21H and after subtraction store the result at 30H and 31H Let us suppose two 8-bit numbers 73 H and BD H are stored at memory locations 20H and 21H respectively. Input:
Program:
FIRST OPERAND (8-bit number) SECOND OPERAND (8-bit number)
Memory Address 20 H 21 H
Numbers to be subtracted 73 BD
MEMORY ADDRESS
MACHINE CODE LABEL OPCODE OPERAND COMMENTS
ORG 0000 H Assembler Directive
0000 H 78, 20 MOV R0,#20H Set source address 20H to R0
0002 H 79, 30 MOV R1,#30H Set destination address 30H to R1
0004 H E6 MOV A,@R0 Move the value from source address to register A (Acc)
0005 H FD MOV R5,A Move the value from Accumulator to R5
0006 H 7C, 00 MOV R4,#00H Clear register R4 to store borrow
0008 H 08 INC R0 Increment R0 to point to the next address 21H
0009 H E6 MOV A,@R0 Move the value from source address 21H to register A
000A H FB MOV R3,A Store the second byte in register R3
000B H 2D MOV A,R5 Get back the first operand to register A
000C H 9B SUBB A,R3 Subtract R3 from A
000D H 50, 0E JNC SAVE Jump if carry is not generated to memory address 0010 H
000F H 0C INC R4 Increment R4 to get carry
0010 H F7 SAVE: MOV @R1,A Store the result from A to memory address 30H
0011 H EC MOV A,R4 Get borrow to register A
0012 H 09 INC R1 Increase R1 to point to the next address 31H
Output:
Result: On subtracting BD H from 73 H, we get B6 H as the difference and 01 H as the borrow So, as the output of the above program, the difference B6 H is stored at the memory address 31H and the borrow 01H is stored at the memory address 30 H.
0013 H F7 MOV @R1,A Store the borrow at memory address 31H
0014 H 80, 14 HALT: SJMP HALT Stop the program
0016 H END Assembler Directive
MEMORY ADDRESS
MACHINE CODE LABEL OPCODE OPERAND COMMENTS
Memory Address 30H 31H
ResultBorrow Difference
01 B6
Write an ALP for 8051 microcontroller to subtract two 16-bit numbers and store the result at 30 H and 32 H. Let us suppose two 16-bit numbers are AB20 H and 65DE H. Treat R4-R5 and R6-R7 as two 16-bit registers where R4 and R6 carry the higher bytes and R5 and R7 carry the lower bytes. Input:
Program:
FIRST OPERAND (16-bit number) SECOND OPERAND (16-bit number)
Higher Byte Lower Byte Higher Byte Lower Byte
R4 R5 R6 R7
AB 20 65 DE
MEMORY ADDRESS
MACHINE CODE LABEL OPCODE OPERAND COMMENTS
ORG 0000 H Assembler Directive
0000 H C3 CLR C Make CY=0
0001 H 7C,AB MOV R4,#AB Load the higher byte of AB20 to register R4
0003 H 7D,20 MOV R5,#20 Load the lower byte of AB20 to register R5
0005 H 7E,65 MOV R6,#65 Load the higher byte of 65DE to register R6
0007 H 7F,DE MOV R7,#DE Load the lower byte of 65DE to register R7
0009 H 78, 32 MOV R0,#32 Set destination address 32H to R0
000B H ED MOV A,R5 Move the value from register R5 to Accumulator A
000C H 9F SUBB A,R7 Subtract the lower byte in R7 from that in A and store the result in A
000D H F6 MOV @R0,A Store the lower byte of the result at memory address 32H
000E H 18 DEC R0 Decrement R0 to point to the memory address 31H
000F H EC MOV A,R4 Move the value from register R6 to Accumulator A
0010 H 9E SUBB A,R6 Subtract the higher byte in R6 from that in A along with borrow and store the result in A
0011 H F6 MOV @R0,A Store the higher byte of the result at memory address 31H
Output:
Result: On subtracting 65DE H from AB20 H, we get 4542 H as the sum and 00 H as the borrow. So, as the output of the above program, the higher byte of the difference, 45H is stored at the memory address 31H, the lower byte of the difference, 42H is stored at the memory address 32 H and the as no borrow is generated so it is not stored at the memory address 30 H.
0012 H 50, 16 JNC HALT Jump if borrow is not generated to memory address 0016H
0013 H 18 DEC R0 Decrement R0 to point to the memory address 30H
0014 H 76, 01 MOV @R0,#01 Store the borrow generated at memory address 30H
0016 H 80, 16 HALT: SJMP HALT Stop the program
0018 H END Assembler Directive
MEMORY ADDRESS
MACHINE CODE LABEL OPCODE OPERAND COMMENTS
Memory Address 31H 32H
ResultHigher Byte Lower Byte
45 42
Write an ALP for 8051 microcontroller to multiply two 8-bit numbers stored at memory location 20H and 21H and after multiplication store the result at 30H and 31H Let us suppose two 8-bit numbers FF H and FE H are stored at memory locations 20H and 21H respectively. Input:
Program:
Result: On multiplying FF H and FE H, we get FD02 H as the result of the multiplication. Of which the higher order byte FD H gets stored in register B and lower byte 02H in register A after multiplication. So, as the output of the above program, the higher order byte FD H is stored at the memory address 30H and the lower order byte 02H is stored at the memory address 31H.
FIRST OPERAND (8-bit number) SECOND OPERAND (8-bit number)
Memory Address 20 H 21 H
Numbers to be multiplied FF FE
MEMORY ADDRESS
MACHINE CODE LABEL OPCODE OPERAND COMMENTS
ORG 0000 H Assembler Directive
0000 H 78, 20 MOV R0,#20H Set source address 20H to R0
0002 H 79, 30 MOV R1,#30H Set destination address 30H to R1
0004 H E6 MOV A,@R0 Move the value from source address to register A (Acc)
0005 H 08 INC R0 Increment R0 to point to the next address 21H
0006 H 86, F0 MOV B,@R0 Move the value from source address 21H to register B
0008 H A4 MUL AB Multiply register A and B
0009 H A7, F0 MOV @R1,B Store the higher order byte of the result from B to memory address 30H
000B H 09 INC R1 Increase R1 to point to the next address 31H
000C H F7 MOV @R1,A Store the lower byte of the result from A to memory address 31H
000D H 80, 0D HALT: SJMP HALT Stop the program
000F H END Assembler Directive
Output:
Write an ALP for 8051 microcontroller to divide an 8-bit number by another 8- bit number stored at memory locations 20H and 21H and after division store the result at 30H and 31H Let us suppose two 8-bit numbers 0E H and 03 H are stored at memory locations 20H and 21H respectively and 0E H is to be divided by 03 H. Input:
Program:
FIRST OPERAND (8-bit number) SECOND OPERAND (8-bit number)
Memory Address 20 H 21 H
Numbers 0E 3
MEMORY ADDRESS
MACHINE CODE LABEL OPCODE OPERAND COMMENTS
ORG 0000 H Assembler Directive
0000 H 78, 20 MOV R0,#20H Set source address 20H to R0
0002 H 79, 30 MOV R1,#30H Set destination address 30H to R1
0004 H E6 MOV A,@R0 Move the value from source address to register A (Acc)
0005 H 08 INC R0 Increment R0 to point to the next address 21H
0006 H 86, F0 MOV B,@R0 Move the value from source address 21H to register B
0008 H 84 DIV AB Divide register A by B
0009 H F7 MOV @R1,A Store the quotient of the result from A to memory address 30H
000A H 09 INC R1 Increase R1 to point to the next address 31H
000B H A7, F0 MOV @R1,B Store the remainder of the result from B to memory address 31H
000D H 80, 0D HALT: SJMP HALT Stop the program
000F H END Assembler Directive
Memory Address 30H 31H
ResultHigher Order Byte Lower Order Byte
FD 02
Result: On dividing 0E H (Reg A) by 03 H (Reg B), the quotient 04H gets stored in Reg A and the remainder 02H in Reg B. So, as the output of the above program, the quotient 04H is stored at the memory address 30H and the remainder 02H is stored at the memory address 31H.