flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How to determine string or number during preprocessing?

Author
Thread Post new topic Reply to topic
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 22 Sep 2012, 16:56
Now, I have following problem.
Thanks to FASM 1.71, at the end my problem with unique string definitions is solved. I managed to make macros that to define string constants only once, even if they are defined different way. With hash table search and some macro tricks, these macros are fast enough to be included in FreshLib.

Now the problem is following - how to make "stdcall" and other call macros to work with string constants. I use following macro:

Code:
macro pushx [arg] {
common
  local flag, cnt
  cnt = 0
  flag = 1
forward
  local sz
  cnt = cnt + 1
  if arg eqtype ''
    virtual at 0
      db arg
      sz = $
    end virtual
    if (sz > 4) | (cnt>1)
      flag = 0
    end if
  end if
common
  local lbl

  if flag
    pushd arg
  else
    pushd lbl
    lbl text arg
  end if
}    


Note that "text" struc does not define the string immediately, but rather stores the text and the name of the label in a list for later definition.

This way, the above solution has one big disadvantage - the string "arg" will always be added to the string list, even if it is not string at all, because the "text" struc is processed in the preprocessing stage, but whether the argument is string or number is determined in the assembling stage.

This way, all procedure arguments across the project (they are thousands) will get added to the strings list and only some very little part of them will be defined as strings later.

So, is it possible to determine the type of the argument in the preprocessing stage and to not insert everything in the list, but only the strings.

Any ideas?

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 22 Sep 2012, 16:56
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 22 Sep 2012, 17:49
I think it was discussed before: http://board.flatassembler.net/topic.php?p=90836
Post 22 Sep 2012, 17:49
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 22 Sep 2012, 18:08
Yes, I just reinvented the wheel and found this solution myself. Embarassed
Although, it is not working as described. I use following code in order to get it to work:
Code:
match x, `arg {
  match x, arg \{
    flg equ TRUE
  \}
}    


EDIT: Never mind, I found the mistake - missing "\". It works now. Wink


Last edited by JohnFound on 22 Sep 2012, 18:17; edited 1 time in total
Post 22 Sep 2012, 18:08
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 22 Sep 2012, 18:17
What do you mean by "not working as described"? This works correctly for me:
Code:
macro tellquoted param
{ 
  match `param,param \{ display "Yes, it's a quoted string: ",param,13,10 \}
}

tellquoted 'ABCD'
tellquoted abcd
tellquoted 1234    
It displays text only in first invocation.
Post 22 Sep 2012, 18:17
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 22 Sep 2012, 18:18
Yes, I found the problem. It was missing "\". Fixed and works now. Thank you for the help.
Post 22 Sep 2012, 18:18
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 22 Sep 2012, 20:24
JohnFound
I'm not really using FreshLib (just took a couple of ideas from there), but if I used it, I'd prefer to have something like this, because it's sometimes very handy to pass a number in the form of 'Hi.' . Besides, it's very useful to be able to pass strings with non-printable characters like 'Hello',13,10,'World!', which would be impossible with your approach. An uncontrollable null-character at the end of such strings is also an unpleasant side-effect of your approach.
Post 22 Sep 2012, 20:24
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 22 Sep 2012, 20:36
l_inc, the exact syntax of the latest macros allows all of described options.
Code:
stdcall MyProc, <"Hello", 13, 10, "World!">    


The strings that are single and up to dword are pushed directly:
Code:
stdcall MyProc, '1234'
; will generate:
push '1234'
; instead of:
push lbl?
lbl? text '1234'    


If you need short strings defined as pointer (rare case) you can use:
Code:
stdcall MyProc, <'123','4'>    


What kind of control you need on the terminating character?
Post 22 Sep 2012, 20:36
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 22 Sep 2012, 20:42
JohnFound
Quote:
the exact syntax of the latest macros allows all of described options.

Oh. Sorry. I didn't think, you implemented it.
Quote:
What kind of control you need on the terminating character?

Well... it's presence. Smile If you allowed non-printable characters to be a part of such strings, then it's also reasonable to allow the user to decide, whether he or she wants to add the null-character:
Code:
stdcall MyProc, <"Hello", 13, 10, "World!", 0>    


Btw. Do you also allow to pass one string in Unicode and one string in ASCII?
Post 22 Sep 2012, 20:42
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 22 Sep 2012, 21:09
l_inc wrote:
Btw. Do you also allow to pass one string in Unicode and one string in ASCII?


Yes and No. If you talk about Windows Unicode (UTF-16) - then "No". This is intentional, because you simply can't use UTF-16 with FASM in this moment. I know FASM have "du" directive, but you can define text using only ASCII text - i.e. less than 256 symbols.

On the other hand, FreshLib uses only UTF-8, so if you have UTF-8 source, "text" macro will define Unicode string in UTF-8 encoding.
Note, that FASM can compile UTF-8.

Also, the future Fresh IDE 3.0 will be equipped with fine Unicode source editor.

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 22 Sep 2012, 21:09
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 22 Sep 2012, 21:21
JohnFound
Quote:
I know FASM have "du" directive, but you can define text using only ASCII text - i.e. less than 256 symbols.

The du directive is redefined by the standard encoding macros, which convert zero-extended ASCII-characters into Unicode using the corresponding code tables. Therefore fasm does support definition of UTF-16 strings. And it would be useful to be able to specify, that I'd like to use the du directive to define a string. In the previously linked example this looks as follows:

Code:
invoke MessageBox,NULL,<ustring "Hello, world!",0>,<ustring "Title",0>,MB_OK    

Both strings would be defined in Unicode, and I could use Cyrillic as well.
Post 22 Sep 2012, 21:21
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 22 Sep 2012, 21:36
You can use Cyrillic, only if your editor is configured to use Cyrillic font.
But you can't use "Cyrillic" and "Japan" in the same time, while using UTF-8, it is possible. FreshLib has some examples using several languages at once:
Image
Post 22 Sep 2012, 21:36
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 22 Sep 2012, 21:44
JohnFound
Quote:
But you can't use "Cyrillic" and "Japan" in the same time, while using UTF-8, it is possible.

That's true. But how about using Unicode functions with Roman? Do understand it right, that the following will display trash with your macros?
Code:
invoke MessageBoxW ,NULL,"Hello, world!","Title",MB_OK    
Post 22 Sep 2012, 21:44
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 22 Sep 2012, 21:56
Yes, it will.
But note, that FreshLib is not intended to be used this way (although it is not forbidden) because the programs written with FreshLib are intended to be portable. So, you should not use "MessageBoxW", but:
Code:
stdcall ShowMessage, [parent], smiInformation, 'Title', 'Message text.', smbOK    

The above procedure needs UTF-8 encoding and will be compiled properly for Windows and Linux with every language you want.
Post 22 Sep 2012, 21:56
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 22 Sep 2012, 22:07
JohnFound
Quote:
The above procedure needs UTF-8 encoding and will be compiled properly for Windows and Linux with every language you want.

Sounds really cool. But also requires really much support. Then last question regarding this fact. Let's say, I'd like to open an image saved in BMP, draw some text above it and save it in PNG format. I'd use GDI+ for that, which requires Roman in Unicode at some places. Would it be possible to do the same using the portable features of FreshLib only?
Post 22 Sep 2012, 22:07
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 22 Sep 2012, 22:34
l_inc wrote:
Would it be possible to do the same using the portable features of FreshLib only?


Not in this moment, but definitely it will be possible one day. Smile
For now, FreshLib has limited features, because I can't write everything as quick as I want. Wink
In this very moment you can open .gif image, write some text ( UTF-8 ) and display it on some window. Of course you can draw the text directly on the window as well.
.png support is planned but not implemented yet.

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9


Last edited by JohnFound on 22 Sep 2012, 22:47; edited 2 times in total
Post 22 Sep 2012, 22:34
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 22 Sep 2012, 22:40
JohnFound
Thank you for the answers. Now I have at least more understanding of the FreshLib motivation. But converting fasm into java sounds to me too laborious.
Post 22 Sep 2012, 22:40
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.