Program Loops

Interactive Tutorial: Understanding Assembly Language Loops with a Real Example

✅What Is a Program Loop?

A program loop is a set of instructions that repeat multiple times until a specific condition is met. In assembly language, loops help perform repetitive tasks like processing multiple data items without writing each instruction manually.

✅Example: Adding 100 Numbers

Our goal is to add 100 numbers stored in memory and store the result in a predefined location. Let's see how this works step by step!

Assembly Code - Complete Program
1     ORG 100 ; Program starts at memory location HEX 100
2     LDA ADS ; Load the first address of operands into accumulator
3     STA PTR ; Store this address into pointer variable (PTR)
4     LDA NBR ; Load constant (-100) into accumulator
5     STA CTR ; Store it in counter (CTR), tracks remaining operands
6     CLA ; Clear accumulator to start summing from 0
7LOP, ADD PTR I ; Add operand pointed by PTR to accumulator
8     ISZ PTR ; Increment pointer (PTR)
9     ISZ CTR ; Increment counter (CTR)
10     BUN LOP ; If CTR is not zero, loop back to LOP
11     STA SUM ; Store final sum into memory location SUM
12     HLT ; Halt the program after sum is calculated
13ADS, HEX 150 ; Define first address of operands
14PTR, HEX 0 ; Memory location for pointer
15NBR, DEC -100 ; Constant to initialize counter
16CTR, HEX 0 ; Memory location for counter
17SUM, HEX 0 ; Memory location to store sum
18     ORG 150 ; Starting location of operands at HEX 150
19-118   DEC 75 ... DEC 23 ; Data values of 100 operands to be summed
119     END ; End of symbolic program

✅Interactive Loop Simulator

Watch how the loop executes step by step. Click the buttons to control the simulation:

Ready to start simulation

Registers & Variables

Accumulator (AC): 0
Program Counter (PC): 100
Pointer (PTR): 0
Counter (CTR): 0
Sum (SUM): 0
Loop Iteration: 0

Current Operation

Instruction: Ready to start
Current Data: N/A
Next Address: 150

✅Step-by-Step Loop Flow

1. Initialize: PTR = 150, CTR = -100, AC = 0
↓
2. ADD PTR I: Add value at address PTR to AC
↓
3. ISZ PTR: Increment pointer to next address
↓
4. ISZ CTR: Increment counter towards zero
↓
5. BUN LOP: If CTR ≠ 0, jump back to step 2

✅Loop Execution Visualization

Iteration PTR (Address) Operand Value CTR Value Accumulator (AC)
1 150 75 -99 75
2 151 63 -98 138
3 152 45 -97 183
... ... ... ... ...
100 1B3 23 0 Sum of 100 numbers

✅Why Are Loops Important?

🎯 Efficiency

Without loops, you'd need 100 separate ADD instructions. The loop makes the program compact and efficient.

📊 Scalability

Easy to modify for different numbers of operands by changing just the counter value.

🔧 Maintainability

Reduces code duplication and makes programs easier to debug and maintain.

💡 Flexibility

The combination of pointer and counter allows easy iteration over data arrays.

✅Remarks

Program loops are fundamental in assembly programming for repetitive tasks. This example demonstrates how a simple loop structure with a pointer and counter can efficiently process large amounts of data, making programs both compact and maintainable.

Start Learning Multiplication Program