id: 10
title: Final Challenge
subtitle: Find the maximum
description: Bring all your skills together!

tutorial:
  - title: The last level!
    text: >
      You've learned registers, arithmetic, bitwise ops, memory, branches and loops.
      Now combine **all of it**!
  - title: The challenge
    text: >
      Memory addresses 0-4 hold five numbers. Find the **maximum value** and its
      **position**. You'll need: a loop + memory reads + compare-and-branch.
  - title: Approach
    text: |
      1. Assume the first number is the largest (R0 = memory[0], R1 = position 0)
      2. Loop through the remaining numbers
      3. If a larger one shows up, update both the max and its position
      4. Continue until all 5 numbers are checked
    code: |
      ; Pseudocode:
      ; R0 = max = mem[0]
      ; R1 = maxIdx = 0
      ; for R4 = 1 to 4:
      ;   R5 = mem[R4]
      ;   if R5 > R0: R0=R5, R1=R4

goal: memory[0..4] holds 5 numbers — store the **maximum** in **R0** and its **position** in **R1**

initialState:
  memory:
    0: 5
    1: 3
    2: 8
    3: 1
    4: 7

testCases:
  - init:
      memory:
        0: 5
        1: 3
        2: 8
        3: 1
        4: 7
    expected:
      registers:
        R0: 8
        R1: 2
  - init:
      memory:
        0: 1
        1: 9
        2: 4
        3: 9
        4: 2
    expected:
      registers:
        R0: 9
        R1: 1

hints:
  - "R0 = max, R1 = position, R4 = loop counter, R5 = current value"
  - "Use MOV R3, R4 / LDR R5, [R3] to read mem[R4]"
  - "Loop body: MOV R3, R4 / LDR R5, [R3] / CMP R5, R0 / BLE skip / MOV R0, R5 / MOV R1, R4 / skip: ADD R4, R4, #1 / CMP R4, #5 / BLT loop"

starThresholds: [12, 15, 20]

starterCode: |
  ; memory[0..4] = [5, 3, 8, 1, 7]
  ; Find the max into R0, the position into R1
  ;
  ; Tip: use R4 as the loop variable
  ; use MOV + LDR to read memory


  XHLT

showMemory: true
memoryRange: [0, 15]
