Circular Buffer Pointer Logic

Hard Assembly Memory
Solve this problem

Implement circular buffer pointer manipulation logic in ARM assembly for a high-throughput DMA-based UART receiver. The routine must update read and write pointers in a ring buffer while preventing overflow and underflow.

Constraints

  • r0 = base address of buffer (not used directly).
  • r1 = write pointer, r2 = read pointer, r3 = buffer size.
  • r4 = operation flag (0 = write, 1 = read).
  • On write: increment write pointer; if equals size, wrap to 0.
  • On overflow (write pointer catches read pointer), return error code -1.
  • On read: increment read pointer; if equals size, wrap to 0.
  • On underflow (read equals write), return error code -2.
  • Updated pointer returned in r0.
  • Must preserve r5-r7.
Starter codeAssembly
1.global cb_ptr2cb_ptr:3    ; TODO: implement pointer logic