FreshLib provides a complete object-oriented programming system for assembly language. This tutorial covers how to define objects with fields and methods, use inheritance and polymorphism, and leverage properties for encapsulated data access.
Topics Covered
Object Definition - object, endobj, fields
Methods - Declaring and implementing object methods
Parameters (Properties) - param with getters and setters
Object Lifecycle - create, destroy
Method Execution - exec, pexec
Properties Access - get, set
Inheritance - Parent-child relationships
Polymorphism - Same interface, different behavior
Type Checking - istype macro
Object Definition
object / endobj
The object macro defines an object type with optional inheritance:
object TAnimal
; Fields (private data storage)
.name dd ? ; Pointer to name string
.age dd ? ; Age in years
.weight dd ? ; Weight in kg
; Methods
method .Create, .pName, .nAge
method .Destroy
method .Speak
method .GetInfo
endobj
Key Points:
Object names should start with T prefix (convention)
Fields are like structure members with dot prefix
Methods are declared with method macro
The first dword is reserved for the vtable pointer
Inheritance
To inherit from a parent object, specify the parent as second argument:
object TDog, TAnimal ; TDog inherits from TAnimal
; Additional fields
.breed dd ?
; Override parent method (same name = override)
method .Speak
; New method specific to TDog
method .Fetch
endobj
Child objects inherit all fields and methods from parents. Redefining a method with the same name creates an override.
Looks up method in vtable for polymorphic dispatch
pexec (Parent Execute)
Call the parent's version of an overridden method:
; Inside TDog.Speak, call TAnimal.Speak first
pexec [.self], TAnimal:Speak
; Then add dog-specific behavior
Properties Access
get
The get macro retrieves a property value:
; Syntax: get TARGET, OBJECT, TYPE:PARAMETER
get eax, [pRect], TRectangle:Width ; Direct field access
get ecx, [pRect], TRectangle:Area ; Calls GetArea method
get [localVar], esi, TAnimal:Age ; Store to memory
set
The set macro assigns a property value:
; Syntax: set OBJECT, TYPE:PARAMETER, VALUE
set [pRect], TRectangle:Width, 50 ; Direct field assignment
set [pRect], TRectangle:Height, eax ; Use register value
set esi, TAnimal:Age, 10 ; When object is in register
get and set generate optimal code. For direct field access, they compile to a single mov instruction!
Type Checking
istype
Check if an object is of a specific type (or inherits from it):
; Syntax: istype OBJECT, TYPE
; Returns: ZF=1 if type matches, ZF=0 if not
istype [pDog], TDog
je .is_a_dog ; ZF=1: It IS a TDog
istype [pDog], TAnimal
je .is_an_animal ; ZF=1: Also a TAnimal (inheritance!)
istype [pDog], TCat
je .is_a_cat ; ZF=0: NOT a TCat
jmp .not_a_cat
How It Works:
Walks the vtable parent chain
Returns ZF=1 if target type is found anywhere in the chain
Polymorphism
Polymorphism means the same method call executes different code based on object type:
; Define objects with same method name
object TAnimal
method .Speak
endobj
object TDog, TAnimal
method .Speak ; Override
endobj
object TCat, TAnimal
method .Speak ; Override
endobj
; Different implementations
method TAnimal.Speak
begin
stdcall FileWriteString, [STDOUT], txt "*Generic sound*", 13, 10
return
endp
method TDog.Speak
begin
stdcall FileWriteString, [STDOUT], txt "Woof!", 13, 10
return
endp
method TCat.Speak
begin
stdcall FileWriteString, [STDOUT], txt "Meow!", 13, 10
return
endp
; Polymorphic usage - same code, different behavior
create [pDog], TDog
create [pCat], TCat
exec [pDog], TAnimal:Speak ; Prints "Woof!"
exec [pCat], TAnimal:Speak ; Prints "Meow!"
Using TAnimal:Speak works for any object inheriting from TAnimal. The actual method called depends on the runtime type!
Demo Programs
Demo 26: Object Basics
File: demo26_object_basics.asm
Demonstrates:
Object definition with fields and methods
create and destroy macros
exec for method calls
Basic method implementation
Demo 27: Inheritance & Polymorphism
File: demo27_inheritance.asm
Demonstrates:
Parent-child object relationships
Method overriding
Polymorphic method dispatch
istype for runtime type checking
Demo 28: Parameters (Properties)
File: demo28_parameters.asm
Demonstrates:
param for property definitions
get and set macros
Direct field vs method-based accessors
Computed (read-only) properties
AsmBB Reference Patterns
While AsmBB is primarily a server application (not GUI), it uses similar patterns with structures: