flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > Enhancement proposal: string size prefix in db

Author
Thread Post new topic Reply to topic
DOS386



Joined: 08 Dec 2006
Posts: 1900
DOS386 29 Mar 2013, 08:01
Code:
db "FASM is (almost) great !!!",0
...
call printzt
    


but there is a better way:

Code:
db 26,"FASM is (almost) great !!!"
...
call printsp
    


unfortunately, the "26" is a pain as I have to evaluate the string length manually and this causes BUG's if I evaluate it incorrectly or change the text later and forget to adjust the size prefix Sad

It would be cool if FASM supported something like:

Code:
db %s,"FASM is (almost) great !!!"
; "%s" is UINT8 size until EOL

db %s,"FASM is (almost) great !!!", %s, "Really !!!"
; "%s" is UINT8 size until next "%s"

db %s,"FASM is (almost) great !!!", %r, "Really !!!" ; 
"%s" is UINT8 size until "%r" , but "%r" doesn't generate any prefix
    


All this limited to 8-bit stuff: only db (not dw, dd, dq, dt, dn, du, ...) and sizes < 256 Byte's.

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 29 Mar 2013, 08:01
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1150
Location: Russian Federation
comrade 29 Mar 2013, 08:57
Have you tried this:
Code:
db mystring.length - mystring
mystring db "FASM is great!!!",0
label mystring.length
    


(don't know if this actually assembles, might be syntactic errors).

Basically you define a label at the beginning of the string, and another label at the end of the string. Then you put a db (which will get backfilled) that contains a byte value with a difference between the two labels.

In the above example, the beginning label is "mystring" and the end label is "mystring.length".
Post 29 Mar 2013, 08:57
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1900
DOS386 29 Mar 2013, 09:05
comrade wrote:
Have you tried this


Sure there are ways to get the length (usable for data >= 256 Byte's), but for my 1000's of short strings (<256 Byte's), I'd prefer a built-in method not polluting and bloating the source too much.
Post 29 Mar 2013, 09:05
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 29 Mar 2013, 09:22
DOS386,

Here you go: macro for generate fixed string. Quite confusing thread title, though.

Preprocessor is a memory-hungry bitch, and macros can irreversibly damage your brain. Wink
Post 29 Mar 2013, 09:22
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1900
DOS386 29 Mar 2013, 10:04
> Here you go: macro for generate fixed string
> http://board.flatassembler.net/topic.php?t=15147

Code:
struc pstring [text*] {
common
  . db .size-1, text
  .size = $-.
}

aa2 pstring "abcd",$FF,0,0,"G"
aa3 pstring 0,0,0
aa4 pstring $FF

; aa5: pstring $FF
;      pstring $FF
    


Thanks ... works ... partially only ... only one string per line, and, even worse, it needs a unique label without doubledot at every single line Sad
Post 29 Mar 2013, 10:04
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 29 Mar 2013, 13:03
DOS386
You could use a generalized macro for resolving such situations:
Code:
;Allows using structures as macros (i.e. a structure will be unnamed)
;usage: defStrucsAsMacros POINT,LINE
;               POINT
;               LINE <1,2>,<1,4>
macro defStrucsAsMacros [name*]
{
        forward
                macro name [arg]
                \{
                        \common
                                \local ..lbl
                                ..lbl name arg
                \}
}

defStrucsAsMacros pstring    

After this you can use pstring without the unique label requirement. And placing multiple statements on the same line is possible with another simple macro:
Code:
;Allows positioning of several instructions on (at) one line
;usage: @1l <CALL FF>,<ADD EAX,T01>,<MOV ECX,S11>,<MOV EBX,M01>,<CALL CP>
macro @1l [i] { forward i }    

_________________
Faith is a superposition of knowledge and fallacy
Post 29 Mar 2013, 13:03
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 29 Mar 2013, 13:11
baldr wrote:
Preprocessor is a memory-hungry bitch

I can't disagree on this. Current memory management even discourages from cleaning up after oneself, as using restore/restruc/purge makes preprocessor consume even more memory rather than releasing it.

_________________
Faith is a superposition of knowledge and fallacy
Post 29 Mar 2013, 13:11
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 29 Mar 2013, 13:29
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:14; edited 1 time in total
Post 29 Mar 2013, 13:29
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1900
DOS386 29 Mar 2013, 13:50
> You could use a generalized macro for resolving such situations
> And placing multiple statements on the same line is possible with another

Interesting, so how to use those 2 ? How close can I approach the bottommost code in topmost post?
Post 29 Mar 2013, 13:50
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 29 Mar 2013, 14:21
DOS386
Quote:
How close can I approach the bottommost code in topmost post?

You can approach your syntax as close as you want. I provided a couple of macros from my library, that allow to overcome the problems you've mentioned, but the syntax is a little bit different.
Quote:
Interesting, so how to use those 2 ?

I'm not sure, I understand what you find unclear. The description and usage examples I provided seem to be pretty comprehensive.

_________________
Faith is a superposition of knowledge and fallacy
Post 29 Mar 2013, 14:21
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1900
DOS386 29 Mar 2013, 15:06
Code:
org $10

struc pstring [text*] {
common 
  . db .size-1, text 
  .size = $-. 
} 
macro DSAM [name*]
{ 
        forward 
                macro name [arg] 
                \{ 
                        \common 
                                \local ..lbl 
                                ..lbl name arg 
                \} 
} 

DSAM pstring

macro @1l [i] { forward i }

bugger: pstring "abcd",$FF,0,0,"G"
        pstring 0,0,0

@1l < aa4: pstring $FF > , < pstring $EE,$AA >

db bugger ; $10
db aa4    ; $1D
    


YES it does work ... upper macro drops the label requirement, lower allows a hacky way to put multiple strings on 1 line :-#
Post 29 Mar 2013, 15:06
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 31 Mar 2013, 18:49
DOS386,

Perhaps this way is less hacky:
Code:
struc pstring [text*] {
common
  . db .size-1, text
  .size = $-.
}

macro pstrings [arg*] {
forward
  ? equ ?
  match name:value, arg \{
    restore ?
    ? equ !
    name pstring value
  \}
  match =?, ? \{
    \local ..name
    ..name pstring arg
  \}
  restore ?
}

pstrings a:"Hello, world!", <b:"Goodbye, world!", 13, 10>, <"That's all folks!", 13, 10>    
It's written only to demonstrate the idea, you may wish to rewrite it to better suit your needs.
Post 31 Mar 2013, 18:49
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1900
DOS386 10 Apr 2013, 13:29
PString example is out: http://board.flatassembler.net/topic.php?t=10872
http://board.flatassembler.net/download.php?id=4673 (macro's NOT used yet ...)

Still, I'd prefer a built-in solution from macros, and maybe another layer:

Code:
db %c
db %s,"FASM is (almost) great !!!"
db %s,"ROS-ASM is not great !!!"
db %s,"MASM is ???"
db %d
    


"%c" would count the elements (here 3) until next "%c" (generating next count or "%d" (generating nothing).

http://queue.acm.org/detail.cfm?id=2010365 Laughing
Post 10 Apr 2013, 13:29
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.