Chapter 7. Functions and Macros

This chapter shows how to group directives together in units for later execution. In other words, how to define and use functions, macros and finally pseudo commands which are a special kind of macros.

7.1. Functions

You can define you own functions which you can use like any of the build in library functions. Here is an example of a function:

.function area(width,height) {
    .return width*height
.var x = area(3,2)
lda #10+area(4,8)

Functions consist of non-byte generating directives like .eval, .for, .var, and .if. When the assembler evaluates the .return directive it returns the value given by the proceeding expression. If no expression is given, or if no .return directive is reached, a null value is returned. Here are some more examples of functions:

// Returns a string telling if a number is odd or even
.function oddEven(number) {
    .if ([number&1] == 0 ) .return “even”
    else .return “odd”

// Inserts null in all elements of a list
.function clearList(list) {
    // Return if the list is null
    .if (list==null) .return

    .for(var i=0; i<list.size(); i++) {

// Empty function – always returns null
.function emptyFunction() {

You can have several functions of the same name, as long as they have different number of arguments. So this is valid code:

.function polyFunction() { .return 0 }
.function polyFunction(a) { .return 1 }
.function polyFunction(a,b) { .return 2 }