Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Division causes unbalanced parentheses

Tags:

x86

assembly

att

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.)

like image 380
Thanatos Avatar asked Jul 24 '16 03:07

Thanatos


People also ask

What is unbalanced parenthesis?

Explanation: It is likely that the number of left parenthesis is not equal to the number of right parenthesis in your expression. Common causes: You added a left parenthesis or removed a right parenthesis from the line of code.

What kind of an error is it if you have unbalanced parentheses in your program?

Unbalanced parentheses result in a compiler error.


1 Answers

/ 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).

like image 98
Thanatos Avatar answered Oct 06 '22 23:10

Thanatos