FreeBASIC マニュアルのトップに戻る

FreeBASIC Naked

目次→命令文→手続きNAKED←オリジナル・サイト

NAKED(裸) 左にメニュー・フレームが表示されていない場合は、ここをクリックして下さい

←リンク元に戻る プログラム開発関連に戻る

序詞/結語コードなしで関数を書きます。

構文:
{Sub | Function} identifier Naked [calling_convention] ( param_list ) [[ Byref ] As data_type]
asm_statements
End {Sub | Function}

パラメタ:
identifier - 手続きの名前。
calling_convention - 手続きの規則を呼びます - CDecl, Pascal, または StdCall が可能。
asm_statements - 手続きの本体のコード。
パラメーターを操作するコード、値を返すコードは、全て、プログラマの手作業で行います。 パラメータの操作や値の返し方は、calling convention によって、変わることに注意してください。
param_list - 手続きに渡されるパラメタ。
data_type - 関数の data type

記述:
Naked を使うと、プログラマーは、コンパイラーがプロローグ/エピローグコードを生成せずに手続きを記述できます。
これは、不要なオーバーヘッドがなく、小さくて高速な Asm 関数を作成する場合に役立ちます。
(したがって、このような Asm ブロックのレジスタ保存はありません)

例:
'' Naked cdecl function (for fbc 32-bit)
Function subtract_c Naked cdecl _   '' parameters pushed onto call stack in reverse order of declaration
    ( _
        ByVal a As Long, _
        ByVal b As Long _        '' parameter pushed onto stack in first
    ) As Long
   
    Asm
        mov eax, dword Ptr [esp+4]  '' eax = a
        Sub eax, dword Ptr [esp+8]  '' eax -= b
        ret                         '' return result in eax
    End Asm
   
End Function

Print subtract_c( 5, 1 ) '' 5 - 1

''---------------------------------------------------------------------------------------------------------------------

'' Naked stdcall function (for fbc 32-bit)
Function subtract_s Naked stdcall _ '' parameters pushed onto call stack in reverse order of declaration
                         _          '' called procedure responsible for removing parameters from stack
                         _          ''   (appending constant to RET instruction specifying number of bytes to release)
    ( _
        ByVal a As Long, _
        ByVal b As Long _        '' parameter pushed onto stack in first
    ) As Long
   
    Asm
        mov eax, dword Ptr [esp+4]  '' eax = a
        Sub eax, dword Ptr [esp+8]  '' eax -= b
        ret 8                       '' return result in eax and 8 bytes (2 integers) to release
    End Asm
   
End Function

Print subtract_s( 5, 1 ) '' 5 - 1

''---------------------------------------------------------------------------------------------------------------------

'' Naked pascal function (for fbc 32-bit)
Function subtract_p Naked pascal'' parameters pushed onto call stack in same order as declaration
                         _          '' called procedure responsible for removing parameters from stack
                         _          ''   (appending constant to RET instruction specifying number of bytes to release)
    ( _
        ByVal a As Long, _       '' parameter pushed onto stack in first
        ByVal b As Long _
    ) As Long
   
    Asm
        mov eax, dword Ptr [esp+8]  '' eax = a
        Sub eax, dword Ptr [esp+4]  '' eax -= b
        ret 8                       '' return result in eax and 8 bytes (2 longs) to release
    End Asm
   
End Function

Print subtract_p( 5, 1 ) '' 5 - 1
 

'' Naked cdecl function (for fbc 32-bit)
'' plus ecx register preserved in asm block by creating user stack
Function subtract_cp Naked cdecl _      '' parameters pushed onto call stack in reverse order of declaration
    ( _
        ByVal a As Long, _
        ByVal b As Long _            '' parameter pushed onto stack in first
    ) As Long
   
    Asm
        push ebp                        '' push ebp onto stack   => esp -= 4
        mov ebp, esp                    '' ebp = esp
                                        ''    => create user stack 4 bytes above call stack
        push ecx                        '' push ecx onto user stack   => esp -= 4
        mov eax, dword Ptr [(ebp+4)+4]  '' eax = a   (supplementary offset of +4 bytes only due to 'push ebp')
        mov ecx, dword Ptr [(ebp+8)+4]  '' ecx = b   (supplementary offset of +4 bytes only due to 'push ebp')
        Sub eax, ecx                    '' eax -= ecx
        pop ecx                         '' pop ecx from user stack   => esp += 4
        mov esp, ebp                    '' esp = ebp
        pop ebp                         '' pop ebp from stack   => esp += 4
                                        ''    => discard user stack
        ret                             '' return result in eax
    End Asm
   
End Function

Print subtract_cp( 5, 1 ) '' 5 - 1
 

プラットホーム差:
QBからの違い:
参照:
手続きに戻る
←リンク元に戻る プログラム開発関連に戻る
ページ歴史:2021-05-21 07:58:37
日本語翻訳:WATANABE Makoto、原文著作者:CountingPine

ホームページのトップに戻る

表示-非営利-継承