You are on page 1of 29

Instruction set and Programming

 When doing data transfer, data stored at source address should be moved to
destination address. The ways by which these addresses are specified are called
addressing modes.

 Data transfer from source to destinations may be divided into three main types:
i. MOV destination, source
ii. PUSH source or POP destination
iii. XCH destination, source

 The following four addressing modes are used to access data:


i. Immediate addressing mode
ii. Direct addressing mode
iii. Register Direct addressing mode
iv. Register Indirect addressing mode

 Memory is divided into the following four distinct physical parts:


i. Internal Special function registers
ii. Internal RAM
iii. External RAM
iv. Internal and External ROM
Immediate Addressing Mode:
 The simplest way to get data to a destination is to make the source data
available as part of the instruction/ opcode.
 The mnemonic for immediate data is the pound sign (#).
 Opcode 74H is saved at
0202 address.
 The data 6AH is saved at
0203 address in the program
memory.
 After reading the opcode
74H, the data at the next
program memory address is
transferred to accumulator A
(E0H is the address of
accumulator).
 Since the instruction is of 2-
bytes and is executed in one
cycle, the program counter
will be incremented by 2 and
will point to 0204 of the
program memory.
Direct Addressing Mode:
 Here, the address of the source data is given as an operand. Eg. MOV A, 04H
 Here 04H is the address of register 4 of register bank#0.
 When this instruction is executed, what ever data is stored in register 04H is moved
to accumulator.
 In the picture below we can see,
register 04H holds the data 1FH.
So the data 1FH is moved to
accumulator.
 This is a 2 byte instruction which
requires 1 cycle to complete.
 The opcode for instruction MOV
A, address is E5H.
 Then program control goes to
next address that is 0203 and look
up the address of the location
(04H) where the source data is
located.
 At 04H the control finds the data
1F and transfers it to accumulator
and hence the execution is
completed.
 Program counter will increment
by 2 and stand in 0204.
1. Place the number 3Bh in internal RAM location 30h to 32h
Method 1
MNEMONIC COMMENT

MOV 30h,#3Bh ; Move the data 3Bh in location 30h


MOV 31h,#3Bh ; Move the data 3Bh in location 31h
MOV 32h,#3Bh ; Move the data 3Bh in location 32h

Method 2

MNEMONIC COMMENT

MOV A,#3Bh ; Move the data 3Bh in the accumulator


MOV 30h,A ; Transfer accumulator data 3Bh in
location 30h
MOV 31h,A ; Transfer accumulator data 3Bh in
location 31h
MOV 32h,A ; Transfer accumulator data 3Bh in
location 32h
2. Copy the data at internal RAM location 70h to R0 and R3. (Assume bank 0)

Method 1
MNEMONIC COMMENT

MOV 00h,70h ; Move contents of location 70h to


register R0 (00h)
MOV 03h,70h ; Move contents of location 70h to
register R3 (03h)

Method 2

MNEMONIC COMMENT

MOV D0h,#00h ; Select bank 0 by putting 00h in PSW


(D0h)
MOV R0,70h ; Move contents of location 70h to
register R0 (bank 0)
MOV R3,70h ; Move contents of location 70h to
register R3 (bank 0)
Register Direct Addressing Mode:
 In this addressing mode we use the register name directly (as source operand).

 Eg. MOV A, R4

 Registers can take value from R0,R1…to R7.

 There are 4 register banks named 0,1,2 and 3. Each bank has 8 registers named
from R0 to R7, so a total of 32 registers.
 Register banks are selected
by the RS0 and RS1 bits of
the Special Function
Register (SFR) named
Processor Status Word
(PSW).
 So in Register addressing
mode, data is transferred to
accumulator from the
register (based on which
register bank is selected).
Register Direct Addressing Mode:
 Eg. MOV A, R4
 This opcode is stored
 The opcode for MOV A, R4 is EC in program memory
address 0202 and
when it is executed
the control goes
directly to R4 of the
respected register
bank (selected in
PSW)
 Data from R4 (2F
stored at 04H) of
register bank #0 is
then moved to
accumulator.
 Program counter will
increment by 1 and
stand in 0203, since it
is 1 byte instruction,
executed in 1 cycle.
MOV A, #17h  Moves data 17 into Accumulator

MOV A, 17h  Moves data from memory location 17


into Accumulator
4. Exchange the contents of the SP and the PSW
Method 1
MNEMONIC COMMENT

MOV A, 81h ; Save contents of SP in the accumulator


temporarily.
MOV 81h, D0h ; Move contents of PSW (D0h) into the SP
(81h)
MOV D0h, A ; Gets the original contents of the SP which
are stored in the accumulator to the PSW
(D0h).

Method 2
MNEMONIC COMMENT

MOV A, 81h ; Save contents of SP in the accumulator coz


exchange can be used only with accumulator and other
register or memory location.
XCH A,D0h ; exchange the contents of the accumulator (which is
having the contents of SP) with that of the PSW (D0h).
MOV 81h, A ; PSW contents that have come in the accumulator
after exchange, are transferred to the SP (81h).
Register indirect Addressing Mode:
 In this addressing mode, address of the data (source data to transfer) is given in
the register operand. Eg. MOV A, @R0
 Here the value inside R0 is considered as an address, which holds the data to be
transferred to accumulator.
 If R0 holds the value 20H, and we have a data 2F H stored at the address 20H,
then the value 2FH will get transferred to accumulator after executing this
instruction.
 The opcode for MOV A,
@R0 is E6H. Assuming that
register bank #0 is selected.
So the R0 of register bank #0
holds the data 20H. Program
control moves to 20H where
it locates the data 2FH and it
transfers 2FH to accumulator.

 This is a single byte


instruction and the program
counter increments 1 and
moves to 0203 of program
memory.
5. Copy the byte at internal RAM address 27h to external RAM address 27h
Method 1
MNEMONIC COMMENT

MOV A, 27h ;Move the contents of internal RAM


location 27h into the accumulator.
MOV R0,#27h ;Load R0 with 27h, so that you can make
use of it for indirect addressing.
MOVX @R0,A ;Move the contents of the accumulator now
into the external location pointed by the
register R0 (27h)

Method 2
MNEMONIC COMMENT

MOV A, 27h ;Move the contents of internal RAM


location 27h into the accumulator.
MOV DPTR,#0027h ;Point the DPTR to the external RAM
location 27h.
MOVX @DPTR,A ;Move the contents of the accumulator now
into the external location pointed by the
DPTR (27h)
 Here R0 and R1 and DPTR can be used to hold the address of the data byte in
external RAM.
 R0 and R1 are limited to external RAM address ranges of 00h to 0FFh, while
DPTR register can address a maximum RAM space of 0000h to 0FFFFh.
 An X is added to the MOV mnemonics to serve as a reminder that the data
move is external to 8051.
 A letter C is added to
the MOV mnemonic to
highlight the use of the
opcode for moving data
from the source address
in the code ROM to the
A register..
 Eg. MOVC A, @A+DPTR and MOVC A, @A+PC
where DPTR is data pointer and PC is program counter (both are 16 bit registers).
 Contents of DPTR are added with present content of accumulator A.
 This addition will result a new data which is taken as the address of source data (to
transfer). The data at this address is then transferred to accumulator.
 DPTR holds the value 01FE, where 01 is located in DPH (higher 8 bits) and FE is
located in DPL (lower 8 bits).
 Accumulator now has the
value 02H.
 A 16 bit addition is
performed and now 01FE
H+02 H results in 0200H.
 What ever data is in 0200 H
will get transferred to
accumulator.
 The other Eg. MOVC A,
@A+PC works the same
way. The only difference is,
instead of adding DPTR
with accumulator, here data
inside program counter
(PC) is added with
accumulator to obtain the
target address.
13. Copy the external code byte at address 007Dh to the SP.

Method 1
MNEMONIC COMMENT

MOV DPTR,#007Ch ; Load address 007Ch in the data pointer.


MOV A,#01h ; Load accumulator with 01h
MOVC A,@A+DPTR ; [A+DPTR = 01+007C = 007D]; Code byte
from external memory 007Dh are moved to the
accumulator
MOV 81h,A ; Contents of the accumulator moved to SP
(81h).
 PUSH opcode copies data from the source address to stack.
 POP opcode copies data from the stack to the destination address.
 The stack grows up in memory as it is PUSHed.
 Excessive PUSHing can make the stack exceed 7Fh, after which point data is lost.
 MOV, PUSH and POP all involve copying the data from the source to the
destination address, the original data in the source is not changed.
 Exchange instruction actually move data in two directions, form source to
destination and destination to source.
 All exchanges use register A.
 When using XCHD, the upper nibble of A and the upper nibble of the address
location in Rp do not change.
 Copy the byte from TCON (88h) to register R2 (02h) using at least four diff. methods.
1. Method 1: Use the direct address for TCON (88h)

2. Method 2: Use the direct address for R2 (02h)

3. Method 3: Use R1 as a pointer to R2 and use the address for TCON

4. Method 4: Push the contents of TCON into direct address 02h (R2)
 Set the timer T0 to an initial setting of 1234h. Use the direct address with an
immediate number to set TH0 and TL0.

 Put the number 34h in register R5, R6 and R7.


1. Method 1: Use an immediate number and register addressing.
 Put the number 34h in register R5, R6 and R7.
2. Method 2: Since the number is the same for each register, put the number in A
and MOV A to each register.

3. Method 3: Copy one direct address to another


 Put the number 8D in Ram locations 30h to 34h
1. Method 1: Use an immediate number to a direct address.

2. Method 2: Using the immediate number in each instruction uses bytes; use a
register to hold the number.
 Method 3: There must be a way to avoid naming each address, the PUSH
opcode can increment to each address.

You might also like