Branching Instructions in the 8051 Microcontroller

Branching Instructions in the 8051 Microcontroller

Verified Sources
May 20, 2026

In microcontroller programming, the execution of instructions is typically sequential; the Program Counter (PC) increments automatically to point to the next instruction in memory . However, creating dynamic applications requires altering this sequential Control Flow based on decisions or loop structures.

Branching instructions allow the 8051 microcontroller to diverge from sequential execution . These instructions are classified into two primary categories:

  1. Unconditional Branching: The program control jumps to a designated target address unconditionally.
  2. Conditional Branching: The program control jumps to a target address only if a specific condition (based on CPU flags or register states) is satisfied.

Understanding how these instructions manipulate the PC is critical for optimizing execution time and managing code memory spaces within the standard 64KB addressable Program Memory .

Footnotes

  1. Intel Corporation, MCS-51 Single-Chip Microcomputer Reference Manual. - Outlines the hardware architecture, register sets, program memory models, and PC manipulation details. 2

  2. Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay, The 8051 Microcontroller and Embedded Systems using Assembly and C. - Provides instruction-by-instruction functional analysis, execution cycles, and relative offset calculation methods.

Optimizing Code Size with Jump Types

Always prefer SJMP or AJMP over LJMP where possible. LJMP requires 3 bytes of program memory, whereas SJMP and AJMP require only 2 bytes . Utilizing shorter jumps when targeting nearby memory locations reduces overall code size and improves instruction cache efficiency.

Footnotes

  1. Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay, The 8051 Microcontroller and Embedded Systems using Assembly and C. - Provides instruction-by-instruction functional analysis, execution cycles, and relative offset calculation methods.

Unconditional jump instructions modify the Program Counter directly without checking any status flags .

1; --- Long Jump (LJMP) --- 2; Jumps to any 16-bit address location within the 64KB code space. 3LJMP 4000H ; PC is loaded with 4000H immediately (3-byte instruction) 4 5; --- Absolute Jump (AJMP) --- 6; Jumps within a 2KB page boundary of current PC. 7AJMP LOCAL_LABEL ; Target must be within same 2KB window (2-byte instruction) 8 9; --- Short Jump (SJMP) --- 10; Relative jump within -128 to +127 bytes from the next instruction. 11SJMP LOOP_START ; Relative offset calculated by assembler (2-byte instruction) 12 13; --- Indirect Jump (JMP @A+DPTR) --- 14; Jump relative to Data Pointer. Excellent for lookup/jump tables. 15MOV DPTR, #JUMP_TABLE 16MOV A, #02H 17JMP @A+DPTR ; PC is loaded with the sum of A and DPTR

Footnotes

  1. Intel Corporation, MCS-51 Single-Chip Microcomputer Reference Manual. - Outlines the hardware architecture, register sets, program memory models, and PC manipulation details.

Calculating the Relative Offset for Short Jumps

  1. 1
    Step 1

    The CPU fetches the SJMP opcode (80H) and its operand (the 8-bit Relative Offset) from program memory.

  2. 2
    Step 2

    The Program Counter (PC) automatically increments to point to the instruction immediately following the current SJMP instruction. Since SJMP is a 2-byte instruction, the PC is incremented by 2 (PCnext=PCcurrent+2PC_{next} = PC_{current} + 2).

  3. 3
    Step 3

    The 8-bit relative offset operand is converted into a 16-bit signed integer using Two's Complement representation. For example, an offset of FEH represents 2-2, while 05H represents +5+5.

  4. 4
    Step 4

    The CPU calculates the target branch address by adding the signed relative offset to the incremented Program Counter (PCtarget=PCnext+offsetPC_{target} = PC_{next} + \text{offset}). The resulting 16-bit address is loaded into the Program Counter to complete the branch.

Deep Dive: Complex Branching Instructions

Beyond simple flag-based checks, the 8051 features advanced conditional instructions that combine comparison, arithmetic, and branching into a single instruction cycle. These are highly efficient for loop counters and decision-making structures.

1. Compare and Jump if Not Equal (CJNE)

The CJNE instruction compares two operands and branches if they are not equal . Additionally, it sets the Carry Flag (CY) if the first operand is less than the second operand, serving as a basis for relational operations (<,>,=<, >, =):

If Operand 1<Operand 2    CY1\text{If } \text{Operand 1} < \text{Operand 2} \implies \text{CY} \leftarrow 1 If Operand 1Operand 2    CY0\text{If } \text{Operand 1} \ge \text{Operand 2} \implies \text{CY} \leftarrow 0

1; Syntax: CJNE destination, source, relative_address 2CJNE A, #50H, NOT_EQUAL ; Compare Accumulator with 50H 3; If A == 50H, execution continues here sequentially: 4EQUAL_FLOW: 5 ; Code for A = 50H 6 SJMP EXIT 7NOT_EQUAL: 8 JC LESS_THAN ; If CY is set (A < 50H), branch 9GREATER_THAN: 10 ; Code for A > 50H 11 SJMP EXIT 12LESS_THAN: 13 ; Code for A < 50H 14EXIT:

2. Decrement and Jump if Not Zero (DJNZ)

The DJNZ instruction is the fundamental loop-control construct in 8051 assembly . It decrements the specified register or memory location by 1 and branches to the target address if the resulting value is not zero. If the register reaches zero, execution falls through to the next sequential instruction.

1; Loop execution 10 times using Register R1 2 MOV R1, #10 ; Initialize counter to 10 3LOOP_START: 4 ; Perform task here 5 CPL P1.0 ; Toggle Port 1 Pin 0 6 DJNZ R1, LOOP_START ; Decrement R1, jump to LOOP_START if R1 != 0 7 ; Execution continues here after 10 iterations (R1 = 0)
InstructionSize (Bytes)CyclesFlags AffectedTarget Range
SJMP22None128 to +127 bytes-128 \text{ to } +127 \text{ bytes}
AJMP22NoneWithin same 2KB2\text{KB} page
LJMP32NoneEntire 64KB64\text{KB} space
CJNE32CY128 to +127 bytes-128 \text{ to } +127 \text{ bytes}
DJNZ2/32None128 to +127 bytes-128 \text{ to } +127 \text{ bytes}

Footnotes

  1. Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay, The 8051 Microcontroller and Embedded Systems using Assembly and C. - Provides instruction-by-instruction functional analysis, execution cycles, and relative offset calculation methods. 2

AJMP Page Boundary Hazards

The AJMP instruction replaces the lower 11 bits of the Program Counter with an immediate value encoded in the opcode, keeping the upper 5 bits of the PC unchanged . Ensure your jump target does not cross a 2KB memory page boundary (0800H,1000H,1800H0800\text{H}, 1000\text{H}, 1800\text{H}, etc.) relative to the instruction following AJMP, otherwise the program will branch to an unintended memory block.

Footnotes

  1. Intel Corporation, MCS-51 Single-Chip Microcomputer Reference Manual. - Outlines the hardware architecture, register sets, program memory models, and PC manipulation details.

Advanced Branching Mechanics & FAQs

Knowledge Check

Question 1 of 4
Q1Single choice

If the current Program Counter (PC) is at address 0100H and the instruction 'SJMP 05H' (which translates to relative offset +05H) is executed, what is the destination PC address?

Explore Related Topics

1

Interfacing Bluetooth and Zigbee with the 8051 Microcontroller

The course explains how to interface Bluetooth (HC‑05) and Zigbee (XBee) modules with an 8051 microcontroller using its UART, covering hardware connections, voltage‑level considerations, UART initialization, and module configuration.

  • UART on the 8051 is set with SCON = 0x50, TMOD = 0x20, TH1 = 0xFD and Timer 1 for 9600 baud with an 11.0592 MHz crystal.
  • HC‑05 operates at 3.3 V TTL; connect TX/RX crosswise and add a level‑shifter on the 8051 TX line.
  • XBee also needs 3.3 V logic; use transparent mode for simple serial pass‑through or API mode for framed packets with addressing.
  • Bluetooth provides a point‑to‑point serial bridge, whereas Zigbee adds PAN IDs, coordinator/end‑device roles, and multi‑node networking.
  • Typical firmware flow: initialize UART → receive bytes → parse commands → control outputs → transmit acknowledgments (API mode adds frame parsing and checksum).
2

Various Addressing Modes of 8051 Microcontroller

The 8051 microcontroller provides multiple addressing modes that define how an instruction identifies the location or value of its operand.

  • Immediate (#data) – constant value encoded in the instruction, used for loading fixed numbers.
  • Register (Rn) – operand resides in CPU registers R0‑R7 (or A/B), giving the shortest and fastest code.
  • Direct (addr) – 8‑bit address is part of the opcode, accessing internal RAM or SFRs directly.
  • Register indirect (@R0, @R1, @DPTR) – a register holds the operand’s address, enabling pointer‑like traversal of memory.
  • Indexed (@A+DPTR or @A+PC) – adds the accumulator to DPTR or PC for table look‑ups in code memory; branch modes (relative, absolute, long) extend this concept for short, page‑limited, and full‑range jumps.
3

Introduction to Compiler Design and Architecture

The course introduces the fundamental structure and operation of modern compilers, describing how source code is transformed through front‑end analysis, intermediate representation, and back‑end generation.

  • Front‑end performs lexical, syntax, and semantic analysis, building a symbol table and an AST independent of the target.
  • An intermediate representation (IR) like three‑address code lets language‑independent optimizations run before back‑end register and instruction selection.
  • Optimization passes (e.g., dead‑code elimination, loop unrolling) on the IR consume about 50 % of compilation CPU time.
  • Top‑down parsers fail on left‑recursive grammars; they are fixed by rewriting A → Aα | β as A → β A' and A' → α A' | ε.