Professional Documents
Culture Documents
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
MICROCOMPUTERS 3
o Memory for the data (bits, bytes and words) and stack
o Timing circuit or unit for the system clock and other timing-related functions
Inside the Computer 4
Inside the Computer 5
Inside the CPU 6
MICROCONTROLLERS 7
o Timer device. This enables the functioning of the system clock, real-time clock and
software timer(s). It enables real-time detection of an event or signal (compare the
time or capture the time on an event). A timer device can function also as a
watchdog timer device
o Device pulse width modulation (PWM). This enables digital to analog conversion
(DAC).
MICROCONTROLLERS 9
o Ports can have network interface and the CPU can process instructions
related to the network processing
o USB/ PCI/ I2C/ CAN/ JTAG/ SDIO (Secure Data IO) interfacing devices
10
Microprocessors vs. Microcontrollers 11
General-purpose microprocessors
o Must add RAM, ROM, I/O ports, and timers externally to make them functional
o Have the advantage of versatility on the amount of RAM, ROM, and I/O ports
Microcontroller
o The fixed amount of on-chip ROM, RAM, and number of I/O ports makes them
ideal for many applications in which cost and space are critical
o In many applications, the space it takes, the power it consumes, and the price per
unit are much more critical considerations than the computing power
MICROPROCESSORS AND MICROCONTROLLERS 12
MICROPROCESSORS AND MICROCONTROLLERS 13
MICROPROCESSORS MICROCONTROLLERS
Microprocessor contains ALU, General purpose Microcontroller contains the circuitry of 14
registers, stack pointer, program counter, clock microprocessor, and in addition it has built in
timing circuit, interrupt circuit ROM, RAM, I/O Devices, Timers/Counters etc.
It has many instructions to move data between It has few instructions to move data between
memory and CPU memory and CPU
Few bit handling instruction It has many bit handling instructions
Less number of pins are multifunctional More number of pins are multifunctional
Single memory map for data and code (program) Separate memory map for data and code
(program)
Access time for memory and IO are more Less access time for built in memory and IO.
Microprocessor based system requires additional It requires less additional hardwares
hardware
More flexible in the design point of view Less flexible since the additional circuits which is
residing inside the microcontroller is fixed for a
particular microcontroller
Large number of instructions with flexible Limited number of instructions with few
addressing modes addressing modes
15
16
3. On-chip RAM
6. Ports
On Chip Resources of a microcontroller 18
7. On-chip registers
9. UART
11. Timers/counters
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
22
8051 23
Characteristics of 8051 microcontroller 24
8 bit CPU:ALU can perform arithmetic and logic functions on 8 bit variables.
On chip clock oscillator
4Kbytes of internal program memory (code memory) [ROM]
128 bytes of internal data memory [RAM]
64 Kbytes of external program memory address space.
64 Kbytes of external data memory address space.
32 bi-directional I/O lines
o can be used as four 8 bit ports or 32 individually addressable I/O lines
Characteristics of 8051 microcontroller 25
o B register
o PSW
o SP
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
8051 Architecture 29
New variants of the classic 8051 are 8052, extended and MX variants.
They have additional resources.
8051 Architecture 30
Each program needs a stack. The 8051 has a stack pointed out by the byte at a special function
register. SP (stack-pointer) is 8-bit register.
Features of 8051 32
There is a program counter (PC) lower byte of it is sent at the bus A0–A7.
o The A0–A7 bus pins are also common to the data bus D0–D7.
o The pins are also common to that of the Port P0 latch and these pins have signals of
Port P0 in single-chip mode.
Features of 8051 33
o The A8– A15 bus pins are used in the expanded chip mode.
o The same pins are used as port P2 in the single-chip mode and A8–A15
bus signals in the expanded mode.
• IE (interrupt enable)
1. 8051 processes instructions and has an 8-bit ALU. 8. Special function registers are
vi. SMOD
5. Two external interrupts through INT0#, INT1# and
pins. vii. SBUF
3. Reset circuit
4. ALU
• ALU does arithmetic and logic operations on the operands.
• 16-bit
• address pointer
• holds the program memory address of the instruction currently being fetched.
PC Program counter • Increments continuously to point to the next instruction.
• The PC consists of 2 bytes
• PCH (higher) and
• PCL (lower).
• 8-bit register
• Saves an operand for operations by ALU.
A Accumulator
• Act as an operand in data transfer operation using the A.
• important function is to accumulate the result after an ALU operation.
• 8-bit register
B B register • Saves a second operand for the ALU
• Also accumulates part of the result of multiplication or division.
• Registers SP, PSW, A, B, IE, IP, P0, P1, P2, P3, SCON, TCON, SMOD, SBUF, PCON,
Special function • TL0,TH0,TL1,TH1 are called SFRs.
SFRs
registers • These are at directly addressable memory.
• SFR address must be directly specified in the instruction and not through a pointer
register.
8051 Architecture 44
- The uses of Subunits and signals
Symbol Full Form Use
Read only • Masked ROM, EPROM or flash EEPROM of 4 kB in 8051 classic family
• (or 8 kB or 16 kB in 8051 family variants).
ROM program
• Internally connects to PC by bus of 12
memory • (or 13 or 14) bits in 4 or 8 or 16 kB versions, respectively.
Internal random • RAM is 128 B memory for the read and write
Internal RAM
access memory • Indirectly as well as directly addressable.
• A RAM address is between 0x00 and 0x7F.
XTAL1 and Pins to the • Pins to the crystal in the oscillator circuit
XTAL2 crystal • about 12 MHz crystal used in classic 8051
8051 Architecture 45
- The uses of Subunits and signals
Symbol Full Form Use
Voltage supply • VDD and VSS for 5V supply and ground connections, respectively.
VCC or VDD and
pin and ground • When 8051version is based on CMOS circuits (in 80C51 version).
VSS (GND)
pin • VCC and GND when based on other circuits
8051 Architecture 46
- The uses of Subunits and signals
Symbol Full Form Use
Program store • Activates for reading the external program memory byte.
PSEN#
enable • The read is active when PSEN becomes 0.
o The addresses of these bits are used in Boolean processing logical operation on
the bits.
Specific SFRs can only be addressed for the byte in them not individual
bits
Special 48
Function
Registers
PSW Register Bits And Flags 49
o Similar convention will be henceforth used for referring to the bits of PSW and
other SFRs.
PSW Register Bits And Flags 52
However, there is no Z flag. This is because of the fact that PSW design limits it to
only 8 bits.
o It is providing for two-user flags and two register bank select in 8051. (A bank is a set of 8
registers).
Z flag is internally accessible to the condition test and branch logic circuit and
instruction like JZ. But to programmer
8051 Pin Diagram 53
8051 Pins 54
8051 Pins 55
8051 Pins 56
Syllabus- Module 5 57
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
MODULE 5
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
8051 Memory Mapping 60
External Memory 61
Memory Mapping Addresses 62
Internal and External ROM 63
On Chip Memory- Internal RAM 64
Internal RAM 65
The IDATA/ DATA RAM between 0x00 and 0x7F has 128 bytes
Detailed Diagram
4 Memory Banks
Detailed Diagram
4 Memory Banks
Each bank is selected by first assigning the RS0 and RS1 bits
b3 and b4 in the PSW.
These 32 bytes are not used for the bit operations with
Boolean processor and bit transfers
80 bytes
of the byte
Parity Flag
User Flag1
Overflow Flag
Register Set Bit 0
Register Set Bit 1
User Flag0
Auxiliary Carry Flag
Carry Flag
Individual bit addresses of PSW 81
Port Address 82
Ports in Single-chip Mode Operations 83
Alternative Functions of Port 3 84
MODULE 5
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
8051 Interrupts 87
Using interrupts, there is handling and servicing of the various devices such as
timers, serial interface and external source interrupt, such as keypad.
8051 Interrupts 88
o When there is return from the ISR, the saved program counter is loaded back
and the interrupted program runs again.
Instructions of Interrupt Service Routine 89
ISR services a set of instructions after each interrupt event from the
interrupt source.
Interrupt is a call event arising from a signal of another internal or
external process, device, circuit or action.
o for example, timeout message (timer overflow.)
• (A corresponding flag indicates overflow message.)
o The new program counter contents should point to that address for the
interrupt service to run the ISR.
o That address is called the ‘Vector Address’ for the given interrupt source.
INTERRUPTS IN 8051 92
o IP (interrupt priority).
handling
IE- Bits 94
IP- Bits 95
Actions on an 96
Interrupt of a
Program by a
Source
Interrupt Vector Address 97
Stage 4: If EA is equal to 1, processor checks whether interrupt for the given source is enabled
(not masked) and if yes, then does the following actions:
1. Pushes PCH and PCL on to the memory stack. Fetches the corresponding ISR_VECTADDR for the
source among the 7 vector addresses.
2. Makes EA bit = 0 temporarily until first instruction within an ISR corresponding to the source
executes.
• The further interrupts from any lower priority interrupt are the only ones disabled until
return from the ISR.
• The higher priority ones are polled at the end of each instruction in the ISR
• Before the return or after the end of critical section in the ISR the EA must be set again to 1 if
future interrupts are to be recognized.
Servicing of interrupts- 5 stages 10
0
Stage 5: Loads into PCL and PCH (program counter address) the start
address of ISR (ISR_VECTAD-DR address) and executes the ISR for the
interrupting source.
o The ISR can call another routine.
o If the 8 bytes ISR space available between two routines (two vector addresses) is
insufficient, another location routine can be executed by jump instruction to vector to
address of remaining ISR.
Sources of interrupts 10
1
Control and Status Bits for the External Sources of Interrupts INT1 and INT0
1. External hardware interrupts INT1 and INT0 use the four lower bits of TCON. When IT1
(INT1 type bit) TCON.2 is reset to 0 the interrupt INT1 is level activated and set to 1, it is
edge triggered. It triggers by negative edge (1 to 0 transition) at INT1 pin.
2. When IT0 (INT0 type bit) TCON.0 is reset to 0 the interrupt INT0 is level activated and set to
1, it is edge triggered. It triggers by negative edge (1 to 0 transition) at INT0 pin.
3. When IE1 (INT1 event flag) TCON.3 sets to 1, the ISR starts and on start of ISR for INT1
service, it resets to 0.
4. When IE0 (INT0 event flag) TCON.1 is sets to 1, the ISR starts and on start of ISR for INT0
service, it resets to 0.
8051 Stack 10
3
A stack is accessed from the memory in the LIFO mode, while a row of
data in the queue is accessed in a FIFO (first in last out) mode.
program code address) onto the stack top, on return from the
routine where the processor pushed that address earlier can be
popped from the stack top and the program will switch back from
the called program (routine) to the calling program.
10
5
MODULE 4
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
Assembly Programming basics 10
8
specification.
Prefix # is used to specify an operand, which is called immediate
o Register
o Direct
o Indirect Register
This mode of addressing uses either an 8- or 16-bit constant value as the source operand
This constant is specified in the instruction, rather than in a register or a memory location
The destination register should hold the same data size which is specified by the source operand
The data bits for instruction are a part of the instruction and are the next byte after the opcode.
E.g.
o ADD A, #030H ;Add 8-bit value of 30H to the accumulator register ;(which is an 8-bit register).
o MOV DPTR, #0FE00H ;Move 16-bit data constant ;FE00H into the 16-bit Data ;Pointer Register.
2. Register Addressing Modes 11
2
Example:
o MOV R0, A
• The instruction transfers the accumulator content into the R0 register. The register
bank (Bank 0, 1, 2 or 3) must be specified prior to this instruction.
3. Direct Addressing Modes 11
3
This mode allows you to specify the operand by giving its actual memory address
(typically in hexadecimal) or by giving its abbreviated name (e.g. P3)
The address of 8-bits is directly specified in the instruction.
o Instructions for the SFRs have only the direct addressing mode in 8051.
o Internal RAM between 0x00 and 0x7F is also accessible by direct addressing.
Example
o MOV R5, 90H ;(MOV R5, P1 for source at the SFR for P1 port. P1 has the address 90H)
o MOV A, 020H ;Transfer the contents of RAM location 20H to the accumulator
4. Indirect register Addressing Mode 11
4
This mode uses a pointer to hold the effective address of the operand
Only registers R0, R1 and DPTR can be used as the pointer registers
o The R0 and R1 registers can hold an 8-bit address whereas DPTR can hold a 16-bit address
o MOV @R0, A ;Store the content of accumulator to the memory location pointed to by
register R0.
o MOVX A, @DPTR ;Transfer the contents from memory location pointed by DPTR into
accumulator.
5. Indexed Addressing Mode 11
5
The Indexed addressing is useful when there is a need to retrieve data from a look-up table
A 16-bit register (data pointer) holds the base address and the accumulator holds an 8-bit
displacement or index value
The sum of these two registers forms the effective address for a JMP or MOVC instruction
Example:
o MOV A, #08H ;Offset from table start
o MOVC A, @A+DPTR ;Gets target value from the table start address + offset and puts it in A.
After the execution of the above instructions, the program will branch to address 1F08H
(1F00H+08H) and the data byte retrieved from that location is transferred into the
accumulator (Code Memory- that’s why MOVC)
Relative Addressing 11
6
This mode of addressing is used with some type of jump instructions, like SJMP
(short jump) and conditional jumps like JNZ
The destination address must be within -128 and +127 bytes from the current
instruction address because an 8-bit offset is used (28 = 256)
Example:
o GoBack: DEC A ;Decrement A
Two instructions associated with this mode of addressing are ACALL and AJMP instructions
These are 2-byte instructions where the 11-bit absolute address is specified as the operand
The upper 5 bits of the 16-bit PC address are not modified. The lower 11 bits are loaded from
this instruction. So, the branch address must be within the current 2K byte page of program
memory (211 = 2048)
Example:
o ACALL PORT_INIT ;PORT_INIT should be located within 2k bytes.
This mode of addressing is used with the LCALL and LJMP instructions
It is a 3-byte instruction and the last 2 bytes specify a 16-bit destination location where the
program branches
The program will always branch to the same location no matter where the program was
previously
Example:
o LCALL TIMER_INIT ;TIMER_INIT address (16-bits long) is specified as the
operand. In C, this will be a function call: Timer_Init().
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
PROGRAMMING BASICS 12
1
o An end instruction can be for reset or halt or jump back to the same instruction or
return
Machine codes mean bytes that are fetched and executed by the CPU.
Functions
Program Counter 12
3
12
Stack 4
Pointer
Program Status Word 12
5
A bit of PSW shows a certain condition of the process and so it is also called a
flag.
Carry flag is used in all processors to implement use of carry (or borrow) in
addition and subtraction.
o Only RLC(rotate left through carry) & RRC (rotate right through carry) affect carry flag.
Parity P flag shows whether the number of bits in register A are odd or even (= 1 if odd).
The programmer uses the PSW. 1 bit as the user flag F1 and PSW.5 bit as the user flag F0.
PSW.4 and PSW.3 are RS1 and RS0 (register set bits)- Used to select Register Banks
Exemplary 9 machine codes, 7 assembly codes and single high-level C language expression
CLASSIFICATION OF THE INSTRUCTIONS 12
8
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
DATA TRANSFER INSTRUCTIONS 13
1
MOV Instructions
MOVC-type Instructions
MOVX-type Instructions
XCH-type Instructions
MOV Instructions 13
2
13
3
Notations used in previous table 13
a. n is 0 or 1 or … or 6 or 7 for a register in the register bank. 4
One of the most important uses of MOV instructions are programming of the SFRs.
Some examples are MOV into
i. TMOD and TCON for timer
Write the control bits 0100 0000 (40H) into the TCON.
o MOV TCON, #40H
Load the external memory address pointer DPTR with 0x1000 (1000H).
o MOV DPTR, 1000H.
o Hence, the byte pointed by R0 can first be moved into A and then to R2.
o MOV R2, A
MOVC-type Instructions 13
9
An MOVC instruction means move (copy) the 8-bit code from one source at the program
memory (internal or external) to the register A destination.
MOVX-type Instructions 14
0
A MOVX instruction means move (copy) the 8-bit data into A and from A
using the external data memory addresses using DPTR or R as the pointer.
MOVX Instructions 14
1
PUSH and POP Instructions for Using the Stack 14
2
o Before the last instruction return from the ISR, there is need to get the saved A and PSW
back.
XCH-type Instructions 14
4
An XCH instruction is for exchanging the A register with a source using the register
(direct or indirect addressing) mode.
An XCHD instruction is to exchange the lower hex-digit (nibbles) between the A
and RAM using indirect addressing.
Advantages of XCHG 14
5
o Similarly, the lower digits can be operated and upper digits can be kept
intact using the XCHD.
XCHG Example 14
6
3. XCH A, P0 (when exchanging A and P0 contents, P0 gets contents of original contents of P1.
A gets the contents of P0, which has A’s original value after the first instruction).
MODULE 5
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
DATA AND BIT-MANIPULATION INSTRUCTIONS 14
9
The source and destination operand addresses are the same and it is only the A
(accumulator) that can be manipulated in 8051.
1. A CLR instruction makes all bits 0s. A=0000 0000 after CLR A.
2. A CPL instruction complements all 1s to 0s and vice versa. For example, if
A=A5H=10100101 then after CPL the A=01011010b.
3. An RL instruction rotates left. For example, bits 10001010b become 00010101b, and the
C flag of PSW also becomes 1 after rotate left.
4. An RLC instruction rotates left through the carry flag. For example, consider bits,
A=10001010b and C=0. RLC makes A=00010100b and the C flag also becomes 1. After
the rotate left through carry the C flag shifts to LSB (least significant bit) at A and MSB
(maximum significance bit) to C.
Data Byte Manipulate Instructions 15
2
5. RR and RRC are similar to RL and RLC instructions and are for the rotate
right and rotate right through carry.
7. The C flag shifts to MSB and the LSB to C in RRC after rotation.
8. SWAP is for swapping between the upper hex-digit with the lower hex-
digit. For example, 89H will become 98H after SWAP.
Excercises 15
3
o XCH A, P2
o CPL A and
o XCH A, P2
Boolean Variable (Bit) Manipulate and Boolean 15
4
Processing Instructions
The bit-manipulation instructions are also called Boolean processing instructions.
o The source or destination operand is either C or bit.
o There can be transfer (copy) or ANDing and ORing using bit and C.
The bit means bit at a bit address. (between the 80H and FFH in select SFRs like
P0, P1, …) and bit addresses between 00H and 7FH for 16 bytes at the internal
RAM between 20H and 2FH.
15
5
Boolean Variable (Bit) Manipulate and Boolean Processing 15
6
Instructions
1. A MOV instruction transfers the bit between C and bit.
3. A CPL instruction complements C or bit. For example, the bit at bit address 93H will be 0, if 1. Note
that 93H is for the bit 3 in byte at P1.
4. A SETB instruction sets C or bit. For example, the bit at bit address BAH = 1 after the SETB. Note that
BAH is the address of bit 2 in SFR IP (Table 3.19).
5. An ANL instruction does ANDing between C and bit or complement of the bit, . For example, assume
bit at P2 bit 3 (address = B3H) = 1 and C = 0. After ANDing C and bit P2.3, C becomes 0.
6. An ORL instruction does ORing between C and bit or complement of the bit, . For example, consider
bit at RS0 = 0 (address D3H). It is a PSW bit. And C=0. After ORing C and bit PSW.3, C becomes 1.
Examples 15
7
o It complements the bit at the bit address 0BH, which is bit 3 at 21H
Write instructions (i) P1 port bit 0 = 0, (ii) IE bit 7 = 1 and (iii) Complement TCON bit 6.
1. CLR P1.0 or CLR 90H to clear the bit at the bit address 90H,which is P1.0.
2. SETB IE.7 or SETB 0AFH to set the bit at the bit address AFH, which is IE.7.
3. CPL TCON.6 or CPL 08EH to complement the bit at the bit address 8EH, which is TCON.6
4. CPL 0BH to complement the bit at the bit address 0BH, which is bit 3 at 21H (remember 00H to 07H
are the bit addresses for RAM at 20H and 08H to 0FH, the bit addresses for RAM at 21H).
MODULE 5
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
ARITHMETIC INSTRUCTIONS 16
0
The instructions are for 8-bit addition, subtraction, increment, decrement, multiply and
division instructions.
2. There are two sets of addition instructions—add without carry and add with carry.
o Both use one of the 4 models of addressing.
3. There is only one set of subtract instruction—subtract with borrow (C). Subtract without
borrow can be done in two steps—CLR C and then SBB B.
4. C, OV and AC are affected as follows
16
o C is set when the addition results in answer > FFH and set when subtraction results in the answer < 00H. 1
o AC is set when an addition or subtraction in the lower-nibble operation changes the value of the upper-nibble.
o AC is used for decimal adjusting the result by DA A instruction after the addition.
5. Multiplication and division instructions use only the register addressing mode and use only A and B.
o OV is affected and is useful in determining whether 8 bits multiplied by 8 bits results in an 8 bit (when OV0) or in 16-
bit (when OV 1) number.
o OV is also useful in determining that denominator was 0 earlier (OV = 1) or not (OV 0) during the division.
6. DA A (decimal adjust accumulator) instruction use only the register addressing mode and uses A.
7. INC DPTR instruction has only one addressing mode—16-bit register (pair) addressing mode and uses DPL
and DPH.
16
2
16
3
16
4
16
5
MODULE 5
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
INSTRUCTIONS FOR LOGICAL OPERATIONS ON 16
8
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
PROGRAM-FLOW CONTROL INSTRUCTIONS 17
4
The instructions for jump, call and reset instructions are called program flow instructions.
o They change the program counter to a value other than the one present after the increment of program counter just after an
instruction fetch.
o A program may jump to another address in case of executing a conditional instruction or while-loop or repeat loop.
o The routine may return to calling routine or program at the end of the instructions.
o The program counter after steadily incrementing after each code of the instruction acquires a new value to start another program-
flow path.
Delay-cycle (NOP) Instructions 17
6
The 8051 has NOP instruction.
o It means no operation, just spent one instruction cycle time
Example : How can we toggle the bits at Port 2 and again toggle after a delay of 10 μs?
[Make the bits = 1s as 0s and 0s as 1s. Delay 10 μs. Again make 1s as 0s and 0s as 1s.]
o Assume that XTAL oscillation frequency = 12 MHz.
o Short Jump: jump to the address within 128 bytes above or below the present address.
Conditional Short Relative Jumps 17
8
Decrement and Conditional Jump on Zero 17
9
Jump After Comparison 18
0
Call to a Routine and Return from Routine 18
1
The 8051 has two call instructions
o Call to any 16-bit address and
These are useful for the call long and absolute to a routine.
Operations in LCALL 18
2
Steps for return from a routine 18
3
Long, absolute call and return instructions in 8051 18
4
INTERRUPT CONTROL FLOW (RETI INSTRUCTION) 18
5
ASCII-RELATED APPLICATION PROGRAM 18
6
It uses the table of ASCII codes for the numbers between 0 and 9.
3. MOV A, R6
4. ADD A, R7 ; Effects AC, AC =0; Effect C, C = 1. [91 + 79 generate auxiliary carry as well as carry].
Auxiliary carry because lower BCD digits addition 1001 + 0001 cause auxiliary carry AC = 1 upper
BCD digits 1001 + 0111 also cause carry. A is now 0A. It is to be adjusted for BCD addition.
5. DA A; decimal adjust by adding 6 in A because the lower digit exceeds 9. A + 6 = 0 and new inter AC = 1.
Add 6 and AC = 1 to higher digit 0 because the higher digit exceeds 9. Result is A = 70. C need not change
if previous carry = 1. Read with C = 1, the result is 170.
Microcontrollers
8051 • Arithmetic instructions
• Logical instructions
o Architecture
• Boolean instructions
o Register Organization
• Control transfer instructions
o Memory and I/O addressing
Simple programs.
o Interrupts and Stack
o 8051 Addressing Modes
o Instruction Set
• Data transfer instructions
Benefits of Assembly Language Program (ALP) 19
1
1. There is efficient use of the memory.
o Machine codes generated from the ALP are compact.
o The ALP codes need smaller memory than in machine codes for the same function in memory after the
compilation of a high level language program.
2. Features of processor instruction set are kept in full view when program is done using ALP.
3. Only few assembly instructions are needed for driving the devices.
o Driving a device means configuring of device by writing control or command words, and writing or reading the
bytes at registers or ports of device.
5. There is full understanding of the process that is taking place at each instance of program execution.
o Time critical codes are therefore programmed in ALP.
6. The code size and code execution speed of the program are known.
Assembler 19
2
Assembler is a program that gives an ease in assembly language programming and
that is used to prepare the machine codes using an input file for the source code
o Assembler is a part of an integrated in a development tool.
o The assembler generates machine codes, assigns addresses to the SFRs, bit addresses to
the bits in the SFRs and code-addresses to the labels.
The assembler also prepares a source code (ALP) listing with the addresses.
o The assembler provides for faster development of the ALP. This is because of the
following:
1. The assembler has a large number of assembler directives and
o use of the program with decision blocks (if then else and looping statements
like C) and
o A structured hierarchical design gives a program with single entry and exit
addresses.
Assembly Language Program 19
4
Write an assembly language program to compute x to the power n where both x and n are
8-bit numbers given by user and the result should not be more than 16 bits.
Assembly Language Program 19
7