Module 2.1 MC
Module 2.1 MC
MODULE-2
2.1 INSTRUCTION SYNTAX.
General syntax for 8051 assembly language is as follows.
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
in which that address
instruction is stored. Unless that specific line of instruction is needed by a branching
instruction in the program, it is not necessary to label that line.
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 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 I/symbol. To improve the programn quality,
programmer may always use comments in the program.
2. Register addressing.
In this addressing mode the register will hold the data. One of the eight general registers
(ROto R7) can be used and specified as the operand.
Eg. MOV A,RO
ADD A,R6
RO - R7 will be selected from the current selection of register bank. The default register bank will be hank 0.
3. Direct addressing
There are two ways to access the internal memory. Using direct address and indirect address, Using
direct addressing mode we can 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
4. Indirect addressing
The indirect addressing mode uses a register to hold the actual address that will be used in data
movement. Registers RO 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 RO and R1 can hold8 bit address and DPTR can hold
16 bit address.
Eg. MOV A,@RO
ADD A,@R1
MOVX A,@DPTR
5. Indexed addressing.
In indexed addressing either the program counter (Pg, or the data pointer (DTPR)-s
used to hold the base address, and the Ais used to hold the offset address. Adding the value of the
base address to the value of the offset address forms the effective address. Indexed addressing is
used with JMP or MOvCinstructions, Look up tables are easily implemented with the help of index
addressing.
Eg. MOVC A, @A+DPTR I/ copies the contents of memory location pointed by the sum of the
accumulator A and the DPTR into accumulator A.
MOVC A, @A+PC 1/copíes the contents of memorylocation pointed bythe sum of the
accumulator Aand the program counter into accumulator A.
6. Relative Addressing.
Relative addressing is used only with conditional jump instructions. The relative address,
(offset), is an 8 bit signed number, which ís automatically added to the PC to make the address of
the next instruction. The 8 bít signed offset value gíves an address range of +127 to -128 locations.
The jump destination is usually specified using a label and the assembler calculates the jump offset
accordingly. The advantage of relative addressing ís that the programn code is easy to relocate and
the address ís relatíve to posítion in the memory.
Eg SJMP LOOP1
JC BACK
7. Absolute addressing
Absolute addressing is used only by the AJMP (Absolute Jump) and ACALL (Absolute Call)
instructions. These are 2 bytes ínstructions. The absolute addressing mode specifies the lowest 11
bit of the memory address as part of the instruction. The upper 5 bit of the destination address are
15
Microcontrollers 4 Sem ECE/TCE
8. Long Addressing
The long addressing mode is used with the instructions LJMP and LCALL. These are 3byte
instructions. The address specifiesa full 16 bit destination address so that ajump or acallcan be
made to a location within a 64 Kbyte code memory space.
Eg. LÊMP FINISH
LCALL DELAY
16
instructions.
transfer
Data
In Instructions
The 8051 2. Microcontrollers
this
instructions
instructions
Logical
5. 4. 3. 2. 1. MULAB
4. 3. 2.
6.Branch instructions
h. g. d. C b. a.
group, instructions
Subroutine Arithmetic Data
Bit instructions #2000H
DPTR,
MOV SUBB
Move Moveaddressing
indirectMoveaddressing MoveaddressingindirectMove indirect) Move Move Move manipulation
transfer A,
iii. i . R1,A
MOV i.
the
i . i.@R0,A MOVX
MOVXA,@R1
i . i. i . i. i . i. MOVA,i . 65H i. MOVA,
#45H i. R4,A MOV i. i.
MOV i. A,R7 #55H
the the the the the an the
the instructions of
@A+DPTR
MOVC MOVC
A, MOV MOV MOV MOV MOV MOV #51HMOVR6, MOVA,R2
contents contents contents 8051
contents contents immediate contents contents instructions
A, 45H, 47H, 45H, R3, A, 30H,
@RO instructions can
@A+PC 65H
@RO 65H R2 #44H
of of of of ofa of
of perform be
program an memory a 8 broadly
external memory memory bit register aregister a
data 4 2
data
location to classified
memory location location A Rn 2
memory registerA to transfer
to
Rn
A
to to under
to to operations
A to another Rn A or
A or to the
or or 4 2
A Rn following us us
A memory Rn to
to to a or of
an to the 2
a memory us
MOVX@DPTR,Aexternal
iv. iii. memory iv. iii. vi. a
location memory following headings.
A,@DPTR
MOVX MOV #0F5A2H
MOVDPTR,
MOV
#0E8H
@RO,
MOV
MOVlocation
memory location @R1, 45H, DPTR,
using location types.
A Ausing
#5467H
direct ECE/TCESem 4
using (direct
direct
and direct
and or
17
Microcontrollers
instructions
withExchange i.
Exchange
digit.k. contentThe of j.
4 PUSH
POP 4POP 1POP 6 PUSH PUSH 1 6#OF3H
MOVR4, #12H MOVR6,
MOVR1, #25H Push
order the
A,54h
XCHii. . DPTR +
A RO
R1
or A
A,@RO
XCHDA,@R1
i XCHD
. i. contents
A,XCHR3 i. and PC
+
A DPTR Register
níbble XCHA,@R1
Pop
of
source
of instructions MOVX@RI A, Direct
the
Exchange FIG. A,
MOVORI A,
Instructions
MOVAddressing
destination [01]=[09]=12H
[04]=[08]=25H |SP]l=08 (SP]=09
[SP]=08[R4]=F3H[R6]=25H
[R1]=12H (SP]=07
internal , (SP]l=07 (SPl=09 [SP]=0A
ie.[06]=[0A]=F3H
register, Addressing MOVX A,
MOVC MOVC @DPTR
the
RAM ie., A. A,
lower @A+ Internal
@A+ RAM
location accumulator.direct [08]=|06]=25H
[0A]=[04]-F3H
[09]=[01]=121 //CONTENT
//CONTENT
//CONTENT//CONTENT
OF //CONTENT Using
order PCDPTR
memory MOV,
whichnibble
OFOFOFOF MOVX
or
//CONTENT
//CONTENT
//CONTENT //CONTENT
//CONTENT R4 R1R6SP External
is indirect RAM
1S IS IS IS and
indirectly of F3H 12H 25H 07
Accumulator
(DEEAULT MOVC
mernory OFOFOF OF OF
addressed 04 01 06 0A 12H09
IS08
(A0-A3) IS IS IS IS TS VALUE) External Internal
25H 12H F3H F3H 25H1 ROM and
wil
by be
the with exchanged ECE/TCESem 4
register.lower
18
Dívision Eg
register.
Multiplication
byteAfter MUL Subtraction Addition instructions.
Arithmetic
Innumbers.8051TheMicrocontrollers
CYACand In CYAC
iii. i. this iii. this
of AB. i. i. i.
MUL AB
B,#OF5HA,#45H
MOV iv. addressing
ii. indirect and
MOV result i . Subtract
i. Subtract
i . i. Subtract
i . i.group, iv. addressing
ii. indirect
i . i. Add i. i.Add i . i.group, Add
This OVflags SUBBSUBBSUBB SUBB SUBB SUBB SUBBSUBB A, ADD ADDC ADD ADD can
OVflags ADDC @R1ADDADDC75H A, the #0B4H
thADDC
e A, the
will
multiplication we we
instruction A,
contents contents
A, contents A, perform
be A, A, A, A, the A, A,the A, A,the have A, A, have
@RO @R1 75H 51H R2 R5 #45H 51H RS #45H
will contents contents#0B4Hcontents
will @RO R2
stored
instructions instructions
be be of of addition,
affected of A A
i1A]=09H, iA]
[AJ=45H
[B|=42Hi(BJ=F5H multiplies
in affected
the
B of of of A with with
x A A A with
(B]register.lower with with
with to registerimmediate to
= by by subtraction.
45 contents
x byte two this contents registerimmediate operation.
this
F5=
8 Rn
4209 ofbit operation. with data
the Rn of
unsigned of data memory
with or with Multiplication
result memory
with without
or or
will without with without
numbers with or
be without carry.
or and
stored or carry. carry.
without without
which division
carry.
in
accumulator are carry
carry
operations
stored
using using
ECE/TCESem 4
and in direct direct
and
A on
higher and
and 8
19 bit
B
Ins
Microcontrollers
DIV
Eg.
unsigned
DA accumulator
LogicalOR Logical
AND Logical Decrement: Increment:
A AB.
(Decimal A,#45H
B,#OFSHMOV MOV
DIVAB
destination,
source:
ORL destination,
source:
ANL leaving
ANL ANLlogically ANL instructions DEC A initial In will INC INC A Eg
2: Eg
1: useWhen
itDEC
to number This
the DA
A,DIRECT
A,#DATA
DIRECT,A decrements
reset decrements cause
increments increments and
value case A,#53H
R1#58H MOV
MOVR1, DA
A,ADDA
R1 MOV MOV A
two Adjust instruction
DA
A,ADDA If If
instruction
AND the to it upper lower BCD remainder
of of to A,#23H
#55H
R1, which
resulting FFh.The
ANL ANL ANL
the DEC DPTR "INC reset After
the INCRn nibblenibblenumbers
bits the the
A, Rn
the DPTR", divides
DIRECT,A,@RÍ is tovalue operand afterAddition). is
of
Rnvalue Carryvalue operand
DEC FFFFh, 0. is is :IA]/
[A]B]=1BH
[A]=0E8H wil
destination.
sourceand The of [A]=ABh// 1/ (A]=78[A]=78
1/ 1/ greater the are
greater stored
of the [A]=11, =[B] be
#DATA in Flagsource DIRECT source added,
addition. 08H, the
incrementingvalue Carry DIRECTINC by = stored
ORL destination. ANL is by C=1. than than E8/1B in
not one. [B]=10H
does by one. Flag by the 8
does two-byte ANSWER
set 1. 1. no
9 9 DA in Bbit
= register.
is If or or
answer 08 unsigned B
a a when If changesthein auxiliary
bitwise bitwise
The the the not the carry workS A register.
H
@Ri
DEC unsigned set
initial IS with
thinitial
e value 111. is is
value remainder
"OR" "AND" value when 1,
carry as a After
value will value @RiINCDPTRINC
Accumulator 6non-BCD
follows. number
in the accumulator is
operation integer
operation
source "rolls cause of
added is division
of value 1,
register 1OH
is 6
number. which
over" 0, value
it "rolls to is
between is to data upperadded
decrementing after
not
between from reset the
of over" is is is
affected. DPTR FFh, changed da nibble. to To resultstored
0 to get
source to 0. a lower
source incrementing from
FFh. is the
"AND" the incremented. 255 after wil in
and nibble. result
and value A
destination, to DA bebyECE/TCESem 4
destination,
instruction 0. A in
will the BCD, stored the
cause If value
20 the we 8
inbit
4 Sem ECE/TCE
Microcontrollers
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
Logical NOT
CPL complements operand, leaving the result in operand. If operand is a single bit then the state of
the bit willbe reversed. If operand is the Accumulator then allthe bits in the Accumulator will be
reversed.
RRA
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.
765 4 3 2 1 0
ACC
RLA
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit 0
765 4 3 2 1
ACC
RRCA
Rotate right through the carry. Each bit is shifted one location to the right, with bit Ogoing into the carry bit in
thePSW, while the carry was at goes into bit 7
C765 43 210
ACC
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.
C 765 4 3 2 1 0
ACC
21
Microcontrollers 4 Sem ECE/TCE
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. Schematically, the relative jump can be shown as follows:
128
Relatlve
Jump Next Jump instruction
range Instruction KXX X
127
22
4Sem ECE/TCE
Microcontrollers
each. The
space is divided into 32 pages of 2 kbyte
menory
o0S 64 kbyte of program
hexadecimal addresses of the pages are given as follows:
00 0000- 07FF
01 0800 - 0FFF
02 1000 - 17FF
03 1800 - 1FFF
1E FO00 - F7FF
1F F800- FFFF
Example: -
LCALL <address 16>
LJMP <address 16>
JMP @A+DPTR
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.
23
4SemHCE/TCE
Microcontrollers
24
Microcontrollers 4 Sem ECE/TCE
f. [PC1oo]= 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.
B. [PC1sa] = [[SP]] ;content of current top of the stack will be moved to higher byte of PC.
h. [SP]=[SP]-1; (SP decrements)
1. [PC7-0] = [SP]l :content of bottom of the stack will be moved to lower
/. [SP]=[SP]-1; (SP decrements again) byte of PC.
25
$1 inshrchon Synt
Emha.
Module-9
Microcontrollers
4 Sem ECE/TCE
UNIT 3
3.1 ASSEMBLER DIRECTIVES.
EQUand SET
EQUand SET directives assign numerical value or
name. register name to the specified symbol
EQUis used to define a constant without storing
defined with EQU should not be redefined. information in the memory. The symbol
SET directive allows redefinition of
symbols at a later stage.
DB (DEFINE BYTE)
The DB directive is used to define an 8 bit data.
DB directive initializes memory with 8
values. The numbers can be in decimal, binary, hex bit
after the decimal number is optional, but for or in ASCII formats. For decimal, the 'D'
requíred. For ASCII, the number is written in binary and hexadecimal, 'B' and "H are
quotation marks (LIKE This).
DATA1: DB 40H ;hex
DATA2: DB 01011100B
DATA3: DB 48 ;binary
;decimal
DATA4: DB 'HELLOW" ; ASCII
END
1. Write a program to add the values of lo cations 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
addressing mode
ORG 0000H ;Set program counter 0000H
MOV A, #OFFH ; Load FFH into A
MOV 50H, A Store contents of Ain location 50H
MOV 51H, A ;Store contents of A in location 5IH
MOV 52H, A ;Store contents of Ain location 52H
MOV53H, 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 lo cation 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
27
Microcontrollers 4Sem ECE/TCE
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.
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
MOVA, #0FFH ;Load FFH into A
MOV RO, #50H ;Load pointer, R0-50H
MOV R5, #08H ;Load counter, R5-08H
Start:MOV @R0, A :Copy contents of A to RAM pointed by RO
INCR0 ;Increment pointer
DJNZ R5, start ;Repeat until R5 is zero
END
6. Write a program to add two Binary Coded Decimal (BCD) numbers stored at locations 60 H
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.
4 Sem ECE/TCE
Microcontrollers
29
Microcontrollers 4Sem ECE/TCE
DÊNZ RS,Loop
DIV AB
MOV 55H,A END
12. Write a program to find the cube of an 8 bit number program is as follows
ORG 0000H
MOV R1,#N
MOV A,RI
MOV B,RI
MUL AB //SQUARE IS COMPUTED
MOV R2, B
MOV B, RI
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
MOVA, # 00H
ADDC A, 52H
MOV 52H, A //CUBEIS STORED IN 52H,51H,50H
END
13. Write a program to exchange the lower nibble of data present in external memory 6000H and
6001H
14. 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 ;Copy number to A
MOV RO, #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 carry fla8
30
Microcontrollers 4 Sem ECE/TCE
15. 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.
ORG 0000H ;Set program counter 0000h
MOV R1,#04 ;Set up loop count to 4
again: MOV A,55H ;Place the least significant byte of data in A
CLRC ;Clear tne carry flag
RLC A ;Rotate contents of A(55h) left through
MOV 55H,A carry
MOV A,56H
RLC A ;Rotate contents of A(56H) left
MOV 56H,A through carry
MOVA,57H
RLC A ; Rotate contents of A
MOV 57H,A (57H) left through carry
DJNZ R1,again ; Repeat until R1 is
END zero
16. Two 8 bit numbers are
stored in location 1000h and
Write a program to find the GCD of 1001h of
the nunbers and store the external data memory.
ALGORITHM result in 2000h.
Step 1 :Initialize external data
Step 2 :Load Aand TEMP with the memory with data and DPTR with address
Step 3 :Are the two operands operands
Step 4 :Is (A) greater equal?Ifyes, go to step 9
than (TEMP) ?Ifyes, go to step
Step 5
Step 6 :Exchange
(A) with (TEMP) such that A
:Perform contains the bigger
Step 7 :If the division operation (contents of Awith contents of number
remainder
Step 8 :Move the
is zero, go to step 9 TEMP)
Step 9 :Save the remainder into Aand go to step 4
ORG 0000H contents ;TEMP in memory and terminate the program
'of
Set program counter
TEMP EQU 70H 0000H
TEMPI EQU71H
MOV DPTR, #1000H ;Copy
MOVX A,@DPTR address 100011 toDPTR
;Copy First
MOV TEMP, A ;Copy First number to A
MOVX A, @DPTR number to temp INC
LOOPS:CjNE A, TEMP,LOOP1 :(A) /= ;Copy Second number to A DPTR
AJMP LOOP2 (TEMP) branch to LOOP1
LOOP1: JNC LOOP3 :(A)= (TEMP) branch to L00P2
NOV TEMPI, A :(A)>
MOV A, TEMP :(A)<(TEMP) (TEMP) branch to LOOP3
LOOP3: MOV TEMP, TEMPI exchange (A) with (TEMP)
MOV B, TEMP
DIV AB
MOV A, B ;Divide (A) by
CJNE A,#00, LOOPS ; Move (TEMP)
LOOP2:
MOV A,TEMP :(A)/=00 remainder to A
MOV DPTR, #2000H
branch LOOPS
to
MOVX @DPTR, A
END ;Store the result in 2000H
31