Beruflich Dokumente
Kultur Dokumente
Programming
Addressing Modes
Mnemonic
Operation
MOV A,
A = <src>
<src>
MOV <dest>,
<dest> = A
A
MOV <dest>,
<dest> = <src>
<src>
Direct
Indirect
Register
Immediate
PUSH <src>
INC SP:
MOV @SP, <scr>
POP <dest>
XCH A,
<byte>
XCHD A,
@Ri
MOVX A,
@Ri
MOVC A,
Read Program Memory at
@A + DPTR (A + DPTR)
MOVC A,
@A + PC
Addressing Modes
Mnemonic
Operation
ADD A,
<byte>
ADDC
A,<byte>
SUBB A,
<byte>
A= A+
<byte>
A= A+
<byte>+C
A= A
<byte>C
INC A
A= A+ 1
Accumulator Only
DEC A
A= A- 1
<byte> =
<byte> 1
B:A= B A
Accumulator Only
DEC <byte>
MUL AB
Direct
Indirect
Register
Immediate
Accumulator Only
DIV AB
A = Int [A/B]
B = Mod
[A/B]
Accumulator Only
DA A
Decimal
Adjust
Accumulator
Accumulator Only
Mnemonic
ANL A,
<byte>
Operation
A = A AND
<byte>
Direct
<byte> =
ANL <byte>,
<byte> AND
A
A
<byte> =
ANL <byte>,
<byte> AND
# data
# data
ORL A,
<byte>
A = A OR
<byte>
<byte> =
ORL <byte>,
<byte> OR #
# data
data
Addressing Modes
Indirect
Register
Immediate
XRL A,
<byte>
A = A XOR
<byte>
<byte> =
XRL <byte>,
<byte> XOR
A
A
<byte> =
XRL <byte>,
<byte> XOR
# data
# data
CLR A
A = 00H
Accumulator only
CLP A
A = NOT A Accumulator only
Rotate ACC
RL A
Accumulator only
Left 1 bit
RLC A
Rotate Left
through
Carry
RR A
Rotate ACC
Accumulator only
Right 1 bit
RRC A
Rotate Right
through
Accumulator only
Carry
Accumulator only
Mnemoni
Operation
c
CJNE
A,<byte>
,rel
CJNE
<byte>,#
data,rel
JZ rel
JNZ rel
Jump if A
=
<byte>
Jump if
<byte> =
#data
Jump if A
=0
Jump if A
0
Addressing Modes
Direct
Indirect
Register
Immediat
e
Accumulator only
Accumulator only
For example,
8800: SJMP 06h
This instruction shift the execution to the
location 8808h. The program counter content
after fetching the 2 byte - SJMP instruction is
8802h. So, 06h added to 8802H results in
8808h.
The syntax for LJMP -LJMP 16-bit address.
After the execution of this instruction the
Program counter -loaded with the 16 bit address
and the execution shifts to that location.
The syntax for AJMP instruction is AJMP 11 bit
jump address.
Mnemonic
ANL C,bit
ANL C,/bit
ORL C,bit
ORL C,/bit
MOV C,bit
MOV bit,C
CLR C
CLR bit
SETB C
SETB bit
CPL C
CPL bit
JC rel
JNC rel
JB bit,rel
JNB bit,rel
JBC bit,rel
Operation
C = C AND bit
C = C AND (NOT bit)
C = C OR bit
C = C OR (NOT bit)
C = bit
bit = C
C=0
bit = 0
C=1
bit = 1
C = NOT C
bit = NOT bit
Jump if C = 1
Jump if C = 0
Jump if bit = 1
Jump if bit = 0
Jump if bit = 1 ; CLR bit
Instruction
ADD
ADDC
SUBB
MUL
DIV
DA
RRC
RLC
SETB C
CLR C
CPL C
0
0
1
0
ANL C,bit
ANL C,/bit
ORL C,bit
ORL C,/bit
MOV C,bit
CJNE
Flags Affected
OV
AC
Example 1
Program to fill a block of memory in internal
RAM with a specific data
Program:
START:
MOV R1, #COUNT ;load number count
MOV RO, #30
;load starting address of memory
LOOP:
MOV @R0, #DATA ;load data to memory
location pointed by R0
INC R0
;Point to next memory location
DJNZ R1, LOOP
;Check for count and loop
Example 2
Program to add three 8-bit numbers
The following program is developed assuming
that the numbers are in memory locations 30h,
31h and 32h of the internal data RAM and the
result is stored in memory locations in 50h and
51h of the internal RAM.
Algorithm:
1.The first byte is moved to the accumulator and
the second byte is added with it.
2.If carry flag is set, register R1 is incremented.
3.The third byte is added with the intermediate
result.
4.If carry flag is set, register R1 is again
incremented
5.The accumulator forms the least significant
byte of the result and register R1 forms the
most significant byte of the result.
Program:
START:
MOV R1, #00h
;Set a register for MSB for result
MOV R0, #30h
;Set starting address for memory
location
MOV A, @R0
;Get a data
INC R0
;Point to next memory location
ADD A, @R0
;Add the data
JNC L1
;check for carry
INC R1
; If carry is present, increment MSB of
result
L1:
INC R0
;Point to next memory location
ADD A, @R0
;Add the third data
JNC L2
:Check for carry
INC R1
; If carry is present, increment
MSB of result
L2:
MOV 50h,A
; Save the result
MOV 51h,R1
Example 3
Program to add two BCD numbers
The following program is developed assuming
that the BCD numbers are in memory locations
30h, and 31h of the internal data RAM and the
result is stored in memory locations in 50h and
51h of the internal RAM with the lower order
sum in 50h and carry if any in 51h.
Algorithm:
The first byte is moved to the accumulator and
is added with the second byte.
The accumulator is now decimal adjusted.
The value 00h is moved to the accumulator and
is added with carry.
The result is stored in the memory locations
50h and 51h.
Program:
MOV A, 30h
ADD A, 31h
DAA
MOV 50h,A
MOV A, #00h
ADDC A, #00h
register
MOV 51h,A
;Get a data
;Add the second data
;Decimal adjust accumulator
;Save the sum
;
;Get the MSB of sum to A
;Save that
Example 4
Program to add two 16 bit data
In this example, the data are assumed to be
initially stored in the external memory
locations. First data is stored in locations 4000H
and 4001H while the second data is stored in
locations 4002H and 4003H.
Program:
MOV DPTR, #4000H
;Point to first data
MOVX A,@DPTR
;
MOV R0,A
;Get the LSB of first data to R0
INC DPTR
;Point to MSB of first data
MOVX A, @DPTR
MOV R1,A
;Get the MSB of first data to R1
INC DPTR
MOVX A,@DPTR
;Get the LSB of second data
ADD A, R0
; Add the LSB of two data
MOV R0,A
; Store the sum to the R0 register
INC DPTR
MOVX A,@DPTR
;Get the MSB of second data
ADDC A,R1
;Add the MSBs of data along with
carry out of previous addition
MOV R1,A
;Store the MSB of sum to R1 register
The sum is stored in the R0 and R1 registers at the end of the execution of above
program.
Example 5
Program to shift a 4-digit BCD number to left by 1
digit. Assume that the data is stored in 30h and 31h.
Algorithm:
The value 00h is stored in the memory location 35h.
The least significant two digits (byte) are moved to
the accumulator.
The nibbles of the accumulator are reversed and
then the least significant nibble is exchanged with
the value stored in the memory location 35h.
The result is stored in the memory location 50h.
The same process is repeated for the next byte and
the result is stored in the memory locations 51h and
52h.
Program:
data
the
data
XCHD A, @R0 ;Move the tens digit to memory location
MOV 50h,A
;A reg has unit digit of BCD data shifted to left
nibble with LSB as 0
MOV A, 31h
;Higher order data is brought to A reg
SWAP A
;Exchange lower and higher order nibbles
XCHD A, @R0 ;Move the thousandth digit to memory and
tens digit to A register
MOV 51h,A
;Save the shifted data to memory
MOV 52h,@R0 ;Save the thousandth digit
Example 6
Program to read a byte from port 0 and
depending upon which bit is set, jump to one of
the 8 different locations.
Algorithm:
First the byte is moved from the port0 to one of
the bit addressable bytes (i.e., within 20h-27h
of the SFR)
Then depending upon which bit is set, control
should be transferred to one of the eight
different locations.
Control returns to the first instruction LOOP
after executing the control block for each bit for
proper operation.
Program:
LOOP:
MOV 20h, PORT0
;Get the data from port 0
JB 00,L1
; Check the LSB using the bit address and
if set jump to relative address L1
JB 01,L2
JB 02,L3
JB 03,L4
JB 04,L5
JB 05,L6
JB 06,L7
JB 07,L8
LJMP LOOP
In the above program L1 toL8 are the 8 bit relative addresses to which
the branching has to take place. The 8 bit relative address is
assumed to be a 2s complement number and branching takes place
above or below the main program.
Example 7
Program to reverse the bits within a byte.
Algorithm:
Assume that the byte to be reversed is stored in register
R0.
Initialize register R1 with 00h and register R2 with 08h.
The byte from register R0 should be loaded into the
accumulator.
The accumulator should be shifted left through carry and
has to be exchanged with register R1. Now, the LSB of the
data is moved to Carry and the shifted data is moved to R1
The accumulator should be shifted right through carry and
has to be exchanged again with register R1. Now the LSB in
the carry is shifted into R1 register. After subsequent shifts,
it is moved into MSB of R1.
Decrement the value stored in register R2 and jump to step
5 if not zero.
Program:
MOV R0, #data
MOV R1, #00h
MOV R2, #08h
MOV A, R0
LOOP:
RLC A
to Carry
XCH A, R1
RRC A
order into R1
XCH A, R1
DJNZ R2, LOOP
repeat.
;Initialise counter
;Get the data
;Bring one bit in
;Move this bit in reversed
; Check for 8 bits and if not,
Example 8
Program to find the biggest number in a block of
data from the memory location 70h to 7Fh.
Algorithm:
Initialise a memory pointer to point to starting point
of the memory location.
Initialise a counter for number of data
The first byte stored in the block is assumed to be
the biggest number and stored in R1 register
The next data is compared with the biggest in R1
I f the data in R1 is smaller, and then the data in the
block is stored as big in the R1 register.
Above process is repeated for all the data in the
block.
Program:
LOOP:
register
NEXT:
MOV R1,#00h
MOV R0,#70h
MOV 30h,@R0
MOV R1,30h
INC R0
MOV A,R1
SUBB A,@R0
JNC NEXT
MOV 30h,@R0 ; If bigger, bring the biggest to R1 register
MOV R1, 30h
CJNE R0, #80h, LOOP;Repeat the above step for all the data.