"Sputnik" help  
Sputnik Help
Language Reference - Preprocessor - Registers

Registers

Registers are built-in macros explicitly designed for temporary variable storage, offering a convenient way to manage values within a Sputnik script without the need to create custom variables.

List of Registers:

PAX
PBX
PCX
PDX
PSI
PDI
PBP
PSP
P8
P9
P10
P11
P12
P13
P14
P15

Usage:
Registers can be employed for storing and manipulating values within the script. Here's a basic example:

#macro foo()
    .mov PAX, 10
    .while PAX
        say PAX;
        .dec PAX
    .endw
#endm

foo();

// PRINTS
// 10
// 9
// 8
// 7
// 6
// 5
// 4
// 3
// 2
// 1

Sputnik's preprocessor, reminiscent of assembly language, incorporates functions like .mov, .add, .sub, and other low-level operations. Additionally, it extends its functionality with higher-level constructs such as .if, .while, .for, .foreach, .eq, and more. This design philosophy aims to make Sputnik easy to use, providing a versatile and efficient tool for developers to work with.

Of course .eq works very similar to .mov as shown here:

#macro foo()
    .eq PAX, 10
    .while PAX
        say PAX;
        .dec PAX
    .endw
#endm

foo();

// PRINTS
// 10
// 9
// 8
// 7
// 6
// 5
// 4
// 3
// 2
// 1

Push and Pop Operations:

One of the most powerful features of registers is the ability to preserve their states between macro calls using push and pop operations. This allows for the creation of macros that can modify registers without affecting the calling environment.

#macro modifyRegisters()
    // Preserve the current state of all registers
    .pushap

    // Modify registers (e.g., increment PAX)
    .inc PAX;
    say "During modification: " . PAX;
    
    // Restore the registers to their previous state
    .popap
#endm

// Set an initial value for PAX
#eq PAX, 100
say "Before modification: " . PAX;

// Call the macro, PAX remains unchanged after the call
modifyRegisters();

// Display the value of PAX after modification
say "After modification: " . PAX;

// PRINTS
// Before modification: 100
// During modification: 101
// After modification: 100

// Explanation:
// The macro modifyRegisters is able to use PAX and modify it during its execution.
// However, upon exiting the macro, PAX is restored to its original value, ensuring
// that the modifications within the macro do not affect the calling environment.

Pushing a variable (macro) and popping to a register:

#macro foo()
    // Define a local variable $tmp and set its value to 10
    local $tmp
    .eq $tmp, 10

    // Push the value of $tmp onto the stack
    .push $tmp

    // Pop the value from the stack and assign it to PAX
    .pop PAX

    // Loop while PAX has a non-zero value
    .while PAX
        say PAX;  // Print the current value of PAX
        .dec PAX; // Decrement the value of PAX
    .endw
#endm

// Call the foo macro
foo();

// PRINTS
// 10
// 9
// 8
// 7
// 6
// 5
// 4
// 3
// 2
// 1

// Explanation:
// The foo macro demonstrates the ability to push the value of any variable
// (in this case, a local variable $tmp) onto the stack and then pop it
// to another variable (PAX in this case). This allows for dynamic manipulation
// of variables within the script, showcasing the flexibility of the Sputnik language.

This explanation highlights the key steps in the foo macro, showcasing the dynamic variable manipulation capabilities offered by the push and pop operations.

Caution: Do Not Undef Registers

Using undef on registers, such as PAX etc, is strongly discouraged. Registers serve as essential built-in macros for temporary variable storage, and attempting to undefine them may result in unintended consequences. This action can lead to data loss, undefined behavior, and potential interference with macros that rely on register states. It is recommended to avoid using undef on registers and instead employ safer approaches like preserving and restoring register states using .pushap and .popap operations during macro calls.

Advantages:

Provides a straightforward way to store and retrieve temporary values.
Simplifies code by avoiding the need for custom variables in certain scenarios.
Enables the creation of macros with state preservation through push and pop operations.

Registers in Sputnik: Akin to Assembly with High-Level Convenience:

In Sputnik, registers such as PAX, PBX, and PCX draw inspiration from assembly language, providing a familiar paradigm for variable storage. The preprocessor meta language employs functions like add and sub, reminiscent of assembly operations, making it accessible to those familiar with low-level programming. Despite their assembly-like nature, these registers offer the convenience of high-level operations, allowing users to perform complex tasks efficiently. This design choice strikes a balance, providing a toolset that is easy to understand, saves time, and enables both low-level and high-level operations within the Sputnik scripting language.

Further Reading:

Push, Pop
PushM, PopM
PushA, PopA
PushAp, PopAp


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org