flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Stuck with sorting macros

Author
Thread Post new topic Reply to topic
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE
I'm trying to produce macros that sort everything in between.

So what ideal code should do:
— collect instructions in macros;
— sort these macros according to their order (in my code, this step is merged with the previous one);
— execute these macros in order (stuck here in calling them in a loop).
Code:
format binary as 'txt'

; == Macros ==

macro startcraziness
{
  macro dummymacro \{
}

lastorder = 0

macro sortme order
{
  exist_sorter#order = 1
  if lastorder < order
    lastorder = order
  end if
  macro sorter#order \{
}

macro endcraziness
{
;  repeat lastorder
;    if defined exist_sorter#%
;      sorter#%
;    end if
;  end repeat
  sorter1
  sorter2
  sorter3
  sorter200
  sorter255
  ; == STUCK: how do I replace this with a loop? ==
}

sortme       fix } sortme
endcraziness fix } endcraziness

; == Test program ==

startcraziness

sortme 3
  db 'should '

sortme 255
    db 'in between!'

sortme 1
  db 'These '

sortme 200
     db 'sort everything '

sortme 2
     db 'macros '

endcraziness

; Result should be:
; "These macros should sort everything in between!"
    
And I guess even if loop there is possible, with very big lastorder it would take much time, so is there any better suggestions?
Post 30 Mar 2009, 05:45
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: 17250
Location: In your JS exploiting you and your system
revolution
I think the first to realise with fasm is the separation of the preprocessor-stage and the assembler-stage. Above you have mixed up the two parts so that makes the macros impossible to work like that.

You need to decide beforehand if you want to use the preprocessor-stage for sorting (hard) or the assembler-stage for sorting (easier).

One place to start looking for examples is the export macros in the windows package. It is an assembler-stage based sorter but does not sort strings of unequal length, it sorts the indices only. So you will need to make a few changes to get it to work with variable length strings.
Post 30 Mar 2009, 06:10
View user's profile Send private message Visit poster's website Reply with quote
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE
Thanks, revolution, but I don't really see where sorting occurs in export macros. What does it sort exactly?

I have fasm 1.67.27 if it does matter.
Post 30 Mar 2009, 06:37
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: 17250
Location: In your JS exploiting you and your system
revolution
This part:
Code:
   common
    local x,y,z,str1,str2,v1,v2
    x = count shr 1
    while x > 0
     y = x
     while y < count
      z = y
      while z-x >= 0
       load v1 dword from names+z*4
       str1=($-RVA $)+v1
       load v2 dword from names+(z-x)*4
       str2=($-RVA $)+v2
       while v1 > 0
    load v1 from str1+%-1
       load v2 from str2+%-1
       if v1 <> v2
    break
      end if
       end while
       if v1<v2
    load v1 dword from names+z*4
        load v2 dword from names+(z-x)*4
    store dword v1 at names+(z-x)*4
     store dword v2 at names+z*4
 load v1 word from ordinal+z*2
       load v2 word from ordinal+(z-x)*2
   store word v1 at ordinal+(z-x)*2
    store word v2 at ordinal+z*2
       else
 break
       end if
       z = z-x
      end while
      y = y+1
     end while
     x = x shr 1
    end while }
    
It sorts the export names into alphabetical order. But, like I mentioned, only the indices are sorted so if you look at an exe file with a hex editor you might think it is unsorted until you follow the indices and see how it works.
Post 30 Mar 2009, 06:53
View user's profile Send private message Visit poster's website Reply with quote
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE
Ah! Now I see.
Now I'm trying to understand how can I use it.

Imagine we have a string sorting macro, how does it help?

I guess we need a list sorting macro...
Post 30 Mar 2009, 07:31
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: 17250
Location: In your JS exploiting you and your system
revolution
Here is a very simple preprocessor sorter, but it still requires a manually set maximum loop counter (256 in this case):
Code:
macro sortme level*,text* {
      @sort#level equ text
        @sort#level#flag equ y
}
macro dosort {
     rept 256 level:0 \{
               match =y,@sort\#level\#flag \\{
                    db @sort\#level
            \\}
  \}
}

sortme 3,'should '
sortme 255,'in between!'
sortme 1,'These '
sortme 200,'sort everything '
sortme 2,'macros '

dosort    
Post 30 Mar 2009, 16:42
View user's profile Send private message Visit poster's website Reply with quote
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE
Thank you, revolution!

I actually wanted to remove a limitation in my patching macros
that "patchat" offsets must be in ascending order,
but trying several times with integrating your sorter, I was able only to complicate the syntax
(i.e. couldn't just remove the limitation without changing the syntax),
so I guess I'll just leave these macros as is, until
— I get more experienced with fasm,
— or somebody suggests a clear syntax solution,
— or Tomasz makes something interesting that allows changing assembling point =)
Post 30 Mar 2009, 18:21
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
Is this more ... logical?
Code:
macro sortme order*,text* {
        sort#order equ text
}
macro dosort {
        rept 256 order:0 \{
                if defined sort\#order
                        db sort\#order
                end if
        \}
}

sortme 3,'should '
sortme 255,'in between!'
sortme 1,'These '
sortme 200,'sort everything '
sortme 2,'macros '

dosort
    
Post 31 Mar 2009, 10:31
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17250
Location: In your JS exploiting you and your system
revolution
Uses the assembler-stage. My posting was preprocessor-stage only.
Post 31 Mar 2009, 11:07
View user's profile Send private message Visit poster's website 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.

Powered by rwasa.