Repository navigation
Naked
Paul Squires edited this page Oct 14, 2026
·
3 revisions
Home › Procedures › Naked
Write functions without prolog/epilog code
{Sub | Function} identifier Naked [calling_convention] ( param_list ) [[ ByRef ] As data_type]
asm_statements
End {Sub | Function}identifiercalling_conventionasm_statementsparam_listdata_type
Naked allows the programmer to write procedures without the compiler generating any prolog/epilog code.
This is useful when writing small, fast functions in Asm without any unnecessary overhead (so, no register preservation for such Asm blocks).
- The default calling convention depends on the target platform, thus it is best to specify the expected calling convention explicitly when using
Naked.
- New to FreeBASIC
'' Naked cdecl function (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 (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 (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 (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 - 1Category: Procedures
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing