flat assembler
Message board for the users of flat assembler.

Index > Main > Optimization of "format MS COFF" connected to C _

Author
Thread Post new topic Reply to topic
mkm79



Joined: 27 Feb 2004
Posts: 8
mkm79 11 Nov 2010, 07:51
Hi all!

I have a little trouble with optimizations.
ASM with FASM
C/C++ with MS VisualStudio
Target machine is 32-bit.
All code is conceptual.

I have the function in C
Code:
int func(int a, int b, int c)
{
    return a+b+c;
}
    


It's used like
Code:
int main (void)
{
    int a1=1,a2=2,a3=3,r;
    r = func(a1,a2,a3);
    return r;
}
    

I've optimized the function "func" with FASM.

Code:
format MS COFF
include '%fasm%\win32a.inc'
public func as '_func@12'
section '.text' code readable executable
proc func a1,a2,a3
    xor eax,eax
    add eax,[a1]
    add eax,[a2]
    add eax,[a3]
    ret
endp
    


After compiled to .OBJ file (MS COFF).
Prototype is

Code:
int func(int a1,int a2, int a3);
    


Until that moment all is working.

Now I tried to optimize the calling strategy.
Code:
int __fastcall func(int a1,int a2, int a3);
    


FASM code
Code:
format MS COFF
include '%fasm%\win32a.inc'
public func as '_func@12'
section '.text' code readable executable
proc func
    xor eax, eax
    add eax, ebx
    add eax, ecx
    add eax, edx
    ret
endp
    


And i don't know the way to say to MS VC compiler through FASM's MS COFF about "which parameters corresponds to which registers".
I need something like:
Code:
format MS COFF
...
public func as '_func@0 {eax,ebx,ecx}'
...
    


Any information?

_________________
Best regards,
Kostya
Post 11 Nov 2010, 07:51
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 11 Nov 2010, 07:57
The fastcall standard is already defined about the order of register assignment. You don't need to "tell" MSVC, you just use them in the order that the fastcall standard defines. MSVC assigns them according to fastcall, and you consume them according to fastcall.
Post 11 Nov 2010, 07:57
View user's profile Send private message Visit poster's website Reply with quote
mkm79



Joined: 27 Feb 2004
Posts: 8
mkm79 11 Nov 2010, 08:06
revolution wrote:
The fastcall standard is already defined about the order of register assignment. You don't need to "tell" MSVC, you just use them in the order that the fastcall standard defines. MSVC assigns them according to fastcall, and you consume them according to fastcall.


MSDN says about __fastcall:
Quote:

Argument-passing order
The first two DWORD or smaller arguments are passed in ECX and EDX registers; all other arguments are passed right to left.


So, it clear that only 2 registers will be used.
But in 32-bit mode we have at least eax,ebx,ecx,edx,esi,edi (and more, depending on target CPU, MMX & SSE registers).

maybe existing some other calling convention "__supercall"?

_________________
Best regards,
Kostya
Post 11 Nov 2010, 08:06
View user's profile Send private message Reply with quote
mkm79



Joined: 27 Feb 2004
Posts: 8
mkm79 11 Nov 2010, 08:16
Intresting.
Borland has different __fastcall
Quote:

The first three parameters are passed (from left to right) in EAX, EDX, and ECX, if they fit in the register. The registers are not used if the parameter is a floating-point or struct type.


Thus, i suppose that in MSVC i should use __asm keyword to call my fast function.

Is there are any info regarding "Best Practices for mixing FASM and MSVC" ?

_________________
Best regards,
Kostya
Post 11 Nov 2010, 08:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 11 Nov 2010, 08:40
Yes, as you discovered compilers differ in what that think is fastcall.

Also keep in mind that you need to comply with the saving and restoring of EBX, EDI, ESI and EBP if you use them. You can't simply use registers as you feel like it, else your program will likely end up crashing.
Post 11 Nov 2010, 08:40
View user's profile Send private message Visit poster's website Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 11 Nov 2010, 12:28
Code:
    mov eax,[a1]
    add eax,[a2]
    add eax,[a3]    


Ahh, that feels better.

_________________
This is a block of text that can be added to posts you make.
Post 11 Nov 2010, 12:28
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4061
Location: vpcmpistri
bitRAKE 11 Nov 2010, 15:05
All parameters passed in ESP - pointer to structure that is. Very Happy
Post 11 Nov 2010, 15:05
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.