This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Memory Curruption Problem

Memory Curruption Problem
by on (#58871)
I have been using the Nerdy Nights codebase from day one, and have never had this many problems. I'm still having problems with memory curruption . For some reason the score output isn't correct despite me fixing the .rs problem. It seems the memory is still being corrupted as i'm getting 3's and 2's where there shouldn't be(score+2,score+3). Heres a diagram of the score display:

1 2 3 4 5 (always 0)
Digit 1 Digit 2 Digit 3 Digit 4 Digit 5 Digit 6

Instead it outputs:

1 2 3 4 (always 0) (always 0)
??? Digit 1 Digit 2 Digit 3 Digit 5 Digit 6

You can grab the latest version of my code from here. Any help in this matter would be greatly appreciated.

Sincerely,

Orbit Ooze

by on (#58901)
Your least significant digit (Score+0) never gets drawn to the screen because you break out of the loop too early:

Code:
      ldx #4            ; set x to 4
SetScore:
      lda Score,x
      clc            ; clear carry
      adc #8         ; add 8 to offset the tile number
      sta $2007         ; draw to the screen
      dex            ; x = x - 1
      bne SetScore      ; Branch to SetScore if result was Not Equal to zero


In this loop, when x is 1 it will draw Score+1 to the screen. Then it decrements x to 0. Then it loops if x is *not* 0. X is 0 however, so it breaks out of the loop. Score+0 is never drawn.

What you want to do is break on X == #$FF instead of x == 0. There is a branch instruction BPL that does this. BPL stands for "Branch if PLus (ie, positive)". It will branch when bit 7 of the result is clear (ie, values in the range #$00-#$7F), so 0 is considered positive.

So to fix your problem, change your bne to a bpl:

Code:
      ldx #4            ; set x to 4
SetScore:
      lda Score,x
      clc            ; clear carry
      adc #8         ; add 8 to offset the tile number
      sta $2007         ; draw to the screen
      dex            ; x = x - 1
      bpl SetScore      ; Branch to SetScore if result is positive


Now when x is decremented from 1 to 0, it will branch up to SetScore and process the last digit. It will read Score+0 and draw it to the screen. Then X is decremented from 0 to FF and the loop ends.