flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > macro for generate fixed string |
Author |
|
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" |
|||
21 Feb 2013, 08:19 |
|
Bob++ 21 Feb 2013, 22:42
Yes,Turbo Pascal-style string. I will test it. @baldr, Thank you so much
|
|||
21 Feb 2013, 22:42 |
|
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 } |
|||
22 Feb 2013, 06:24 |
|
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 = $-. } |
|||
22 Feb 2013, 10:23 |
|
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 |
|||
22 Feb 2013, 10:39 |
|
l_inc 22 Feb 2013, 14:32
revolution wrote:
I'd avoid subtracting 1 in this case. |
|||
22 Feb 2013, 14:32 |
|
revolution 22 Feb 2013, 16:07
l_inc wrote:
|
|||
22 Feb 2013, 16:07 |
|
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 OTOH, one can write struc utf32 macro and use pdwstring fix counted utf32. |
|||
22 Feb 2013, 16:09 |
|
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. 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 } |
|||
23 Feb 2013, 00:19 |
|
Bob++ 02 Mar 2013, 01:09
Thanks very much for the all answers.
|
|||
02 Mar 2013, 01:09 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.