0
0
ARM Architectureknowledge~5 mins

Subroutine call convention (AAPCS) in ARM Architecture

Choose your learning style9 modes available
Introduction

The Subroutine Call Convention (AAPCS) defines clear rules for how functions communicate in ARM systems. It helps different parts of a program work together smoothly by agreeing on how to pass data and return results.

When writing functions that need to call other functions in ARM-based programs.
When mixing code compiled by different tools or languages on ARM processors.
When debugging or optimizing ARM assembly code involving function calls.
When designing libraries or APIs for ARM systems to ensure compatibility.
When learning how ARM processors handle function calls and data passing.
Core Concept
ARM Architecture
No single code syntax; it is a set of rules about register usage and stack management during function calls.
AAPCS specifies which registers are used to pass arguments and return values.
It defines which registers must be preserved by the called function and which can be freely used.
Key Points
Registers r0 to r3 carry the first four arguments to a function. The first argument is in r0, the second in r1, and so on. The function returns its result in r0.
ARM Architecture
r0-r3: Used to pass up to four integer or pointer arguments to a function.
r0: Also used to return the result from a function.
Registers r4 to r11 must keep their values after the function call. If the function changes them, it must save their original values and restore them before returning.
ARM Architecture
r4-r11: Callee-saved registers. The called function must save and restore these if it uses them.
The stack pointer manages memory for temporary data during function calls, like local variables or saved registers.
ARM Architecture
sp (stack pointer): Points to the current top of the stack, used for local variables and saving registers.
The link register stores where the program should return after the function finishes.
ARM Architecture
lr (link register): Holds the return address when a function is called.
Detailed Explanation

This example shows a simple function that adds two numbers. The first two numbers are passed in registers r0 and r1. The function adds them and returns the result in r0. The link register (lr) is used to return to the caller.

ARM Architecture
; Example ARM assembly function following AAPCS

; Function: add_two_numbers
; Adds two integers passed in r0 and r1, returns result in r0

add_two_numbers:
    add r0, r0, r1    ; Add r1 to r0, result in r0
    bx lr             ; Return to caller using link register

; Caller code example

    mov r0, #5        ; First argument = 5
    mov r1, #7        ; Second argument = 7
    bl add_two_numbers ; Call function
    ; Result now in r0 (should be 12)
OutputSuccess
Important Notes

Following AAPCS ensures that functions can call each other without corrupting data.

Not following the convention can cause bugs that are hard to find, like wrong results or crashes.

Stack alignment is important; the stack pointer must be 8-byte aligned before calling a function.

Summary

AAPCS defines how ARM functions pass arguments and return values using registers.

Registers r0-r3 are for arguments and return values; r4-r11 must be preserved by the called function.

The link register (lr) holds the return address, and the stack pointer (sp) manages local data.