flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Automatic size optimization

Author
Thread Post new topic Reply to topic
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE
Let fasm do some tricky optimizations for us.

I suggest writing automatic size optimization macros here.

Or, if you know optimizing trick, but don't know how to / too lazy / otherwise unable to write a macro, post it here and someone (or I) will convert it to macro.

I start:
Code:
macro mov op1, op2
{
  if op2 eqtype 0
    if op2 = 0
      if op1 eqtype eax
        xor op1, op1
      else
        and op1, 0
      end if
    else if op2 = -1
      or op1, -1
    else if op2 = 1 & op1 in <eax,ebx,ecx,edx,esi,edi,ebp,esp>
      xor op1, op1
      inc op1
    else
      mov op1, op2
    end if
  else
    mov op1, op2
  end if
}
    

As I don't know 64-bit assembly well, my macros will be for 32-bit and 16-bit mode, mostly 32-bit.
Post 18 Jul 2008, 00:47
View user's profile Send private message Visit poster's website Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 166
Location: Maryland, USA
rCX
Thats a good idea. Here are a few I use (in 16 bits)

Code:
macro mov op1,op2
{
  if ( op1 in <ds,es,ss>   &   ( (op2 in <cs,ds,es,ss>)|(op2 eqtype 1)) ) ;ex. "mov es,ds" or "mov es,0xA000"
             push word op2
               pop word op1
        else if ((op1 eqtype word [si])) & ((op2 eqtype word [si]))         ;ex. "mov word [si],word [di]"; can not be bytes
          push op2
            pop op1
     else
                mov op1,op2
 end if
}
    


Code:
macro xchg op1,op2
{
 if (op1 eqtype word [si]) & (op2 eqtype word [si])  ;ex. xchg word [si],word [di]
               push op1
            push op2
            pop op1
             pop op2
     else
                xchg op1,op2
        end if
}
    
Post 18 Jul 2008, 01:01
View user's profile Send private message Reply with quote
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE
rCX, but that's extending syntax, not optimizing?
Post 18 Jul 2008, 01:05
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
There is a thread about this already but unfortunately I can't find it... If someone found it I would like to merge this thread into the old one.
Post 18 Jul 2008, 01:37
View user's profile Send private message Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 166
Location: Maryland, USA
rCX
Grom PE wrote:
rCX, but that's extending syntax, not optimizing?

Yeah your probably right. But it does make my life a little easier Wink
Post 18 Jul 2008, 01:54
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
LocoDelAssembly wrote:
There is a thread about this already but unfortunately I can't find it... If someone found it I would like to merge this thread into the old one.
Maybe this thread?
Post 18 Jul 2008, 03:39
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Yes, that one but seems to be a mess, it is glued with many discussion about how the fasm's code generation should be so it would be better that each author re-posts its working macros here...

Thanks for finding it revolution.
Post 18 Jul 2008, 03:50
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
LEA size optimisation:
Code:
macro lea [stuff] {
        common
      local b1,b2,b3,address
      address equ
 match reg=,[addr],stuff\{address equ addr\} 
    match reg=,size[addr],stuff\{address equ addr\} 
        match reg=,size =ptr addr,stuff\{address equ addr\} 
    virtual
             b1=0
                b2=0
                leA stuff
           if ($-$$)=2
                 load b1 byte from $$
                        load b2 byte from $$+1
              else if ($-$$)=3
                    load b1 byte from $$
                        load b2 byte from $$+1
                      load b3 byte from $$+2
                      if ~(b1=8dh & (b2 and 307o)=004o & b3=24h) & \ ;lea reg,[esp]
                     ~(b1=8dh & (b2 and 307o)=105o & b3=00h)     ;lea reg,[ebp]
                               b1=0
                        end if
              else if ($-$$)=6 & ~address eq
                  load b1  byte from $$
                       load b2  byte from $$+1
                     if b1=8dh & (b2 and 307o)=005o ;lea reg,[immediate]
                             b1=100h
                     else
                                b1=0
                        end if
              end if
      end virtual
 if b1=08dh
          if ~((b2 and 7o)=((b2 and 70o) shr 3))
                      db 08bh,b2 or 300o
          else
                        ;do nothing
         end if
      else if b1=100h
             db 0b8h or ((b2 and 70o) shr 3)
             dd address
  else
                leA stuff
   end if
}    

TEST size optimisation:
Code:
macro test op,val {
  local address,opcode,length,start
   start=$
     address equ
 match [addr], op \{ address equ addr \}
 match size [addr], op \{ address equ addr \}
    match size =ptr addr, op \{ address equ addr \}
 if val eqtype 0
             virtual at 0
                        test op,val
                 length=$-$$
                 repeat $
                            load opcode byte from %-1
                           if opcode <> 66h & opcode <> 67h & \
                              opcode <> 64h & opcode <> 65h & \
                              opcode <> 26h & opcode <> 2eh & \
                              opcode <> 36h & opcode <> 3eh
                                    break
                               end if
                      end repeat
          end virtual
         if opcode = 0F7h & ~ address eq
                 if val = (val) and 7Fh
                              test byte [address],val
                     else if val = (val) and 7F00h
                               test byte [address+1],(val) shr 8
                   else if val = (val) and 7F0000h
                             test byte [address+2],(val) shr 16
                  else if val = (val) and 0FF000000h
                          test byte [address+3],(val) shr 24
                  else if val = (val) and 07FFFh
                              test word [address],val
                     else if val = (val) and 07FFF00h
                            test word [address+1],(val) shr 8
                   else if val = (val) and 0FFFF0000h
                          test word [address+2],(val) shr 16
                  else
                                test op,val
                 end if
              else if op eq eax & val = (val) and 7Fh
                 test al,val
         else if op eq eax & val = (val) and 7F00h
                       test ah,(val) shr 8
         else if op eq ebx & val = (val) and 7Fh
                 test bl,val
         else if op eq ebx & val = (val) and 7F00h
                       test bh,(val) shr 8
         else if op eq ecx & val = (val) and 7Fh
                 test cl,val
         else if op eq ecx & val = (val) and 7F00h
                       test ch,(val) shr 8
         else if op eq edx & val = (val) and 7Fh
                 test dl,val
         else if op eq edx & val = (val) and 7F00h
                       test dh,(val) shr 8
         else if op eq ax & val = (val) and 7Fh
                  test al,val
         else if op eq ax & val = (val) and 0FF00h
                       test ah,(val) shr 8
         else if op eq bx & val = (val) and 7Fh
                  test bl,val
         else if op eq bx & val = (val) and 0FF00h
                       test bh,(val) shr 8
         else if op eq cx & val = (val) and 7Fh
                  test cl,val
         else if op eq cx & val = (val) and 0FF00h
                       test ch,(val) shr 8
         else if op eq dx & val = (val) and 7Fh
                  test dl,val
         else if op eq dx & val = (val) and 0FF00h
                       test dh,(val) shr 8
         else
                        test op,val
         end if
      else
                test op,val
 end if
}    
Post 18 Jul 2008, 04:08
View user's profile Send private message Visit poster's website Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
revolution, what does the extended lea macro do?
Post 18 Jul 2008, 18:31
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
comrade wrote:
revolution, what does the extended lea macro do?
It converts lea reg1,[reg2] to mov reg1,reg2 and lea reg,[constant] to mov reg,constant and lea reg,[reg] to nothing.
Post 18 Jul 2008, 18:47
View user's profile Send private message Visit poster's website Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 166
Location: Maryland, USA
rCX
revolution, so is the lea optimization for 16bit or 32bit?
Post 19 Jul 2008, 02:15
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
rCX wrote:
revolution, so is the lea optimization for 16bit or 32bit?
32 bit.
Post 19 Jul 2008, 02:21
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
might be 16 bit too for "r32 = esi+ebx" (with 66 prefix), no? too much alcohol right now to figure myself Razz
Post 19 Jul 2008, 04:32
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
vid wrote:
might be 16 bit too for "r32 = esi+ebx" (with 66 prefix), no? too much alcohol right now to figure myself Razz
Nope, any prefix will cause the lea macro to skip any optimisations.
Post 19 Jul 2008, 06:50
View user's profile Send private message Visit poster's website Reply with quote
KeSqueer



Joined: 07 Mar 2008
Posts: 2
KeSqueer
And my word:
Code:
macro   mov op1, op2
{
    if op1 eqtype eax & op2 eqtype 0 & op2 >= -0x80 & op2 < 0x80
        push op2
        pop op1
    else
        mov     op1, op2
    end if
}     
Post 21 Sep 2008, 23:59
View user's profile Send private message ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
just take care not to use those macros when you don't have stack (mode switching, etc..) Razz
Post 22 Sep 2008, 00:22
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number 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.