assembly coding conventions

Other misc things
Post Reply
PainfulDiodes
New member
Posts: 7
Joined: Tue Mar 11, 2025 2:32 pm

assembly coding conventions

Post by PainfulDiodes »

Hey!

I have been using z88dk with my simple z80 homebrew. I have a small monitor program written in assembly, and have a set of test/example programs in assembly and C.

I have gradually been evolving some conventions with my code - to make it easier to understand and maintain, and also so that the monitor is easier to interface with. These are generally "what makes sense to me", but I'd like to know if there are general conventions that would be good to follow, or whether my conventions could be confusing or setting me up to fail.

In assembly I have "callable" labels in lowercase, jump addresses and data labels in uppercase. I have started indicating that a label is private by prepending with an underscore:

Code: Select all

START:
    ld hl,WELCOME_MSG
    call puts
    ...

_myprivatefunction:
    ld a, SOME_VALUE
    ...

puts:
    ...
Is this use of a leading underscore for private labels going to be confusing or cause some clash downstream - when using C?

When I come to write example assembly programs that call into the monitor, I have now started to include the map file generated z88dk-z80asm in my examples, which works great, although I'm likely going to remove all the "private" labels from the map file.

When it comes to building C programs that call into the monitor, this too is working fine by including the map file. I have an asm file that I include on the zcc build - something like:

Code: Select all

include "monitor.map"

PUBLIC fgetc_cons
PUBLIC _fgetc_cons

fgetc_cons:
_fgetc_cons:
    call    getchar ; monitor function - waits for input
    ld      l,a
    ld      h,0
    ret

PUBLIC _readchar

_readchar:
    call readchar ; monitor function
    ld l,a
    ld h,0                  
    ret 
fgetc_cons is from the z88dk examples of course. I've added _readchar. But I couldn't use the "readchar" label as it already exists in my monitor.map

The above code compiles and runs fine, but is it OK to omit the label variant without the underscore? Why in the z88dk example are both variants included?

Obviously I could rename the label in the monitor program, or rename it only in the map file. But maybe it would be better to prepend all the monitor labels for better separation? I'd like to understand what the z88dk / general best practice considerations are!

Thanks in advance!
User avatar
dom
Well known member
Posts: 2317
Joined: Sun Jul 15, 2007 10:01 pm

Re: assembly coding conventions

Post by dom »

I think a lot of depends on personal preference and whatever works for you. However, some comments that might help you:

* By default labels in z80asm are private to the assembler file. They only become public when you global/public them
* Functions in user code are exposed to C code with a _ prefix
* The exception to this is for __LIB__ functions with sccz80 which don't have a prefix

These last two points explain why the label is exposed in both forms - the _ variant is for linking with sdcc, and the one without is for sccz80. However, this really is a historical artefact and the use of __LIB__ is no longer required apart from for variadic functions.
User avatar
feilipu
Member
Posts: 59
Joined: Tue Nov 15, 2016 5:02 am

Re: assembly coding conventions

Post by feilipu »

Have a look at the z88dk-z80asm -gf option which will generate an assembly include file, which you can use to link to your monitor or other assembly code. You can also make them public and write to a file with -gpf.
PainfulDiodes
New member
Posts: 7
Joined: Tue Mar 11, 2025 2:32 pm

Re: assembly coding conventions

Post by PainfulDiodes »

Thanks dom, that's helpful, especially why there are 2 variants.

Thanks feilipu - I will give that some more thought, using global/public could be the way to go.I was able to use -g, but not -p - maybe I need to upgrade? Not something I want to do right away, but will put some time aside to dig deeper.

Thanks again!
Post Reply