BEC405A - Module 2
BEC405A - Module 2
Module – 2
8051 Instruction Set
General syntax for 8051 assembly language is as follows.
LABEL: OPCODE OPERAND ; COMMENT
OPCODE: Opcode is the symbolic representation of the operation. The assembler converts the
opcode to a unique binary code (machine language).
OPERAND: While opcode specifies what operation to perform, operand specifies where to perform that
.IN
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 thedata,
or data itself. C
COMMENT: Always comment will begin with ; or // symbol. To improve the program quality,
N
programmer may always use comments in the program.
SY
Addressing Modes
The CPU can access data in various ways, which are called addressing modes
U
VT
Direct
modes
Indexed
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 19
G.
.IN
We can use EQU directive to access immediate data
C
N
SY
Direct addressing
mode
MOV A,4 ;is same as
MOV A,R4 ;which means copy R4 into A
Register addressing
mode
.IN
In contrast to immediate addressing mode
o There is no “#” sign in the operand
Example 2-1
Write code to send 55H to ports P1 and P2, using (a) their names (b) their addresses
Solution :
(a) MOV A,#55H ;A=55H
MOV P1,A ;P1=55H
MOV P2,A ;P2=55H
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 21
G.
Example 2-2
Show the code to push R5 and A onto the stack and then pop them back them into R2 and B,
where B = A and R2 = R5
Solution:
PUSH 05 ;push R5 onto stack
PUSH 0E0H ;push register A onto stack
POP 0F0H ;pop top of stack into B
;now register B = register A
POP 02 ;pop top of stack into R2
;now R2=R6
.IN
Accessing Memory- Register-Indirect Addressing Mode
A register is used as a pointer to the data
• Only register R0 and R1 are used for this purpose
C
• R2 – R7 cannot be used to hold the address of an operand located in RAM
When R0 and R1 hold the addresses of RAM locations, they must be preceded by the “@”
N
sign
MOV A,@R0 ; move contents of RAM whose
SY
Example 2-3
Write a program to copy the value 55H into RAM memory locations 40H to 41H using
VT
(a) direct addressing mode, (b) register indirect addressing mode without a loop, and (c) with a loop
Solution:
(a)
MOV A,#55H ;load A with value 55H
MOV 40H,A ;copy A to RAM location 40H
MOV 41H,A ;copy A to RAM location 41H
(b)
MOV A,#55H ;load A with value 55H
MOV R0,#40H ;load the pointer. R0=40H
MOV @R0,A ;copy A to RAM R0 points to
INC R0 ;increment pointer. Now R0=41h
MOV @R0,A ;copy A to RAM R0 points to
(c)
MOV A,#55H ;A=55H
MOV R0,#40H ;load pointer.R0=40H,
MOV R2,#02 ;load counter, R2=3
AGAIN: MOV @R0,A ;copy 55 to RAM R0 points to
INC R0 ;increment R0 pointer
DJNZ R2,AGAIN ;loop until counter = zero
The advantage is that it makes accessing data dynamic rather than static as in direct
addressing mode
• Looping is not possible in direct addressing mode
Example 2-4
Write a program to clear 16 RAM locations starting at RAM address 60H
Solution:
CLR A ;A=0
MOV R1,#60H ;load pointer, R1=60H
MOV R7,#16 ;load counter, R7=16
AGAIN: MOV @R1,A ;clear RAM R1 points to
INC R1 ;increment R1 pointer
DJNZ R7,AGAIN ;loop until counter=zero
Example 2-5
.IN
Write a program to copy a block of 10 bytes of data from 35H to 60H
Solution:
MOV R0,#35H ;source pointer
MOV R1,#60H ;destination pointer
MOV R3,#10 ;counter
C
BACK: MOV A,@R0 ;get a byte from source
N
MOV @R1,A ;copy it to destination
INC R0 ;increment source pointer
SY
R0 and R1 are the only registers that can be used for pointers in register indirect
addressing mode
VT
Since R0 and R1 are 8 bits wide, their use is limited to access any information in the
internal RAM
Whether accessing externally connected RAM or on-chip ROM, we need 16-bit pointer
In such case, the DPTR register is used.
Indexed addressing mode is widely used in accessing data elements of look-up table
entries located in the program ROM.
The look-up table allows access to elements of a frequently used table with minimum
operations
The instruction used for this purpose is MOVC A,@A+DPTR
• Use instruction MOVC, “C” means code
• The contents of A are added to the 16-bit register DPTR to form the 16-bit address
of the needed data
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 23
G.
Example 2-6
Write a program to get the x value from P1 and send x2 to P2, continuously
Solution:
ORG 0
MOV DPTR,#300H ;LOAD TABLE ADDRESS
MOV A,#0FFH ;A=FF
MOV P1,A ;CONFIGURE P1 INPUT PORT
BACK: MOV A,P1 ;GET X
MOV A,@A+DPTR ;GET X SQAURE FROM TABLE
MOV P2,A ;ISSUE IT TO P2
SJMP BACK ;KEEP DOING IT
ORG 300H
XSQR_TABLE:
DB 0,1,4,9,16,25,36,49,64,81
.IN
END
C
N
SY
U
VT
.IN
Data Transfer using immediate and register addressing modes
Mnemonic C Operation
A data MOV does not alter the contents of the data source address.
VT
A copy of the data is made from the source and moved to the destination address.
The contents of the destination address are replaced by the source address contents.
Mnemonic Operation
MOV A,#0Flh Move the immediate data byte F1h to the A register
MOV A,R0 Copy the data in register R0 to register A
MOV DPTR,#0ABCDh Move the immediate data bytes ABCDh to the DPTR
MOV R5,A Copy the data in register A to register R5
MOV R3,#1Ch Move the immediate data byte 1Ch to register R3
Mnemonic Operation
MOV A, addr Copy data from direct address add to register A
MOV add, A Copy data from register A to direct address addr
MOV Rr, addr Copy data from direct address add to register Rr
MOV addr, Rr Copy data from register Rr to direct address addr
MOV addr, #n Copy immediate data byte n to direct address addr
MOV instructions that refer to direct addresses above 7Fh that are not SFRs will result in
errors.
The SFRs are physically on the chip; all other addresses above 7Fh do not physically exist.
.IN
Moving data to a port changes the port latch; moving data from a port gets data from the port
pins.
Moving data from a direct address to itself is not predictable and could lead to errors.
Mnemonic
C Operation
N
MOV A, 80h Copy data from the port 0 pins to register A
MOV 80h, A Copy data from register A to the port 0 latch
SY
MOV 3Ah, #3Ah Copy immediate data byte 3Ah to RAM location 3Ah
MOV R0, 12h Copy data from RAM location 12h to register RO
U
The indirect addressing mode uses a register to hold the actual address that will finally be
used in the data move;
The register itself is not the address, but rather the number in the register.
Indirect addressing for MOV opcodes uses register R0 or R1, often called "data pointers," to
hold the address of one of the data locations, which could be a RAM or an SFR address.
The mnemonic symbol used for indirect addressing is the "at" sign, which is printed as @.
Mnemonic Operation
MOV @Rp, #n Copy the immediate byte n to the address in Rp
MOV @Rp, addr Copy the contents of add to the address in Rp
MOV @Rp, A Copy the data in A to the address in Rp
MOV addr, @Rp Copy the contents of the address in Rp to addr
MOV A, @Rp Copy the contents of the address in Rp to A
Mnemonic Operation
.IN
MOV A, @R0 Copy the contents of the address in RO to the A register
MOV @R1, #35h Copy the number 35h to the address in R1
MOV addr, @R0 Copy the contents of the address in RO to addr
MOV @R1, A
C
Copy the contents of A to the address in R1
N
MOV @R0, 80h Copy the contents of the port 0 pins to the address in R0
SY
Registers R0. R1, and the DPTR can be used to hold the address of the data byte in
external RAM.
U
R0 and R1 are limited to external RAM address ranges of 00h to 0FFh, while the DPTR
register can address the RAM space of 0000h to 0FFFFh.
VT
An X is added to the MOV mnemonics to serve as a reminder that the data move is external to
the 8051
Mnemonic Operation
MOVX A, @Rp Copy the contents of the external address in Rp to A
MOVX A, @DPTR Copy the contents of the external address in DPTR to A
MOVX @Rp, A Copy data from A to the external address in Rp
MOVX @DPTR, A Copy data from A to the external address in DPTR
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 27
G.
Mnemonic Operation
MOVX @DPTR, A Copy data from A to the 16-bit address in DPTR
MOVX@R0, A Copy data from A to the 8-bit address in R0
MOVXA, @R1 Copy data from the 8-bit address in R1 to A
MOVX A, @DPTR Copy data from the 16-bit address in DPTR to A
Data moves between RAM locations and 8051 registers are made by using MOV and MOVX
opcodes. The data is usually of a temporary or "scratch pad" nature and disappears when
the system is powered down.
There are times when access to a preprogrammed mass of data is needed, such as when
using tables of predefined bytes. This data must be permanent to be of repeated use and is
.IN
stored in the program ROM.
Access to this data is made possible by using indirect addressing and the A register in
conjunction with either the PC or the DPTR.
In both cases, the number in register A is added to the pointing register to form the address
in ROM where the desired data is to be found.
C
The data is then fetched from the ROM address so formed and placed in the A register.
N
The original data in A is lost, and the addressed data takes its place.
SY
Mnemonic Operation
Copy the code byte, found at the ROM address formed
MOVC A, @A+DPTR
by adding A and the DPTR, to A
U
Mnemonic Operation
MOV DPTR, #1234h Copy the immediate number 1234h to the DPTR
MOV A, #56h Copy the immediate number 56h to A
MOVC A, @A+DPTR Copy the contents of address 128Ah to A
Copies the contents of address 4059h to A if the PC
MOVC A, @A+PC contained 4000h and A contained 58h when the opcode
is executed
The PC is incremented by one (to point to the next instruction) before it is added to A to form
the final address of the code byte.
All data is moved from the code memory to the A register.
MOVC is normally used with internal or external ROM and can address 4K of internal or 64K
bytes of external code.
28 DEPT. OF ELECTRONICS & COMMUNICATION E N G G . | ATMECE M Y S O R E
The PUSH and POP opcodes specify the direct address of the data.
The data moves between an area of internal RAM, known as the stack, and the specified direct
address.
.IN
The stack pointer special-function register (SP) contains the address in RAM where data from the
source address will be PUSHed, or where data to be POPed to the destination address is found.
The SP register actually is used in the indirect addressing made but is not named in the mnemonic.
It is implied that the SP holds the indirect address whenever PUSHing or POPing.
C
A PUSH opcode copies data from the source address to the stack.
SP is incremented by one before the data is copied to the internal RAM location contained in SP so
N
that the data is stored from low addresses to high addresses in the internal RAM.
The stack grows up in memory as it is PUSHed. Excessive PUSHing can make the stack exceed
SY
7Fh (the top of internal RAM), after which point data is lost.
A POP opcode copies data from the stack to the destination address.
SP is decremented by one after data is copied from the stack RAM address to the direct destination
to ensure that data placed on the stack is retrieved in the same order as it was stored.
U
VT
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 29
G.
MOV, PUSH, and POP opcodes all involve copying the data found in the source address to the
destination address; the original data in the source is not changed.
Exchange instructions actually move data in two directions: from source to destination and
from destination to source.
All addressing modes except immediate may be used in the XCH (exchange) opcodes
.IN
All exchanges are internal to the 8051.
C
All exchanges use register A.
When using XCHD, the upper nibble of A and the upper nibble of the address location in Rp
N
do not change.
SY
Arithmetic instructions
The 8051 can perform addition, subtraction. Multiplication and division operations on 8 bit numbers.
The 24 arithmetic opcodes are grouped into as follows
U
Mnemonic Operation
VT
.IN
These instructions affect 3 bits in PSW:
C = 1 if result of add is greater than FF
AC = 1 if there is a carry out of bit 3
C
OV = 1 if there is a carry out of bit 7, but not from bit 6, or vice versa.
N
SY
U
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 31
G.
Example 2-8
Write a program to find the sum of the values. At the end of the program, register A
should contain the low byte and R7 the high byte.
Solution:
MOV R0,#40H ;load pointer
MOV R2,#5 ;load counter
CLR A ;A=0
MOV R7,A ;clear R7
AGAIN: ADD A,@R0 ;add the byte ptr to by R0
JNC NEXT ;if CY=0 don’t add carry
INC R7 ;keep track of carry
NEXT: INC R0 ;increment pointer
DJNZ R2,AGAIN ;repeat until R2 is zero
.IN
ADDC and Addition of 16-Bit Numbers
Example 2-9
Write a program to add two 16-bit numbers. Place the sum in R7 and R6;
U
The binary representation of the digits 0 to 9 is called BCD (Binary Coded Decimal)
Unpacked BCD
In unpacked BCD, the lower 4 bits of the number represent the BCD number, and the rest
of the bits are 0
• Ex. 00001001 and 00000101 are unpacked BCD for 9 and 5
Packed BCD
In packed BCD, a single byte has two BCD number in it, one in the lower 4 bits, and one in
the upper 4 bits
• Ex. 0101 1001 is packed BCD for 59H
MOV A, #17H
ADD A, #28H
The result above should have been 17 + 28 = 45 (0100 0101).
To correct this problem, the programmer must add 6 (0110) to the
low digit: 3F + 06 = 45H.
DA Instruction
DA A ;decimal adjust for addition
The DA instruction is provided to correct the aforementioned problem associated with BCD
addition
The DA instruction will add 6 to the lower nibble or higher nibble if need
.IN
Example:
MOV A,#47H ;A=47H first BCD operand
MOV B,#25H ;B=25H second BCD operand
ADD A,B
C
;hex(binary) addition(A=6CH)
DA A ;adjust for BCD addition(A=72H)
N
The “DA” instruction works only on A.
In other word, while the source can be an operand of any addressing mode, the destination
SY
Summary of DA instruction
After an ADD or ADDC instruction
U
If the lower nibble (4 bits) is greater than 9, or if AC=1, add 0110 to the lower 4 bits
If the upper nibble is greater than 9, or if CY=1, add 0110 to the upper 4 bits
VT
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 33
G.
Example 2-10
Write a program to find the sum of all the numbers. The result must be in BCD.
Solution:
MOV R0,#40H ;Load pointer
MOV R2,#5 ;Load counter
CLR A ;A=0
MOV R7,A ;Clear R7
AGAIN: ADD A,@R0 ;add the byte pointer to by R0
DA A ;adjust for BCD
JNC NEXT ;if CY=0 don’t accumulate carry
INC R7 ;keep track of carries
NEXT: INC R0 ;increment pointer
.IN
DJNZ R2,AGAIN ;repeat until R2 is 0
To make SUB out of SUBB, we have to make CY=0 prior to the execution of the instruction
Notice that we use the CY flag for the borrow
SUBB when CY = 0
U
Solution:
4C 0100 1100 0100 1100
- 6E 0110 1110 1001 0010
-22 01101 1110
SUBB when CY = 1
This instruction is used for multi-byte numbers and will take care of the borrow of the lower operand
CLR C
MOV A,#62H ;A=62H
SUBB A,#96H ;62H-96H=CCH with CY=1
MOV R7,A ;save the result
MOV A,#27H ;A=27H
SUBB A,#12H ;27H-12H-1=14H
MOV R6,A ;save the result
Solution:
We have 2762H - 1296H = 14CCH.
.IN
Multiplication of Unsigned Numbers
The 8051 supports byte by byte multiplication only
The byte are assumed to be unsigned data
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 35
G.
Write a program to get hex data in the range of 00 – FFH from port 1 and convert it to
decimal. Save it in R7, R6 and R5.
Solution:
MOV A, #0FFH
MOV P1, A ;make P1 an input port
MOV A, P1 ;read data from P1
MOV B, #10 ;B=0A hex
DIV AB ;divide by 10
MOV R7, B ;save lower digit
MOV B, #10
DIV AB ;divide by 10 once more
MOV R6, B ;save the next digit
MOV R5, A ;save the last digit
.IN
Logical instructions
A large part of machine control concerns sensing the on-off states of external switches,
making decisions based on the switch states, and then turning external circuits on or off.
Sensing and control implies a need for byte and bit opcodes that operate on data using
Boolean operators.
C
All 8051 RAM areas, both data and SFRs, may be manipulated using byte opcodes.
N
Many of the SFRs, and a unique internal RAM area that is bit addressable, may be operated
upon at the individual bit level.
Bit operators are notably efficient when speed of response is needed.
SY
Bit operators yield compact program code that enhances program execution speed.
U
destination
• The destination is normally the accumulator
• The source operand can be a register, in memory, or immediate
.IN
C
N
SY
The XRL instruction can be used to clear the contents of a register by XORing it with
itself.
Show how XRL A, A clears A, assuming that AH = 45H.
45H 0100 0101
U
Example 2:12
Read and test P1 to see whether it has the value 45H. If it does, send 99H to P2;
otherwise, it stays cleared.
Solution: XRL can be used to
MOV P2,#00 ;clear P2
MOV P1,#0FFH ;make P1 an input port
MOV R3,#45H ;R3=45H
MOV A,P1 ;read P1
XRL A,R3
JNZ EXIT ;jump if A is not 0 If both registers have
MOV P2,#99H the same value, 00 is
EXIT: ... placed in A. JNZ and
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 37
G.
MOV A, #56H
CPL A ;now A=A9H
;0101 0110(56H)
;becomes 1010 1001(A9H)
To get the 2’s complement, all we have to do is to add 1 to the 1’s complement
.IN
The CJNE instruction compares two operands, and jumps if they are not equal
The destination operand can be in the accumulator or in one of the Rn registers
The source operand can be in a register, in memory, or immediate
• The operands themselves remain unchanged
C
It changes the CY flag to indicate if the destination operand is larger or smaller.
Notice in the CJNE instruction that any Rn register can be compared with an immediate value
N
There is no need for register A to be involved
The compare instruction is really a subtraction, except that the operands remain unchanged
Flags are changed according to the execution of the SUBB instruction.
SY
Example 2 – 13
Write a program to read the temperature and test it for the value 75.
U
the following.
If T = 75 then A = 75
VT
If T < 75 then R1 = T
If T > 75 then R2 = T
Solution:
MOV P1,#0FFH ;make P1 an input port
MOV A,P1 ;read P1 port
CJNE A,#75,OVER;jump if A is not 75
SJMP EXIT ;A=75, exit
OVER: JNC NEXT ;if CY=0 then A>75
MOV R1,A ;CY=1, A<75, save in R1
SJMP EXIT ; and exit
NEXT: MOV R2,A ;A>75, save it in R2
EXIT: ...
RR A ; rotate right A
In rotate right
The 8 bits of the accumulator are rotated right one bit, and
Bit D0 exits from the LSB and enters into MSB, D7
RL A ; rotate left A
In rotate left
The 8 bits of the accumulator are rotated left one bit, and
Bit D7 exits from the MSB and enters into LSB, D0
.IN
MOV A,#72H ;A = 0111 0010
RL A ;A = 1110 0100
RL A ;A = 1100 1001
Example 2- 14
Write a program that finds the number of 1s in a given byte.
MOV R1,#0
MOV R7,#8 ;count=08
MOV A,#97H
AGAIN: RLC A
JNC NEXT ;check for CY
INC R1 ;if CY=1 add to count
NEXT: DJNZ R7,AGAIN
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 39
G.
SWAP A
It swaps the lower nibble and the higher nibble
In other words, the lower 4 bits are put into the higher 4 bits and the higher 4 bits are put into
the lower 4 bits
SWAP works only on the accumulator (A)
Example 2- 15
(a) Find the contents of register A in the following code.
(b) In the absence of a SWAP instruction, how would you exchange the nibbles?
Write a simple program to show the process.
Solution:
(a)
MOV A,#72H ;A = 72H
SWAP A ;A = 27H
(b)
MOV A,#72H ;A = 0111 0010
.IN
RL A ;A = 1110 0100
RL A ;A = 1100 1001
RL A ;A = 1001 0011
RL A ;A = 0010 0111
C
N
packed BCD to two ASCII numbers and place them in R2 and R6.
Solution:
SY
RR A ;rotate right
RR A ;rotate right
RR A ;rotate right SWAP A
RR A ;rotate right
ORL A,#30H ;A=32H, ASCII char. ’2’
MOV R2,A ;save ASCII char in R2
Branch instructions
Repeating a sequence of instructions a certain number of times is called a loop.
Loop action is performed by DJNZ reg, Label
• The register is decremented by one
• If it is not zero, it jumps to the target address referred to by the label
• Prior to the start of loop the register is loaded with the counter for the number of
repetitions
• Counter can be R0 – R7 or RAM location
Example 2 – 17
Write a program to
.IN
(a) load the accumulator with the value 55H, and
(b) complement the ACC 700 times
MOV A,#55H ;A=55H
MOV R3,#10 ;R3=10, outer loop count
C
NEXT: MOV R2,#70 ;R2=70, inner loop count AGAIN:
CPL A ;complement A register
DJNZ R2,AGAIN ;repeat it 70 times
N
DJNZ R3,NEXT
SY
Conditional Jumps
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 41
G.
Example 2 – 18
Find the sum of the values 79H, F5H, E2H. Put the sum in registers R0 (low byte) and
R5 (high byte).
MOV A,#0 ;A=0
MOV R5,A ;clear R5
ADD A,#79H ;A=0+79H=79H
JNC N_1 ;if CY=0, add next number
INC R5 ;if CY=1, increment R5
N_1: ADD A,#0F5H ;A=79+F5=6E and CY=1
JNC N_2 ;jump if CY=0
INC R5 ;if CY=1,increment R5 (R5=1)
N_2: ADD A,#0E2H ;A=6E+E2=50 and CY=1
JNC OVER ;jump if CY=0
INC R5 ;if CY=1, increment 5
OVER: MOV R0,A ;now R0=50H, and R5=02
.IN
Example 2 – 19
Unconditional Jumps
The unconditional jump is a jump in which control is transferred unconditionally to the target
location.
Unconditional jumps do not test any bit or byte to determine whether the jump should be taken.
The jump is always taken.
.IN
C
N
SY
U
VT
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 43
G.
.IN
1. Write a program to add the values of locations 50H and 51H and store the result in locations
in 52h and 53H.
2. Write a program to store data FFH into RAM memory locations 50H to 58H using direct
U
addressing mode
VT
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.
.IN
END
4. Write a program to add two 16 bit numbers stored at locations 51H-52H and 55H-56H and
C
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
N
result is stored in high address.
SY
5. Write a program to store data FFH into RAM memory locations 50H to 58H using indirect
addressing mode.
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 45
G.
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.
.IN
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
C
;Clear RAM location pointed by DPTR
INC DPTR ;Increment DPTR
N
DJNZ R6, again ;Loop until counter R6=0
END
SY
8. Write a program to compute 1 + 2 + 3 + N (say N=15) and save the sum at70H
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.
10. 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,@R0
INC R0
DJNZ R5,Loop
.IN
DIV A B
MOV 55H,A
END
C
11. Write a program to find the cube of an 8 bit number program is as follows
N
ORG 0000H
MOV R1,#N
MOV A,R1
SY
MOV B,R1
MUL A B
MOV R2, B
MOV B, R1
U
MUL A B
MOV 50,A
VT
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
END
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 47
G.
12. Write a program to exchange the lower nibble of data present in external memory 6000H
and 6001H
.IN
13. Write a program to count the number of and o's of 8 bit data stored in location 6000H.
ORG 0000
C
; Set program counter 00008
MOV DPTR, #6000h ; Copy address 6000H to DPTR
N
MOVX A, @DPTR ; Copy number to A
MOV R0,#08 ; Copy 08 in R0
SY
END
14. Write a program to shift a 24 bit number stored at 57H-55H to the left logically four places.
Assume that the least significant byte of data is stored in lower address.
15. 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
.IN
TEMPI EQU 71H
MOV DPTR, #1000H ; Copy address 100011 to DPTR
MOVX A, @DPTR ; Copy First number to A
MOV TEMP, A ; Copy First number to temp INC DPTR
MOVX A, @DPTR
C
; Copy Second number to A
LOOPS: CJNE A, TEMP, LOOP1 ; (A) /= (TEMP) branch to LOOP1
N
AJMP LOOP2 ; (A) = (TEMP) branch to L00P2
LOOP1: JNC LOOP3 ; (A) > (TEMP) branch to LOOP3
SY
****************************************************************************************
ATMECE M Y S O R E | D E P T . O F E L E C T R O N I C S & C O M M U N I C A T I O N E N G 49
G.
.IN
C
N
SY
U
VT