BCBMASMĻϱ

by Fpc rls 2006.06

˵̼Ի˵˵ֻ˵ãǰдСоBCBMASMĻϱ̣ûʱһˣһµʱıʼǣש




Ϊʲô

ڻϱ
masmmacroǿ󣬶ϸڵľȷƣ
C/C++ģ黯GUI׿
ΪʲôTASmֿ֧ⲻȫmacro̫ãڲ
ΪʲôBCBVCȲVCVCҲmasmļ⡣BCBҪOMFʽasmģ飬apiʹкܴ鷳Ҫǽ




һڸ߼ asm{ }궨巽ʽӦСΧڣȱֻasmCı֧masmĸ߼Ӧ
CʹmasmobjlibԣȱǺѱ֤ԣͨ
ʹdllܷ㣬Ҫļ


巽
1__cdecl ʽ
masmڣ
.386
.model flat
option casemap:none
...
PUBLIC	_Add3

.code
...
_Add3:
...
ret
end

ע⿪ʼ.model stdcallأԱŵǰҪӸ_
Ȼ루Ӧκϵͳlibκapi
CУ
    #ifdef __cplusplus
    extern "C" {
    #endif

    int __cdecl Add3(int, int);

    #ifdef __cplusplus
    } /* extern "C" */
    #endif
ע___cdeclӦû_
˷ʽȱmasmвӦapimasmlibstdcallʽBCBlibɲã




2__stdcall ʽ
masmڣ
.386
.model flat, stdcall
option casemap:none
...
PUBLIC	Add3

.code
...
Add3:
...
ret
end

ע⿪ʼ.model stdcallأԱŵǰ治Ҫ_Ӧapi
Ȼ
CУ
    #ifdef __cplusplus
    extern "C" {
    #endif

    int __stdcall _Add3(int, int);

    #ifdef __cplusplus
    } /* extern "C" */
    #endif
עҪ___stdcallӦò_
apiʱ˷ʽʹãmasmʹapiBCBʱʾmasmlibOMFʽ
BCBlib..һmasmɵobjapiǰ_stdcallʽobjɾ

apisyscallʽ򲻲_߻޸stack(in masm)
apicʽ_ҵ߻޸stack(in masm)
apistdcallʽ(ӦĬϷʽ)_߲޸stack(in masm)ɵapiһ8֮βͣBCBlibǷǳɾģ_β͵BCBϡֹobjapiеĿǰѽ





in masm:

.386
.model flat, stdcall				;ʹflat, stdcallģʽ
option casemap:none				;Сд
includelib kernel32.lib				;ҪʹBCBlib, omfʽҪmasmlib
GetModuleHandleA	proto :dword		;Ҫʹõapiǰmasmincļ
PUBLIC	c Add3	; :dword, :dword		;Ḷ́עʹCģʽǰ_

.code
Add3	proc	near c aa:dword, bb:dword	;proc岿֣push ebp...õ˱
;Add3:
	;jmp	@f
	invoke	GetModuleHandleA, 0		;ʹapi
@@:
	mov	eax, aa				;ʹñ
	add	eax, bb				;
	ret					;أɵstack
Add3	endp					;̽
end						;

עBCBҪOMFʽobjҪbldomf.batasmģ

ɵobjapi nameǰжݣΪʹ֮BCBlibݣֶ취objfixerһ£֮ͿBCBˡ

in BCB
in *.h file:
#ifdef __cplusplus
extern "C" {					;ⲿC
#endif

int __cdecl Add3(int, int);			;C declare ʽmasmбһ

#ifdef __cplusplus
} /* extern "C" */
#endif

in *.cpp file:
...
    ShowMessage(Add3(33,42));
...

Ϳͨ




ִѵ㣺
̵໥ûǺ鷳ģҪСĵĴⲻͬcallģʽĹʽܻᵼ¶ջƽ⣬Ҫϸdebug


дñȽң⣬ԼҲӭQQ6565XXX





ڲͬģʽժmasmbcb
Calling Conventions

                              C    SYSCALL STDCALL  BASIC  FORTRAN  PASCAL
                          +-------+-------+-------+-------+-------+-------+
  Leading Underscore      |   X   |       |   X   |       |       |       |
                          |-------+-------+-------+-------+-------+-------|
  Capitalize All          |       |       |       |   X   |   X   |   X   |
                          |-------+-------+-------+-------+-------+-------|

  Arguments Left to Right |       |       |       |   X   |   X   |   X   |
                          |-------+-------+-------+-------+-------+-------|
  Arguments Right to Left |   X   |   X   |   X   |       |       |       |
                          |-------+-------+-------+-------+-------+-------|
  Caller Stack Cleanup    |   X   |       |   *   |       |       |       |
                          |-------+-------+-------+-------+-------+-------|
  BP Saved                |       |       |       |   X   |   X   |   X   |

                          |-------+-------+-------+-------+-------+-------|
  :VARARG Allowed         |   X   |   X   |   X   |       |       |       |
                          +-------+-------+-------+-------+-------+-------+
 
  * The STDCALL language type uses caller stack cleanup if the :VARARG
    parameter is used. Otherwise, the called routine must clean up the
    stack.
 
  The language type (langtype) determines the naming and calling conventions
  used by the assembler. This allows you to share code and data with

  modules written with other languages. Set the language type with the
  .MODEL or OPTION LANGTYPE: directives or with the /G<x> command-line
  option. Several directives allow you to specify a langtype
  parameter to temporarily override the language type.
 
  You can use the /H command-line option to limit the length of names
  sent to the object file. Use this option to work with languages that
  limit the maximum length of identifiers.
                                    -o-


in BCB

Calling Convention options tell the compiler which calling sequences to generate for function calls. The C, Pascal, and Register calling conventions differ in the way each handles stack cleanup, order of parameters, case, and prefix of global identifiers.

You can use the __cdecl, __pascal, __fastcall, or __stdcall keywords to override the default calling convention on specific functions.

In C++Builder code, you will normally use the default C calling convention.

MSVC __fastcall

(Command-line switch: -pm)

This option tells the compiler to substitute the __msfastcall calling convention for any function without an explicitly declared calling convention.

C

(Command-line switch: -pc, -p-)

This option tells the compiler to generate a C calling sequence for function calls (generate underbars, case sensitive, push parameters right to left). This is the same as declaring all subroutines and functions with the __cdecl keyword. Functions declared using the C calling convention can take a variable parameter list (the number of parameters does not need to be fixed).

You can use the __pascal, __fastcall, or __stdcall keywords to specifically declare a function or subroutine using another calling convention.

Pascal

(Command-line switch: -p)

This option tells the compiler to generate a Pascal calling sequence for function calls (do not generate underbars, all uppercase, calling function cleans stack, pushes parameters left to right). This is the same as declaring all subroutines and functions with the 
__pascal keyword. The resulting function calls are usually smaller and faster than those made with the C (-pc) calling convention. Functions must pass the correct number and type of arguments.

You can use the __cdecl, __fastcall, or __stdcall keywords to specifically declare a function or subroutine using another calling convention.

Register

(Command-line switch: -pr)

This option forces the compiler to generate all subroutines and all functions using the Register parameter-passing convention, which is equivalent to declaring all subroutine and functions with the __fastcall keyword. With this option enabled, functions or routines expect parameters to be passed in registers.

You can use the __pascal, __cdecl, or __stdcall keywords to specifically declare a function or subroutine using another calling convention.

Standard Call

(Command-line switch: -ps)

This option tells the compiler to generate a Stdcall calling sequence for function calls (does not generate underscores, preserve case, called function pops the stack, and pushes parameters right to left). This is the same as declaring all subroutines and functions with the __stdcall keyword. Functions must pass the correct number and type of arguments.

You can use the __cdecl, __pascal, __fastcall keywords to specifically declare a function or subroutine using another calling convention.

Default = C (-pc)

