Search code examples
assemblyx86masm

How to concatenate strings at assembly time (using CATSTR?)


I have multiple build configurations of a project, and they each need to have some globally defined strings. These strings should really be built at assembly time by concatenating a root and a config-specific suffix, for example, I might have the root "TABLE" and config A would use the suffix "ALPHA" and config "B" would use the suffix "BETA", so when I build A, I end up with my globally defined string having the value "TABLEALPHA" and for B, the value "TABLEBETA"

Ideally, the suffix is provided via the command line, via a /D type definition.

A complication is that we use a proprietary source language that first "compiles" to MASM then uses MASM to assemble. Which makes it hard to provide good sample code for what I've tried.

If someone could provide a snippet of MASM source showing the definition of the root literal string, a string literal suffix, and how to use CATSTR (or @CatStr???) to define a new global symbol that contains the concatenation of the two strings, I would hope (fingers crossed!) to be able to take it from there.


Solution

  • Here's a complete working example that combines the string constant "TABLE" with a compile-time defined string named SUFFIXB and shows the result in a messagebox.

    ; ml /c /coff /DSUFFIXB=BETA test.asm
    ; link /SUBSYSTEM:WINDOWS test.obj
    
    .586p
    .model flat, stdcall
    option casemap:none
    
    include \masm32\include\windows.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\user32.inc
    
    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\user32.lib
    
    ROOT textequ <TABLE>
    msg CATSTR <">, ROOT, SUFFIXB, <">
    
    .data
    dlgmsg db msg,0
    dlgtitle db "Title",0
    
    .code
    start:
    invoke MessageBoxA, NULL, ADDR dlgmsg, ADDR dlgtitle, MB_OK 
    invoke ExitProcess, NULL 
    
    end start
    

    As a side note, masm's /EP option can be useful when trying to figure out preprocessor syntax, as it allows you to see what your code looks like after it has gone through the preprocessor.