Romasm Linker System

Linking user code with standard library functions

Overview

The Romasm Linker (linker/romasm-linker.js) combines user-written Romasm code with standard library functions, allowing you to call functions like sin, cos, sqrt without copying their code.

How It Works

Step 1: Load Standard Library

The linker loads and assembles stdlib files:

  • stdlib/trig.romasm - Trigonometric functions
  • stdlib/math.romasm - Basic math functions
  • stdlib/calculus.romasm - Calculus functions
  • stdlib/advanced-math.romasm - Advanced math
  • stdlib/sine-taylor.romasm - High-precision sine

It extracts function labels (like sin:, cos:) and records their addresses.

Step 2: Assemble User Code

Your Romasm code is assembled normally. When you write CALL sin, the assembler creates an unresolved label reference:

{
    opcode: 'CA',
    operands: [{
        type: 'label',
        value: 0,           // Placeholder
        labelName: 'sin'    // Function name for linker
    }]
}

Step 3: Link Functions

The linker:

  1. Combines stdlib instructions with user instructions
  2. Resolves CALL sin to the actual stdlib function address
  3. Creates a function map for reference
; Before linking:
CALL sin  ; Unresolved, value = 0

; After linking:
CALL sin  ; Resolved to stdlib address, e.g., value = 42

Using the Linker

In the Calculator

The linker is automatically used when you call stdlib functions. Just write:

LOAD R0, 3000
CALL sin
PRINT R0

The calculator engine handles linking automatically.

In the IDE

You may need to manually enable stdlib linking, or include stdlib code directly in your program.

Function Resolution

When the linker encounters CALL function_name:

  1. It checks if function_name exists in the stdlib
  2. If found, replaces the placeholder address with the real address
  3. If not found, logs a warning (but doesn't fail - in case it's a user-defined function)

Available Functions

Functions available through the linker include:

  • Math: factorial, power, sqrt
  • Trig: sin, cos
  • Calculus: derivative_x_squared, integral_x_squared, etc.
  • Advanced: exp, ln

See the Standard Library documentation for complete lists.

Related Documentation