Search code examples
assemblynasmmasmgnu-assembleratt

How to declare local variables in macro asm of gas like decalaring it in macro asm with %local in macro asm of nasm or local in macro asm of masm?


the way to declare local variables in macro asm of nasm is like:%local old_ax:word,and the way to declare local variables in macro asm of masm is like:local old_ax:WORD,so what's the way to declare local variables in macro asm of gas?


Solution

  • gas is primarily intended as a compiler backend, not for human use. As such, it's lacking some features, among others this one. You can of course try to make a macro to do this, something along these lines:

    .intel_syntax noprefix
    .globl main
    
    .macro local head, tail:vararg
    # assume dwords for simplicity
    .set localsize, localsize + 4
    .set \head, -localsize
    .ifnb \tail
    local \tail
    .endif
    .endm
    
    main:
        .set localsize, 0
        local old_ax, old_dx
        enter localsize, 0
        mov [ebp+old_ax], ax
        mov [ebp+old_dx], dx
        leave
        ret
    

    But in general you can just use the equates for stack offsets directly, such as:

    main:
        .set old_ax, 4
        .set old_dx, 0
        sub esp, 8
        mov [esp+old_ax], ax
        mov [esp+old_dx], dx
        add esp, 8
        ret