Figure 7 — LAMBDA Clamp: Macro-Like Code Reuse
Clamp function exists once in memory, invoked three times (R, G, B channels) via LAMBDA. Zero stack access per invocation — machine-status fast path throughout.
CODE MEMORY LAYOUT
Clamp function exists exactly once — three invocations share the same code
clamp: (exists once at CR2+offset)
CMP DR1, #0
MOVLT DR1, #0
CMP DR1, #255
MOVGT DR1, #255
RETURN
Caller: RGB Processing
MOV DR1, DR5
LAMBDA CR2, clamp
MOV DR5, DR1
... (continues below in detail)
3 invocations
1 copy of code
ANNOTATED INSTRUCTION SEQUENCE
Addr
Instruction
Purpose
Cycles
LAMBDA flag
Stack
■ R CHANNEL
0x00
MOV DR1, DR5
Load R channel value into argument register
1
0
empty
0x04
LAMBDA CR2, clamp
Apply clamp code via X permission on CR2
1
1 (SET)
none!
+0
CMP DR1, #0
Compare with minimum (0)
1
1
—
+4
MOVLT DR1, #0
Conditional: set 0 if below minimum
1
1
—
+8
CMP DR1, #255
Compare with maximum (255)
1
1
—
+C
MOVGT DR1, #255
Conditional: set 255 if above maximum
1
1
—
+10
RETURN
Fast path: flag=1 → restore PC from status, clear flag
1
0 (CLR)
no pop!
0x08
MOV DR5, DR1
Store clamped R back
1
0
empty
R total: 8 cycles
■ G CHANNEL
0x0C
MOV DR1, DR6
Load G channel value into argument register
1
0
empty
0x10
LAMBDA CR2, clamp
Same code, same GT, second invocation
1
1 (SET)
none!
... clamp body (4 instructions) + RETURN fast path ...
6
1→0
no pop!
0x14
MOV DR6, DR1
Store clamped G back
1
0
empty
G total: 8 cycles
■ B CHANNEL
0x18
MOV DR1, DR7
Load B channel value into argument register
1
0
empty
0x1C
LAMBDA CR2, clamp
Same code, same GT, third invocation
1
1 (SET)
none!
... clamp body (4 instructions) + RETURN fast path ...
6
1→0
no pop!
0x20
MOV DR7, DR1
Store clamped B back
1
0
empty
B total: 8 cycles
PERFORMANCE SUMMARY
LAMBDA Approach
Total cycles (3 channels):
24 cycles
Stack accesses:
0 (zero)
Code copies in memory:
1
Domain crossings:
0 (same domain)
If CALL Were Used Instead
Total cycles (3 channels):
~48+ cycles
Stack accesses:
6 (push+pop ×3)
Code copies in memory:
1 (same)
mLoad revalidations:
9 (CR5+CR6+CR14 ×3)
KEY INSIGHTS
Macro-like reuse without duplication:
The clamp code exists once in memory. LAMBDA jumps to it via X permission on the GT.
Unlike a macro, the code is not copied at each call site. Unlike CALL, there is no stack overhead.
Machine-status fast path:
LAMBDA saves PC+4 to a machine-status register (not the stack). RETURN checks the LAMBDA-active
flag and restores directly — zero memory access. Three invocations = zero stack operations.
Security preserved:
Each invocation still checks X permission on CR2. The GT governs access — no bypass, no escalation.
3 invocations → 1 code block
R
G
B
clamp
5 instr