0% found this document useful (0 votes)
144 views77 pages

8051 Assembly Language

?
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views77 pages

8051 Assembly Language

?
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 77

8051 Assembly language

programming

CHAPTER 4:
3160914: MM
BY : H. V. HIRVANIYA
8051 Assembly language programming

 INSTRUCTION SYNTAX.
LABEL: OPCODE OPERAND ;COMMENT
 LABEL : (This Is Not Necessary Unless That Specific Line Has To Be Addressed).
The label is a symbolic address for the instruction.
 When the program is assembled, the label will be given specific address in which that instruction
is stored.
 OPCODE: Opcode is the symbolic representation of the operation.
 The assembler converts the opcode to a unique binary code (machine language).
8051 Assembly language programming

 INSTRUCTION SYNTAX.
LABEL: OPCODE OPERAND ;COMMENT
 OPERAND: While opcode specifies what operation to perform, operand specifies
where to perform that action.
 The operand field generally contains the source and destination of the data. In some cases only
source or destination will be available instead of both.
 The operand will be either address of the data, or data itself.
 COMMENT: Always comment will begin with ; or // symbol.
 To improve the program quality, programmer may always use comments in the program.
8051 Instructions

 The instructions of 8051 can be broadly classified under the following headings.

1. Data transfer instructions


2. Arithmetic instructions
3. Logical instructions
4. Branch instructions
5. Subroutine instructions
6. Bit manipulation instructions
8051 Instructions

 Data transfer instructions

 a. Move the contents of a register Rn to A


 i. MOV A,R2
 ii. MOV A,R7
 b. Move the contents of a register A to Rn
 i. MOV R4,A
 ii. MOV R1,A
8051 Instructions

 Data transfer instructions

 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
 iii. MOV 30H, #44H
 iv. MOV @R0, #0E8H
 v. MOV DPTR, #0F5A2H
 vi. MOV DPTR, #5467H
8051 Instructions

 Data transfer instructions

 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
8051 Instructions

 Data transfer instructions

 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
8051 Instructions

 Data transfer instructions

 h. Move the contents of program memory to A


 i. MOVC A, @A+PC
 ii. MOVC A, @A+DPTR
8051 Instructions

 Data transfer instructions


 i. Push and Pop instructions
 [SP]=07 //CONTENT OF SP IS 07 (DEFAULT VALUE)
 MOV R6, #25H [R6]=25H //CONTENT OF R6 IS 25H
 MOV R1, #12H [R1]=12H //CONTENT OF R1 IS 12H
 MOV R4, #0F3H [R4]=F3H //CONTENT OF R4 IS F3H
 PUSH 6 [SP]=08 [08]=[06]=25H //CONTENT OF 08 IS 25H
 PUSH 1 [SP]=09 [09]=[01]=12H //CONTENT OF 09 IS 12H
 PUSH 4 [SP]=0A [0A]=[04]=F3H //CONTENT OF 0A IS F3H
 POP 6 [06]=[0A]=F3H [SP]=09 //CONTENT OF 06 IS F3H
 POP 1 [01]=[09]=12H [SP]=08 //CONTENT OF 01 IS 12H
 POP 4 [04]=[08]=25H [SP]=07 //CONTENT OF 04 IS 25H
8051 Instructions

 Data transfer instructions


 j. Exchange instructions: The content of source ie., register, direct memory or indirect
memory will be exchanged with the contents of destination ie., accumulator.
 i. XCH A,R3
 ii. XCH A,@R1
 iii. XCH A,54h
 k. Exchange digit. Exchange the lower order nibble of Accumulator (A0-A3) with lower
order nibble of the internal RAM location which is indirectly addressed by the register.
 i. XCHD A,@R1
 ii. XCHD A,@R0
8051 Instructions

 Arithmetic instructions.

 The 8051 can perform addition, subtraction, multiplication and division operations on 8
bit numbers.
8051 Instructions

 Arithmetic instructions.
 Addition :In this group, we have instructions to
I. Add the contents of A with immediate data with or without carry.
1. ADD A, #45H
2. ADDC A, #OB4H
CY AC and OV flags will be affected
by this operation.
II. Add the contents of A with register Rn with or without carry.
1. ADD A, R5
2. ADDC A, R2
III. Add the contents of A with contents of memory with or without carry using direct and indirect addressing
1. ADD A, 51H
2. ADDC A, 75H
3. ADD A, @R1
4. ADDC A, @R0
8051 Instructions

 Arithmetic instructions.
 Subtraction : In this group, we have instructions to
I. Subtract the contents of A with immediate data with or without carry.
1. SUBB A, #45H
2. SUBB A, #OB4H
CY AC and OV flags will be affected
by this operation.
II. Subtract the contents of A with register Rn with or without carry.
1. SUBB A, R5
2. SUBB A, R2
III. Subtract the contents of A with contents of memory with or without carry using direct and indirect addressing
1. SUBB A, 51H
2. SUBB A, 75H
3. SUBB A, @R1
4. SUBB A, @R0
8051 Instructions

 Arithmetic instructions.
 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.
CY AC and OV flags will be affected
MOV A,#45H ;[A]=45H by this operation.
MOV B,#0F5H ;[B]=F5H
MUL AB ;[A] x [B] = 45 x F5 = 4209
;[A]=09H, [B]=42H
8051 Instructions

 Arithmetic instructions.
 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.
CY AC and OV flags will be affected
MOV A,#45H ;[A]=0E8H by this operation.

MOV B,#0F5H ;[B]=1BH


DIV AB ;[A] / [B] = E8 /1B = 08 H with remainder 10H
;[A] = 08H, [B]=10H
8051 Instructions

 Arithmetic instructions.
 Decimal Adjust After Addition:
 DA A. 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.
8051 Instructions

 Arithmetic instructions.
 Decimal Adjust After Addition:
 DA A. CY AC and OV flags will be affected
by this operation.
MOV R1,#55H
ADD A,R1 // [A]=78
DA A // [A]=78 no changes in the accumulator after DA A

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
8051 Instructions

 Arithmetic instructions.
 Increment: increments the operand by one.
1. INC A
2. INC Rn
3. INC DIRECT
4. INC @Ri
5. INC DPTR
 INC increments the value of source by 1. If the initial value of register is FFh, incrementing the value will
cause it to reset to 0. The Carry Flag is not set when the value "rolls over" from 255 to 0.
 In the case of "INC DPTR", the value two-byte unsigned integer value of DPTR is incremented. If the initial
value of DPTR is FFFFh, incrementing the value will cause it to reset to 0.
8051 Instructions

 Arithmetic instructions.
 Decrement: decrements the operand by one.
1. DEC A
2. DEC Rn
3. DEC DIRECT
4. DEC @Ri

 DEC decrements the value of source by 1.


 If the initial value of is 0, decrementing the value will cause it to reset to FFh.
 The Carry Flag is not set when the value "rolls over" from 0 to FFh.
8051 Instructions

 Logical Instructions.
 Logical AND: ANL destination, source: ANL does a bitwise "AND" operation between source and
destination, leaving the resulting value in destination.

 The value in source is not affected. "AND" instruction logically AND the bits of source and destination.
ANL A,#DATA ANL A, Rn
ANL A,DIRECT ANL A,@Ri
ANL DIRECT,A ANL DIRECT, #DATA
8051 Instructions

 Logical Instructions.
 Logical OR: ORL destination, source: ORL does a bitwise "OR" operation between source and destination,
leaving the resulting value in destination.

 The value in source is not affected. " OR " instruction logically OR the bits of source and destination.
ORL A,#DATA ORL A, Rn
ORL A,DIRECT ORL A,@Ri
ORL DIRECT,A ORL DIRECT, #DATA
8051 Instructions

 Logical Instructions.
 Logical Ex-OR:
 XRL destination, source: XRL does a bitwise "EX-OR" operation between source and destination, leaving
the resulting value in destination.
 The value in source is not affected. " XRL " instruction logically EX-OR the bits of source and destination.

XRL A,#DATA XRL A,Rn


XRL A,DIRECT XRL A,@Ri
XRL DIRECT,A XRL DIRECT, #DATA
8051 Instructions

 Logical Instructions.
 Logical NOT:
 CPL complements operand, leaving the result in operand.
 If operand is a single bit then the state of the bit will be reversed.
 If operand is the Accumulator then all the bits in the Accumulator will be reversed.

CPL A, CPL C, CPL bit address


SWAP A – Swap the upper nibble and lower nibble of A.
8051 Instructions

 Logical Instructions.
 Rotate Instructions :
 RR A :
This instruction is rotate right the accumulator. Its operation is illustrated below. Each bit is shifted one
location to the right, with bit 0 going to bit 7.
8051 Instructions

 Logical Instructions.
 Rotate Instructions :
 RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit 0
8051 Instructions

 Logical Instructions.
 Rotate Instructions :
 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
8051 Instructions

 Logical Instructions.
 Rotate Instructions :
 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.
8051 Instructions

 Branch (JUMP) 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.
8051 Instructions

 Branch (JUMP) Instructions.


 Jump and Call Program Range:
 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) or less than (the address following the jump by 128 or less) is
called a relative jump.
8051 Instructions

 Branch (JUMP) Instructions.


 Jump and Call Program Range:
 Relative Jump:-
8051 Instructions

 Branch (JUMP) Instructions.


 Jump and Call Program Range:
 Relative Jump:-
 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: -
1. Short jump range (-128 to 127 from the instruction following the jump instruction)
8051 Instructions

 Branch (JUMP) Instructions.


 Jump and Call Program Range:
 Relative Jump:- Instructions that use 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 <destination byte>, <source byte>, <relative address>
DJNZ <byte>, <relative address>
JZ <relative address>
JNZ <relative address>
8051 Instructions

 Branch (JUMP) Instructions.


 Jump and Call Program Range:
 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
8051 Instructions

 Branch (JUMP) Instructions.


 Jump and Call Program Range:
 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
8051 Instructions

 Branch (JUMP) Instructions.


 Jump and Call Program Range:
 Short Absolute Jump:-
 It can be seen that the upper 5bits of the program counter (PC) hold the page number and the
lower 11bits of the PC hold the address within that page.
 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.
8051 Instructions

 Branch (JUMP) Instructions.


 Jump and Call Program Range:
 Short Absolute Jump:-
 Advantage: The instruction length becomes 2 bytes.
 Example of short absolute jump: -
ACALL <address 11>
AJMP <address 11>
8051 Instructions

 Branch (JUMP) Instructions.


 Jump and Call Program Range:
 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 <address 16>
LJMP <address 16>
JMP @A+DPTR
8051 Instructions

 Branch (JUMP) Instructions.


 Another classification of jump instructions is
1. Unconditional Jump
2. Conditional Jump
8051 Instructions

 Branch (JUMP) Instructions.


 The unconditional jump:
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.
8051 Instructions

 Branch (JUMP) Instructions.


 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
8051 Instructions

 Branch (JUMP) Instructions.


 Bit level jump instructions:

 Bit level JUMP 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.
8051 Instructions

 Branch (JUMP) Instructions.


 Subroutine CALL And RETURN Instructions:

 Subroutines are handled by CALL and RET instructions


 There are two types of CALL instructions:
1. LCALL address(16 bit)
2. ACALL address(11 bit)
8051 Instructions

 Branch (JUMP) Instructions.


 Subroutine CALL And RETURN Instructions:
 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.
8051 Instructions

 Branch (JUMP) Instructions.


 Subroutine CALL And RETURN Instructions:
 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 SCALL instruction works as follows.
a. During execution of SCALL, [PC] = [PC]+2; (if address where LCALL resides is say, 0x8549; during execution
of this instruction [PC] = 8549h + 2h = 854Bh
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., 4B 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., 85 will be stored in memory location 09.With these the
address (0x854B) which was in PC is stored in stack.
8051 Instructions

 Branch (JUMP) Instructions.


 Subroutine CALL And RETURN Instructions:
 RET instruction:
 RET instruction pops top two contents from the stack and load it to PC.
a. [PC15-8] = [[SP]] ;content of current top of the stack will be moved to higher byte of PC.
b. [SP]=[SP]-1; (SP decrements)
c. [PC7-0] = [[SP]] ;content of bottom of the stack will be moved to lower byte of PC.
d. [SP]=[SP]-1; (SP decrements again)
8051 Instructions

 Bit manipulation instructions.


 8051 has 128 bit addressable memory, Bit addressable SFRs and bit addressable PORT pins.
 It is possible to perform following bit wise operations for these bit addressable locations.
8051 Instructions

 Bit manipulation instructions.


 LOGICAL AND:

 ANL C,BIT(BIT ADDRESS):


 LOGICALLY AND’ CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT IN
CARRY
 ANL C, /BIT:
 LOGICALLY AND’ CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS,
STORE RESULT IN CARRY
8051 Instructions

 Bit manipulation instructions.


 LOGICAL OR:

 ORL C,BIT(BIT ADDRESS):


 LOGICALLY OR’ CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
 ORL C, /BIT:
 LOGICALLY OR’ CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE
RESULT IN CARRY
8051 Instructions

 Bit manipulation instructions.


 CLR bit:
 CLR bit:
 CONTENT OF BIT ADDRESS SPECIFIED WILL BE CLEARED
 CLR C:
 CONTENT OF CARRY WILL BE CLEARED
8051 Instructions

 Bit manipulation instructions.


 CPL bit:

 CPL bit:
 CONTENT OF BIT ADDRESS SPECIFIED WILL BE COMPLEMENTED
 CPL C:
 CONTENT OF CARRY WILL BE COMPLEMENTED
ASSEMBLER DIRECTIVES

 ASSEMBLER DIRECTIVES.
 Assembler directives tell the assembler to do something other than creating the machine code for
an instruction.
 In assembly language programming, the assembler directives instruct the assembler to
1. Process subsequent assembly language instructions
2. Define program constants
3. Reserve space for variables
ASSEMBLER DIRECTIVES

 ASSEMBLER DIRECTIVES.
 ORG (origin):
 The ORG directive is used to indicate the starting address.
 It can be used only when the program counter needs to be changed.
 The number that comes after ORG can be either in hex or in decimal.
Eg: ORG 0000H ;Set PC to 0000.
ASSEMBLER DIRECTIVES

 ASSEMBLER DIRECTIVES.
 EQU and SET:
 EQU and SET directives assign numerical value or register name to the specified symbol name.
 EQU is used to define a constant without storing information in the memory. The symbol defined
with EQU should not be redefined.
 SET directive allows redefinition of symbols at a later stage.
ASSEMBLER DIRECTIVES

 ASSEMBLER DIRECTIVES.
 DB (DEFINE BYTE):
 The DB directive is used to define an 8 bit data. DB directive initializes memory with 8 bit values.
 The numbers can be in decimal, binary, hex or in ASCII formats.
For decimal, the 'D' after the decimal number is optional, but for binary and hexadecimal,
'B' and ‘H’ are required. For ASCII, the number is written in quotation marks (‘LIKE This’).
DATA1: DB 40H ;hex
DATA2: DB 01011100B ;binary
DATA3: DB 48 ;decimal
DATA4: DB 'HELLOW’ ;ASCII
ASSEMBLER DIRECTIVES

 ASSEMBLER DIRECTIVES.
 END:
 The END directive signals the end of the assembly module.
 It indicates the end of the program to the assembler.
 Any text in the assembly file that appears after the END directive is ignored.
 If the END statement is missing, the assembler will generate an error message.
ASSEMBLY LANGUAGE PROGRAMS

1. Write a program to add the values of locations 50H and 51H and store the result in
locations in 52h and 53H.
ORG 0000H ; Set program counter 0000H
MOV A,50H ; Load the contents of Memory location 50H into A ADD ADD A,51H ; Add the
contents of memory 51H with CONTENTS A
MOV 52H,A ; Save the LS byte of the result in 52H
MOV A, #00 ; Load 00H into A
ADDC A, #00 ; Add the immediate data and carry to A
MOV 53H,A ; Save the MS byte of the result in location 53h
END
ASSEMBLY LANGUAGE PROGRAMS

2. 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
ASSEMBLY LANGUAGE PROGRAMS

3. Write a program to subtract a 16 bit number stored at locations 51H-52H from 55H-56H and store the result in
locations 40H and 41H. Assume that the least significant byte of data or the result is stored in low address. If the result
is positive, then store 00H, else store 01H in 42H.
ORG 0000H ; Set program counter 0000H
MOV A, 55H ; Load the contents of memory location 55 into A
CLR C ; Clear the borrow flag
SUBB A,51H ; Sub the contents of memory 51H from contents of A
MOV 40H, A ; Save the LSByte of the result in location 40H
MOV A, 56H ; Load the contents of memory location 56H into A
SUBB A, 52H ; Subtract the content of memory 52H from the content A
MOV 41H, ; Save the MSbyte of the result in location 415.
MOV A, #00 ; Load 005 into A
ADDC A, #00 ; Add the immediate data and the carry flag to A
MOV 42H, A ; If result is positive, store00H, else store 0lH in 42H
ASSEMBLY LANGUAGE PROGRAMS

4. 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
ASSEMBLY LANGUAGE PROGRAMS

5. Write a program to store data FFH into RAM memory locations 50H to 58H using
indirect addressing mode.
ORG 0000H ; Set program counter 0000H
MOV A, #0FFH ; Load FFH into A
MOV RO, #50H ; Load pointer, R0-50H
MOV R5, #08H ; Load counter, R5-08H
Start: MOV @RO, A ; Copy contents of A to RAM pointed by R0
INC RO ; Increment pointer
DJNZ R5, start ; Repeat until R5 is zero
END
ASSEMBLY LANGUAGE PROGRAMS

6. Write a program to add two Binary Coded Decimal (BCD) numbers stored at locations 60H and
61H and store the result in BCD 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 00004
MOV A,60H ; Load the contents of memory location 6.0.H into A
ADD A,61H ; Add the contents of memory location 61H with contents of A
DA A ; Decimal adjustment of the sum in A
MOV 52H, A ; Save the least significant byte of the result in location 52H
MOV A,#00 ; Load 00H into .A
ADDC A,#00H ; Add the immediate data and the contents of carry flag to A
MOV 53H,A ; Save the most significant byte of the result in location 53:,
END
ASSEMBLY LANGUAGE PROGRAMS

7. Write a program to clear 10 RAM locations starting at RAM address 1000H.


ORG 0000H ;Set program counter 0000H
MOV DPTR, #1000H ;Copy address 1000H to DPTR
CLR A ;Clear A
MOV R6, #0AH ;Load 0AH to R6
again: MOVX @DPTR,A ;Clear RAM location pointed by DPTR
INC DPTR ;Increment DPTR
DJNZ R6, again ;Loop until counter R6=0
END
ASSEMBLY LANGUAGE PROGRAMS

8. 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
ASSEMBLY LANGUAGE PROGRAMS

9. 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 53
END
ASSEMBLY LANGUAGE PROGRAMS

10. Ten 8 bit numbers are stored in internal data memory from location 5oH. Write a
program to increment the data.
Assume that ten 8 bit numbers are stored in internal data memory from location 50H, hence R0 or
R1 must be used as a pointer.
ORG 0000H
MOV R0,#50H
MOV R3,#0AH
Loopl: INC @R0
INC RO
DJNZ R3, Loopl
END
ASSEMBLY LANGUAGE PROGRAMS

11. Write a program to find the average of MOV R0,#40H


five 8 bit numbers. Store the result in H. MOV R5,#05H
(Assume that after adding five 8 bit numbers, MOV B,R5
the result is 8 bit only).
CLR A
ORG 0000H
Loop: ADD A,@RO
MOV 40H,#05H
INC RO
MOV 41H,#55H
DJNZ R5,Loop
MOV 42H,#06H
DIV AB
MOV 43H,#1AH
MOV 55H,A
MOV 44H,#09H
END
ASSEMBLY LANGUAGE PROGRAMS

12. Write a program to find the cube of an 8 bit MOV 51,B


number program is as follows. MOV A,R2
ORG 0000H MOV B, R1
MOV R1,#N MUL AB
MOV A,R1 ADD A, 51H
MOV B,R1 MOV 51H, A
MUL AB //SQUARE IS MOV 52H, B
COMPUTED
MOV A, # 00H
MOV R2, B
ADDC A, 52H
MOV B, R1
MOV 52H, A //CUBE IS
MUL AB STORED IN
MOV 50,A 52H,51H,50H
ASSEMBLY LANGUAGE PROGRAMS

80
13. Write a program to exchange the lower
nibble of data present in external memory INC DPL ; Increment pointer

6000H and 6001H MOVX A, @DPTR ; Copy contents of 60018 to A


XCHD A, @R0 ; Exchange lower
ORG 0000H ; Set program counter
nibble of A with
00h
RAM pointed by RO
MOV DPTR, #6000H ; Copy address 6000H to
MOVX @DPTR, A ; Copy contents of A to 60018
DPTR DEC DPL ; Decrement pointer
MOVX A, @DPTR ; Copy contents of 60008 MOV A, @R0 ; Copy cont of RAM
pointed by R0 to
to A A
MOV R0, #45H ; Load pointer, MOVX @DPTR, A ; Copy cont of A to RAM
R0=45H pointed by
DPTR
MOV @RO, A ; Copy cont of A to
ASSEMBLY LANGUAGE PROGRAMS

14. Write a program to count the number of carry flag


and o's of 8 bit data stored in location JC NEXT ; If CF = 1, branch to
next
6000H. INC R2 ; If CF = 0, increment R2 AJMP
ORG 00008 ; Set program NEXT2
counter 00008
NEXT: INC R3 ; If CF = 1, increment R3
MOV DPTR, #6000h ; Copy address 6000H to DPTR
NEXT2: DJNZ RO,BACK ; Repeat until RO is zero
MOVX A, @DPTR ; Copy number to A
END
MOV R0,#08 ; Copy 08 in RO
MOV R2,#00 ; Copy 00 in R2
MOV R3,#00 ; Copy 00 in R3
CLR C ; Clear carry flag
BACK: RLC A ; Rotate A through
ASSEMBLY LANGUAGE PROGRAMS

MOV A,56H
15. Write a program to shift a 24 bit number
stored at 57H-55H to the left logically four through RLC A ; Rotate contents of A (56H) left
carry
places. Assume that the least significant MOV 56H,A
byte of data is stored in lower address.
MOV A,57H
ORG 0000H ; Set program counter 0000h
RLC A ; Rotate contents of A (57H) left
MOV R1,#04 ; Set up loop count to 4 through carry
again: MOV A,55H ; Place the least significant byte of data in MOV 57H,A
A
DJNZ R1,again ; Repeat until R1 is zero
CLR C ; Clear tne carry flag
END
RLC A ; Rotate contents of A (55h) left
through carry
MOV 55H,A
ASSEMBLY LANGUAGE PROGRAMS

16. Two 8 bit numbers are stored in location 1000h and 1001h of external data memory.
Write a program to find the GCD of the numbers and store the result in 2000h.
ALGORITHM
Step 1 :Initialize external data memory with data and DPTR with address
Step 2 :Load A and TEMP with the operands
Step 3 :Are the two operands equal? If yes, go to step 9
Step 4 :Is (A) greater than (TEMP) ? If yes, go to step 6
Step 5 :Exchange (A) with (TEMP) such that A contains the bigger number
Step 6 :Perform division operation (contents of A with contents of TEMP)
Step 7 :If the remainder is zero, go to step 9
Step 8 :Move the remainder into A and go to step 4
Step 9 :Save the contents 'of TEMP in memory and terminate the program
ASSEMBLY LANGUAGE PROGRAMS

AJMP LOOP2 ; (A) = (TEMP) branch to L00P2


16. Two 8 bit numbers are stored in location 1000h
and 1001h of external data memory. Write a LOOP1: JNC LOOP3 ; (A) > (TEMP) branch to LOOP3

program to find the GCD of the numbers and store NOV TEMPI, A ; (A) < (TEMP) exchange (A) with
(TEMP)
the result in 2000h.
MOV A, TEMP
ORG 0000H ; Set program counter 0000H
MOV TEMP, TEMPI
TEMP EQU 70H
LOOP3: MOV B, TEMP
TEMPI EQU 71H
DIV AB ; Divide (A) by (TEMP)
MOV DPTR, #1000H ; Copy address 100011 to DPTR
MOV A, B ; Move remainder to A
MOVX A, @DPTR ; Copy First number to A
CJNE A,#00, LOOPS ; (A)/=00 branch to LOOPS
MOV TEMP, A ; Copy First number to temp INC DPTR
LOOP2: MOV A, TEMP
INC DPTR
MOV DPTR, #2000H
MOVX A, @DPTR ; Copy Second number to A
MOVX @DPTR, A ; Store the result in 2000H
MOV TEMPI,A
END
LOOPS: CJNE A, TEMP, LOOP1 ; (A) /= (TEMP) branch to LOOP1
ASSEMBLY LANGUAGE PROGRAMS

INC R6 ; increment the counter


17. find out how many equal bytes between
two memory blocks 10h to 20h and 20h to
30h. NOMATCH: INC R0 ; otherwise go for second number
INC R1
MOV R7, #0AH ; initialize counter by 10d
DJNZ R7, NXT ; decrease r7. if zero then over
MOV R0, #10H ; get initial location of block1
MOV R1, #20H ; get initial location of block2 otherwise move next
MOV R6, #00H ; equal byte counter. Starts from
zero
NXT: MOV A, @R0 ; get content of block 1 in acc
MOV B, A ; move it to B
MOV A, @R1 ; get content of block 2 in acc
CJNE A, B, NOMATCH ; compare both if equal
ASSEMBLY LANGUAGE PROGRAMS

SJMP OUT ; if number is larger


18. given block of 100h to 200h. Find out
how many bytes from this block are greater LOWER: JNC OUT ; jump out

then the number in r2 and less then number in CJNE A, 20H, LIMIT ; check lower limit

r3. Store the count in r4.. SJMP OUT ; if number is lower


LIMIT: JC OUT ; jump out
MOV DPTR, #0100H ; get initial location
INC R4 ; if number within limit
MOV R7, #0FFH ; counter
MOV R4, #00H ; number counter increment count
MOV 20H, R2 ; get the upper and OUT: INC DPTR ; get next
lower limits in location
MOV 21H, R3 ; 20h and 21h DJNZ R7, NXT ; repeat until block
completes
NXT: MOVX A, @DPTR ; get the content in acc
CJNE A, 21H, LOWER ; check the upper limit first
Assignment

 Write assembly language program count number of 0’s and 1’s in a byte stored in external memory
location C500H. Draw flowchart.
 Write an assembly language program to convert given binary number to 3 digit BCD number. Draw
flowchart.
 Write an assembly language program to accept 10 numbers from port 1 and store them in RAM locations
starting from 50H. Draw flowchart.
 What are the addressing modes for 8051? Explain in brief giving suitable example.
 Write an assembly program to add two 16 bit numbers.
 Write an assembly program to find factorial of a number.
Thank you…

You might also like