Re: [cc65] Using asm inline assembly as part of expression

From: Ullrich von Bassewitz <>
Date: 2013-01-13 22:21:23

On Sun, Jan 13, 2013 at 09:21:40PM +0100, Stefan wrote:
>    As far i understand from the manual, an asm statement is always of type
>    void and can be part of an expression. This is, what i don't understand:
>    - If the asm statement is of type void and i cannot convert it to another
>    type, how can it be part of an expression ? Is this a contradiction or am
>    i wrong?

In C, "void" is a type. The standard says (in "6.2.5 Types", if you want to
look it up): "The void type comprises an empty set of values; it is an
incomplete type that cannot be completed."

This is the reason, why you can't convert it to anything else (there is one
exception when the compiler is in cc65 mode, but that behaviour is not
standard compliant).

An expression can yield something of type void. An example is

        int a;
        (void) a;       /* The (ignored) result is of type void */

>    - What is the right ways of using asm statements as an part of an
>    expression?

Having a subexpression of type void doesn't prevent you from using it within
an expression. The standard says about the comma operator (6.5.17):

"The left operand of a comma operator is evaluated as a void expression; there
is a sequence point after its evaluation. Then the right operand is evaluated;
the result has its type and value."

So the comma operator evaluates the left operand as a void expression. This
works (of course) if the left expression does already have a void type.

So an example of correct __asm__ usage in expressions is:

        a = (__asm__ ("lda #$00"),
             __asm__ ("ldx #$00"),

A and X are loaded, and the right operand of the comma operator tells the
compiler that the expression result is the primary register __AX__. If you
look through the headers, you will find that this construct is used heavily
within them.



Ullrich von Bassewitz                        
To unsubscribe from the list send mail to with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Sun Jan 13 22:21:30 2013

This archive was generated by hypermail 2.1.8 : 2013-01-13 22:21:33 CET