Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Assembler with dynamic symbol creation
2016-06-12 06:19
Hypnosis

Registered: Mar 2015
Posts: 36
Assembler with dynamic symbol creation

I have read some documentation for an assembler that allowed labels or symbols to be created dynamically, basically by concatenating strings. Which assembler was that?
2016-06-12 07:27
doynax
Account closed

Registered: Oct 2004
Posts: 212
CA65 supports this, as do a few others I expect. If push comes to shove you can always do ad-hoc meta-programming or preprocessing by adding external tools to the build process.

In CA65 the syntax is something along these lines:
	.mac smc_store name
ident(.concat(.string(name), "_value")) := *+1
	lda #$00
ident(.concat(.string(name), "_addr")) := *+1
	sta a:$0000
	.endmac
2016-06-12 08:29
Hypnosis

Registered: Mar 2015
Posts: 36
Ah, there it is. Thanks!
2016-06-12 17:08
Compyx

Registered: Jan 2005
Posts: 631
Why would you want to do that? Usually when I run into this, my "you're doing it wrong" alarm goes off.

There might be a valid use for this, but I'm not seeing it.
2016-06-12 17:47
Oswald

Registered: Apr 2002
Posts: 5022
probaby some complicated speedcode, where writing native generator would take less time than macroing the shit out of it.
2016-06-12 18:37
MagerValp

Registered: Dec 2001
Posts: 1058
For example if you want a small DSL for writing display lists, like this one here: https://twitter.com/MagerValp/status/741681367516340224
2016-06-12 18:44
doynax
Account closed

Registered: Oct 2004
Posts: 212
Quoting Compyx
Why would you want to do that? Usually when I run into this, my "you're doing it wrong" alarm goes off.
The main place I am using it as a handy way of forwarding metadata and type information to external tools, sort of like C++ name mangling. For instance to encode breakpoints/watchpoints for VICE by running a post-processing script on the label list to transcribe magic labels into monitor commands.

Quoting Oswald
probaby some complicated speedcode, where writing native generator would take less time than macroing the shit out of it.
Indeed but also note that CA65 has scopes which often can achieve the same ends in a cleaner way, at least when operating within a single compilation unit.
2016-06-12 21:13
Compyx

Registered: Jan 2005
Posts: 631
Quoting doynax
Quoting Compyx
Why would you want to do that? Usually when I run into this, my "you're doing it wrong" alarm goes off.
The main place I am using it as a handy way of forwarding metadata and type information to external tools, sort of like C++ name mangling. For instance to encode breakpoints/watchpoints for VICE by running a post-processing script on the label list to transcribe magic labels into monitor commands.[/quote[

I stand corrected, that does indeed sound like a valid use case.

Quoting doynax

Quoting Oswald
probaby some complicated speedcode, where writing native generator would take less time than macroing the shit out of it.
Indeed but also note that CA65 has scopes which often can achieve the same ends in a cleaner way, at least when operating within a single compilation unit.


Speedcode was my first guess, but then again, when generating speedcode, you already all you need to know about entry points or whatever and just 'poke' that in other (speed)code.

I'm kind of surprised CA65 doesn't handle scopes spread over various translation units. But since it's part of a C compiler, it makes sense, C having only a couple of namespaces/scopes.
2016-06-13 05:25
Hypnosis

Registered: Mar 2015
Posts: 36
Quoting Compyx
Why would you want to do that? Usually when I run into this, my "you're doing it wrong" alarm goes off.


I think the general case is a meta programming loop with some code that has to match and reference things in another loop. I can see if I can dig up my latest exact use case.
2016-06-13 06:51
Trash

Registered: Jan 2002
Posts: 122
Sometimes you have speedcode that havn't got a fixed size (and the size-changes may not be cyclic), then it is nice to be able to generate labels when generating it in order to access them from another set of speedcode.
2016-06-13 09:39
Oswald

Registered: Apr 2002
Posts: 5022
Quote: Sometimes you have speedcode that havn't got a fixed size (and the size-changes may not be cyclic), then it is nice to be able to generate labels when generating it in order to access them from another set of speedcode.

just store the "labels" into a table while generating speedcode A, then read the table while generating speedcode B that is referencing A. I do this all the time, and not much code in native 6510 either.
2016-06-13 17:19
Count Zero

Registered: Jan 2003
Posts: 1825
AFAIR Dreamass supports that as well.
2016-06-14 05:23
Slammer

Registered: Feb 2004
Posts: 416
Quoting doynax
Indeed but also note that CA65 has scopes which often can achieve the same ends in a cleaner way, at least when operating within a single compilation unit.
Doynax got a good point. Doing things with scopes often gives cleaner solutions.
2016-06-15 17:34
Hypnosis

Registered: Mar 2015
Posts: 36
Quoting doynax
Indeed but also note that CA65 has scopes which often can achieve the same ends in a cleaner way, at least when operating within a single compilation unit.


How would scopes solve the problem?
2016-06-15 17:40
Hypnosis

Registered: Mar 2015
Posts: 36
Creating a meta-list of addresses where the data is defined and read from the list where addresses should be used would work for my simple case. Does assemblers handle this without requiring data and code to be defined in a certain source code order? I think Kickassembler does. How about the others?
2016-06-15 18:06
Slammer

Registered: Feb 2004
Posts: 416
I don't know the functionality of CA65, but in general the solutions I have seen involving scoping seem better than those where you generate numbered labels to one big global scope. Scoping is also better than putting labels into a list yourself. In KickAss the scoping solution works well for accessing labels in executed macro calls, and I'am planning to expand the functionality to loops as well. So accessing the label of one loop from another could look like this:

        .const count=25
loop1:  .for(var i=0; i<count; i++) {
           lda #i
           sta loop2[i].color+1
        }


loop2:  .for(var i=0; i<count; i++) {
color:     lda #0
           sta $d020
        }

Today you have to put your labels in a list yourself, but I find the above better.

Edit: Scoping solution = you access the scope instance of a given iteration of the loop
2016-06-15 20:16
soci

Registered: Sep 2003
Posts: 474
Example of doynax translated:
smc_store       .segment name
\{name}_value   = *+1
                lda #$00
\{name}_addr    = *+1
                sta @w$0000
                .endm

                *= $1000
                #smc_store test
                sta test_value
But don't do it, this is evil ;)
smc_store       .macro
value           = *+1
                lda #$00
addr            = *+1
                sta @w$0000
                .endm

                *= $1000
test            #smc_store
                sta test.value
Somewhat better.

Hypnosis: Later 64tass versions tolerate "wrong" definition and use ordering.
					    *= $1000
.1000	4c 06 10	jmp $1006	    jmp l[0]
.1003	4c 07 10	jmp $1007	    jmp l[1]

=[$1006,$1007]				l   = [b1, b2]

.1006	ea		nop		b1  nop
.1007	ea		nop		b2  nop
Unsurprisingly it has suboptimal performance compared to proper ordering.

Slammer: I was thinking about this sort of list generation some time ago for .rept when it was still hard to implement it. Now it'd be much easier, but I can't think of a compelling use for it in my sources (yet). Even if it'd be available now probably I'd still write the above example like this:
    *= $1000
count = 25
    .for i = 0, i < count, i = i + 1
    lda #i
    sta (+)+1
    .section speedcode
+    lda #0
     sta $d020
    .send
    .next

    .dsection speedcode ; right after the modifier
This compiles the two code parts in tandem so an anonymous label is good enough. Results in:
.1000	a9 01		lda #$00	    lda #i
.1002	8d 7e 10	sta $107e	    sta (+)+1
.107d	a9 00		lda #$00	+    lda #0
.107f	8d 20 d0	sta $d020	     sta $d020
.1005	a9 01		lda #$01	    lda #i
.1007	8d 83 10	sta $1083	    sta (+)+1
.1082	a9 00		lda #$00	+    lda #0
.1084	8d 20 d0	sta $d020	     sta $d020
...
In case of more labels it'd name them properly and stick in a .block scope to avoid the symbol collisions.
2016-06-16 05:23
Hypnosis

Registered: Mar 2015
Posts: 36
Interesting use of sections!
2016-06-16 05:27
Hypnosis

Registered: Mar 2015
Posts: 36
Quoting Slammer
Scoping solution = you access the scope instance of a given iteration of the loop


Ah, that was the missing part in my puzzle. I haven't seen this before.
2016-06-19 11:15
Slammer

Registered: Feb 2004
Posts: 416
Soci: I prefer the scoping solution so I will stick to that, but I guess its a matter of taste. And nobody says you can't do both. Seems like we pretty much agree on scoping for macro executions...
.macro smc_store() {
      .label value =*+1
      lda #$00
      .label addr = *+1
      sta.a $0000
}

      *= $1000
test: smc_store()
      sta test.value 
2016-06-22 10:48
Peacemaker

Registered: Sep 2004
Posts: 243
at the moment i doing it like this with kickass:

.const count=25
loop1: .for(var i=0; i<count; i++) {
lda #i
sta loop2+1+i*5
}


loop2: .for(var i=0; i<count; i++) {
color: lda #0
sta $d020
}
2016-06-22 11:49
Oswald

Registered: Apr 2002
Posts: 5022
Quote: at the moment i doing it like this with kickass:

.const count=25
loop1: .for(var i=0; i<count; i++) {
lda #i
sta loop2+1+i*5
}


loop2: .for(var i=0; i<count; i++) {
color: lda #0
sta $d020
}


lda #i
ldy #$01
sta (fe),y

lda fe
clc
adc #$05
sta fe
bcc *+4
inc ff
2016-06-22 11:52
Peacemaker

Registered: Sep 2004
Posts: 243
thanks oswald, but that is slower =)
2016-06-22 12:14
Oswald

Registered: Apr 2002
Posts: 5022
no, thats the generator. just wanted to show how simple is it you macro nerds :)
2016-06-22 12:35
Compyx

Registered: Jan 2005
Posts: 631
But what are 'fe' and 'ff', macro's? ;)
2016-06-22 12:37
chatGPZ

Registered: Dec 2001
Posts: 11133
those are not macros, those are labels!
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
goerp/F4CG
tlr
Compyx/Focus
Mixer
Guests online: 128
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 Memento Mori  (9.6)
10 Bromance  (9.5)
Top onefile Demos
1 It's More Fun to Com..  (9.7)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 Dawnfall V1.1  (9.5)
8 Quadrants  (9.5)
9 Daah, Those Acid Pil..  (9.5)
10 Birth of a Flower  (9.5)
Top Groups
1 Nostalgia  (9.3)
2 Oxyron  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Musicians
1 Vincenzo  (9.8)
2 Rob Hubbard  (9.7)
3 Stinsen  (9.7)
4 Jeroen Tel  (9.6)
5 Linus  (9.6)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.076 sec.