Skip to content

NAKED


编写不带序言/尾声代码的函数。

语法

vb
{Sub | Function} identifier Naked [calling_convention] ( param_list ) [[ Byref ] As data_type]

asm_statements

End {Sub | Function}

参数

identifier - 过程的名称。

calling_convention - 过程的调用约定,可以是 CDeclPascalStdCall

asm_statements - 过程体中的代码。处理参数和返回值的代码必须全部手动完成。请注意,执行这些操作的方法可能因调用约定而异。

param_list - 传递给过程的参数。

data_type - 函数的数据类型

描述

Naked 允许程序员编写不让编译器生成任何序言/尾声代码的过程。

这在用 Asm 编写小型、快速函数而不需要任何不必要开销时非常有用(因此,对于此类 Asm 块不需要保存寄存器)。

示例

start GeSHi

vb
'' 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

end GeSHi

start GeSHi

vb
'' 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

end GeSHi

平台差异

  • 默认调用约定取决于目标平台,因此在使用 Naked 时最好显式指定预期的调用约定。

与 QB 的差异

  • FreeBASIC 新增功能

参见

  • Asm
  • Calling Conventions
  • Function
  • Sub
  • CDecl
  • Pascal
  • StdCall

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系