Discover how subroutines work in assembly programming with this interactive demonstration based on a real program that shifts numbers left by 4 positions.
| Address | Label | Instruction | Comment |
|---|---|---|---|
| 100 | ORG 100 | Main program | |
| 101 | LDA X | Load X | |
| 102 | BSA SH4 | Branch to subroutine | |
| 103 | STA X | Store shifted number | |
| 104 | LDA Y | Load Y | |
| 105 | BSA SH4 | Branch to subroutine again | |
| 106 | STA Y | Store shifted number | |
| 107 | X, | HEX 1234 | |
| 108 | Y, | HEX 4321 | |
| 109 | SH4, | HEX 0 | Store return address here |
| 10A | CIL | Circulate left once | |
| 10B | CIL | ||
| 10C | CIL | ||
| 10D | CIL | Circulate left fourth time | |
| 10E | AND MSK | Set AC(13-16) to zero | |
| 10F | BUN SH4 I | Return to main program | |
| 110 | MSK, | HEX FFF0 | Mask operand |
A subroutine is a reusable block of code that performs a specific task. Instead of repeating code, we can call the subroutine multiple times.
Branch and Save Address - Saves the return address and jumps to the subroutine. Essential for knowing where to return!
BUN SH4 I - Uses indirect addressing to return to the saved location in the main program.
The subroutine shifts bits left by 4 positions using CIL (Circulate Left) instructions, effectively multiplying by 16.
Watch how the program executes step by step, calling the subroutine twice to shift both X and Y values.
This program demonstrates the power of subroutines by using the same shift-left-4 routine twice:
CIL (Circulate Left): Rotates all bits in the accumulator one position left. The leftmost bit moves to the rightmost position.
AND MSK: Uses mask FFF0ââ to clear the rightmost 4 bits, ensuring a clean left shift (not rotation).
Mathematical Effect: Left shifting by 4 bits is equivalent to multiplying by 2â´ = 16.