0
0
ARM Architectureknowledge~5 mins

Nested subroutine calls in ARM Architecture

Choose your learning style9 modes available
Introduction

Nested subroutine calls let one function call another function inside it. This helps break tasks into smaller steps.

When a task can be divided into smaller tasks that need their own code.
When you want to reuse a piece of code multiple times from different places.
When organizing code to make it easier to read and maintain.
When a function needs to get results from another function before continuing.
Core Concept
ARM Architecture
BL subroutine_name  ; Branch with Link to call a subroutine
...                  ; code inside subroutine
BX LR                ; Return from subroutine

BL instruction calls a subroutine and saves return address in LR (Link Register).

BX LR returns control to the calling function using the address in LR.

Key Points
Call subroutine1 and continue after it finishes.
ARM Architecture
BL subroutine1
; code continues here after subroutine1 returns
subroutine1 calls subroutine2 before returning.
ARM Architecture
subroutine1:
  BL subroutine2
  BX LR
subroutine2 does its task and returns.
ARM Architecture
subroutine2:
  ; do some work
  BX LR
Detailed Explanation

This program shows nested calls: START calls subroutine1, which calls subroutine2. Each returns to the caller using BX LR.

ARM Architecture
; Main program
START:
  BL subroutine1  ; Call first subroutine
  ; Continue main program

subroutine1:
  ; Call second subroutine inside first
  BL subroutine2
  BX LR           ; Return to caller

subroutine2:
  ; Do some work here
  BX LR           ; Return to subroutine1
OutputSuccess
Important Notes

Each BL saves the return address in LR, so nested calls work correctly.

Make sure each subroutine ends with BX LR to return properly.

Stack management may be needed for more complex nested calls to save registers.

Summary

Nested subroutine calls let functions call other functions inside them.

Use BL to call and BX LR to return.

This helps organize code into smaller, reusable parts.