RPGHacker / asar

(Now) official repository of the SNES assembler Asar, originally created by Alcaro
Other
199 stars 42 forks source link

How to calculate string size? #264

Closed hellow554 closed 1 year ago

hellow554 commented 1 year ago

What I want is to write a macro that fills a table with strings and pads them to a certain size, in my case 12 bytes.

So my macro looks something like this:

macro CreateStrLut(st)
    !counter #= 0

    while !counter < 12
        if !counter < sizeof(st)
            db <st[!counter]>
        else
            db 0
        endif
        !counter #= !counter+1
    endwhile
endmacro

%CreateStrLut("All")

Problem 1 is, that I can't pass strings as arguments to macros (error: (Einvalid_macro_param_name): Invalid macro parameter name.) and second that it tries to interpret "All" as struct name.

How can I achieve my goal?

RPGHacker commented 1 year ago

I think there is an unmerged pull request with a function to access string size. I'm not sure if/when it'll be merged, though. If I remember correctly, my concern with it was that I wanted clarity in the naming right away. Since Asar 2.0 is going to support UTF-8, I wanted a clear distinction between "byte length" and "character length" of strings. Though I don't remember if we ever reached a consensus there.

Actually, I couldn't find the pull request when looking for it right now, so I don't remember if it was rejected or already merged into some branch...

hellow554 commented 1 year ago

Maybe #201 ?

Are you talking about the https://github.com/RPGHacker/asar/tree/asar_2_beta branch? If yes, are the docs updated? If so, I can give it a try and some feedback if you like :)

RPGHacker commented 1 year ago

Yeah, I think that was the one.

Yeah, that's the current Asar 2.0 dev branch. I don't know how up-to-date the documentation on that branch is. I always try to update it whenever I make larger changes, but I'm not sure if the same is necessarily true for all collaborators, so that branch might have a few features (and/or WIP features) that aren't documented yet.

hellow554 commented 1 year ago

So I managed to achieve my goal with the following code:

macro CreateLut(...)
    fillbyte $00
    !idx #= 0
    while !idx < sizeof(...)
        db "<...[!idx]>"

        padbyte $00
        pad $108000+(!idx+1)*12

        !idx #= !idx+1
    endwhile
endmacro

%CreateLut("All", "A", "And", "But", "Come", "Dream", "Deathtole") ; and so forth...

It's not pretty, but does the job ;)