Instructions
Instructions
Instructions
- ORG (origin)
indicates the beginning address of source program.
The number that comes after ORG can be either hex or decimal.
Eg. 1. ORG 0250
2. ORG 0100H; tells assembler that source program starts at the
address 100H in the program memory.
- END
indicates to the assembler the end of the source assembly instructions.
SET directive:
Similar to the EQU directive, the SET directive is used to assign a value
or implicit operand to a user defined symbol. The difference however, is
that with the EQU directive, a symbol can only be defined once.
POINTER SET R0 ;Symbol equated to register 0
POINTER SET R1 ;POINTER redefined to register 1
COUNTER SET 10 ;Symbol initialized to 10
DB (define byte)
used to define 8-bit data and store them in assigned memory locations.
Define data can be in decimal, binary, hex, or ASCII formats. Can be
used to implement lookup table technique.
DATA :
DATEA1 :
The BIT Directive assigns an internal bit memory direct address to the
symbol.
CF BIT 0D7H
OFF_FLAG BIT 6
Addressing modes are the methods of accessing the data used by the
instructions.
Addressing mode
Example
Remarks
Immediate Addressing
MOV A,#20h
Register Addressing
MOV A, R5
MOV A,30h
ADD A,@R0
Direct Addressing
Indirect Addressing
IIndexed addressing
| MOVC A, @A+PC |
Address of data is
obtained by adding offset
to base register
MOV
8-bit data transfer for internal RAM and the SFR.
MOV A, Rn
MOV A, addr
1. addr is of either
MOV A, @Ri
internal RAM or SFR
MOV A, #data
2. Only R0 or R1 to be
MOV Rn, A
used for indirect
MOV Rn, addr
MOV Rn, #data
addressing
MOV addr, A
3. Both source and
MOV addr, Rn
destination can not be
MOV addr, addr
specified indirectly
MOV addr, @Ri
MOV addr, #data
MOV @Ri, A
MOV @Ri, addr
MOV @Ri, #data
MOV
1-bit data transfer involving the CY flag
MOV C, bit
MOV bit, C
MOV
16-bit data transfer involving the DPTR
MOV DPTR, #data
MOVX
X means the data movement is external to
the 8051
Data transfer between the accumulator and a
byte from external data memory.
MOVX
MOVX
MOVX
MOVX
A, @Ri
A, @DPTR
@Ri, A
@DPTR, A
MOVC
Move Code Byte
Load the accumulator with a byte from program
memory (internal /external ROM)
use indexed addressing
MOVC
MOVC
A, @A+DPTR
A, @A+PC
PUSH / POP
Push and Pop a data byte onto the stack.
The data byte is identified by a direct address
from the internal RAM locations.
PUSH
POP
0E0H
40H
PUSH addr
SP SP+1
(M)SP (M)addr
POP addr
(M)addr (M)SP
(SP) (SP)-1
XCH
Exchange accumulator and a byte variable
XCH A, Rn
XCH A, direct
XCH A, @Ri
XCHD
Exchange lower digit of accumulator with the lower
digit of the memory location specified.
XCHD A, @Ri
Ex.1 Copy the contents of external data memory location having address
3000h to internal data memory location having address 25h using
indirect addressing.
MOV DPTR, #3000h
MOV R0, #25h
MOVX A,@DPTR
MOV @RO,A
Ex.2 Copy the contents of external program memory location having
address 3025h to internal data memory location having address 25h
MOV DPTR, #3000h
MOV A, #25h
MOV R0, #25h
MOVC A,@A+DPTR
MOV @RO,A
Ex. 3 Exchange the content of internal data memory location 50h and
external data memory location FF00h
MOV DPTR, #0FF00h
MOVX A, @DPTR
MOV R0, 50h
MOV 50H, A
MOV A, R0
MOVX @DPTR, A
SUBB A,src
#byte / @Rn / Ri / byte
Mnemonics
Operands
Bytes/Cycles
ADD
A, Rn
1/1
ADDC
A, direct
2/1
SUBB
A, @Ri
1/1
A, #data
2/1
INC
1/1
DEC
Rn
1/1
direct
2/1
@Ri
1/1
INC
DPTR
1/2
MUL
AB
1/4
DIV
AB
1/4
DA
1/1
Arithmetic Instructions
ADD
8-bit addition between the accumulator (A) and
a second operand.
The result is always in the accumulator.
The CY flag is set/reset appropriately.
ADDC
8-bit addition between the accumulator, a
second operand and the previous value of the
CY flag.
Useful for 16-bit addition in two steps.
The CY flag is set/reset appropriately.
SUBB
Subtract with Borrow.
Subtract an operand and the previous value of the
borrow (carry) flag from the accumulator.
A A - <operand> - CY.
The result is always saved in the accumulator.
The CY flag is set/reset appropriately.
CLR
MOV
ADD
MOV
MOV
ADDC
MOV
C
A, #44H
A, #CAH
R1, A
A, #1EH
A, #56H
R2, A
DA A
Add 34 to 49 BCD
MOV A, #34H
ADD A, #49H
DA
INC
Increment the operand by one.
The operand can be a register, a direct address, an
indirect address, the data pointer.
DEC
Decrement the operand by one.
The operand can be a register, a direct address, an
indirect address.
MUL AB / DIV AB
Multiply A by B and place result in A:B.
Divide A by B and place result in A:B.
MUL AB
Uses registers A and B as both source and destination registers
Numbers in A and B are multiplied, then put the lower-order byte of
the product in A and the high-order byte in B
The OV flag is set to 1 if the product > FFh
Note that the C flag is 0 at all times
DIV AB
uses registers A and B as both source and destination registers
The number in A is divided by B. The quotient is put in A and the
remainder (if any) is put in B
The OV flag is set to 1 if B has the number 00h (divide-by-zero error)
Note that the C flag is 0 at all times
Divide 95 by 10
MOV A,#95
MOV B,#10
DIV AB
Result:
A =09 (quotient) , B=05 (remainder)
Program : Treat R7-R6 and R5-R4 as two register pairs containing 16 bit
numbers. Subtract first from second. Store the result in 20h (lower byte)
and 21h (higher byte).
CLR C
MOV A, R4
SUBB A, R6
MOV 20H, A
MOV A, R5
SUBB A, R7
MOV 21H, A
; clear carry
; get first lower byte
; subtract it with other
; store the result
; get the first higher byte
; subtract from other
; store the higher byte
LOGICAL INSTRUCTIONS
There are instructions available for the 8051 to implement the following
logic functions
AND
OR
XOR (exclusive-OR)
NOT (invert/complement)
ANL / ORL
Work on byte sized operands or the CY flag.
ANL A, Rn
ANL A, direct
ANL A, @Ri
ANL A, #data
ANL direct, A
ANL direct, #data
ANL C, bit
ANL C, /bit
XRL
Works on bytes only.
CPL / CLR
Complement / Clear.
Work on the accumulator or a bit.
CLR A
All bits in register A are cleared
CPL A
All bits in register A are complemented (inverted)
CLR P1.2
CPL C
*Note that CLR and CPL instructions operate on register A
or a bit in bit addressable register.
The bit shifted out is used as the new bit shifted in.
May include the C flag in the operation.
Useful in inspecting the bits in a byte one by one and also useful for
multiplication and division in powers of 2.
RL A
Rotates A one bit position to the left
RLC A
Rotates A and the carry flag one bit position to the left
RR A
Rotates A one bit position to the right
RRC A
Rotates A and the carry flag one bit position to the right
Note that for RLC and RRC, you have to know the C flag first
We can use rotate instructions for data serialization.
Serializing data is a way of sending a byte of data one bit at a time
through a single pin of microcontroller.
BRANCHING INSTRUCTIONS
Jumps can
1. be conditional or unconditional
2. be relative or absolute short or absolute long (range of jump)
3. test bit or byte
Infinite Loops
Jump on Bit
JB / JNB b, raddr
Jump if the specified bit is set / cleared.
Any addressable bit can be specified.
if (A) < (M)addr then jump to the relative address & CY is set
if (A) > (M)addr then jump to the relative address & CY is reset
No Operation
NOP
Subroutines
call to the subroutine
Main:
...
acall sublabel
...
...
sublabel:...
the subroutine
...
ret
Why Subroutines?
Subroutines allow us to have "structured"
assembly language programs.
This is useful for breaking a large design
into manageable parts.
It saves code space when subroutines can be
called many times in the same program.
ORG 100H
MOV DPTR,#200H
MOV A,R3
MOVC A,@A+DPTR
RET
ORG 200H
SQR: DB 0,1,4,16,25,36,49,64,81
END
Machine cycles
The oscillator formed by the crystal and associated circuit generates a
pulse train at the crystal frequency fosc.
The minimum time required by the microcontroller to complete a
simple instruction, or part of a more complex instruction, is the machine
cycle.
The machine cycle consists of a sequence of six states, numbered S1
through to S6, with each state time lasting for two oscillator periods.
Thus a machine cycle takes 12 oscillator periods.
1
1
4
2
1
2
1
1
1
Nextbit:
MOV A,#52H
SETB P1.3
CLR P1.3
MOV R0,#8 ;counter
RRC A
MOV P1.3,C
DJNZ R0,nextbit
SETB P1.3
CLR P1.3
1)Transfer the block of data from 20h to 30h to external location 1020h
to 1030h.
nxt:
2)Find out how many corresponding equal bytes between two memory
blocks 10h to 20h and 20h to 30h.
MOV R7, #11H
; initialize counter by 17d
MOV R0, #10H
; get initial location of block1
MOV R1, #20H
; get initial location of block2
MOV R6, #00H
; equal byte counter. Starts from zero
NXT: MOV A, @R0
; get content of block 1 in acc
MOV B, A
; move it to B
MOV A, @R1
; get content of block 2 in acc
CJNE A, B, NOMATCH ; compare both if equal
INC R6
; increment the counter
NOMATCH:
INC R0
; otherwise go for second number
INC R1
DJNZ R7, NXT ; decrease r7. if zero then over otherwise move next
Compare each byte one by one from both blocks. Increase the count
every time when equal bytes are found
3) Given block of 255 bytes starting from 100h. Find out how many
bytes from this block are greater than the number in r2 and less then
number in r3. Store the count in r4.
MOV DPTR, #0100H
MOV R7, #0FFH
MOV R4, #00H
MOV 20H, R2
MOV 21H, R3
NXT: MOVX A, @DPTR
CJNE A, 21H, LOWER
SJMP OUT
LOWER: JNC OUT
CJNE A, 20H, LIMIT
SJMP OUT
LIMIT: JC OUT
INC R4
OUT: INC DPTR
DJNZ R7, NXT
take each byte one by one from given block. two limits are given: higher limit in r3 and
lower limit in r2. check first higher limit and then lower limit if the byte is in between these
limits then increment the count.