Search code examples
assemblygccarm

Labeling stack local variables (asm)


In an ASM function, one can use the stack to create a local workspace for variables.

What I am wondering is, can you create labels to reference them vs using an explicit numeric offset from the stack pointer?

Ex: one wants to ldr r0, sp+4 but use a symbol name instead? What is the correct way to do this? I can't imagine people remembering numeric offsets for everything.


Solution

  • This really depends on the assembler. For the GNU toolchain (GCC with GAS), you can use an .S file (instead of an .s), and the gcc compiler driver will preprocess it like a C source file. So you could write

    #define VAR sp+4
    

    and then use

            ldr r0, VAR
    

    Note that macros are not properly scoped (restricted to the current function), so this can be quite confusing.