title: Looping through memory using Zero Page indirect addressing on the 6502
body: <p>I have written a small 6502 program to loop through memory and use STA Zero Page Indirect Addressing to store a value in 128 consecutive memory locations, but the program doesn't appear to be working. It should store 128 copies of the value $01 to memory address $0400 on-wards but it does not. What am I doing wrong?</p>
  <pre><code>        *=$8001
  
          ; set $00,$01 to $0400
          LDA #$00
          STA $00
          LDA #$04
          STA $01
  
  
          CLC             ; Clear carry and decimal mode
          CLD
          LDX #128        ; Loop 128 times
          LDY #0
  
  loop    LDA #$01        ; the value to store
          STA ($00),Y     ; store A to the address held in $00,$01
          
                          ; Add 1 to $00,$01 (16-bit addition)
  
          LDA $00
          ADC #1
          STA $00
          LDA $01
          ADC #0
          STA $01
                          ; do it 127 more times
          DEX
          BNE loop
          RTS
  </code></pre>