flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How to replace char in name?

Author
Thread Post new topic Reply to topic
Jin X



Joined: 06 Mar 2004
Posts: 104
Location: Russia
Jin X
Code:
macro make x
{
  // magic
}

make _1.5e_2    

Macro 'make' must generate label _1.5e_2 with value -1.5e-2, i.e. replace underscores by minus Smile
How can I do this?
Post 12 Feb 2021, 17:39
View user's profile Send private message Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 104
Location: Russia
Jin X
Or vice versa make -1.5e-2 must do the same (replace minus by underscore when defining label).
Post 12 Feb 2021, 17:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18182
Location: In your JS exploiting you and your system
revolution
The usual method for naming constants is to describe their meaning, not their value.
Code:
pi = 3.1415927 ; good
v3.1415927 = 3.1415927 ; useless and redundant

exchange_rate = 2.84510 ; good
v2.84510 = 2.84510 ; useless, no idea what it means    
With regard to your specific question. Take note of these three things:

1) Labels can only be constructed at the preprocessor stage.
2) Strings can only be scanned at the assembler stage.
3) Preprocessor runs before the assembler.

So you can't scan a string using the assembler, and then later use the preprocessor to crate the label.
Post 13 Feb 2021, 00:25
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18182
Location: In your JS exploiting you and your system
revolution
Seeing as the minus is one of the "special" characters, and if you limit your replacement to only special characters, then you can use the preprocessor to construct a new label name.

Something like this::
Code:
macro def_scan {
        macro scan out, in \{
                local has_minus
                has_minus equ n
                match o =, =- b, out, in \\{
                        out equ o\\#_
                        def_scan
                        scan out, b
                        has_minus equ y
                \\}
                match o =, a =- b =, =n, out, in, has_minus \\{
                        out equ o\\#a\\#_
                        def_scan
                        scan out, b
                        has_minus equ y
                \\}
                match o =, =n, out, has_minus \\{
                        out equ o\\#in
                \\}
        \}
}def_scan
macro build_value [number] {
        forward
        local build_label
        build_label equ v       ; labels can't start with a digit
        scan build_label, number
        match lbl, build_label \{
                lbl equ number
        \}
}

build_value 100e-2, 10e-1, -1.0, -10e-1, 0.1e1, -0.1e1, 1.0
dd v100e_2, v10e_1, v_1.0, v_10e_1, v0.1e1, v_0.1e1, v1.0    
But do please consider what I wrote above. Making label names the same as the value doesn't add anything IMO.
Post 13 Feb 2021, 07:17
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 954
Location: Belarus
DimonSoft
It might make. Sometimes having an FP constant instead of constructing it (simple things like 1.0, 2.0, 0.5, etc. are obviously constructible) makes machine code smaller and source code more readable. But this has the disadvantage of having to define lots of labelled memory operands. And if you have two or three of them having the same value, merging them into a single memory location is a good thing to do.

You’re right that having meaningless value-based names in the source is a bad thing, but for such cases it might be a way to go. And wrapping the generation of such stuff into macros is a good way to make it valid from code quality point of view: noone really cares about names in automatically generated code. After all, in HLL you would just have floating-point literals in such places which are basically magic numbers but are quite valid for some formulas.
Post 13 Feb 2021, 07:43
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 104
Location: Russia
Jin X
I need to define labels that will be used primarily by another macro.
Like this:
Code:
calc 4 3 / pi * r dup dup * * *
; ...
def r, ?
def 4
def 3
def pi, 3.14159265359    

So there's no need to assign special names in many cases.
Is there a need to give special name for 3 or 4 ? I think, no.

If the 2nd argument for def is absent then it assumed equal to the 1st.
So def 4 creates smth like fp_4 dd 4; def r, ? creates fp_r dd ?.

I thought about using underscore instead of minus because minus can't be a part of name.
To use def _1.5e_2 to shorten def _1.5e_2, -1.5e-2.
But now I understand that it's not so good idea because realization is too bulky and underscore looks not so cool...
Post 13 Feb 2021, 09:47
View user's profile Send private message Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 104
Location: Russia
Jin X
But I could use dash instead of hyphen, e.g. def –1.5e–2 (visually almost the same as hyphen) Smile
Is there no way to replace dash by hyphen (minus)?
Post 13 Feb 2021, 14:00
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.