Memory & Stack

NCL provides 4096 addressable memory cells, numbered $000 through $FFF.

Memory is shared by the active program, program data, and the data stack.

Each memory cell stores a string of up to 511 Unicode characters. Memory cells do not have fixed types. An instruction determines how the contents of a location are interpreted.

For example:

S.MOVE $000 "42"
MOVE $001 $000
F.MOVE $002 $000

The same text stored in $000 is interpreted as a string, integer, or floating-point value depending on the instruction reading it.

Memory Capacity

The address space always contains 4096 possible locations, but those locations are not backed by a fixed amount of physical storage.

The active program and allocated memory cells share an aggregate capacity of 128 KiB.

A program may therefore use all 4096 addresses when the stored values are small, or exhaust available storage while using considerably fewer addresses when storing large strings.

Reading an unallocated location produces an empty string:

""

When interpreted as an integer, this becomes 0.

Writing an empty string with a string operation frees an ordinary memory location:

S.MOVE $000 ""

This has the same effect on ordinary memory as:

FREE $000

Writing an integer zero is different:

MOVE $000 0

This stores the string "0" and leaves the location allocated.

Locations

Instructions that read or write memory operate on locations.

A location may be:

  • an ordinary memory address;
  • a CPU register.

Ordinary memory addresses are written using a $ followed by a three-digit hexadecimal address:

$000
$001
$0FF
$100
$FFF

Addresses range from $000 through $FFF.

Registers use names rather than addresses, but participate in the same location-resolution system.

Registers

NCL provides several special-purpose CPU registers:

Register Description
pc Program counter.
sp Data stack pointer.
er Error register.

Unlike ordinary memory, registers have special CPU behavior.

pc — Program Counter

pc contains the current program line.

Writing to pc changes program execution.

MOVE pc 10

Execution continues at program line 10.

Writing to pc immediately ends the current program line. Any remaining coalesced instructions on that line are skipped.

Control-flow instructions such as JUMP, CALL, and conditional branches operate by modifying pc.

sp — Stack Pointer

sp contains the current data stack pointer.

The stack grows downward through ordinary memory.

At the beginning of a program:

sp = 4096

The first value pushed onto the stack is therefore stored at:

$FFF

sp is an ordinary writable location and may be relocated by a program.

MOVE sp 0x800

Subsequent stack operations use the new position.

The data stack is described in Stack.

er — Error Register

er contains the most recent nonfatal error reported by an instruction.

Errors are represented by symbolic strings:

VALUE_ERROR
STRING_OVERFLOW
STACK_HEAP_COLLISION
MATH_ERROR

Programs may read, write, or clear er like any other location:

S.MOVE $000 er
FREE er

Some errors are fatal and terminate the active program rather than returning control to it.

Indirect Addressing

A location may be resolved indirectly by enclosing another location in square brackets:

[$000]

The enclosed location is read, and its value is interpreted as another location.

For example:

MOVE $000 9
MOVE [$000] 42

$000 contains "9", which resolves to memory address $009.

The second instruction is therefore equivalent to:

MOVE $009 42

Address values may also use hexadecimal notation:

S.MOVE $000 "0x009"
MOVE [$000] 42

This also resolves to $009.

Indirect addressing uses the same location resolver as direct addressing, so an indirect reference may resolve to a register:

S.MOVE $100 "pc"
MOVE [$100] 42

This writes 42 to pc.

Indirect references may themselves be nested:

[[$000]]

Each level reads a location and resolves its contents as another location.

A maximum of 8 levels of indirection may be resolved. Exceeding this limit, or attempting to resolve an invalid location, produces a fatal ADDRESS_ERROR.

Memory Operations

Memory operations move values between locations.

Memory itself is untyped. The particular instruction determines how a value is interpreted while it is being moved.

Moving Values

The MOVE family reads a value using a particular interpretation and stores its textual representation in the destination.

MOVE <dst> <src>
S.MOVE <dst> <src>
F.MOVE <dst> <src>
V.MOVE <dst> <src>
V.ROT.MOVE <dst> <src>
Verb Interpretation
MOVE Integer
S.MOVE String
F.MOVE Floating-point
V.MOVE Vector
V.ROT.MOVE Rotation

Operands

Position Name Type Description
1 dst Location Destination location.
2 src Value Value to move.

Modified locations

Location Description
dst Interpreted value.

Examples

Move an integer:

MOVE $000 42

Move a string without changing its representation:

S.MOVE $000 "0xFF"

Move a floating-point value:

F.MOVE $000 f3.14159

Move a vector:

V.MOVE $000 <1, 2, 3>

Move a rotation:

V.ROT.MOVE $000 <0, 0, 0, 1>

The source interpretation affects the value that is stored.

For example:

S.MOVE $000 "0xFF"
MOVE $001 $000

produces:

$000 = "0xFF"
$001 = "255"

S.MOVE preserves the original string, while MOVE interprets it as an integer before storing it.

Swapping Values

The SWAP family exchanges the values of two locations using a particular interpretation.

SWAP <a> <b>
S.SWAP <a> <b>
F.SWAP <a> <b>
V.SWAP <a> <b>
V.ROT.SWAP <a> <b>
Verb Interpretation
SWAP Integer
S.SWAP String
F.SWAP Floating-point
V.SWAP Vector
V.ROT.SWAP Rotation

Operands

Position Name Type Description
1 a Location First location.
2 b Location Second location.

Modified locations

Location Description
a Previous interpreted value of b.
b Previous interpreted value of a.

Examples

Swap two integer values:

SWAP $000 $001

Swap two strings without changing their representation:

S.SWAP $100 $101

Swap two vectors:

V.SWAP $200 $201

Because the selected verb determines interpretation, swapping may normalize the stored representation of both values.

For example:

S.MOVE $000 "0xFF"
S.MOVE $001 "010"
SWAP $000 $001

produces integer representations of the exchanged values rather than preserving the original strings.

FREE — Free

FREE <dst>

Clears a location.

For ordinary memory, this removes the location's allocation.

Operands

Position Name Type Description
1 dst Location Location to clear.

Modified locations

Location Description
dst Cleared.

Examples

Free an ordinary memory location:

FREE $000

Free an indirectly addressed location:

FREE [$000]

Clear the error register:

FREE er

Registers may also be targeted by FREE. Their normal register-specific write behavior still applies.

For example:

FREE pc

writes the cleared value through the program-counter mechanism and therefore affects program execution.

Stack

NCL provides a data stack built directly from ordinary memory.

The stack grows downward from the address stored in sp.

At program start:

sp = 4096

The first push decrements sp to 4095 and writes to:

$FFF

The next push writes to:

$FFE

and so on.

There is no separately reserved stack region. Stack values and ordinary program data occupy the same memory address space.

A program may relocate the stack by writing to sp:

MOVE sp 0x800

The next push then writes to $7FF.

Stack and Ordinary Memory

Because the stack is ordinary memory, stack entries may be accessed directly.

sp points to the current top of the stack, so indirect addressing may be used to inspect or modify it:

MOVE $000 [sp]

This reads the top stack value without popping it.

Likewise:

MOVE [sp] 42

replaces the top stack value.

No address is inherently reserved for the stack. $FFF, for example, may be used as ordinary program memory when the stack is empty.

If a push reaches a location that is already allocated, the push still succeeds and overwrites that location. er is set to:

STACK_HEAP_COLLISION

This allows the program to detect that its stack has collided with other allocated memory without introducing a protected stack boundary.

Attempting a stack operation outside the valid memory address range produces a fatal:

STACK_ERROR

Stack Operations

Like memory movement, stack operations are interpretation-specific.

The stack itself remains untyped ordinary memory.

Pushing Values

The PUSH family places a value onto the data stack.

PUSH <value>
S.PUSH <value>
F.PUSH <value>
V.PUSH <value>
V.ROT.PUSH <value>
Verb Interpretation
PUSH Integer
S.PUSH String
F.PUSH Floating-point
V.PUSH Vector
V.ROT.PUSH Rotation

A push:

  1. Decrements sp.
  2. Verifies that the resulting address is valid.
  3. Reports STACK_HEAP_COLLISION if the location is already allocated.
  4. Writes the interpreted value to the resulting memory location.

Operands

Position Name Type Description
1 value Value Value to push.

Modified locations

Location Description
sp Decremented by one.
[sp] Pushed value.

Examples

Push an integer:

PUSH 42

Push a string:

S.PUSH "Hello!"

Push a floating-point value:

F.PUSH f3.14159

Push a vector:

V.PUSH <1, 2, 3>

Push a rotation:

V.ROT.PUSH <0, 0, 0, 1>

Popping Values

The POP family removes the top value from the data stack and writes it to a destination.

POP <dst>
S.POP <dst>
F.POP <dst>
V.POP <dst>
V.ROT.POP <dst>
Verb Interpretation
POP Integer
S.POP String
F.POP Floating-point
V.POP Vector
V.ROT.POP Rotation

A pop:

  1. Reads the value at [sp].
  2. Interprets it according to the selected POP instruction.
  3. Frees the stack location.
  4. Increments sp.
  5. Writes the interpreted value to the destination.

Operands

Position Name Type Description
1 dst Location Destination location.

Modified locations

Location Description
[sp] Freed.
sp Incremented by one.
dst Popped value.

Examples

Pop an integer:

POP $000

Pop a string:

S.POP $000

Pop a floating-point value:

F.POP $000

Pop a vector:

V.POP $000

Pop a rotation:

V.ROT.POP $000

Mixed Stack Interpretation

The data stack does not record the type used to push a value.

A value may therefore be pushed using one interpretation and popped using another:

S.PUSH "255"
POP $000

The stack contains the string "255". POP interprets that value as an integer and stores:

255

Likewise:

S.PUSH "3.14"
F.POP $000

pushes an ordinary string and later interprets it as a floating-point value.

This is the same behavior as ordinary memory: the stored value does not determine its type. The instruction reading the value determines how it is interpreted.

Call Stack

The data stack is separate from the call stack used by CALL and RET.

The call stack stores return addresses internally and does not occupy ordinary memory or use sp.

It has a maximum depth of 32 return addresses.

CALL :work
HALT

:work
D.TXT "Hello!"
RET

CALL records the return destination on the call stack before transferring execution.

RET removes the most recent return destination and resumes execution there.

The call stack cannot be accessed through ordinary memory operations.

Calling a subroutine when the call stack is full, or returning when it is empty, produces a fatal STACK_ERROR error.

See Branching for CALL, RET, and other control-flow operations.