Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Vector Operations

Instructions for reading, writing, and querying register-held vectors.

CodeMnemonicArgumentsStack EffectRegister EffectDescription
0x50VECPUSHreg: Register\([\ldots, v] \to [\ldots]\)mutatePop \(v\). Append \(v\) to reg’s VecInt.
0x51VECGETreg: Register\([\ldots, i] \to [\ldots, v]\)readPop \(i\). Bounds-check: \(0 \le i < \text{len}\). Push \(\text{vec}[i]\).
0x52VECSETreg: Register\([\ldots, i, v] \to [\ldots]\)mutatePop \(v\), then \(i\). Bounds-check: \(0 \le i < \text{len}\). Set \(\text{vec}[i] \leftarrow v\).
0x53VECLENreg: Register\([\ldots] \to [\ldots, n]\)readreg must hold VecInt or VecXqmx. Push \(\lvert\text{vec}\rvert\) as i64.
0x54SLACKindices: Register, coeffs: Register\([\ldots, \text{start}, \text{cap}] \to [\ldots]\)mutatePop cap and start. Append \(S = \lfloor\log_2(\text{cap})\rfloor + 1\) slack entries to both vecs.

Type Requirements

  • VECPUSH, VECGET, and VECSET require the register to hold VecInt.
  • VECLEN accepts both VecInt and VecXqmx.
  • SLACK requires both registers to hold VecInt. It appends (does not overwrite) so that item variables and slack variables coexist in one vec pair. If cap <= 0, no elements are appended.
  • All indexing operations perform bounds checking and error with IndexOutOfBounds on violation.

Example

VEC r0          ; r0 = empty VecInt
PUSH 10
VECPUSH r0      ; r0 = [10]
PUSH 20
VECPUSH r0      ; r0 = [10, 20]
PUSH 0
VECGET r0       ; stack = [..., 10]

SLACK Details

SLACK indices coeffs pops capacity (top) then start_index from the stack. It computes \(S = \lfloor\log_2(\text{capacity})\rfloor + 1\) and appends:

  • To indices: \([\text{start}, \text{start}+1, \ldots, \text{start}+S-1]\)
  • To coeffs: \([1, 2, 4, \ldots, 2^{S-1}]\)

This generates binary-weighted slack variables for inequality-to-equality conversion. Combined with EQUALITY, it enforces knapsack-style capacity constraints without manual coefficient loops.

VEC r5            ; indices
VEC r6            ; coeffs
; ... populate with item indices and weights ...
PUSH 3            ; start_index (first slack var index)
PUSH 10           ; capacity
SLACK r5 r6       ; appends 4 slack entries (floor(log2(10))+1 = 4)