flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How about fast call convention ? I have macros...

Author
Thread Post new topic Reply to topic
Blid



Joined: 19 Apr 2007
Posts: 31
Location: Russia, Novorossysk
Blid 19 Apr 2007, 10:12
I wrote this for fast call (all arguments are in reg32). I was using it many
times. Please look ot this, find bugs, and improve.

macro fcall procname,arg_1,arg_2,arg_3,arg_4,arg_5,arg_6,arg_7,arg_8
{
;arg_1 = eax, arg_2 = ecx,arg_3 = edx,arg_4 = ebx,arg_5 = esi,
;arg_6 = edi, arg_7 = ebp,arg_8 = esp
; version 1.01
if ~ arg_1 eq
if arg_1 eq eax
else
if arg_1 eq 0
xor eax,eax
else
mov eax,arg_1
end if
end if
end if
if ~ arg_2 eq
if arg_2 eq ecx
else
if arg_2 eq 0
xor ecx,ecx
else
mov ecx,arg_2
end if
end if
end if
if ~ arg_3 eq
if arg_3 eq edx
else
if arg_3 eq 0
xor edx,edx
else
mov edx,arg_3
end if
end if
end if
if ~ arg_4 eq
if arg_4 eq ebx
else
if arg_4 eq 0
xor ebx,ebx
else
mov ebx,arg_4
end if
end if
end if
if ~ arg_5 eq
if arg_5 eq esi
else
if arg_5 eq 0
xor esi,esi
else
mov esi,arg_5
end if
end if
end if
if ~ agr_6 eq
if arg_6 eq edi
else
if arg_6 eq 0
xor edi,edi
else
mov edi,arg_6
end if
end if
end if


if ~ agr_7 eq
if arg_7 eq ebp
else
if arg_7 eq 0
xor ebp,ebp
else
mov ebp,arg_7
end if
end if
end if

if ~ agr_8 eq
if arg_8 eq esp
else
if arg_8 eq 0
xor esp,esp
else
mov esp,arg_8
end if
end if
end if
call procname }

example:
fcall MainF,eax,ecx,esi,0,6,ebx
will be:

004021E8 89D8 MOV EAX,EBX
; arg eq. ecx , there is no mov ecx,ecx!
004021EA 89F2 MOV EDX,ESI
: arg eq. 0, no mov ebx,0! xor ebx,ebx
004021EC 31DB XOR EBX,EBX
004021EE BE 04104000 MOV ESI,DIALOG.00401004
004021F3 89DF MOV EDI,EBX
004021F5 E8 7CFFFFFF CALL DIALOG.00402176

;thanks to all and sorry for my ugly english Sad.
Post 19 Apr 2007, 10:12
View user's profile Send private message Reply with quote
Blid



Joined: 19 Apr 2007
Posts: 31
Location: Russia, Novorossysk
Blid 19 Apr 2007, 10:14
sorry ! error:
fcall MainF,ebx,ecx,esi,0,WIN32FINDDATA,ebx instead
fcall MainF,eax,ecx,esi,0,6,ebx

; Blid
Post 19 Apr 2007, 10:14
View user's profile Send private message Reply with quote
hidden



Joined: 14 Feb 2007
Posts: 49
hidden 20 Apr 2007, 23:02
Quote:
fcall MainF,ebx,ecx,esi,0,6,ebx

result:
Quote:
mov eax, ebx
; mov ecx, ecx ; skipped
mov edx, esi
xor ebx, ebx ; We shouldn't change it, before we get this value
mov esi, 6
mov edi, ebx ; Already xor'ed
It would work this way:
Quote:
mov eax, ebx
; mov ecx, ecx ; skipped
mov edx, esi
mov edi, ebx
xor ebx, ebx
mov esi, 6
But there would be different situation:
Quote:
fcall MainF,ebx,ecx,esi,edi,6,ebx
So...

_________________
Image Lang: (eng|рус)
Post 20 Apr 2007, 23:02
View user's profile Send private message Reply with quote
Blid



Joined: 19 Apr 2007
Posts: 31
Location: Russia, Novorossysk
Blid 21 Apr 2007, 20:50
Thanks for you 1'st post!
Yes, it will be so. (I know it Smile.
It was a example (may be a litle ugly).
Position of argument and register are know in fastcall (see macros).
And then you send arguments to MainF, you know they positin in reg.
Ofcourse 'invoke' (stdcall) more simple. You can push anything, (+ any oder)as you wish. But you code will be giant (very big) and slow.
What is why i use my fcall macro.

; sorry, my eng. is poor
Post 21 Apr 2007, 20:50
View user's profile Send private message Reply with quote
chris



Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
chris 27 Apr 2007, 03:10
here is my solution:
Code:
macro fcall proc*,arg1,arg2,[arg]
{
  common
    if ~arg1 eq
      if ~arg1 eq ecx
        mov ecx,arg1
      end if
      if ~arg2 eq
        if ~arg2 eq edx
          mov edx,arg2
        end if
      end if
    end if
  reverse
    pushd arg
  common
    call proc
}

macro finvoke proc*,arg1,arg2,[arg]
{
  common
    if ~arg1 eq
      if ~arg1 eq ecx
        mov ecx,arg1
      end if
      if ~arg2 eq
        if ~arg2 eq edx
          mov edx,arg2
        end if
      end if
    end if
  reverse
    pushd arg
  common
    call [proc]
}                        
    
Post 27 Apr 2007, 03:10
View user's profile Send private message Reply with quote
Blid



Joined: 19 Apr 2007
Posts: 31
Location: Russia, Novorossysk
Blid 08 May 2007, 17:22
it is a good idea.
Thanks for reply.
Your macro very small, compact.
But you are not using registers as it possible .
You send two arg to function in registers, others will be store in memory.
The instruction wich acsess to mem, using ebp as base has size
3 bytes (against mov reg32,reg32-2bytes) and slower by 5 times.
That is my purpose.
How about defining proc , using reg as var?
Can you help my, i think it is good idea :--).
Post 08 May 2007, 17:22
View user's profile Send private message Reply with quote
Blid



Joined: 19 Apr 2007
Posts: 31
Location: Russia, Novorossysk
Blid 08 May 2007, 17:28
And you should use eax for arg! This more compact and faster.
But official documentation (i have it) about call conventions
against eax. I do not anderstand.
Post 08 May 2007, 17:28
View user's profile Send private message Reply with quote
Blid



Joined: 19 Apr 2007
Posts: 31
Location: Russia, Novorossysk
Blid 01 Jul 2007, 10:23
Code:

;look at this macro - it more complex and powerfull
macro qcall name*,arg1,arg2,arg3,arg4
{
  LoadArg eax,arg1
  LoadArg ecx,arg2
  LoadArg edx,arg3
  LoadArg ebx,arg4
  call name
}
macro LoadArg reg32*,arg
{
; equ's   '
  1      equ TRUE
  FALSE equ NULL
  0     equ NULL
  if ~ arg eq         ;process if arg
       if arg eq reg32
       else
    if arg in <0,1,-1,NULL,TRUE,FALSE>
     xor reg32,reg32
     if arg eq 1
              inc reg32
      end if
      if arg eq -1
             dec reg32
      end if
     else
         OptimC reg32,arg
   end if
       end if
 end if
;restore's   '
  restore 1
  restore 0
  restore FALSE
}
macro OptimC reg32,arg
{
 if  arg eqtype [0]
  if (arg eqtype 1)
     if (arg>-129)&(arg<127)
       push arg
       pop reg32
     else
       mov reg32,arg
     end if
  else
     mov reg32,arg
  end if
 else
  if (arg eqtype 1)
     if (arg>-129)&(arg<127)
    push arg
    pop reg32
     else
      mov reg32,arg
     end if
  else
      if arg eqtype EAX
    mov reg32,arg
      else
         lea reg32,[arg]
      end if
  end if
 end if
}


    
Post 01 Jul 2007, 10:23
View user's profile Send private message Reply with quote
Blid



Joined: 19 Apr 2007
Posts: 31
Location: Russia, Novorossysk
Blid 01 Jul 2007, 10:26
Code:
start:
                qcall xxx,start,129,[esi+eax],[esp]

                qcall xxx,[start],129,<esi+eax>,esp

                qcall xxx,eax,5,<esi+eax>,esp

                qcall xxx,2,esp,<esi+eax>,TRUE

                qcall xxx,FALSE,NULL,<esi+eax>,TRUE     
    

;--------------------------disasm-----------------------------------------
Code:
.code:00401008 start:                                  ; DATA XREF: .code:starto
.code:00401008                                         ; .code:0040101Dr
.code:00401008                 mov     eax, offset start
.code:0040100D                 mov     ecx, 81h
.code:00401012                 mov     edx, [esi+eax]
.code:00401015                 mov     ebx, [esp]
.code:00401018                 call    sub_401000
.code:0040101D                 mov     eax, dword ptr start
.code:00401022                 mov     ecx, 81h
.code:00401027                 lea     edx, [esi+eax]
.code:0040102A                 mov     ebx, esp
.code:0040102C                 call    sub_401000
.code:00401031                 push    5
.code:00401033                 pop     ecx
.code:00401034                 lea     edx, [esi+eax]
.code:00401037                 mov     ebx, esp
.code:00401039                 call    sub_401000
.code:0040103E                 push    2
.code:00401040                 pop     eax
.code:00401041                 mov     ecx, esp
.code:00401043                 lea     edx, [esi+eax]
.code:00401046                 xor     ebx, ebx
.code:00401048                 inc     ebx
.code:00401049                 call    sub_401000
.code:0040104E                 xor     eax, eax
.code:00401050                 xor     ecx, ecx
.code:00401052                 lea     edx, [esi+eax]
.code:00401055                 xor     ebx, ebx
.code:00401057                 inc     ebx
.code:00401058                 call    sub_401000
    
Post 01 Jul 2007, 10:26
View user's profile Send private message Reply with quote
Blid



Joined: 19 Apr 2007
Posts: 31
Location: Russia, Novorossysk
Blid 01 Jul 2007, 10:28
;all are optimized by size Smile
Post 01 Jul 2007, 10:28
View user's profile Send private message Reply with quote
Blid



Joined: 19 Apr 2007
Posts: 31
Location: Russia, Novorossysk
Blid 05 Aug 2007, 20:10
this next step:

Code:
Arguments = 1
macro Arguments name,reg1,reg2,reg3,reg4
{

 if ~reg1 eq
    name#.argum1 equ reg1
 end if
 if ~reg2 eq
    name#.argum2 equ reg2
 end if
 if ~reg3 eq
    name#.argum3 equ reg3
 end if
 if ~reg4 eq
    name#.argum4 equ reg4
 end if
}
    


;example
Code:

memset equ memfil
proc memfil stdcall uses edi
;arg : (buff,char,size)
if  (defined Arguments)&(Arguments=1)

 Arguments memfil, edx,\;edx pointer to memory
                  al,\ ;eax (al) char to fill
                ecx  ;ecx size to fill
end if

;return: eax=pointer to memory
           push edx
            mov edi,edx

     mov ah,al   ;eax = multipl copy of al
       ror eax,8
           mov ah,al
           ror eax,8
           mov ah,al

       push ecx
            shr ecx,2
           rep stosd
           pop ecx
     and ecx,3
           rep stosb
           pop eax     ;return pointer to buff
         ret
endp 
    

;code:

qcall memfil,3,dwBuffer,5*4
;will be:

Code:
         mov edx,dwBuffer
         mov al,3
         mov ecx,5*4
         call memfil
    
Post 05 Aug 2007, 20:10
View user's profile Send private message Reply with quote
Blid



Joined: 19 Apr 2007
Posts: 31
Location: Russia, Novorossysk
Blid 05 Aug 2007, 20:12
this next step:

Code:
Arguments = 1
macro Arguments name,reg1,reg2,reg3,reg4
{

 if ~reg1 eq
    name#.argum1 equ reg1
 end if
 if ~reg2 eq
    name#.argum2 equ reg2
 end if
 if ~reg3 eq
    name#.argum3 equ reg3
 end if
 if ~reg4 eq
    name#.argum4 equ reg4
 end if
}
    


;example
Code:

memset equ memfil
proc memfil stdcall uses edi
;arg : (buff,char,size)
if  (defined Arguments)&(Arguments=1)

 Arguments memfil, edx,\;edx pointer to memory
                  al,\ ;eax (al) char to fill
                ecx  ;ecx size to fill
end if

;return: eax=pointer to memory
           push edx
            mov edi,edx

     mov ah,al   ;eax = multipl copy of al
       ror eax,8
           mov ah,al
           ror eax,8
           mov ah,al

       push ecx
            shr ecx,2
           rep stosd
           pop ecx
     and ecx,3
           rep stosb
           pop eax     ;return pointer to buff
         ret
endp 
    

;code:

qcall memfil,3,dwBuffer,5*4
;will be:

Code:
         mov edx,dwBuffer
         mov al,3
         mov ecx,5*4
         call memfil
    
Post 05 Aug 2007, 20:12
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.