Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ECD – 321

MICROCONTROLLER
&
EMBEDDED
SYSTEMS

LECTURE NOTES - 09

SUBMITTED BY :
Shivam Gulati -15476
Rishabh Gupta – 15477
OVERVIEW OF ADD INSTRUCTION
(To understand Rn, Direct, @Ri, #Data)

ADD A,Rn

Add the value present in register Rn with Accumulator content.

A = A + value of (Rn)

Ex. ADD A,R2

A= 03H + R2 = 05H

Result: 03H+05H=08H

A=08H R2 = 05H

ADD A,direct

Add the value present in memory with Accumulator content.

A = A + value present at memory address.


-- 67H
Ex. ADD A, 66H 07H 66H
-- 65H
A= 03H + Direct -- 64H
Result: 03H+07H=0AH

A= 0AH

ADD A,@Ri

Add the value present in memory where memory address is present in either R0 or R1 with Accumulator
content.
-- 64H
Ex. ADD A,@R0
07H 63H
A= 03H R0 = 62H 06H 62H
-- 61H
@R0

Result: 03H + 06H = 09H


A= 09H
ADD A,#data
A=05H
Add the immediate value with Accumulator content.
+
Ex. ADD A, #03H
03H
Result: 05H + 03H = 08H
A = 08H

-------------------------------------------------------------------------------------------------------------

ACALL and AJMP INSTRUCTION :-

Instructions OpCode Bytes Flags Instructions OpCode Bytes Flags


ACALL page0 0x11 2 None AJMP page0 0x01 2 None
ACALL page1 0x31 2 None AJMP page1 0x21 2 None
ACALL page2 0x51 2 None AJMP page2 0x41 2 None
ACALL page3 0x71 2 None AJMP page3 0x61 2 None
ACALL page4 0x91 2 None AJMP page4 0x81 2 None
ACALL page5 0xB1 2 None AJMP page5 0xA1 2 None
ACALL page6 0xD1 2 None AJMP page6 0xC1 2 None
ACALL page7 0xF1 2 None AJMP page7 0xE1 2 None

ACALL: ACALL unconditionally calls a subroutine at the indicated code address. ACALL pushes the
address of the instruction that follows ACALL onto the stack, least-significant-byte first, most-significant-
byte second. The Program Counter is then updated so that program execution continues at the
indicated address.

The new value for the Program Counter is calculated by replacing the least-significant-byte of the
Program Counter with the second byte of the ACALL instruction, and replacing bits 0-2 of the most-
significant-byte of the Program Counter with 3 bits that indicate the page. Bits 3-7 of the most-
significant-byte of the Program Counter remain unchaged.

Since only 11 bits of the Program Counter are affected by ACALL, calls may only be made to routines
located within the same 2k block as the first byte that follows ACALL.

AJMP: AJMP unconditionally jumps to the indicated code address. The new value for the Program
Counter is calculated by replacing the least-significant-byte of the Program Counter with the second byte
of the AJMP instruction, and replacing bits 0-2 of the most-significant-byte of the Program Counter with
3 bits that indicate the page of the byte following the AJMP instruction. Bits 3-7 of the most-significant-
byte of the Program Counter remain unchaged.

Since only 11 bits of the Program Counter are affected by AJMP, jumps may only be made to code
located within the same 2k block as the first byte that follows AJMP.

EXAMPLE: If the label THERE represents an instruction at address 0F46H and the instruction AJMP
THERE is in memory at locations 0900H and 0901H, the assembler will encode the instruction as

0900H AJMP THERE

09002 -- 0F46H THERE: --


00001 111 1st byte (opcode + A8 – A10 ) --
--

-- --
0100 0110 2nd byte (A7 - A0)
--

The underlined bits are the low-order 11 bits of the destination address,

0F46H = 00001 111 0100 0110 B. The upper five bits in the program counter will not change when this
instruction executes.

Note that both the AJMP instruction and the destination are within the 2K page bounded by 0800H and
0FFFH, and therefore have the upper five address bits in common.

 By default all the ports i.e, port 0, port 1, port 2, port 3 of the 8051 microcontroller are output ports.
 To make entire pins of ports as input pins write ‘1’ to all the pins like this:
MOV P0, #0FFH (to make all bits of port 0 as input port).
 To make individual pin of that port as input pin set individually the bits/pin.
SETB P0.1 (to make 1st bit of port 0 as input pin).

PROGRAM 1)
To continuously read number from Port 0 and cumulatively add it.

READ NO. PORT 0


Cumulative sum
PORT 1

Index PORT 2
Put the cumulative sum at port 1 and index (how many no’s added together) at port 2.

CODE:-

LABEL INSTRUCTION COMMENTS


CLR A To reset the accumulator
MOV R1,#00H To reset the R1
MOV P0,#0FFH To make the port 0 as input port
INFLOOP: MOV R0,P0 Move (read) the contents (number) from port 0 to
register 0
ADD A,R0 To add the no. obtain from port 0 with the contents of
accumulator
MOV P1,A out the value of cumulative sum obtained through port 1
INC R1 Increment the value of R1, this will keep track of index
MOV P2,R1 Output the value of R1 (index) through port 2
LJMP INFLOOP Long jump to label ‘INFLOOP’ to continue this process
infinite time

The use of this program is in Checksum. A checksum is a small-sized datum derived from a block
of digital data for the purpose of detecting errors which may have been introduced during
its transmission or storage. It is usually applied to an installation file after it is received from the
download server. By themselves, checksums are often used to verify data integrity but are not relied
upon to verify data authenticity.

PROGRAM 2)
continuously read number from Port 0 and cumulatively add only those values which are divisible by
3.

Put the cumulative sum at port 1 and index (how many no’s added together) at port 2.

CODE:-

LABEL INSTRUCTION COMMENTS


CLR A To reset the accumulator
MOV R1,#00H To reset the R1
MOV R2,#00H To reset the R2
MOV P0,#0FFH To make the port 0 as input port
INFLOOP: MOV R0,P0 Move (read) the content (number) from port 0 to register
0
MOV A,R0 Move the number from R0 to accumulator,(which will act
as dividend) to perform the operation of divisibility by 3
MOV B,#03H Move the divisor i.e, 3 in B register
DIV AB Perform the operation of division
Remainder will be obtain in B register, &
Quotient will obtain in Accumulator
MOV A,B Move the value of remainder from B register to
accumulator
JNZ INFLOOP If there is a zero continue the process (i.e, no. is divisible
by 3) continue the code
Else if there is non zero value in accumulator jump to
label ‘INFLOOP’ (i.e, no. is not divisible by 3) and hence
continue the process of reading data from port 0
MOV A,R1 Move the value of R1 to accumulator
ADD A,R0 Add the no. obtained from port 0 (no. is divisible by 3)
with the content of accumulator
MOV R1,A Move the content of accumulator to R1 (for cumulative
addition, for the next time)
MOV P1,A out the value of cumulative sum obtained through port 1
INC R2 Increment the value of R2, this will keep track of index
MOV P2,R2 Output the value of R2 (index) through port 2
LJMP INFLOOP Long jump to label ‘INFLOOP’ to continue this process
infinite time

You might also like