Algorithms with State · Lesson 18 of 30 · about 15 minutes
Reverse a Memory Buffer
Copy a buffer backward using source and destination pointers.
01 / Explain
Understand the idea
A pointer is a value used as an address. To reverse four bytes without destroying the input, read source addresses 3, 2, 1, 0 and write destination addresses 16, 17, 18, 19. The two pointers move in opposite directions.
This is a reverse copy, not an in-place reverse. Keeping source and destination regions separate avoids overwriting data you have not read yet. The checker seeds different buffers and inspects both the reversed result and the unchanged source.
02 / Try
Watch it happen
The source remains 1, 2, 3, 4 while cells 16–19 become 4, 3, 2, 1.
STORE 1 0
STORE 2 1
STORE 3 2
STORE 4 3
LOAD 3 R1
LOAD 16 R2
LOOP 4
LOAD [R1]
STORE R2
LOAD R1
SUB 1
COPY R0 R1
LOAD R2
ADD 1
COPY R0 R2
RETURN
HALTUse Step to follow one instruction at a time. You can change the example and replay it.
03 / Challenge
Make it work
Four bytes are seeded at addresses 0–3. Copy them in reverse order into addresses 16–19, leaving the originals unchanged. Use a loop and register addresses; no output is required.
The checker runs your current editor program in a fresh machine for each of 3 test cases. It supplies inputs and seeded memory itself; the lab’s current output and memory do not decide your result.
LOAD 3 R1
LOAD 16 R2
LOOP 4
# Read through R1, write through R2, move both pointers.
RETURN
HALTNeed a hint?
STORE R2 uses the value in R2 as the address.
Reveal a worked solution
Read the program, predict each instruction’s effect, then step through it in the lab.
LOAD 3 R1
LOAD 16 R2
LOOP 4
LOAD [R1]
STORE R2
LOAD R1
SUB 1
COPY R0 R1
LOAD R2
ADD 1
COPY R0 R2
RETURN
HALTEmoji CPU lab
Emoji program
Type LOAD, ADD, or another opcode then Space to insert emoji. Ctrl/⌘ + Enter runs or pauses; Escape pauses; Ctrl/⌘ + ] indents. Tab moves focus. Labels use a colon. Jumps use zero-based instruction addresses.
Instruction map and breakpoints (0)
Breakpoints stop before an instruction. Run resumes past the stopped breakpoint once; Step executes it directly. Editing source clears old breakpoints and machine state.
CPU registers
- R0
- 0
- R1
- 0
- R2
- 0
- R3
- 0
- R4
- 0
- R5
- 0
- R6
- 0
- R7
- 0
Stacks and loop frames
SP = 255 − data depth − call depth. The stack is separate from memory.
Data stack (bottom → top)
Empty
Call return addresses (bottom → top)
Empty
Loop frames
Empty
Output and input
Run a PRINT instruction to see output.
Queued input: Empty
Memory · 256 bytes · 0 nonzero
Each cell shows address:value. R = read this step; W = written this step. Select a cell to inspect or initialize it before execution. Use arrow keys to move, Home/End for the row, and Ctrl/⌘ + Home/End for the whole memory.
Execution trace · 0 entries
Recent entries below. Inspect any zero-based index to see complete detached before/after state.
Check your challenge
You can run this check any time. Every case must pass to record completion.
Progress uses localStorage only. It stays on this browser and is never sent to a server.