flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > macro for generate fixed string

Author
Thread Post new topic Reply to topic
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++ 21 Feb 2013, 02:22
Can I generate a macro that given "foo" generate:

Code:
string db 3,"foo"
    


I have no idea how to do this..."foo" isn't in memory(yet) then
Code:
$-string    
isn't possible. I'm very new to macroslanguage's fasm.
Thanks in advance.
Post 21 Feb 2013, 02:22
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 21 Feb 2013, 08:19
Bob++,

By fixed you've meant Turbo Pascal-style string (i.e. db length, "literal")? There are several ways, one of them is to use store directive:
Code:
struc pstring [literal=255 dup 0] {
common
  . db 0, literal
  store $-.-1 at .
}

aa      pstring "aa"    ; yields aa db 2, "aa"
bbb     pstring "bbb"   ; yields bbb db 3, "bbb"    
If given string literal is too long, store will fail (you may add more meaningful diagnostic message though).
Post 21 Feb 2013, 08:19
View user's profile Send private message Reply with quote
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++ 21 Feb 2013, 22:42
Yes,Turbo Pascal-style string. I will test it. @baldr, Thank you so much Smile
Post 21 Feb 2013, 22:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 22 Feb 2013, 06:24
Rather than using store a local label can be used also.
Code:
struc pstring [text] {
        common
        local .length
        . db .length,text
        .length = $-.-1
}    
Post 22 Feb 2013, 06:24
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 22 Feb 2013, 10:23
Even local directive is not necessary, given that struc-macro label should be unique, and using standard processing of symbols starting with dot (within struc-macro body):
Code:
struc pstring [text*] {
common
  . db .size-1, text
  .size = $-.
}    
Post 22 Feb 2013, 10:23
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 22 Feb 2013, 10:39
And if you want to point directly to the string and have the length at an offset of -1:
Code:
struc pstring [text*] {
        common
          db .size
        . db text
        .size = $-.
}    


Edited out the subtraction of 1 as suggested below


Last edited by revolution on 22 Feb 2013, 16:06; edited 1 time in total
Post 22 Feb 2013, 10:39
View user's profile Send private message Visit poster's website Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 22 Feb 2013, 14:32
revolution wrote:
Code:
.size = $-.-1    

I'd avoid subtracting 1 in this case.
Post 22 Feb 2013, 14:32
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 22 Feb 2013, 16:07
l_inc wrote:
revolution wrote:
Code:
.size = $-.-1    

I'd avoid subtracting 1 in this case.
Thanks. I've fixed it above.
Post 22 Feb 2013, 16:07
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 22 Feb 2013, 16:09
It can be generalized, too:
Code:
struc counted [def*] {
common
  local element_size, count
  match directive value, def \{
    virtual at 0
      directive ?
      element_size = $
    end virtual
    . directive count, value
    count = ($-.)/element_size-1
  \}
}

pstring fix counted db
pwstring fix counted du    
Though not too much — only db/du accept string literals. Wink

OTOH, one can write struc utf32 macro and use pdwstring fix counted utf32.
Post 22 Feb 2013, 16:09
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 23 Feb 2013, 00:19
baldr
Quote:
Though not too much — only db/du accept string literals.

I sometimes generalize for db/display . But it doesn't make much sense here. Btw. generalizing definition of count is a little strange. More consistent would be using same type for the count across all specializations of your generalization. Smile
Quote:
one can write struc utf32 macro and use pdwstring fix counted utf32

I'm not sure about how many would agree with me, but my simple rule is that "fixing is bad. Never fix unless it's unavoidable". Thus I'd rewrite the example like this:
Code:
struc pstring [args] { common . counted db args }    
Post 23 Feb 2013, 00:19
View user's profile Send private message Reply with quote
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++ 02 Mar 2013, 01:09
Thanks very much for the all answers. Very Happy
Post 02 Mar 2013, 01:09
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.