Search code examples
c++gccinline-assembly

Error using GCC Intel Assembly: invalid operands (.text and UND sections) for +


I am writing inline assembly and I came across a error that I don't know how to fix. This part of the code raises an error. It is supposed to add "i" to "source" and "array" addresses, and copy the contents of the byte at "source" to "array".

int main()
{
    char* _source = new char [1];
    _source[0] = 1;
    char* array = new char[1];
    unsigned int i = 0;
    __asm volatile (
        "mov eax, $[source] \n\t"
        "add eax, $[i] \n\t"
        "mov bh, [eax] \n\t"
        "mov ecx, $[array] \n\t"
        "add ecx, $[i] \n\t"
        "mov [ecx], bh \n\t"
        :
        : "r" "source" (_source), "r" "array" (array), "r" "i" (i)
        : "eax", "bh", "ecx", "memory"
    );
}

This code is executed using gcc.

gcc -m32 -masm=intel -o test.cpp 

And the errors that show up

C:\Users\geish\AppData\Local\Temp\ccMCDck3.s:34: Error: invalid operands (.text and *UND* sections) for `+'
C:\Users\geish\AppData\Local\Temp\ccMCDck3.s:35: Error: invalid operands (.text and *UND* sections) for `+'
C:\Users\geish\AppData\Local\Temp\ccMCDck3.s:37: Error: invalid operands (.text and *UND* sections) for `+'
C:\Users\user\AppData\Local\Temp\ccMCDck3.s:38: Error: invalid operands (.text and *UND* sections) for `+'
C:\Users\user\AppData\Local\Temp\ccMCDck3.s:56: Error: invalid operands (.text and *UND* sections) for `+'
C:\Users\user\AppData\Local\Temp\ccMCDck3.s:57: Error: invalid operands (.text and *UND* sections) for `+'
C:\Users\user\AppData\Local\Temp\ccMCDck3.s:59: Error: invalid operands (.text and *UND* sections) for `+'
C:\Users\user\AppData\Local\Temp\ccMCDck3.s:60: Error: invalid operands (.text and *UND* sections) for `+'

Solution

  • Your use of named operands is wrong in a few ways. Instead of "r" "source" (_source), you should specify the operand as: [source] "r" (_source), where [source] specifies its name, _source is the C variable, and "r" is the constaint to use. And you should access the operand with %[source], not $[source].