Search code examples
assemblyx86-64intelmasm

Create a for loop in MASM with a variable to create distinct PROC functions


I'm trying to create a set of procs based on a 'for loop' counter. MASM doesn't appear to have a traditional for-next type loop, so I'm trying to use a variable and REPT.

Eg:

ix = 0
rept 2
    foo_&ix& proc
        mov rax, ix
        ret
    foo_&ix& endp
    ix = ix + 1
endm

...

; call the generated procs
call foo_0
call foo_1

However ml64.exe is returning error A2008: syntax error : foo_, so it's like it isn't using the variable.

I assume I'm not defining and using variables correctly. Constants are created with ix equ 0, but these don't appear to be able to be altered.


Solution

  • From the comments above, use CatStr:

    .code
    ix = 0
    rept 2
        @CatStr(<foo_>, %ix, < proc>)
        mov rax, ix
        ret
        @CatStr(<foo_>, %ix, < endp>)
        ix = ix + 1
    endm
    
    ...
     
    ; call the generated procs
        call foo_0
        call foo_1
    

    All credit to @Micheal Petch