0% found this document useful (0 votes)
34 views29 pages

Chapter 2 Software Model 8051

Chapt 2 tcs

Uploaded by

kailasjagtap646
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views29 pages

Chapter 2 Software Model 8051

Chapt 2 tcs

Uploaded by

kailasjagtap646
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Classification of instructions

• Classification in a number of ways


1. Based on the function
2. Based on the instruction size
One byte ,two byte and three byte instructions
(How much memory in bytes used for storing
the instructions in memory)
3.Based on the size of operand supported.
Supports bit sized operands and byte operands

• The first classification is most widely used


Classification of 8051 instructions based on the
function of instructions
• Data transfer instructions
Copies information from source into
destination. These instructions do not affect
the status of flags
MOV d,s
MOVX A,@DPTR MOVX @DPTR,A
MOVX A,@Rs MOVX @Rd,A
MOVC A,@A+DPTR
PUSH dir addr POP dir addr
XCH d,s
INSTRUCTION SET
• Arithmetic instructions
• In these type of instructions one of the operand
has to be accumulator. These instructions change
the status of various flag conditions.
• ADD d,s d-destination s- source
d <- d+s destination can be register, direct or indirect
address. Source can be a register, direct address,
indirect address or immediate data
ADDC d,s SUBB d,s INC s DEC s MUL AB
DIV AB DAA
INSTRUCTION SET
• Logical instruction
• In these type of instructions one of the operand has to
be accumulator. These instructions change the status
of various flag conditions.
ANL d,s ORL d,s XRL d,s CPL A CLR A
RL A RRA RLC A RRC A
SWAP A
INSTRUCTION SET
• Bit manipulation instruction
These instructions also change the status of various flag
conditions
CLR bit SETB bit, CPL bit CLR C CMP C
ANL C,bit ORL C,bit XRL C,bit
MOV C,bit MOV bit,C
JC rel addr JNC rel addr
JNB bit,addr JB bit,addr JBC bit,addr
INSTRUCTION SET
• Program control/Branching instructions

SJMP rel addr AJMP 11 bit addr LJMP 16 bit addr


ACALL 11 bit addr LCALL 16 bit addr RET RETI
JMP @A+DPTR
JZ addr JNZ addr
CJNE d, s, addr DJNZ s, addr

• Machine control instruction


NOP
ADDRESSING MODES
These are different ways in which operands can be
addressed or specified in an instruction. They are
Immediate addressing: Data is actually a part of instruction
MOV A,#20H 20H is the data. What follows # is actual
operand
Direct addressing: Address of data in RAM is specified.
MOV A,20H Here 20H is the RAM address.
Register addressing: Here the register that holds the
operand is specified in the instruction MOV A,B
Register indirect addressing: Here the address of operand
in RAM is stored in a register. So the address is fetched
from that register and then the data is accessed. In the
instruction that register is specified with@ symbol. Only
R0 and R1 register can hold RAM addresses
MOV A,@R1
ADDRESSING MODES
Indexed addressing: Here two registers are used to
provide effective address of operand. One register holds
the base address and other the offset or displacement
value. The effective address is obtained by adding the
two values
MOVC A,@A+DPTR A holds offset, DPTR holds base addr
JMP @A+DPTR
Relative address: Here an 8 bit relative address is the part
of the instruction. The effective address will be current
address +/- 8 bit relative address
SJMP rel addr JB bit rel addr.
ADDRESSING MODES
Bit addressing: In this case the operand specified in the
instruction is of bit type.
SETB P1.5 P1.5 is the 5th bit of port P1
CLR C C is the carry bit

Implied addressing: No operands need to be specified.


The machine knows where the operand is present
NOP RET IRET
ASSEMBLER DIRECTIVES
• These are also called Pseudo instructions. They are directives or
commands to the assembler regarding the program being
assembled
• Such directives will be executed without actually decoding them.
A true instruction has to go through a decoding phase before
executing it
ORG 00H .It indicates start of assembly program in memory.
Address has to be 0 as PC has address 0 upon reset. It has to be the
first statement of any assembly program
END Indicates end of assembly. No instructions after END will be
executed. So END has to be last statement
EQU a variable is equated with a data. In the program this variable
will be replaced by the equated data X EQU 20H
DB (define byte) Allocates one byte for the variable
X DB 20H 20H is stored as a byte (8 bits) ie X is a byte variable
PROGRAMS
• Have to write/prepare common programs
Arithmetic : ADD SUB MUL DIV
Array based : Adding elements of array, finding largest and
smallest from an array, checking number of times a
character/integer occurs in array
Programs like packing 2 BCD in a byte , unpacking a packed
BCD, Parity of given data, checking even/odd.
Port programming: reading a number/array from a port
displaying a number/array on port, accepting numbers
from port until a special character is typed, Square wave
with different duty cycles on a port pin.
Programs with single bit operands
Code conversion: Decimal to binary/octal/Hex
Binary/hex/octal to decimal
Programming 8051 using C
• Why C?
1. Portability
2. Instructions to interact at machine level like assembly program
3. Easily available, ease of programming and large number of
library functions
4. Machine independent

• Data types in C
Char, int (unsigned and signed)- standard types

• Standard loop structures


For loop, if then else, do while
Additional data types for 8051
Sbit: assign a variable to a port pin. By changing the value of the
variable the data on the port pin changes. User can modify a
port pin by assigning a variable name to it
sbit sq=P2^5, sbit m=P1^0 using m one can change data on
port pin P1.5

bit: with a variable a bit location in RAM can be accessed


bit Z=bit addr. When z is modified actually contents of the
memory bit whose address is specified gets modified.

Sfr: One can have access to a special function register by


specifying its address
Sfr P0=0x80 So when we say P0 in the program it is assigned
with address 0x80
Programming 8051 using C
• Logic/bitwise operators

AND ‘&’ , OR ‘I’ , XOR ‘^’ , NOT ‘~’


Suppose A=0x30 (00110000b), then ~A=0xcf(11001111b)
0x25 I 0x43 =(00100101b OR 01000011b)=(01100111b)=0x67

• Bitwise shift operator

>> right shift << left shift


If m=0x42, then m=m<<3 will give new value of m as 0x10
01000010b shifted left 3 bits will be 00010000b

Normally arrays are initialized in RAM area. If an array is to be


initialized in ROM ie code area then it can be done as shown
Code unsigned char vowel[]=“aeiou” .The word ‘code ‘will
initialize this array vowel in ROM
Programming 8051 using C
• Data space usage by 8051 c compiler
1. First 8 bytes of RAM are allocated to bank 0 addr 0-7
2. Individual variables addr 08h and beyond
3. Array elements addr after the variables
4. Stack addr after the array

• C program structure
#include <reg51.h>// very important. Has all register declaration
of 8051
Bit/ sbit/ delay declarations
Main()
{
program body
}
Practice programs: Same as assembly programs expected.

You might also like