0% found this document useful (0 votes)
6 views18 pages

Unit-II: 8051 Assembly Language Programming: 2.1 Addressing Modes in 8051 Microcontroller

Microcontroller in instrumental and control

Uploaded by

chavdamayur440
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)
6 views18 pages

Unit-II: 8051 Assembly Language Programming: 2.1 Addressing Modes in 8051 Microcontroller

Microcontroller in instrumental and control

Uploaded by

chavdamayur440
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/ 18

1 Micro Controller in Instrumentation

Unit-II: 8051 Assembly Language Programming

2.1 Addressing Modes in 8051 Microcontroller


Addressing modes define the way in which the operand of an instruction is specified. The
8051 microcontroller supports several addressing modes:

1. Immediate Addressing Mode:


o In this mode, the operand is specified directly in the instruction itself.
o The value of the operand is provided immediately after the opcode.
o Example: MOV A, #25H
 Here, the accumulator A is loaded with the immediate value 25H.

2. Register Addressing Mode:


o In this mode, the operand is specified as a register.
o The data is directly moved between registers.
o Example: MOV A, R1
 Here, the content of register R1 is moved to the accumulator A.

3. Direct Addressing Mode:


o The operand is specified by the address of a memory location.
o The data is accessed directly from the specified address.
o Example: MOV A, 30H
 The content of memory location 30H is moved to the accumulator A.

4. Indirect Addressing Mode:


o The address of the operand is specified indirectly through a register.
o Registers R0 or R1 are often used to hold the memory address.
o Example: MOV A, @R0
 The content of the memory location pointed to by R0 is moved to the
accumulator A.

5. Indexed Addressing Mode:


o Used to access data stored in a lookup table in program memory.
o The address of the operand is the sum of a base register and an offset value.
o Example: MOVC A, @A+DPTR ; here C means Code
 The content of the program memory, pointed to by A + DPTR, is
moved to the accumulator A.

A Look-Up Table (LUT) in the 8051 microcontroller is a predefined table stored in


memory (typically in ROM or Flash memory) that allows for quick data retrieval
based on a known index or key. Look-up tables are often used in situations where a
program needs to repeatedly access specific values without recalculating them each
time. Instead of performing complex computations, the 8051 microcontroller can
quickly retrieve the value from the table, improving efficiency.

6. Relative Addressing Mode:


o Used in branch instructions, where the operand specifies the address relative to
the current program counter.

M S Khokhar
2 Micro Controller in Instrumentation

o Example: SJMP LABEL


 The program jumps to the address specified by LABEL, relative to the
current PC value.

7. Bit Addressing Mode:


o Used to access and manipulate individual bits within a byte.
o The 8051 has a special bit-addressable area in the internal RAM.
o Example: SETB P1.0
 This instruction sets bit 0 of Port 1.

External Data Moves


 External data moves involve moving data to and from external memory.
 MOVX instructions are used to access external memory.
o Example: MOVX A, @DPTR
 The content of the external memory, pointed to by DPTR, is moved to
the accumulator A.

PUSH and POP Opcodes


 PUSH: This instruction is used to save data onto the stack.
o Example: PUSH A
 The content of the accumulator A is pushed onto the stack.
 POP: This instruction is used to retrieve data from the stack.
o Example: POP A
 The data on the top of the stack is popped into the accumulator A.
2.2 Instructions

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.

1. Moving Data Instructions:

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:

Perform basic arithmetic like addition, subtraction, multiplication, and division.

 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

Loops allow a set of instructions to be executed repeatedly, based on certain conditions.


Loop control is typically done using conditional or unconditional jump instructions.

o Used for creating loops in programs.


o Example: DJNZ R2, LABEL – Decrements R2 and jumps to LABEL if R2 is
not zero.
 DJNZ (Decrement and Jump if Not Zero):
o This instruction is used to create loops. It decrements a register or memory
location and jumps to a specified label if the result is not zero.
o Syntax: DJNZ Rn, label
o Example:
MOV R0, #5 ; Load 5 into register R0
LOOP: DJNZ R0, LOOP ; Decrement R0, if not zero jump to LOOP
 Here, the value of R0 is decremented by 1 each time the DJNZ
instruction is executed. The loop continues until R0 becomes zero.
 JMP (Jump):
o The JMP instruction transfers control to another part of the program
unconditionally. This is often used in combination with a conditional
instruction to repeat certain sections of code.
o Syntax: SJMP label or LJMP label
 SJMP: Short jump, used for jumping within a 256-byte range.
 LJMP: Long jump, allows jumping anywhere within the 64KB code
memory.
 CJNE (Compare and Jump if Not Equal):
o This is a conditional jump instruction that compares two values (one in a
register or memory and the other immediate or in a register). If the values are
not equal, the program jumps to a specified label.
o Syntax: CJNE A, #data, label
o Example:
MOV A, #5 ; Load accumulator with 5
CJNE A, #5, NEXT ; Compare A with 5, if not equal jump to NEXT

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

Example: Loop and Call Combined


MAIN: MOV R0, #10 ; Load R0 with 10 (for loop)
ACALL DELAY ; Call delay subroutine
DJNZ R0, MAIN ; Decrement R0, if not zero, repeat loop

DELAY: MOV R1, #255 ; Inner loop for delay


DELAY_LOOP: DJNZ R1, DELAY_LOOP
RET ; Return to the main program
In this example:
 A loop is created using the DJNZ instruction, where the program calls the DELAY
subroutine 10 times.
 The DELAY subroutine contains another loop for generating a delay, and after
completion, the RET instruction returns control to the main program.

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.

2.3 The Structure of Assembly Language Programming with Examples

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.

STRUCTURE OF ASSEMBLY LANGUAGE PROGRAM

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.

EXAMPLE PROGRAM: SIMPLE DATA MOVEMENT AND ADDITION


ORG 0000H ; Set the starting address

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

SJMP START ; Infinite loop

END ; End of program

EXPLANATION OF THE EXAMPLE PROGRAM:


 ORG 0000H: The program starts at address 0000H.
 MOV A, #10H: Loads the immediate value 10H into the accumulator A.
 MOV R0, #20H: Loads the immediate value 20H into register R0.
 ADD A, R0: Adds the content of R0 to the accumulator A.
 SJMP START: Keeps the program running in a loop by jumping back to the START
label.

2.4 ASSEMBLING AND RUNNING AN 8051 PROGRAM

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

2.4.1 STEPS OF ASSEMBLY LANGUAGE PROGRAMMING:

1. Editor: Write the code in a text editor.


2. Assembler: Converts the assembly code into machine code (object file).
3. Cross-Compiler: Converts high-level language into machine code.
4. Linker: Combines multiple object files into a single executable file.
5. Locator: Assigns memory locations for variables and code.
6. Compiler: Translates the entire program into a binary executable file.

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.

7. Simulator and Debugger (Optional):


o Before loading the program into the microcontroller, simulators like EdSim51
or Proteus can simulate the code behavior.
o The debugger helps find and correct any errors by stepping through the
program.

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.

Flow of Program Execution:


1. Write the program using an Editor (save as .asm).
2. Assemble the program using an Assembler to generate object code.
3. Use a Linker to combine object files into a .hex file.
4. (Optional) Use a Locator to assign memory addresses.
5. (Optional) Use a Cross-Compiler if necessary to match the target microcontroller.
6. Use a Loader to upload the program to the 8051 microcontroller.

2.4.2 THE PROGRAM COUNTER AND ROM SPACE IN 8051:

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.

ROM SPACE IN 8051

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.

INTERACTION BETWEEN THE PROGRAM COUNTER AND ROM

1. Fetching Instructions from ROM:


o The PC fetches the instructions stored in ROM. When the 8051 starts, the PC
starts at 0000H, and the first instruction is fetched from ROM address 0000H.
The PC then increments after each instruction fetch.
2. Jumps and Calls:
o When executing JMP, CALL, or RET instructions, the program counter is
updated with the new memory address, and the corresponding instruction is
fetched from that ROM location.
o Example: A long jump (LJMP) to 2000H will load the address 2000H into
the PC, and the next instruction will be fetched from ROM address 2000H.

M S Khokhar
11 Micro Controller in Instrumentation

3. Addressing External ROM:


o If the program is stored in external ROM, the 8051 fetches the instructions
from external memory. The program counter still points to the location in the
external ROM, and the instruction is fetched from there.

EXAMPLE OF ROM AND PC INTERACTION:

Consider the following code stored in ROM:


ORG 0000H ; Start at address 0000H
MOV A, #55H ; Instruction at 0000H
ADD A, #0AH ; Instruction at 0002H
SJMP HERE ; Instruction at 0004H, jump to label "HERE"
HERE: NOP ; No operation at address 0006H
 The PC starts at 0000H after reset and fetches the MOV A, #55H instruction.
 The PC is then incremented to 0002H to fetch the ADD A, #0AH instruction.
 The PC moves to 0004H to fetch SJMP HERE. This is a short jump, so the PC is
loaded with 0006H (the address of HERE).
 The PC then fetches the NOP instruction from 0006H.

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.

2.4.3 8051 FLAG BITS AND PSW REGISTER:

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.

PROGRAM STATUS WORD (PSW) REGISTER

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.

PSW REGISTER FORMAT:

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

o Set (1): When there's a carry or borrow.


o Cleared (0): No carry or borrow.
o Example:
ADD A, R1 ; If carry occurs, CY is set to 1

 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

 F0 (User-Defined Flag, PSW.5): This is a general-purpose flag that can be used by


the programmer for any desired purpose. It does not change automatically during
program execution.

 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).

RS1 RS0 Register Bank


0 0 Bank 0 (00H-07H)
0 1 Bank 1 (08H-0FH)
1 0 Bank 2 (10H-17H)
1 1 Bank 3 (18H-1FH)

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

2.4.4 8051 REGISTER BANK AND STACK:

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.

REGISTER BANK IN 8051

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.

KEY POINTS ABOUT REGISTER BANKS:

1. Four Register Banks:


o The 8051 has four register banks, each consisting of 8 registers (R0 to R7).
o These banks are located at different memory locations within the internal
RAM.

Register Bank RAM Address


Bank 0 00H to 07H
Bank 1 08H to 0FH
Bank 2 10H to 17H
Bank 3 18H to 1FH

2. Selection of Register Banks:


o The active register bank is selected using the RS0 (PSW.3) and RS1 (PSW.4)
bits in the Program Status Word (PSW) register.
o By setting these bits, the 8051 microcontroller determines which bank of
registers (R0-R7) will be used in the program.

RS1 RS0 Register Bank


0 0 Bank 0 (00H-07H)
0 1 Bank 1 (08H-0FH)
1 0 Bank 2 (10H-17H)
1 1 Bank 3 (18H-1FH)

3. Advantages of Using Register Banks:


o By switching between register banks, you can manage different sets of data
without changing the actual data stored in the registers.
o This feature is particularly useful in interrupt handling, where the
microcontroller can switch to a different register bank to handle the interrupt
routine without disturbing the main program's data.

M S Khokhar
14 Micro Controller in Instrumentation

4. Instructions with Register Banks:


o Instructions like MOV, ADD, and SUBB can be used with registers R0-R7,
and depending on the selected register bank, the memory locations for these
registers will vary.

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.

KEY FEATURES OF THE STACK:

1. Stack Pointer (SP):


o The Stack Pointer (SP) is an 8-bit register in the 8051 that holds the address
of the top of the stack.
o After a reset, the SP is initialized to 07H, meaning the stack starts from
memory location 08H (just after register bank 0).
o The stack grows upwards as data is pushed onto it, i.e., each new item is
stored in the next available memory location.

2. Pushing and Popping Data:


o Push: When data is pushed onto the stack, the SP is incremented by 1, and the
data is stored at the new memory location pointed to by the SP.
o Pop: When data is popped from the stack, the SP is decremented by 1, and the
data is retrieved from the memory location it points to.
o Instructions for stack operations include:
 PUSH <src>: Pushes the contents of a register or memory location
onto the stack.
 POP <dest>: Pops the top value of the stack into a register or memory
location.
o Example:
MOV A, #55H ; Load 55H into A
PUSH ACC ; Push the value in A (55H) onto the stack
POP B ; Pop the top value of the stack into B (B = 55H)

3. Stack Use During Subroutines and Interrupts:


o The stack is used to store the return address during subroutine (CALL, RET)
instructions and interrupts. When a subroutine or interrupt occurs, the address
of the next instruction is pushed onto the stack, allowing the program to return
to that location after the subroutine or interrupt has completed.

4. Customizing the Stack:


o The default stack location (after register bank 0) can be changed by manually
setting the SP to a different address. This allows the programmer to allocate a

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.

INTERACTION BETWEEN REGISTER BANK AND STACK

1. Register Bank Impact on Stack Operations:


o If a program is using different register banks, the stack operations (PUSH and
POP) do not directly affect the register banks. However, since the stack and
register banks share the same internal RAM space, careful memory
management is necessary to avoid conflicts between register banks and stack
data.

2. Register Banks and Subroutines:


o During subroutine calls, it’s often useful to switch to a different register bank.
This allows the subroutine to use its own set of registers without altering the
data in the main program.
o After the subroutine completes, the program can switch back to the original
register bank.

EXAMPLE PROGRAM USING REGISTER BANK AND STACK

MOV PSW, #08H ; Select Register Bank 1


MOV R0, #12H ; Load 12H into R0 of Bank 1

PUSH ACC ; Push the accumulator onto the stack


MOV A, R0 ; Copy R0 into A (A = 12H)

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

2.5 8051 DATA TYPES AND DIRECTIVES


1. DB (Define Byte):
o Used to define data in byte format.
o Example: DATA DB 0AH stores 0AH in memory.
2. ORG (Origin):
o Sets the starting address for the code.
o Example: ORG 0000H sets the program's starting address to 0000H.
3. EQU (Equate):
o Defines a constant value.
o Example: COUNT EQU 05H defines COUNT as 05H.
4. END:
o Marks the end of the program.
o Example: END tells the assembler that the program has finished.

8051 DATA TYPES

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

DIRECTIVES IN 8051 ASSEMBLY LANGUAGE


Directives are commands used by the assembler to guide the conversion of assembly code
into machine code. Unlike instructions, directives are not executed by the microcontroller at
runtime; instead, they tell the assembler how to process the code.
Here are the most commonly used directives:

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

ORG 0030H ; Set new starting address to 0030H


MY_TABLE DB 10H, 20H, 30H ; Data stored at address 0030H
o Explanation:
 The program starts execution from address 0000H. The ORG directive
can be used to organize code and data in different sections of memory.
3. EQU (EQUATE)
 Purpose: The EQU directive is used to define a constant. Once defined, the constant
can be used anywhere in the program by referring to its label. This makes programs
easier to read and maintain.
 Syntax:
label_name EQU constant_value
 Example:
MAX_VALUE EQU 100 ; Define a constant MAX_VALUE = 100
MOV A, #MAX_VALUE ; Use the constant in an instruction
o Explanation:
 MAX_VALUE is a constant with the value 100. Instead of hard-
coding the value 100 throughout the program, the constant
MAX_VALUE is used, making the program more readable and easier
to modify.
4. END (END PROGRAM)
 Purpose: The END directive tells the assembler that this is the end of the program or
source file. The assembler stops processing the file at this point.
 Syntax:
END
 Example:
ORG 0000H
MOV A, #55H ; Sample instruction
END ; End of the program
o Explanation:
 After the END directive, no more instructions or code will be
assembled. This is used to mark the completion of the assembly
program.

EXAMPLE PROGRAM USING DIRECTIVES

ORG 0000H ; Start program at address 0000H


MOV A, #MAX_VAL ; Move constant value into A
MOV P1, A ; Output the value to Port 1

ORG 0030H ; Data starts at address 0030H

M S Khokhar
18 Micro Controller in Instrumentation

MY_TABLE DB 0AH, 0BH, 0CH ; Define a data table

MAX_VAL EQU 55H ; Define a constant value

END ; End of the program

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

You might also like