Calypsi-tool-chains icon indicating copy to clipboard operation
Calypsi-tool-chains copied to clipboard

65816 inline assembly bugs

Open ksherlock opened this issue 9 months ago • 2 comments
trafficstars

%N syntax for inline assembly substitution doesn't seem to work if the optional output (%0) is missing.

void print_char(int c) {

  // _WriteChar tool call
  __asm(
    " pei %1\n"
    " ldx ##0x180c\n"
    " jsl 0xe10000\n"
    : /* no output */
    : "Kdp16"(c) /* input */
    : "c", "x", "y" /* clobbered */
  );
}
error: invalid operand number in inline asm string
    " pei %1\n"
    ~~~~~~~^~~~

it works with a named symbol (eg, %[c])

How can I access the top/bottom word of a dp32 register class?

Something like this:

void print_string(const char *str) {

  // _WriteCString tool call
  __asm(
    " pei %[str]+2\n"
    " pei %[str]\n"
    " ldx ##0x200c\n"
    " jsl 0xe10000\n"
    : /* no output */
    : [c]"Kdp32"(str) /* input */
    : "c", "x", "y" /* clobbered */
  );

}

gives me an error

internal error: unexpected assembler errors:
expression cannot be represented in object file format

Finally, an inconsistency with the assembler and compiler over pea:

void pea(const char *str) {

  __asm(
    " pea #0x1234\n"
    " pla"
    : /* no output */
    : /* no input */
    : "c" /* clobbered */
  );

}

this works correctly when generating an object file (-c).

However, the generated assembly code (-S) include an extra # which the assembler does not like

pea:
;
;    __asm(
;      " pea #0x1234\n"
;      " pla"
;      : /* no output */
;      : /* no input */
;      : "c" /* clobbered */
            pea     ##0x1234
            pla
;    );
;
;  }
            rtl
: invalid operand field
            pea     ##0x1234

ksherlock avatar Feb 19 '25 23:02 ksherlock

When there is no output the argument list is supposed to start with %0, but there is a bug in that it results in an error. I have fixed this for the next release.

How can I access the top/bottom word of a dp32 register class?

You are doing it right, but the expression handling runs into issues with that the operands need to use the .tiny relocation operator to access the direct page. Adding an offset causes the expression to bind incorrectly (due to precedence rules) which results in the "cannot be represented in the object format error". I want to postpone fixing this as the fix is non-trivial and I need to think a bit about how to do it the best way.

The problem with pea is also something I want to wait with.

Let me know if any of these two issues are urgent to fix for you?

hth313 avatar Feb 26 '25 04:02 hth313

This is not urgent. Thanks.

ksherlock avatar Feb 26 '25 13:02 ksherlock