Programming Model of 8051
Programming Model of 8051
Here, the address for the operand is the sum of contents of DPTR and
Accumulator.
NOTE: Some other Addressing Modes like Absolute Addressing
Mode, Relative Addressing Mode and Long Addressing Mode.
EXTERNAL DATA MOVES
An X is added to the MOV mnemonics to remind
eXternal data move
CODE MEMORY (ROM) DATA MOVES
PUSH & POP OPCODES
DATA EXCHANGE
SUMMARY
LOGICAL OPERATIONS
Bit or Byte level Boolean instructions
BOOLEAN 8051 MNEMONIC
OPERATOR
AND ANL (AND logical)
OR ORL (OR logical)
XOR XRL (exclusive OR logical)
NOT CPL (complement)
RRC Rotate a byte and the carry to the right; the LSB
becomes the carry, and the carry the MSB
SWAP Exchange the low and high nibbles in a byte
BYTE LEVEL LOGICAL OPERATION - I
Mnemonic Operation
ANL A,#n AND each bit of A with the same bit of immediate number n; put the results in A
ANL A,add AND each bit of A with the same bit of the direct RAM address; put the results
in A
ANL A,Rr AND each bit of A with the same bit of register Rr; put the results in A AND
each bit of A with the same bit of the contents of the RAM address contained
in Rp; put the results in A
ANL A,@Rp AND each bit of A with the direct RAM address; put the results in the direct
RAM address
ANL add,A AND each bit of the RAM address with the same bit in the number n; put the
result in the RAM address
ANL add,#n OR each bit of A with the same bit of n; put the results in A
ORL A,#n OR each bit of A with the same bit of the direct RAM address; put the results in
A
ORL A,add OR each bit of A with the same bit of register Rr; put the results in A OR each
bit of A with the same bit of the contents of the RAM address contained in Rp;
put the results in A
ORL A,Rr OR each bit of A with the direct RAM address; put the results in the direct RAM
address
ORL A,@Rp OR each bit of the RAM address with the same bit in the number n; put the
result in the RAM address
ORL add.A XOR each bit of A with the same bit of n; put the results in A
BYTE LEVEL LOGICAL OPERATION - II
Mnemonic Operation
XRL A,#n XOR each bit of A with the same bit of register Rr; put the
results in A XOR each bit of A with the same bit of the contents
of the RAM address contained in Rp; put the results in A
XRL A,add XOR each bit of A with the direct RAM address; put the results in
the direct RAM address
XRL A,Rr Operation
XRL A,@Rp AND each bit of A with the same bit of immediate number n; put
the results in A
XRL add,A AND each bit of A with the same bit of the direct RAM address;
put the results in A
XRL add,#n XOR each bit of the RAM address with the same bit in the
number n; put the result in the RAM address
CLR A Clear each bit of the A register to zero
CPL A Complement each bit of A; every I becomes a 0, and each 0
becomes a 1
CAUTION
If the direct address destination is one of the port SFRs, the data latched in the SFR, not the pin data, is
used.
BIT LEVEL LOGICAL OPERATION
Bit-Level Boolean Operations
ANL C,b AND C and the addressed bit; put the result in C
ANL C,/b AND C and the complement of the addressed bit; put the result in C; the
addressed bit is not altered
ORL C,b OR C and the addressed bit; put the result in C
ORLC,/b OR C and the complement of the addressed bit; put the result in C; the
addressed bit is not altered
CPLC Complement the C flag
RLC A Rotate the A register and the carry flag. as a ninth bit, one
bit position to the left;
RR A Rotate the A register one bit position to the right;
RRC A Rotate the A register and the carry flag, as a ninth bit, one
bit position to the right;
SWAP A Interchange the nibbles of register A; put the high nibble
in the low nibble position and the low nibble in the high
nibble position
CAUTION
Know the state of the carry flag when using RRC or RRL.
Rotation and swap operations are limited to the A register.
ARITHMETIC OPERATION
Mnemonic Operation
SUBB A,#n Subtract immediate number n and the C flag from A; put the result in A
SUBB A,add Subtract the contents of add and the C flag from A; put the result in A
SUBB A,Rr Subtract Rr and the C flag from A; put the result in A
SUBB A,@Rp Subtract the contents of the address in Rp and the C flag from A; put the result
in A
CAUTION :- Remember to set the carry flag to zero if it is not to be included as part of the
subtraction operation.
MUL AB Multiply A by B: put the low-order byte of the product in A, put the high-order
byte in B
Jump unconditionally
Call a subroutine
END
CAUTION
All jump addresses, such as ADDA and ADDR, must be within + 127d, -128d of the instruction
following the jump opcode. If the addressable bit is a flag bit and JBC is used, the flag bit will be
cleared. Do not use any label names that are also the names of registers in the 8051. These are
called "reserved" words and will cause great agitation in the assembler.
JUMPS – BYTE JUMPS
Mnemonic Operation
CJNE A,add,radd Compare the contents of the A register with the contents of the direct address; if
they are not equal, then jump to the relative address; set the carry flag to I if A is
less than the contents of the direct address; otherwise, set the carry flag to 0
CJNE A,#n,radd Compare the contents of the A register with the immediate number n; if they are
not equal, then jump to the relative address; set the carry flag to I if A is less than
the number; otherwise, set the carry flag to 0
CJNE Compare the contents of register Rn with the immediate number n; if they are not
Rn,#n,radd equal, then jump to the relative address; set the carry flag to I if Rn is less than the
number; otherwise, set the carry flag to 0
CJNE Compare the contents of the address contained in register Rp to the number n; if
@Rp,#n,radd they are not equal, then jump to the relative address; set the carry flag to I if the
contents of the address in Rp are less than the number; otherwise, set the carry flag
to 0
DJNZ Rn,radd Decrement register Rn by I and jump to the relative address if the result is not zero;
no flags are affected
DJNZ add,radd Decrement the direct address by I and jump to the relative address if the result is
not O; no flags are affected unless the direct address is the PSW
JZ radd Jump to the relative address if A is 0; the flags and the A register are not changed
JNZ radd Jump to the relative address if A is not O; the flags and the A register are not
changed
UNCONDITIONAL JUMPS
Mnemonic Operation
JMP @A+DPTR Jump to the address formed by adding A to the DPTR; this is an unconditional
jump and will always be done; the address can be anywhere in program
memory; A, the DPTR, and the flags are unchanged
AJMP sadd Jump to absolute short range address sadd; this is an unconditional jump and
is always taken; no flags are affected
LJMP ladd Jump to absolute long range address ladd; this is an unconditional jump and is
always taken; no flags are affected
SJMP radd Jump to relative address radd; this is an unconditional jump and is always
taken; no flags are affected
NOP Do nothing and go to the next instruction; NOP (no operation) is used to
waste time in a software timing loop; or to leave room in a program for later
additions; no flags are affected
CAUTION
DJNZ decrements first, then checks for 0. A location set to 00h and then decremented goes to FFh,
then FEh, and so on, down to 00h.
CJNE does not change the contents of any register or RAM location. It can change the carry flag to 1
if the destination byte is less than the source byte.
There is no zero flag; the JZ and JNZ instructions check the contents of the A register for 0. JMP @A+
DPTR does not change A, DPTR, or any flags.
CALL AND RETURNS
Mnemonic Operation
ACALL sadd Call the subroutine located on the same page as the address of the
opcode immediately following the ACALL instruction: push the
address of the instruction immediately after the call on the stack
LCALL ladd Call the subroutine located anywhere in program memory space; push
the address of the instruction immediately following the call on the
stack
RET Pop two bytes from the stack into the program counter
RETI Pop two bytes from the stack into the program counter and reset the
interrupt enable Hip-Hops
CAUTION
Set the stack pointer above any area of RAM used for additional register banks or data memory. The
stack may only be 128 bytes maximum; which limits the number of successive calls with no returns
to 64.
Using RETI at the end of a software called subroutine may enable the interrupt logic erroneously. To
jump out of a subroutine (not recommended), adjust the stack for the two return address bytes by
POPing it twice or by moving data to the stack pointer to reset it to its original value.
Use the LCALL instruction if your subroutines are normally placed at the end of your program
8051 DATA TYPES
The DB directive is the most widely used data directive
in the assembler. It is used to define the 8-bit data. When
DB is used to define data, the numbers can be in
decimal, binary, hex, or ASCII formats.
8051 DIRECTIVES
ORG (origin)
The ORG directive is used to indicate the beginning of the address. The number that comes after
ORG can be either in hex or in decimal. If the number is not followed by H, it is decimal and the
assembler will convert it to hex. Some assemblers use “.ORG” (notice the dot) instead of “ORG”
for the origin directive. Chk your assembler.
EQU (equate)
This is used to define a constant without occupying a memory location. The EQU directive does
not set aside storage for a data item but associates a constant value with a data label so that when
the label appears in the program, it’s constant value will be substituted for the label.
COUNT EQU 25
MOV R2,#COUNT
END directive
Another important pseudo-code is the END directive. This indicates to the assembler the end of
the source (asm) file. The END directive is the last line of an 8051 program, meaning that in the
source code anything after the END directive is ignored by the assembler. Some assemblers use “.
END” (notice the dot) instead of “END”.
RULES FOR LABELS IN ASSEMBLY LANGUAGE
By choosing label names that are meaningful, a programmer can
make a program much easier to read and maintain. There are
several rules that names must follow.
Each label name must be unique.
The names used for labels in Assembly language programming consist
of alphabetic letters in both uppercase and lowercase, the digits 0
through 9, and the special characters question mark (?), period (.), at
(@), underline (_), and dollar sign ($).
The first character of the label must be an alphabetic character. In other
words it cannot be a number.
Every assembler has some reserved words that must not be used as
labels in the program.
Foremost among the reserved words are the mnemonics for the
instructions. For example, “MOV” and “ADD” are reserved since they
are instruction mnemonics.
In addition to the mnemonics there are some other reserved words.
Check your assembler for the list of reserved words.
An Assembly language instruction consists of four
fields:
My file.asm
Assembler
Program
Myfile.lst
Other .obj files
Myfile.obj
Linker Program
Myfile.abs
0H
Program
Myfile.hex
LED INTERFACING WITH 8051
MICROCONTROLLER
LED INTERFACING WITH 8051
MICROCONTROLLER
Digit a b c d e f g dp Hex
0 1 1 1 1 1 1 0 0 3FH
1 0 1 1 0 0 0 0 0 06H
2 1 1 0 1 1 0 1 0 5BH
3 1 1 1 1 0 0 1 0 4FH
4 0 1 1 0 0 1 1 0 66H
5 1 0 1 1 0 1 1 0 6DH
6 1 0 1 1 1 1 1 0 7DH
7 1 1 1 0 0 0 0 0 07H
8 1 1 1 1 1 1 1 0 7FH
9 1 1 1 1 0 1 1 0 6FH
INTERFACING 7-SEGMENT DISPLAY TO 8051
ASSEMBLY PROGRAM TO DISPLAY 0 TO 9 ON SSD
ORG 000H
START: MOV A,#00001001B
MOV B,A
MOV R0,#0AH
LABEL: MOV A,B
INC A
MOV B,A
MOVC A,@A+PC
MOV P1,A
ACALL DELAY
DEC R0
MOV A,R0
JZ START
SJMP LABEL
DB 3FH // digit drive pattern for 0
DB 06H // digit drive pattern for 1
DB 5BH // digit drive pattern for 2
DB 4FH // digit drive pattern for 3
DB 66H // digit drive pattern for 4
DB 6DH // digit drive pattern for 5
DB 7DH // digit drive pattern for 6
DB 07H // digit drive pattern for 7
DB 7FH // digit drive pattern for 8
DB 6FH // digit drive pattern for 9
DELAY: MOV R4,#05H
WAIT1: MOV R3,#05H
WAIT2: MOV R2,#05H
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2
DJNZ R4,WAIT1
RET
END
?
THANK YOU!