I'm trying to add a compact #define macro in a cc65 program for performing various low-level tasks efficiently. For some of the macros, I need to pass a non-constant value to the assembler part. I studied the cc65 guide about inline assembler but could not get it to work.

Here is a minimal example

#include <stdio.h>
#include <stdlib.h>

#define set_bgcolor(C) (asm("lda #%b",C),asm("sta $D021"))

int main (void)
  int x=3;
  set_bgcolor(1);   //this works
  set_bgcolor(1+1); //this works as well
  set_bgcolor(x);   //this fails to compile "Error: Constant integer expression expected"
  return EXIT_SUCCESS;

What is the proper definition of the macro in line 4 to make this work?

  • I am aware that I can make a macro for changing the background color without using inline assembler, but my general question would be how to pass a calculated value to the inline assembler part.
    – Peter B.
    May 18, 2020 at 14:47
  • 1
    In this specific case, wouldn't you just do something like uint8_t* bgcolor = 0xd021; *bgcolor = x? May 18, 2020 at 15:58

1 Answer 1


There is a pseudo variable __AX__ of type unsigned int that can be assigned a value that then ends up in register A (lo byte) and X (hi byte). So, the following macro definition works as intended:

#define set_bgcolor(C) (__AX__ = (C),asm("sta $D021"))

I found this under "Extensions" in the cc65 user guide.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .