This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

"Inline" functions in Assembly

"Inline" functions in Assembly
by on (#152306)
Is there a way to let the compiler copy a certain portion of the code into another portion?

Let's say I have a function:
Code:
MyFunction:
  ; Do stuff
  ; Do more stuff
  RTS


And now I call it somewhere:
Code:
  ; Something
  JSR MyFunction
  ; Something else


Now let's say I only call the function in one location in my whole program. I.e. I could write the function code directly into that place, but I choose to put it somewhere else for organization purposes.

So, is there a way to do something like this?
Code:
INLINE MyFunction
  ; Do stuff
  ; Do more stuff
END INLINE
(Note that there's no RTS this time.)

And then somewhere else, I write:
Code:
  ; Something
  INSERT INLINE MyFunction
  ; Something else


And in the end, the compiler treats the code as if it has been written like that:
Code:
  ; Something
  ; Do stuff
  ; Do more stuff
  ; Something else
Re: "Inline" functions in Assembly
by on (#152307)
Most assemblers let you create macros. Here's reference for ca65.
Re: "Inline" functions in Assembly
by on (#152308)
Yeah, that's pretty much what I was looking for. Thanks.
Re: "Inline" functions in Assembly
by on (#152524)
How do I make a macro that I declared in one file visible in another file? The typical .import/.export commands that are used for constants, variables and functions don't seem to work.
Re: "Inline" functions in Assembly
by on (#152528)
The same way you make a C static inline function available to other files: put it in a header file.

  1. Put the macro into a file whose name ends with .inc. (In another post, it was decided to reserve .h for C and C++ headers.)
  2. At the top of each assembly language source code file, add .include "mymacpack.inc" or whatever you called it.
  3. If you are using Make or another dependency-based build system, add a dependency on mymacpack.inc for each object file compiled from a source code file that includes the macro pack, so that the source files get reassembled when the macro changes.
Re: "Inline" functions in Assembly
by on (#152548)
Thanks for the help.

I knew about the include files, but I thought that you can export the macro like with functions and variables. But yeah, in C, you would do it the same with inline functions, that's of course right.
So, I'll do it with an include file.