This demo is supposed to take a single digit string such as "3", then return the value of that third byte stored in the DataSection under a label called "Lookup:", very simple.

Everything seems fine except if I use "result = result + value", where result was 0 to start with, the resulting value isn't the value!?

Code: Select all

```
Define result.i, sNextChar.s
sNextChar = "3": result = 0
Debug "Val=" + Str(Val(sNextChar)) ;Correctly returns 3
Debug("Peek=" + Str( PeekA(?Lookup))) ;Correctly returns 0 (Lookup value index 0)
Debug("Peek=" + Str( PeekA(?Lookup + Val(sNextChar)) )) ;Correctly returns 3 (Lookup value index 3)
;THIS WORKS:
result = PeekA(?Lookup + Val(sNextChar))
Debug("result=" + Hex(result)) ;Correctly returns 3
;THIS FAILS, why!? the logic is essentially the same
result = 0
result = result + PeekA(?Lookup + Val(sNextChar)) ;(change Val(sNextChar) to 3 and it works, even though Val(sNextChar) correctly returns 3)
Debug("result=" + Hex(result)) ;result is now 0x43353E !??!? (Expected: 3). And im only adding a single byte (PeekA) to result.i, which was 0 ... but somehow going from 0 to a strange address
DataSection
Lookup:
! db 0,1,2,3,4,5 ;eg if sNextChar = "3" we get ?Lookup+Val("3")= the fourth value which is 3
EndDataSection
```