8051 Microcontroller: Branching, Looping, and Bit Manipulation Instructions

Overview of Module 5 and 8051 Memory Organization

Module 5 focuses on LOOP and CALL instructions, Assembly Language Programming (ALP), and interfacing techniques for the 8051 microcontroller. Covered topics include the generation of Square, Sine, and Triangular waveforms using Digital-to-Analog Converter (DAC) interfacing, as well as Stepper motor interfacing. Key references include Text 3, sections 3.1, 3.2, 13.2, and 17.2.

8051 Memory Specifications

The 8051 microcontroller features distinct memory spaces with specific sizes and address ranges:

  • Internal RAM: 128128 bytes, residing in the address range 00H00H to 7FH7FH.
  • Internal ROM: 4 KB4\,KB, residing in the address range 0000H0000H to 0FFFH0FFFH.
  • External RAM: Up to 64 KB64\,KB, spanning the address range 0000H0000H to FFFFHFFFFH.
  • External ROM: Up to 64 KB64\,KB, spanning the address range 0000H0000H to FFFFHFFFFH.

Memory Paging and Program Memory

The total program memory of the 8051 is 64 KB64\,KB, covering addresses from 0000H0000H to FFFFHFFFFH. This memory is architecturally divided into 3232 pages to facilitate specific jump and call instructions. Each page is exactly 2 KB2\,KB (20482048 bytes) in size.

  • Page 0: 0000H0000H to 07FFH07FFH
  • Page 1: 0800H0800H to 0FFFH0FFFH
  • Page 2: 1000H1000H to 17FFH17FFH
  • … and continuing through the address space.

Unconditional Jump Instructions

Unconditional jumps are branching instructions used to control program flow without checking any specific flags or conditions.

SJMP (Short Jump)

  • Description: Performs a jump within a restricted short range.
  • Range: −128-128 to +127+127 bytes from the current program counter.
  • Syntax: SJMP label
  • Example Code:assembly MOV A, #00H SJMP SKIP MOV A, #FFH SKIP: NOP ; Accumulator A remains 00H     

LJMP (Long Jump)

  • Description: Allows the program to jump to any location within the entire 64 KB64\,KB program memory space.
  • Syntax: LJMP address
  • Example: LJMP 2000H (Jumps to memory address 2000H2000H).\n

AJMP (Absolute Jump)

  • Description: Jumps to an address within the same 2 KB2\,KB memory page currently being accessed by the program counter.
  • Syntax: AJMP address
  • Example: AJMP 0100H

Conditional Jump Instructions

These instructions execute a jump only if a specific condition (determined by flags or bit states) is met.

Accumulator-Based Jumps

  • JZ (Jump if Zero): Jumps to the specified label if the Accumulator (AA) is exactly zero.
    • Syntax: JZ label
    • Example:assembly MOV A, #00H JZ ZERO_LABEL ; Jump occurs because A = 0         
  • JNZ (Jump if Not Zero): Jumps to the specified label if the Accumulator (AA) is not zero.
    • Syntax: JNZ label
    • Example:assembly MOV A, #05H JNZ NOT_ZERO ; Jump occurs because A = 5         

Carry Flag-Based Jumps

  • JC (Jump if Carry): Jumps if the Carry Flag (CC) is set to 11.
    • Syntax: JC label
    • Example:assembly SETB C JC CARRY_LABEL ; Jump occurs         
  • JNC (Jump if No Carry): Jumps if the Carry Flag (CC) is cleared to 00.
    • Syntax: JNC label
    • Example:assembly CLR C JNC NO_CARRY ; Jump occurs         

Bit-Based Jumps

  • JB (Jump if Bit Set): Jumps to the label if the specified bit is 11.
    • Syntax: JB bit, label
    • Example:assembly SETB P1.0 JB P1.0, BIT_SET ; Jump occurs         
  • JNB (Jump if Bit Not Set): Jumps to the label if the specified bit is 00.
    • Syntax: JNB bit, label
    • Example:assembly CLR P1.0 JNB P1.0, BIT_CLEAR ; Jump occurs         
  • JBC (Jump if Bit Set and Clear): Jumps if the bit is 11, then automatically clears that bit to 00 after the jump.
    • Syntax: JBC bit, label
    • Example:assembly SETB P1.0 JBC P1.0, LABEL ; Jumps and then sets P1.0 to 0         

Loop Control Instructions

DJNZ (Decrement and Jump if Not Zero)

  • Description: This is the primary instruction for creating loops. It decrements the specified register or memory location by 11 and performs a jump if the resulting value is not zero. If the value becomes zero, the program continues with the next instruction in sequence.
  • Syntax: DJNZ reg, label
  • Example (Looping 5 times):assembly MOV R0, #05H LOOP: DJNZ R0, LOOP ; Decrements R0; if R0 ≠ 0, jump to LOOP     

Subroutine Instructions

Subroutines are blocks of code that can be called from different parts of a program to perform specific tasks.

  • ACALL (Absolute Call): Calls a subroutine within the same 2 KB2\,KB memory range as the current program counter.
    • Syntax: ACALL address
    • Example: ACALL SUB1
  • LCALL (Long Call): Calls a subroutine located anywhere in the entire program memory space.
    • Syntax: LCALL address
    • Example: LCALL 3000H
  • RET (Return): Used at the end of a subroutine to return the program flow to the instruction following the original CALL.
    • Example:assembly SUB1: MOV A, #55H RET         
  • RETI (Return from Interrupt): A specialized return instruction used at the conclusion of an Interrupt Service Routine (ISR) to restore the program flow and signal that the interrupt processing is complete.

Bit Manipulation Instructions

These instructions allow for the manipulation of individual bits in the bit-addressable RAM and I/O ports.

Set, Clear, and Complement

  • SETB (Set Bit): Sets the specified bit or the Carry Flag to 11.
    • Syntax: SETB bit / SETB C
    • Examples: SETB P1.0 (Port 1 bit 0 = 11); SETB C (Carry = 11).
  • CLR (Clear Bit): Sets the specified bit or the Carry Flag to 00.
    • Syntax: CLR bit / CLR C
    • Examples: CLR P1.0 (Port 1 bit 0 = 00); CLR C (Carry = 00).
  • CPL (Complement Bit/Carry): Toggles the state of the bit (1→01 \rightarrow 0 or 0→10 \rightarrow 1).
    • Syntax: CPL bit / CPL C
    • Example (Toggling P1.0):assembly SETB P1.0 CPL P1.0 ; P1.0 changes from 1 to 0         

Bit Movement

  • MOV C, bit: Copies the state of the specified bit into the Carry Flag.
    • Syntax: MOV C, bit
    • Example: MOV C, P1.0 (Carry flag takes state of Port 1 bit 0).
  • MOV bit, C: Copies the context of the Carry Flag into the specified bit.
    • Syntax: MOV bit, C
    • Example: MOV P1.1, C (Sets bit P1.1 to match Carry).

Bit Logic Operations

  • ANL C, bit: Performs a logical AND between the Carry Flag and the specified bit; the result is stored in the Carry Flag.
    • Syntax: ANL C, bit
    • Example:assembly MOV C, P1.0 ANL C, P1.1 ; Carry = P1.0 AND P1.1         
  • ORL C, bit: Performs a logical OR between the Carry Flag and the specified bit; the result is stored in the Carry Flag.
    • Syntax: ORL C, bit
    • Example:assembly MOV C, P1.0 ORL C, P1.1 ; Carry = P1.0 OR P1.1