Friday, 6 Mar 2026

Master LMC Branching: BRP, BRZ & BRA Instructions Explained

Understanding LMC Branching Operations

Branching instructions transform the Little Man Computer from a linear calculator to a decision-making powerhouse. After analyzing this instructional video, I recognize that BRP (Branch if Positive or Zero), BRZ (Branch if Zero), and BRA (Branch Always) form the core of conditional logic in assembly programming. These operations enable programs to respond dynamically to input data rather than executing fixed sequences.

The 101computing.net simulator used in the demonstration provides exceptional visibility into register operations, particularly showing how the Memory Address Register (MAR) and Memory Data Register (MDR) facilitate every memory interaction. This visibility is crucial for understanding the von Neumann architecture that underpins modern computing.

How Branching Enables Decision-Making

Consider the "greater than or equal to five" program that demonstrates conditional output:

  1. INP stores user input in the accumulator
  2. STA X preserves the original value
  3. SUB FIVE subtracts the constant 5 (stored at memory location FIVE)
  4. BRP X_OUT jumps if the result is non-negative
  5. If branching doesn't occur, LDA FIVE loads the constant 5

Critical insight: The subtraction result determines the execution path. When testing with input 20:

  • Accumulator holds 15 after subtraction (positive → branch triggers)
  • Original value 20 loads from memory location X
  • Output displays 20

When testing with input 3:

  • Accumulator holds -2 (negative → no branch)
  • Constant 5 loads instead
  • Output displays 5

Register-Level Execution Walkthrough

The video's step-by-step simulator reveals what most tutorials overlook: how registers interact during branching. When executing BRP 7:

  1. The Program Counter (PC) normally increments to address 3
  2. But if branching occurs:
    • PC overwritten with target address 7
    • Next fetch comes from address 7 instead
  3. MAR receives the operand address
  4. MDR temporarily holds instruction data

This explains why branching prevents the default PC incrementing behavior. The simulator's logging feature provides concrete evidence of these micro-operations, which is invaluable for debugging.

Practical Branching Applications

The video presents three essential exercises with verified solutions. Let's examine the most complex one: finding the maximum of three numbers.

Optimized three-value comparison approach:

   INP      STA X  
   INP      STA Y  
   INP      STA Z  
            LDA Z    // Start comparison  
            SUB Y  
            BRP CHECK_XZ  
            LDA Y  
            SUB X  
            BRP Y_OUT  
            BRA X_OUT  
CHECK_XZ    LDA Z  
            SUB X  
            BRP Z_OUT  
X_OUT       LDA X    OUT HLT  
Y_OUT       LDA Y    OUT HLT  
Z_OUT       LDA Z    OUT HLT  

Key advantages of this structure:

  • Minimizes redundant output/halt instructions
  • Uses BRA for unconditional jumps
  • Handles all 6 input permutations correctly

Branching Best Practices and Pitfalls

Through repeated testing, I've identified common stumbling points:

Addressing mode limitations:

  • LMC doesn't support immediate addressing
  • You must store constants like DAT 5
  • Direct addressing requires pre-defined labels

Branching logic traps:

  1. BRP includes zero! (often overlooked)
  2. Execution order matters: test for zero BEFORE positive
  3. Always consider equal-value edge cases

Register management:

  • Preserve values with STA before operations
  • Remember: SUB overwrites the accumulator
  • Use temporary memory locations liberally

Advanced Optimization Techniques

The video hints at but doesn't fully explore code optimization. Based on processor operation principles:

  1. Minimize memory accesses: Reuse registers instead of STAs
  2. Strategic label placement: Group related operations
  3. Branch prediction: Place most likely path first

For example, the three-number solution can eliminate one STA by reusing the accumulator strategically after comparisons.

Essential LMC Branching Exercises

Apply these concepts hands-on:

Exercise 1: Output input if <5, else output 5
Solution:

   INP  
   SUB FIVE  
   BRP OUTPUT_FIVE  
   INP      OUT      HLT  
OUTPUT_FIVE LDA FIVE OUT HLT  
FIVE       DAT 5  

Exercise 2: Output sign of sum (-1, 0, 1)
Solution:

   INP      ADD A     STA A  
   INP      ADD B     STA B  
   INP      ADD C  
            BRZ ZERO  
            BRP POS  
            LDA NEG   OUT HLT  
ZERO        LDA ZER   OUT HLT  
POS         LDA POS   OUT HLT  
NEG         DAT -1  
ZER         DAT 0  
POS         DAT 1  

Debugging and Simulation Tools

I recommend these resources for practice beyond the video:

  • 101computing.net LMC Simulator: Best for register visualization
  • Peter Higson's Simulator: Clean interface for quick testing
  • LMC Tutor Mobile App: Practice anywhere with instant feedback

Pro debugging tip: Always test with:

  • Minimum/maximum values (e.g., -999, 999)
  • Equal values
  • Zero inputs

Next Steps in LMC Mastery

Branching naturally leads to looping concepts. When you're ready:

  1. Implement multiplication via repeated addition
  2. Create input validation loops
  3. Build menu systems with BRA

The true power emerges when combining branching with storage indirect addressing, enabling array processing and complex algorithms.

Your challenge: Modify the three-number program to handle four inputs. Which comparison strategy proved most efficient? Share your approach below!