Unit-II: 8051 Assembly Language Programming: 2.1 Addressing Modes in 8051 Microcontroller
Unit-II: 8051 Assembly Language Programming: 2.1 Addressing Modes in 8051 Microcontroller
M S Khokhar
2 Micro Controller in Instrumentation
The instruction MOV A, #55H in 8051 assembly language means the following:
MOV: This is the opcode for the "Move" instruction. It is used to copy data from one
location to another.
A: This specifies the destination, which is the accumulator register. The accumulator
is a special-purpose register widely used in arithmetic and logical operations.
#55H: The # symbol indicates that the operand is an immediate value (a constant).
55H is the hexadecimal value to be moved into the accumulator.
Explanation:
The instruction loads the hexadecimal value 55H directly into the accumulator A.
These instructions are used to transfer data between registers, memory locations, and I/O
ports.
MOV (Move): Transfers data from a source to a destination. The source can be an
immediate value, a register, or a memory location, and the destination can be a
register or a memory location.
o Example: MOV A, #25H moves the immediate value 25H into the
accumulator (A).
MOVX (Move External): Transfers data between external memory and a register
(usually the accumulator). It is used for accessing external RAM or I/O devices.
o Example: MOVX A, @DPTR moves data from the external memory location
pointed to by DPTR into the accumulator.
M S Khokhar
3 Micro Controller in Instrumentation
MOVC (Move Code): Transfers data from the program memory (code memory) to a
register, typically the accumulator. It’s often used to read lookup tables stored in code
memory.
o Example: MOVC A, @A+DPTR moves the content from the code memory
location pointed to by the sum of A and DPTR into the accumulator.
2. Arithmetic Operations:
ADD (Add): Adds the contents of a register or memory location to the accumulator.
o Example: ADD A, R1 adds the contents of register R1 to the accumulator.
SUBB (Subtract with Borrow): Subtracts the contents of a register or memory
location and the borrow flag from the accumulator.
o Example: SUBB A, R2 subtracts the contents of register R2 and the borrow
flag from the accumulator.
INC (Increment): Increments the contents of a register or memory location by 1.
o Example: INC A increments the contents of the accumulator by 1.
DEC (Decrement): Decrements the contents of a register or memory location by 1.
o Example: DEC R0 decrements the contents of register R0 by 1.
MUL (Multiply): Multiplies the contents of the accumulator (A) and register B,
storing the result in A and B (A = low byte, B = high byte).
o Example: MUL AB multiplies the contents of A and B.
DIV (Divide): Divides the contents of the accumulator by the contents of register B,
storing the quotient in A and the remainder in B.
o Example: DIV AB divides the contents of A by B.
3. Logical Operations:
Perform bitwise logical operations like AND, OR, XOR, and complement.
ANL (Logical AND): Performs a bitwise AND operation between the accumulator
and another register or memory location.
o Example: ANL A, R3 performs a bitwise AND between A and R3.
ORL (Logical OR): Performs a bitwise OR operation between the accumulator and
another register or memory location.
o Example: ORL A, 40H performs a bitwise OR between A and the contents of
memory location 40H.
XRL (Logical Exclusive OR): Performs a bitwise XOR operation between the
accumulator and another register or memory location.
o Example: XRL A, R4 performs a bitwise XOR between A and R4.
CLR (Clear): Clears the contents of a register or bit, setting it to 0.
o Example: CLR A clears the accumulator, setting it to 0.
CPL (Complement): Complements the contents of a register or bit, flipping all the
bits (0 to 1, and 1 to 0).
o Example: CPL A complements the contents of the accumulator.
RL (Rotate Left): Rotates the bits of the accumulator left one bit position.
o Example: RL A rotates the bits of the accumulator left.
RLC (Rotate Left through Carry): Rotates the bits of the accumulator left through
the carry flag.
M S Khokhar
4 Micro Controller in Instrumentation
o Example: RLC A rotates the bits of the accumulator left, including the carry
flag.
RR (Rotate Right): Rotates the bits of the accumulator right one bit position.
o Example: RR A rotates the bits of the accumulator right.
RRC (Rotate Right through Carry): Rotates the bits of the accumulator right
through the carry flag.
o Example: RRC A rotates the bits of the accumulator right, including the carry
flag.
4. Stack Operations:
Instructions that involve pushing and popping data to and from the stack.
PUSH (Push onto Stack): Saves the contents of a register onto the stack.
o Example: PUSH 01H pushes the contents of register R1 onto the stack.
POP (Pop from Stack): Retrieves the last saved value from the stack and places it
into a specified register.
o Example: POP 01H pops the top of the stack into register R1.
5. Jump Instructions:
These instructions alter the flow of execution based on conditions or specific instructions.
SJMP (Short Jump): Jumps to a relative address within a range of -128 to +127
bytes from the current PC (Program Counter).
o Example: SJMP LABEL jumps to the label if it is within the range.
LJMP (Long Jump): Jumps to a specified 16-bit address anywhere within the 64 KB
memory space.
o Example: LJMP 3000H jumps to address 3000H.
AJMP (Absolute Jump): Jumps to a specified address within the same 2K page of
program memory.
o Example: AJMP 0100H jumps to address 0100H within the same page.
JZ (Jump if Zero): Jumps to a specified address if the accumulator is zero.
o Example: JZ LABEL jumps to LABEL if A = 0.
JNZ (Jump if Not Zero): Jumps to a specified address if the accumulator is not zero.
o Example: JNZ LABEL jumps to LABEL if A ≠ 0.
JC (Jump if Carry): Jumps to a specified address if the carry flag is set.
o Example: JC LABEL jumps to LABEL if carry = 1.
JNC (Jump if No Carry): Jumps to a specified address if the carry flag is not set.
o Example: JNC LABEL jumps to LABEL if carry = 0.
DJNZ (Decrement and Jump if Not Zero): Decrements the contents of a register
and jumps to a specified address if the result is not zero.
o Example: DJNZ R2, LABEL decrements R2 and jumps to LABEL if R2 ≠ 0.
CJNE (Compare and Jump if Not Equal): Compares the contents of a register with
an immediate value or another register and jumps to a specified address if they are not
equal.
o Example: CJNE A, #05H, LABEL compares A with 05H and jumps to
LABEL if A ≠ 05H.
6. Loop Instructions:
M S Khokhar
5 Micro Controller in Instrumentation
7. Call Instructions:
Call instructions are used to invoke subroutines, which are blocks of code that can be
executed and then return control back to the main program. These instructions allow for
modularity and reusability of code.
o
Used for calling subroutines in programs.
Example: LCALL SUBROUTINE – Calls the subroutine labeled
o
SUBROUTINE.
ACALL (Absolute Call):
o This instruction calls a subroutine located within the same 2KB page of
memory. The program counter is saved onto the stack, and control is
transferred to the subroutine. After the subroutine finishes, the RET instruction
is used to return to the main program.
o Syntax: ACALL label
o Example:
MAIN: ACALL SUBROUTINE ; Call subroutine
M S Khokhar
6 Micro Controller in Instrumentation
...
SJMP MAIN ; Main loop
SUBROUTINE:
; Subroutine code here
RET ; Return from subroutine
Here, ACALL SUBROUTINE transfers control to the subroutine
labeled SUBROUTINE. The RET instruction returns control to the
instruction following the ACALL.
LCALL (Long Call):
o This instruction calls a subroutine located anywhere within the 64KB code
memory space. Similar to ACALL, the program counter is saved onto the
stack, and control is transferred to the subroutine.
o Syntax: LCALL label
o Example:
LCALL DELAY ; Call delay subroutine located anywhere in code space
RET (Return from Subroutine):
o The RET instruction is used to return from a subroutine to the point where the
subroutine was called. The address is retrieved from the stack.
o Syntax: RET
RETI (Return from Interrupt):
o Similar to RET, but it is used specifically to return from an interrupt service
routine (ISR). It also clears the interrupt flag.
o Syntax: RETI
Summary
Loop instructions like DJNZ and CJNE help in repeating a block of code multiple
times.
Call instructions like ACALL and LCALL allow for calling subroutines, making
programs modular and organized.
RET and RETI return control to the main program or interrupt routine after
executing a subroutine or interrupt.
M S Khokhar
7 Micro Controller in Instrumentation
The structure of an assembly language program for the 8051 microcontroller follows a
standard format. It includes sections like the origin directive, code for initialization,
instructions, and termination.
1. Header (Optional)
o Comments or program title to describe the program.
o For example, ; LED Blink Program
2. Directives
o ORG: Used to set the origin or starting address of the program.
o END: Marks the end of the program.
Example:
ORG 0000H ; Set starting address
3. Code Segment
o The actual program logic begins after the origin. This includes data
movement, arithmetic operations, control flow instructions, and subroutines.
4. Subroutines (Optional)
o Modular code blocks to perform specific tasks like delay, data retrieval, etc.
o Example: ACALL DELAY (A subroutine call for a delay function)
5. Comments
o Begin with a ; and are used to describe the purpose of the code or instruction.
START:
MOV A, #10H ; Load immediate value 10H into Accumulator
MOV R0, #20H ; Load immediate value 20H into Register R0
ADD A, R0 ; Add the value in R0 to Accumulator
To execute an assembly language program on the 8051 microcontroller, several steps are
involved, from writing the code to loading it into the microcontroller. Each step involves
different tools like editors, assemblers, and linkers.
M S Khokhar
8 Micro Controller in Instrumentation
In Details:
1. Editor:
o An editor is used to write the assembly code. This is simply a text editor where
you create and save the .asm (assembly language) file.
o Example: Notepad, Keil µVision editor.
Process:
Write the assembly program and save it with the extension .asm.
Example:
ORG 0000H
MOV A, #55H
END
2. Assembler:
o The assembler converts the human-readable assembly code into machine code
(object code), which is understandable by the microcontroller.
o The output is typically an object file with a .obj or .hex extension.
Process: Assembly language code (.asm) → Object file (.obj or .hex).
Example Tool: Keil Assembler, MASM, TASM.
3. Cross-Compiler:
o A cross-compiler is used when you're developing code on a host system but
need the code to run on a different target system (like the 8051
microcontroller).
o It converts high-level code or assembly code into binary code that can be
executed on the 8051.
Process: Compile the code to match the target microcontroller's instruction set.
4. Linker:
o The linker combines multiple object files or modules into a single executable
file. If a program is too large or has multiple parts (e.g., libraries), the linker
merges them into a single .hex file.
Process: Object files (.obj) → Single executable file (.hex).
Example Tool: Keil Linker.
5. Locator:
o The locator assigns the physical addresses in the memory space for the code
and data. It maps the code to the specific memory locations in the
microcontroller.
o Process: Map addresses to memory space.
M S Khokhar
9 Micro Controller in Instrumentation
6. Compiler:
o If the program is written in a high-level language (such as C), the compiler
translates the high-level code into assembly language or machine code.
However, for pure assembly language programs, the compiler step is often not
required.
Process: High-level code (.c) → Machine code.
8. Loader:
o The loader transfers the .hex file into the 8051 microcontroller's memory for
execution. This step involves flashing the program into the microcontroller.
Example Tool: Flash programmer, Keil µVision.
The Program Counter (PC) is a special-purpose 16-bit register in the 8051 microcontroller
that holds the memory address of the next instruction to be executed.
KEY FEATURES:
1. Holds the Address of the Next Instruction:
o The PC always points to the address in the memory (ROM) where the next
instruction is located.
o After fetching the current instruction, the PC is automatically incremented to
point to the next instruction.
2. Starts from 0000H:
o After a system reset, the PC is initialized to 0000H, meaning the program
execution starts from the beginning of the memory space.
3. Sequential Instruction Execution:
o Normally, the PC is incremented by 1 after each instruction, meaning the
instructions are executed sequentially from memory.
o For example, if the PC holds the value 0002H, after executing the current
instruction, it will automatically move to 0003H (the address of the next
instruction).
4. Program Flow Control:
o The PC is modified by instructions like JMP, CALL, RET, and others that
alter the flow of the program.
M S Khokhar
10 Micro Controller in Instrumentation
o Example: A JMP instruction will load a new address into the PC, causing the
program to jump to a different location in the ROM.
5. 16-Bit Capacity:
o The PC is 16 bits wide, which means it can address up to 64KB (65536
addresses) of memory space.
The Read-Only Memory (ROM) in the 8051 microcontroller is used to store the program
code. It is non-volatile, meaning the data remains even when the power is turned off.
KEY FEATURES:
1. Internal ROM Size:
o The 8051 microcontroller typically has 4KB of internal ROM, although this
can vary depending on the specific version of the 8051 family (e.g., 8031 has
no ROM, 8052 has 8KB).
2. Program Storage:
o The ROM is used to store the program code that is executed by the
microcontroller. The instructions are fetched from the ROM as the program
runs.
o Program execution begins from address 0000H after reset, as the PC starts
from this address.
3. ROM Address Space:
o The internal ROM in 8051 microcontrollers is mapped from address 0000H
to 0FFFH (for 4KB).
o If the program code exceeds the internal ROM space, external ROM can be
used, and the microcontroller can be configured to fetch instructions from
external memory.
4. Access to External ROM:
o The 8051 can access external ROM by using the MOVX instruction or by
connecting to external memory using the /PSEN (Program Store Enable)
pin. This allows the program code to exceed the internal ROM capacity.
5. Code Memory (ROM) Expansion:
o For programs that require more memory than what the internal ROM provides,
external ROM up to 64KB can be connected.
o The EA (External Access) pin is used to switch between internal and external
ROM. If the EA pin is tied low, the 8051 will only use external ROM.
M S Khokhar
11 Micro Controller in Instrumentation
SUMMARY:
The Program Counter (PC) in the 8051 microcontroller holds the address of the next
instruction to be executed and is critical for controlling program flow.
The ROM space is where the program is stored and is non-volatile. The 8051
typically has 4KB of internal ROM, but it can access external ROM if needed.
The interaction between the PC and ROM is central to program execution, with the
PC fetching instructions sequentially from ROM unless directed otherwise by jump or
call instructions.
In 8051 Assembly Language Programming, the Program Status Word (PSW) register is a
key component in the functioning of the microcontroller. It contains various flag bits that
provide information about the outcome of arithmetic and logical operations.
The PSW register is an 8-bit register that holds important status flags and control bits, which
indicate the current state of the microcontroller's operations. These flags reflect the results of
arithmetic and logical operations.
Bit 7 6 5 4 3 2 1 0
Name CY AC F0 RS1 RS0 OV - P
CY (Carry Flag, PSW.7): Used for arithmetic operations, especially in addition and
subtraction. It indicates if a carry or borrow has occurred from the most significant
bit.
M S Khokhar
12 Micro Controller in Instrumentation
AC (Auxiliary Carry Flag, PSW.6): Indicates a carry from the lower nibble (bit 3)
to the higher nibble (bit 4) in binary-coded decimal (BCD) operations.
o Set (1): When there's a carry from bit 3 to bit 4.
o Cleared (0): No carry occurs between bit 3 and bit 4.
o Example:
ADD A, #09H ; If A = 07H, AC is set due to carry from bit 3 to 4
RS1, RS0 (Register Bank Select Bits, PSW.4 and PSW.3): These two bits are used
to select one of the four register banks (Bank 0, Bank 1, Bank 2, Bank 3) in the 8051
microcontroller. Each register bank consists of eight registers (R0-R7).
o Example:
MOV PSW, #18H ; Select Bank 3
OV (Overflow Flag, PSW.2): This flag is set when an arithmetic overflow occurs,
i.e., when the result of a signed arithmetic operation is too large to fit in the
destination register.
o Set (1): When an overflow occurs.
o Cleared (0): No overflow occurs.
o Example:
ADD A, #80H ; If A is 80H, OV is set due to overflow
(Unused Bit, PSW.1): This bit is not used and is reserved for future use.
P (Parity Flag, PSW.0): This flag reflects the parity (even or odd) of the
accumulator. It is automatically set or cleared after each operation to indicate whether
the number of 1s in the accumulator is even or odd.
o Set (1): If the number of 1s in the accumulator is odd (odd parity).
o Cleared (0): If the number of 1s is even (even parity).
o Example:
MOV A, #55H ; P is set because the number of 1s in 55H is odd (01010101)
M S Khokhar
13 Micro Controller in Instrumentation
In 8051 Assembly Language Programming, the register bank and stack are essential
components that provide memory for data manipulation and temporary storage during
program execution.
The register bank in the 8051 microcontroller refers to a set of 8 registers (R0-R7) that can
be used for general-purpose data storage and manipulation. These registers are located in the
internal RAM (address 00H to 1FH) and are divided into four banks.
M S Khokhar
14 Micro Controller in Instrumentation
o Example:
MOV PSW, #08H ; Select Bank 1 (08H-0FH)
MOV R0, #55H ; Load 55H into R0 of Bank 1 (address 08H)
STACK IN 8051
The stack is a special region of the internal RAM used for temporary storage of data during
program execution. It is particularly useful for saving the return addresses during subroutine
calls, interrupt handling, and for temporary variable storage.
M S Khokhar
15 Micro Controller in Instrumentation
custom region of RAM for the stack, useful when working with larger
programs that require more internal RAM.
5. Stack Overflow:
o Since the stack grows upwards and the internal RAM is limited (128 bytes), if
the stack pointer exceeds the available memory range, a stack overflow
occurs, potentially overwriting other important data in memory. Care should
be taken to prevent the stack from growing beyond the allocated space.
POP ACC ; Pop the accumulator from the stack (A = original value)
In this example:
The program uses Register Bank 1 to store data in R0.
The stack is used to save and restore the accumulator's value temporarily, preventing
data loss during program execution.
SUMMARY:
Register Banks: The 8051 has four register banks (R0-R7), and the active register
bank is selected using the PSW register's RS bits. Register banks allow efficient data
handling and switching between different sets of registers, particularly useful during
interrupts.
Stack: The stack is used for temporary storage, particularly for return addresses
during subroutine calls and interrupts. The SP register points to the top of the stack,
and the stack grows upwards in memory. The PUSH and POP instructions are used to
manipulate the stack.
M S Khokhar
16 Micro Controller in Instrumentation
Although assembly language is low-level and does not provide the same range of data types
as high-level languages, it does support basic data types that represent numbers and
characters.
1. Byte (8-bit):
o The basic data type in 8051 assembly language is a byte, which is an 8-bit
value.
o Values range from 00H to FFH in hexadecimal (0 to 255 in decimal).
o Example:
MOV A, #25H ; Move 25H (8-bit value) into accumulator A
2. Bit (1-bit):
o The 8051 supports bit-level operations. Each bit can be either 0 or 1.
o Example:
SETB P1.0 ; Set bit 0 of Port 1 to 1
1. DB (DEFINE BYTE)
Purpose: The DB directive is used to define and allocate memory for byte-sized data.
It is often used to store constants, ASCII characters, or tables of data in memory.
Syntax:
label_name DB byte_value
Example:
MY_DATA DB 0FFH ; Define byte data 0FFH
TEXT_MSG DB 'HELLO' ; Define ASCII string 'HELLO'
o Explanation:
MY_DATA is a label assigned to the memory location where the byte
0FFH is stored.
TEXT_MSG defines a series of ASCII characters, where each
character takes 1 byte in memory.
M S Khokhar
17 Micro Controller in Instrumentation
2. ORG (ORIGIN)
Purpose: The ORG directive is used to specify the starting address for the following
block of code or data. It tells the assembler to place the subsequent instructions or
data at the specified address in memory.
Syntax:
ORG address
Example:
ORG 0000H ; Set starting address to 0000H
MOV A, #55H ; Start of the program at address 0000H
M S Khokhar
18 Micro Controller in Instrumentation
In this example:
The program starts at memory address 0000H due to the ORG 0000H directive.
A constant MAX_VAL is defined using the EQU directive, which is used in the
program to load a value into the accumulator.
The DB directive is used to define a data table starting at address 0030H.
The END directive marks the end of the program.
SUMMARY:
DB (Define Byte): Used to allocate memory and store byte-sized data, such as
constants or strings.
ORG (Origin): Specifies the starting address for code or data.
EQU (Equate): Defines constants for better readability and maintainability.
END: Marks the end of the program, signalling the assembler to stop processing.
M S Khokhar