Search code examples
assemblyx86att

Division causes unbalanced parentheses


In GNU as (the GNU assembler), the following code assembles without error:

mov $(80 * 24 + 4), %cx

However, this code does not:

mov $(80 * 24 / 4), %cx

Emitting the highly unexpected error:

example.S: Assembler messages:
example.S:42: Error: unbalanced parenthesis in operand 1.

The only difference is that the latter uses division, instead of addition. This should be valid, according to the manual.

($<expression> embeds an immediate into the assembled output; i.e., a constant. The arithmetic is performed at "compile-time". I could work out the math, but it makes more sense in its expanded form.)


Solution

  • / signifies the start of a comment, in my particular case.

    --divide

    On SVR4-derived platforms, the character / is treated as a comment character, which means that it cannot be used in expressions. The --divide option turns / into a normal character. This does not disable / at the beginning of a line starting a comment, or affect using # for starting a comment.

    This post on the binutils mailing list also suggests a similar story:

    For compatibility with other assemblers, '/' starts a comment on the i386-elf target. So you can't use division. If you configure for i386-linux (or any of the bsds, or netware), you won't have this problem.

    I happen to be assembling for the x86_64-elf target, which I presume is sufficiently similar to the mentioned i386-elf (the former is for the amd64 or "x86_64" arch, the latter is the same but for the older 32-bit x86 architecture).