Forum Index
Stack Fault and ValueS() Question

 
Author Message
Don_Auld



Joined: 27 Jun 2007
Location: Vancouver Canada

Posted: 15 November 2008, 6:27 AM    Post subject: Stack Fault and ValueS() Question

I'm experiencing a stack fault problem when I pass a Mid() function as parameter 1 to a ValueS() subroutine. The following code, while nonsensical, reliably reproduces the problem.

Code:
Option Explicit

Sub Main()
   Dim S As String
   Dim Sng As Single
   Dim Success As Boolean
   Dim I As Integer
   
   'Enable Stack Checking
   Call StackCheck(True)
   
   'Say Hello
   Console.Writeline ("Program Start: ")

   'Reset Flags: 2=HW Reset, 4 Brown out, 5=Power, 8=Watchdog, 16 JTAG reset
   Select Case Register.ResetFlags
      Case 2
         Console.Writeline (" [Reset Button]")
      Case 5
         Console.Writeline (" [Power]")
      Case 8
         Console.Writeline (" [Watchdog Timeout]")
         Console.WriteLine ("Fault Type: " & CStr(Register.FaultType))
         Console.WriteLine ("Task Control Block: " & CStr(Register.FaultData))
         Console.WriteLine ("Instruction Address: " & CStr(Register.FaultData2))
         Console.Writeline ("Reset cause MCUSR     : &H" & cstrhex(Register.MCUCSR) )
   End Select
         
   Console.WriteLine ("Register.ResetFlags: &H" & CStrHex(Register.ResetFlags))
      
   S = "0A1B2C3D4"
   Sng = 0.0
   Success = False
   
   Do
      For I = 1 To Len(S)
         Call ValueS(Mid(S,I,1),Sng,Success) 'THE PROBLEM!
      Next I
   Loop
   
End Sub

My Environment:
ZX24a v2.6.2
Zbasic IDE v1.4.5
Compiler version: v2.6.4

I solved my problem by passing a string to the ValueS() function.

Code:
   Do
      For I = 1 To Len(S)
         NewStr = Mid(S,I,1)
         Call ValueS(NewStr,Sng,Success)
      Next I
   Loop

I'm inexperienced with ZBasic and I'm not sure if i am using the ValueS() function incorrectly or if there is a problem in ZBasic.

Also, In my first example, if I comment out either the Do/Loop or the For/Next loop the problem disappears.

Don
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Location: Portland, OR

Posted: 15 November 2008, 21:41 PM    Post subject: Re: Stack Fault and ValueS() Question

Don_Auld wrote:
I'm experiencing a stack fault problem when I pass a Mid() function as parameter 1 to a ValueS() subroutine. [...]I'm inexperienced with ZBasic and I'm not sure if i am using the ValueS() function incorrectly or if there is a problem in ZBasic.
There is no problem with the way that you've used ValueS(). We have confirmed the problem and verified that it only occurs in VM mode devices (such as the ZX-24a).

The issue is that the string returned by Mid() is not being freed in the code that implements ValueS(). Consequently, when you do this inside an infinite loop, the heap grows until it collides with the stack, causing the stack fault that you reported.

As you've noted, saving the string returned from Mid() to a temporary variable and then passing that variable to ValueS() avoids the problem and is therefore a good workaround.

We will post a VM update when we've completed the testing.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Location: Portland, OR

Posted: 22 November 2008, 0:22 AM    Post subject: Re: Stack Fault and ValueS() Question

dkinzer wrote:
We will post a VM update when we've completed the testing.
The VM update that corrects this problem is available on the Downloads Page.
Back to top
Display posts from previous:   
Page 1 of 1

 



ZBasic Microcontrollers Home
All content Copyright © 2005, 2006, 2007, 2008 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group