id: 7
title: Memory Read and Write
subtitle: Open up the bigger storage
description: Learn the LDR and STR instructions

tutorial:
  - title: What is memory?
    text: >
      8 registers isn't a lot! **Memory** is like a row of 256 lockers, each with
      its own number (0-255).
  - title: LDR — load from memory
    text: >
      Put the address into a register, then use `LDR` to read from there:
    code: |
      MOV R1, #0       ; address = 0
      LDR R0, [R1]     ; R0 = memory[0]
  - title: STR — store into memory
    text: >
      `STR` writes a register's value into memory:
    code: |
      MOV R1, #5       ; address = 5
      STR R0, [R1]     ; memory[5] = R0
  - title: Offset addressing
    text: >
      You can also add an offset: `[R1, #4]` means address R1+4:
    code: |
      MOV R1, #0
      LDR R0, [R1, #0]  ; memory[0]
      LDR R2, [R1, #1]  ; memory[1]

goal: memory[0]=**10**, memory[1]=**20** — store their sum into **memory[2]**

initialState:
  memory:
    0: 10
    1: 20

testCases:
  - init: {}
    expected:
      memory:
        2: 30

hints:
  - "LDR the values into registers, add them, then STR the result back"
  - "MOV R3, #0 sets a base address; LDR R0, [R3, #0] reads the first value"
  - "Answer: MOV R3, #0 / LDR R0, [R3, #0] / LDR R1, [R3, #1] / ADD R2, R0, R1 / STR R2, [R3, #2] / XHLT"

starThresholds: [6, 8, 10]

starterCode: |
  ; memory[0]=10, memory[1]=20
  ; Compute the sum and store into memory[2]
  ;
  ; Tip: MOV an address into a register first
  ; then use LDR/STR to read/write memory


  XHLT

showMemory: true
memoryRange: [0, 15]
