Welcome

Friday, August 29, 2025

SNasm

 Code Assemble !

Assembling your first application isn't quite as simple as it is on the C64 with Kick.

There are a couple of important assembler directives that must be used or what you assemble won't work at all.

The first is:

OPT ZXNEXT

This option will enable the extended Spectrum Next functions such as the NEXTREG command which is used to talk to the hardware registers of the machine.

ORG $C000

This option tells the assembler where to start the code addressing from, the address $C000 is a common place to start applications, especially for new devs who don't yet know the hardware. The 1st 16k is ROM, this can be paged out but not until the program has loaded and is running, the second 16k is, by default, used for the screen and user defined tiles, this depends upon the screen mode and can be moved. 

The top 32k is all yours!

Finally:

savenex "../bin/main.nex",StartAddress,StackStart

is required, this tells SNasm how to write out the file so that it can be executed on the next.

The filename is simply the path and name of the file to produce, to keep things tidy I like to drop this into a bin (or binary) directory which can then be passed to the emulator.

The StartAddress is the address of the first line of the program that will be run, usually this is the same as the ORG value set above but you can specify any memory address to start the code. One very important thing to note is that if you wish to start your code in a particular memory block make sure that the label is AFTER the ORG command not before it.

ie

    ORG $C000
StartAddress:
    ...

NOT

StartAddress:
    ORG $C000
   ...

doing the the latter will set the start code to end of the previous code block (or zero if its at the top of the code). Guess how I found that out.

Finally the Next needs to know where its stack is, unlike the 6502 the stack is not in a fixed memory page.

Firstly you need to allocate a block of memory for the stack, the size of the stack really depends on how and what the program does. If the program has a lot of recursion,  for example, more stack space is required. Usually I would suggest 256 byte as a reasonable value.

A simple and predictable way to define this is:

ds 255
StackStart:
db 0

Remember the stack grows backwards in memory so we define the label after the reserved space and the first element on the stack is zeroed.

Obviously you could just set a variable to an area of memory you expect to be free but I prefer to define it, it save tears later. 

It doesn't really matter where the stack is place although my preference is to put it at the end of any blocks of data rather than at the end of a block of code. This way if the stack grows beyond the reserved space it corrupts data rather than code, which is easier to trouble shoot, especially if it graphics data as it will be obvious something is overwriting. Corrupt code just does weird stuff that is not always obvious to diagnose. 

No comments:

Post a Comment

SNasm

 Code Assemble ! Assembling your first application isn't quite as simple as it is on the C64 with Kick. There are a couple of important ...