![]() |
for-next repeats forever because of data type limit - Printable Version +- Forum (https://www.boriel.com/forum) +-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12) +--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11) +---- Forum: Bug Reports (https://www.boriel.com/forum/forumdisplay.php?fid=15) +---- Thread: for-next repeats forever because of data type limit (/showthread.php?tid=197) |
for-next repeats forever because of data type limit - programandala.net - 04-09-2010 Hi all, I'm working on my first port from Sinclair Basic to ZX Basic. I'm excited about it. I'd love to program compiled versions of some old programs of mine, but first I need to acquaint with this wonderful and impressive tool. I'm using ZX Basic 1.2.0 under Debian GNU/Linux. Well, I think I found a kind of bug. Here you are the test: Code: ' The following loop works as expected: I found the problem because I needed a loop from 0 to 255, so I used the ubyte data type for the index variable. Then I realized the loop repeated forever! I guess the reason is NEXT increases the loop index before cheking it, so 256 becomes 0 after storing it back into the ubyte variable and the check gets wrong: the loop starts again because the index is zero! I guess the same happens with other data types, as long as their limits are reached, but I didn't try. Do you think this is a language feature or a compiler bug? ![]() I'm programming a project in FreeBASIC too, but I didn't try this. I'll try a similar code and I'll see what happens. Re: for-next repeats forever because of data type limit - boriel - 04-09-2010 programandala.net Wrote:Hi all,First of all, 1.2.0 is *VERY* old and might have lot of bugs. Please, download 1.2.5 or 1.2.6-beta here: <!-- m --><a class="postlink" href="http://www.boriel.com/files/zxb/">http://www.boriel.com/files/zxb/</a><!-- m --> Quote:Unfortunately, it's a feature. :| This is something already discused: you're iterating a loop 256 times using a byte counter. It's really hard to do this, even in assembler, because you loop over the entire counter range (256), so the count will always overflow. The FOR sentence in basic is very powerful, it's equivalent to the following C code (if you don't know C, it does not matter): Code: unsigned char b2; Code: DIM b2 As Ubyte = 0; Re: for-next repeats forever because of data type limit - programandala.net - 04-09-2010 boriel Wrote:First of all, 1.2.0 is *VERY* old and might have lot of bugs. Please, download 1.2.5 or 1.2.6-beta here: <!-- m --><a class="postlink" href="http://www.boriel.com/files/zxb/">http://www.boriel.com/files/zxb/</a><!-- m --> Done. I installed 1.2.0 some months ago, and I forgot to upgrade. boriel Wrote:Unfortunately, it's a feature. :| This is something already discused: you're iterating a loop 256 times using a byte counter. It's really hard to do this, even in assembler, because you loop over the entire counter range (256), so the count will always overflow. The FOR sentence in basic is very powerful, it's equivalent to the following C code (if you don't know C, it does not matter): Thank you for the explanation. I understand. But at first sight I supposed it would work, because the index values were in range (0-255) for ubyte. You're right, that means 256 iterations, but that's the range a ubyte can hold: from 0 to 255. I supposed the compile time action of NEXT would be to check if the limit (255) had been reached, and don't loop again if so. But it seems it first increases the index. I think the current behaviour is less logical. Anyway, it's not an important! I just wanted to confirm the issue. I'll check what FreeBASIC does in such cases. Re: for-next repeats forever because of data type limit - boriel - 04-09-2010 programandala.net Wrote:In fact, the semantic of FOR is not that. For example:boriel Wrote:Thank you for the explanation. I understand. But at first sight I supposed it would work, because the index values were in range (0-255) for ubyte. You're right, that means 256 iterations, but that's the range a ubyte can hold: from 0 to 255. I supposed the compile time action of NEXT would be to check if the limit (255) had been reached, and don't loop again if so. But it seems it first increases the index. I think the current behaviour is less logical. Code: FOR i = 0 TO 254 STEP 2: The meaning for FOR sentence is: "Repeat this loop until (i + step) be GREATER THAN upper limit". Since (i + STEP) is truncated to the current type of variable i (Ubyte in this case), this condition is never satisfied. Keep in mind that FOR can be used with any STEP size (not just STEP 1). In the following version (1.2.6) the compiler might issue a Warning under these circumstances. I suggest you to try the LOOP workaround code I put above. Quote:I'll check what FreeBASIC does in such cases.Please, do, since I'm trying to get close to FreeBasic as much as possible (preserving most of ZX BASIC on the other side ;-)) Re: for-next repeats forever because of data type limit - programandala.net - 04-11-2010 boriel Wrote:Quote:I'll check what FreeBASIC does in such cases.Please, do, since I'm trying to get close to FreeBasic as much as possible (preserving most of ZX BASIC on the other side ;-)) I modified the code to check it with FreeBASIC, and it works exactly the same way: the 0-255 loop repeats forever when the index variable is ubyte: Code: ' FreeBASIC By the way, the compatibility with FreeBASIC would be great: for certain programs, one source code could be compiled both for the ZX Spectrum and for other platforms --to some extent, with some conditional compilation. I'm starting working on some projects with FreeBASIC and ZX Basic, so I'll have the chance to explore the issue. |