When compiling the below test code I get the error:
sdcc -msm83 test.c
test.asm:51: Error: undefined symbol encountered during assembly
void test( unsigned int count ) {
if ( count == 0 ) return;
__asm
loop:
ret
__endasm;
}
which processes the following assembly:
39 ;--------------------------------------------------------
40 ; code
41 ;--------------------------------------------------------
42 .area _CODE
43 ;test.c:2: void test( unsigned int count ) {
44 ; ---------------------------------
45 ; Function test
46 ; ---------------------------------
47 _test::
48 ;test.c:4: if ( count == 0 ) return;
49 ld a, d
50 or a, e
51 jr Z, 00103$
52 ;test.c:12: __endasm;
53 loop:
54 ret
55 00103$:
56 ;test.c:13: }
57 ret
58 .area _CODE
59 .area _INITIALIZER
60 .area _CABS (ABS)
Find files attached.
sdcc -v
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502 4.2.2 #13446 (Linux)
This is a know “bug”.
Don’t use named labels, they don’t work when they get embedded into structures that gets translated into jumps.
You have to use something like:
“3.11.4 Use of Labels within Inline Assembler” explicitly says:
The compiler internally uses reusable symbols for all jumps. By introducing normal named labels, you limit the scope of reusable labels. In your example are
jr Z, 00103$and00103$:in different scopes, that’s why the assembler complains that label00103$is not defined.Last edit: Sebastian Riedel 2022-05-17
Thanks, I didn't realize it was an accepted limitation.
What I understand is the asm/endasm creates a new scope and the $00103 label is within that scope, rather than the if statement's scope.
The work around I made here is to remove the if/return statement to the calling function, and then it compiles fine. I'll avoid mixing C and asm code in the same function. While technically incorrect to use (and nicely documented), I find named labels improve the readability of my (already barely comprehensible) assembly code.
As an aside, that link to the manual complains about it being a security risk. And that made me scared, so I didn't download it. (Too many bad experiences from my IT department, no doubt). I'm using FireFox on Ubuntu 20.04.
http://sdcc.sourceforge.net/doc/sdccman.pdf is the official manual. It’s probably also included in the downloads.
I used an anchored link directly to the chapter, which doesn’t work anyways if you don’t use the builtin PDF viewer of firefox/chrome.
edit:
It could be that it generates that warning because sdcc.sourceforge.net doesn’t use https, but not sure.
edit2:
You probably got that warning because I linked a non-https file from a https site (this ticket system). So it might work when you go directly to http://sdcc.sourceforge.net/ and click on the documentation on the left side.
Last edit: Sebastian Riedel 2022-05-18
Diff:
It's not the asm/endasm that makes a new scope. It's your label "loop:" that starts a new scope. And then the jump can no longer see its label. In inline asm you should only use numeric local labels and they must be below 100.
I wanted to add the main reason I reported this issue is this worked fine under sdcc 4.1.
The reason I mention the documentation link issue, is that I suspect most browsers will probably complain about non-secure links and refuse to download it, so I suspect most people won't be able see to any the links you post from the manual.