Question about ASM library in SDCC

Door opcode

Expert (108)

afbeelding van opcode

15-05-2021, 02:02

Hi there!

I am pretty good with assembly language, but have very little experience with C. Would someone please help me understand the principles behind an ASM library for C? To be honest this isn't MSX related, but I was tasked with creating a game library for a similar platform using Z80. So I would like to understand how my ASM routines should interface with SDCC for example.I saw some examples online, but I found the interface kind of inefficient, using the stack.

Any help would be greatly appreciated.

Aangemeld of registreer om reacties te plaatsen

Van Bengalack

Champion (404)

afbeelding van Bengalack

15-05-2021, 08:28

I'm not 100% sure what you are asking, but I can say what I do.

I use SDCC quite a bit, and interface with asm all the time. I also use inline asm in C. Have a look here:

I also use __naked when I can (this can only be used if the C-function, in fact, is all assembly), and __z88dk_fastcall as often as I can.

__naked skips the so-called prologue and epilogue (if I understand it correctly, all the pushing and popping of registers before and after a call).

__z88dk_fastcall is limited to ONE parameter only, and up to 32 bits. Passing parameters in HL or DEHL.

There is another keyword that is related here callee_saves, another one which gives the responsibility of the prologue and epilogue over to the callee, but I can't get it to work. I have a question at the SDCC-forums about it, but no answer yet:

Worth mentioning is that SDCC is working on getting more parameter passing over to registers. Head over here and vote it up:

Note also that you can use the optimize flag when compiling to get faster code. This used to gain at least 10% performance (I used: "--opt-code-speed --max-allocs-per-node 2000000"), but with the latest version 4.1, many of these optimizations are coming out of the normal compile, and there is not too much to be gained.

Van Bengalack

Champion (404)

afbeelding van Bengalack

15-05-2021, 08:40

Well, apart from all that, I mostly put assembler code in plain .s or .asm-files, and assemble them into .rel files using SDCC. Any function in the asm-files is used in the c-files using the extern-keyword, like:

extern void enterTitleScreen();

in the .s file this function must be called:


The two colons tells the assembler that the symbol is public.

And at the end, you can just use sdcc to link all .rel-files together. Or .rel-file(s) + c-file in one go.

You can have a look at how things are done in the Fusion-C package, or even in a more pedagogic way, in DamnedAngel's packs:

And... well. there is more. The first .rel file, must be the crt0-file. It places the org, and the layout of "areas" (code, data, initializer), and must be handed over to the linked first.

Van opcode

Expert (108)

afbeelding van opcode

21-05-2021, 12:39

Thank you so much for all the info. Very useful and I really appreciate it. Now working on the new C library.

Some proof of concept done using the new hardware. We should have an additional more capable tile plane soon, for now using TMS9928 tile plane.