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 > KickAssembler macro expansion ...
2019-02-24 22:21
Taskmaster
Account closed

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: 27
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
Account closed

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: 172
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
Account closed

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: 1648
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: 424
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: 416
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
REBEL 1/HF
Paul Bearer
Linus/MSL
Mike
Didi/Laxity
Krill/Plush
Guests online: 83
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 Layers  (9.6)
2 No Listen  (9.6)
3 Party Elk 2  (9.6)
4 Cubic Dream  (9.6)
5 Copper Booze  (9.6)
6 Rainbow Connection  (9.5)
7 Dawnfall V1.1  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Crackers
1 Mr. Z  (9.9)
2 Antitrack  (9.8)
3 OTD  (9.8)
4 Fungus  (9.8)
5 S!R  (9.8)

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