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


Forums > C64 Coding > KickAssembler macro expansion ...
2019-02-24 22:21
Taskmaster

Registered: Feb 2018
Posts: 22
KickAssembler macro expansion ...

I want to do something like this but it's eluding me in KickAssembler:

.macro AllocString( _name_ )
{
	_name_:
		.fill 100,32
		.byte 0
}

AllocString( string1 )
AllocString( string2 )


My hope would be that this code would be generated:

string1:
	.fill 100,32
	.byte 0
string2:
	.fill 100,32
	.byte 0


But instead I get errors about "_name_" is being redefined ...

Is this sort of thing not possible in Kick? Just curious if it's possible to generate code like that ...
2019-02-25 18:07
0xDB

Registered: Aug 2015
Posts: 17
Any labels put inside a scope are not visible from outside that scope. Macro calls are encapsulated in a scope, so what you are trying to do would not work even if Kick Assembler supported turning the parameter into a label because you can not access the label from outside the scope.

The error you are getting about "_name_" being redefined is because "_name_" is a parameter of the macro and then you try to redefine it as a label.

You could still achieve something similar to what you are trying to do by using a hashtable ( http://theweb.dk/KickAssembler/webhelp/content/ch06s04.html ) where you put the name of the string as key and the current program counter as value.

e.g. like this:
.var stringtable = Hashtable()


.macro AllocString( _name_ )
{
	.eval stringtable.put( _name_, *)
		.fill 100,32
		.byte 0
}

* = $0800

AllocString( "string1" )
AllocString( "string2" )


.var keys = stringtable.keys()
.for (var i=0; i<keys.size(); i++)
{
  .var k = keys.get(i)
  .print k + " is at $" + toHexString(stringtable.get(k),4)
}


Personally I would not do that though as it makes the code less readable. Instead I'd change the macro to allow setting a size and clear character via parameters and then just write the definitions like this:

string1: newString(100, ' ')
string2: newString(50, '@')

Since you have to type out the names of the strings anyway with or without support for having them turned into a unique label, having the name as a parameter of a macro would not make writing the code any less tedious.
2019-02-25 19:01
Taskmaster

Registered: Feb 2018
Posts: 22
Thanks for the reply ... String was probably a bad choice of example code, I apologize. It's just what I was working on at the time. :)

Good point about the scoping. Blah.

I guess macros in Kick aren't going to work the way I want them to.

What I'm trying to do is generalize a system where I can use a form of code snippets.

Like, here's a piece of code I use a lot. The only differences are that some of the label names used inside change to suit the situation. I want some way in Kick to drop in these snippets, replace the label names inside that need replacing, and insert the finished snippet into the code before compilation.

I had this in my home brew assembler and I fear it's something I've come to rely on but maybe nobody else supports... heh.
2019-02-26 12:18
Stone

Registered: Oct 2006
Posts: 134
ca65 supports label generation from macros. The simple way:
.macro AllocString name
name:
  .res 100,32
  .byte 0
.endmacro

  lda #<str1
  ldy #>str1
  jsr $ab1e
  ...
AllocString str1

and through strings or concatenation of strings:
.macro AllocString2 name
.ident(name): 
  .res 100,32
.ident(.concat(name, "_end")): 
.ident(.concat(name, "_len")) = .ident(.concat(name, "_end")) - .ident(name)
.endmacro

  ldy #0
loop:
  lda str2,y
  beq done
  jsr $ffd2
  iny
  cpy #str2_len
  bne loop
done:
  ...
AllocString2 "str2"

or even
.macro AllocString3 name
AllocString2 .string(name)
.endmacro

AllocString3 str3
2019-02-26 12:40
Taskmaster

Registered: Feb 2018
Posts: 22
Yeah, that's what I'm looking for ... maybe Kick was the wrong choice for my workflow. :)

Thanks, I'll poke around ca65 and maybe a few others...
2019-02-26 15:54
Frantic

Registered: Mar 2003
Posts: 1372
If you're looking for purely textual stuff when you talk about macro expansion, you could use a separate general purpose preprocessor I guess?

I really have no idea what to use, and I haven't tried it myself, but maybe something like this would work in that case?

http://boldinventions.com/index.php?option=com_content&view=art..
2019-02-26 18:52
Mr. SID

Registered: Jan 2003
Posts: 358
If you want a generic pre-processor (like C/C++) then k2asm has one called k2pp that is python-based, so you can also embed python code to emit labels/data into your source-code.
http://k2devel.sourceforge.net[/url]
Your example would roughly look like this (with a variable length):

#pybegin
def allocString(name, length):
	print "%s: .buf %d,32" % (name, length)
	print ".byte 0"
#pyend

;....

#pybegin
	allocString("str1", 100)
	allocString("str2", 8)
#pyend


After pre-processing, this would look like:

str1: .buf 100,32
.byte 0
str2: .buf 8,32
.byte 0
2019-02-27 21:48
Slammer

Registered: Feb 2004
Posts: 403
In Kick Assembler you would normally access labels inside the macro by putting a label infront of the macro execution:
        *=$1000
start:  inc c1.color
        dec c2.color
c1:     :setColor()
c2:     :setColor()
        jmp start

.macro setColor() {
        .label color = *+1
        lda #0
        sta $d020
}

In a similar way you can access for-loops and if's. Read about it here: http://theweb.dk/KickAssembler/webhelp/content/ch09s07.html

Don't know if it helps your code snippets case.
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
bugjam
Snabel
hedning/G★P
Dymo/G★P
Alakran_64
GI-Joe/MYD
ZeSmasher/F4CG
msolajic
Sentinel/Excess
Jazzcat/Onslaught
Mibri/HF
Guests online: 64
Top Demos
1 Uncensored  (9.7)
2 Unboxed  (9.7)
3 Edge of Disgrace  (9.7)
4 Coma Light 13  (9.6)
5 Comaland 100%  (9.6)
6 Lunatico  (9.6)
7 The Shores of Reflec..  (9.6)
8 X Marks the Spot  (9.5)
9 Old Men in Used Cars  (9.5)
10 C=Bit 18  (9.5)
Top onefile Demos
1 Bad Boy  (9.7)
2 Smile to the Sky  (9.6)
3 Daah, Those Acid Pil..  (9.5)
4 Dawnfall V1.1  (9.4)
5 FMX Music Demo  (9.4)
6 Rewind  (9.4)
7 Crystal Gazer  (9.4)
8 Official X2018 Report  (9.4)
9 Arok 20 Invitation  (9.4)
10 Pandemoniac Part 2 o..  (9.4)
Top Groups
1 PriorArt  (9.7)
2 Oxyron  (9.5)
3 Booze Design  (9.4)
4 Censor Design  (9.3)
5 Performers  (9.3)
Top Original Suppliers
1 hedning  (9.8)
2 Derbyshire Ram  (9.4)
3 Irata  (9.2)
4 Fungus  (9.1)
5 Jazzcat  (9.1)

Home - Disclaimer
Copyright © No Name 2001-2019
Page generated in: 0.048 sec.