Libraries dependenies / symbol duplicates with SDASZ80

By DamnedAngel

Master (139)

DamnedAngel's picture

01-01-2019, 18:33

Hi MSXers,

Newbie ASM question here, but, first, Happy New Year for everybody! May 2019 be a year full of ROM images, Z80 opcodes and memory dumps for each of you!

I am working on a C + ASM project using SDCC + SDASZ80 and all was well when I had simple one-level includes among the ASM code.

As I progress with the development, the code is getting more complex and now I am starting to need (unless there is a simpler way, of course) of multi-level includes, which is resulting in symbol duplicates ("Multiple definition of XXXXXXX").

I have an ASM VDP lib, from which I build a rel file, and then link together with the final c code. This was fine, until the point I build an ASM menu lib, which includes the ASM VDP lib (so I can draw the menus on screen) which rel file also needs to be linked to the final c code.

The problem is that VDP symbols now gets duplicated, since menu rel file also contains them. And that will happen with other screen structures as well.

SDASZ80's manual mention external (global) expressions, but it does not show examples of usage, so I am a little lost here...

Does anybody have any clues on this?

best,

Login or register to post comments

By jltursan

Prophet (2131)

jltursan's picture

01-01-2019, 19:01

At least in ANSI-C you can avoid multiple definitions with some precompiler directives:

#IFNDEF MYSYMBOL
#DEFINE MYSYMBOL
#ENDIF

I suppose SDCC will use something pretty close to the above if not the same...

By Pencioner

Paladin (907)

Pencioner's picture

01-01-2019, 19:17

there's also ANSI-C `extern` directive to solve this problem, therefore you declare symbols with it as external in header files you include in two laces, and add one C file which has a definition of this symbol, so it is defined only once. so multiple declarations for compiler and only one symbol definition for linker

By DamnedAngel

Master (139)

DamnedAngel's picture

01-01-2019, 23:23

Hi jltursan and Pencioner,

Thanks for your answers!
I think I was not clear in my first post, however.

I know the #IFNDEF pattern and the C "extern" directive. In fact, I am already using them and the communication ASM <-> C is smooth. The problem is in the repetition of symbols in ASM REL files.

I am under the impression that I should use external expressions in asm, in the same way we use extern in C, but I don't know the exact syntax/structure that SDASZ80 requires...

best,

By jltursan

Prophet (2131)

jltursan's picture

02-01-2019, 20:33

I'm with Pencioner that the problem must be solved with a wise "extern" use. In the SDCC manual this piece of code illustrates some extern use:

In the following example the function c_func calls an assembler routine asm_func, which takes two parameters.

extern int asm_func(unsigned char, unsigned char);
int c_func (unsigned char i, unsigned char j)
{
return asm_func(i,j);
}
int main()
{
return c_func(10,9);
}

The corresponding assembler function is:

.globl _asm_func_PARM_2
.globl _asm_func
.area OSEG
_asm_func_PARM_2:
.ds 1
.area CSEG
_asm_func:
mov a,dpl
add a,_asm_func_PARM_2
mov dpl,a
mov dph,#0x00
ret

You have an external ASM routine that can be declared as prototype along multiple files but is defined only in one place. This doesn't works for you?

By DamnedAngel

Master (139)

DamnedAngel's picture

05-01-2019, 13:55

Hi jltursan (and Pencioner),

I still think your solution is not exactly what I am after (but I am willing to be wrong! LOL! ).

I made two drawings to futher explain the situation. This is what WORKS (including the prototypes in C, with the "external" keyword and corresponding asm files:

Now, this is what DOES NOT work (please note the new asm libraries (LIB4.ASM ... LIBn.ASM) which .includes LIB3.ASM. Note also the dupilcation of symbol "A" in LIB3.REL, LIB4.REL and LIBn.REL. This, as I see it, has nothing to do with symbol duplication in C):

What I think I need is to be able to declare symbol "A" as extern in LIB4.ASM through LIBn.ASM, as I already do in C. That, however, I don't know how to do ;( .

Thanks in advance for you interest and efforts on this.
best,

By Grauw

Ascended (8208)

Grauw's picture

05-01-2019, 15:12

DamnedAngel wrote:

What I think I need is to be able to declare symbol "A" as extern in LIB4.ASM through LIBn.ASM, as I already do in C. That, however, I don't know how to do Crying .

I think that’s what you need to do, yes. The syntax can be assembler-specific. Doesn’t SDASZ80 have documentation? Search for keywords “extern” “symbol” or something like that.

By Pencioner

Paladin (907)

Pencioner's picture

05-01-2019, 15:46

@DamnedAngel I'm sorry i almost didn't use SDASZ80 - i prefer sjasm - but as @Grauw said, there's mostly likely the equivalent of the "extern" in it (if not - that's a ready-to-go improvement request to devs)

By DamnedAngel

Master (139)

DamnedAngel's picture

07-01-2019, 23:42

Hi all,

Just to register that I found the solution.

Actually, apparently there is no trick at all. It seems to be fairly straight forward. I just was excessively noob on the matter.

I created a separate file, as an "asm header" declaring all symbols as

.globl symbol1
.globl symbol2
...

Then I .included this file in the several libs which needed to chain access the original lib. The assembled REL files carry the information of the unresolved symbol, which is only resolved by the linker.

Thanks for the help, everybody!