FAQ  •  Register  •  Login

Out of memory when using uinteger array (*solved*)

<<

Darkstar

Posts: 60

Joined: Tue Mar 29, 2011 1:05 am

Post Wed Jun 08, 2011 7:14 pm

Out of memory when using uinteger array (*solved*)

  Code:
' tn = TriangleNumber, mt = MaximumTriangles (# of), rn = RandomNumber, GraphicWidth, GraphicHeight, zn = ZeroNumber (Modifier) 'DIM udg(7) AS UBYTE => {0,1,3,7,15,31,63,127}
INK 6: PAPER 1: FLASH 0: BRIGHT 1: OVER 0: INVERSE 0: DIM tn, mt, rn, gw, gh, znw, znh AS UBYTE: DIM x(4,4) AS UINTEGER: DIM y(4,4) AS UINTEGER
'DIM x(4,4) AS FLOAT: DIM y(4,4) AS FLOAT
tn=0: mt=0: rn=0: znw=0: znh=16
gw=255: gh=175+znh: BORDER 1: CLS: PRINT AT 10,5;"The Sierpinski Triangle";: tn=1: mt=4 '256-1 '176-1
x(tn,1)=znw: y(tn,1)=znh: x(tn,2)=gw/2: y(tn,2)=gh: x(tn,3)=gw: y(tn,3)=znh: GOSUB FirstPoint: tn=tn+1
x(tn,1)=znw: y(tn,1)=gh: x(tn,2)=gw/2: y(tn,2)=znh: x(tn,3)=gw: y(tn,3)=gh: GOSUB FirstPoint: tn=tn+1
x(tn,1)=znw: y(tn,1)=gh: x(tn,2)=gw: y(tn,2)=gh/2: x(tn,3)=znw: y(tn,3)=znh: GOSUB FirstPoint: tn=tn+1
x(tn,1)=gw: y(tn,1)=gh: x(tn,2)=znw: y(tn,2)=gh/2: x(tn,3)=gw: y(tn,3)=znh: GOSUB FirstPoint
tn=1: PRINT AT 12,4;"Press any key to continue";
ScanAnyKey:
IF INKEY$="" THEN GO TO ScanAnyKey: END IF
RANDOMIZE : CLS
DrawTriangles:
rn=INT (RND*3)+1
x(tn,4)=(x(tn,4)+x(tn,rn))/2
y(tn,4)=(y(tn,4)+y(tn,rn))/2
PLOT x(tn,4),y(tn,4)
tn=tn+1: IF tn>mt THEN tn=1: END IF
GO TO DrawTriangles
FirstPoint:
x(tn,4)=x(tn,2): y(tn,4)=y(tn,2): RETURN


This crashes after a few pixels drawn with an out of memory error while if you use float it works fine under
the latest version of the compiler. In version 1.2.6 uinteger works without a hitch. The compilation options are:
zxb Triangles.bas --optimize=2 --output=TRIANGLES.ASM --asm --org=24576 --array-base=1 --string-base=1 --heap-size=1024
zxbasm TRIANGLES.ASM --output=TRIANGLES --tap --BASIC --autorun

On to another subject, what is the correct syntax for "DIM udg(7) AS UBYTE => {0,1,3,7,15,31,63,127}"
as a multidimensional array? I tried "DIM ma(2,2) AS UBYTE => {1,4,6,2,0,55}" but it did not work.
<<

Darkstar

Posts: 60

Joined: Tue Mar 29, 2011 1:05 am

Post Thu Jun 09, 2011 6:34 am

Re: Out of memory when using uinteger array

"rn=CAST(UINTEGER,RND)*3+1"

Crashes the machine, UBYTE as well.
<<

boriel

Site Admin

Posts: 1143

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Wed Jul 13, 2011 8:22 pm

Re: Out of memory when using uinteger array

Darkstar wrote:
  Code:
' tn = TriangleNumber, mt = MaximumTriangles (# of), rn = RandomNumber, GraphicWidth, GraphicHeight, zn = ZeroNumber (Modifier) 'DIM udg(7) AS UBYTE => {0,1,3,7,15,31,63,127}
[...]
FirstPoint:
x(tn,4)=x(tn,2): y(tn,4)=y(tn,2): RETURN


This crashes after a few pixels drawn with an out of memory error while if you use float it works fine under
the latest version of the compiler. In version 1.2.6 uinteger works without a hitch. The compilation options are:
zxb Triangles.bas --optimize=2 --output=TRIANGLES.ASM --asm --org=24576 --array-base=1 --string-base=1 --heap-size=1024
zxbasm TRIANGLES.ASM --output=TRIANGLES --tap --BASIC --autorun

Sorry for this mega-delay (I'm alive), but I've been *REALLY* busy (okay, I went to NY for two weeks, but had lot of stuff piled up upon return). I will check this ASAP.

On to another subject, what is the correct syntax for "DIM udg(7) AS UBYTE => {0,1,3,7,15,31,63,127}"
as a multidimensional array? I tried "DIM ma(2,2) AS UBYTE => {1,4,6,2,0,55}" but it did not work.

Use "vector inside vectors":
  Code:
DIM ma(1 TO 2, 2) AS UBYTE => {{1, 4, 6}, {2, 0, 55}}

Remember that arrays start from 0, so DIM(2, 2) is equivalent to DIM(0 TO 2, 0 TO 2) => 9 cells array.
You can read more about it here: http://www.boriel.com/wiki/en/index.php/ZX_BASIC:DIM
<<

boriel

Site Admin

Posts: 1143

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Wed Jul 13, 2011 8:37 pm

Re: Out of memory when using uinteger array

Darkstar wrote:"rn=CAST(UINTEGER,RND)*3+1"

Crashes the machine, UBYTE as well.

Sorry, I can't make the machine to crash with the above sentence. Is this part of a greater program?

Please download the lastest compiler version (1.2.8s703) and check if it work now. Otherwise, send me the --asm generated file of the above sentence to debug it.
<<

boriel

Site Admin

Posts: 1143

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Sat Jul 16, 2011 12:38 am

Re: Out of memory when using uinteger array

Okay, both bugs seems to be related, and fixed. Even more, the compiler produces a little better code now.
Download latest version 1.2.8-s705 and tell me if it works, please.
<<

Darkstar

Posts: 60

Joined: Tue Mar 29, 2011 1:05 am

Post Mon Jul 18, 2011 7:40 pm

Re: Out of memory when using uinteger array

  Code:
' tn = TriangleNumber, mt = MaximumTriangles (# of), rn = RandomNumber, GraphicWidth, GraphicHeight, zn = ZeroNumber (Modifier)
INK 6: PAPER 1: FLASH 0: BRIGHT 1: OVER 0: INVERSE 0: DIM tn, mt, rn, gw, gh, znw, znh AS UBYTE: DIM x(4,4) AS UINTEGER: DIM y(4,4) AS UINTEGER
tn=0: mt=0: rn=0: znw=0: znh=16
gw=255: gh=175+znh: BORDER 1: CLS: PRINT AT 10,5;"The Sierpinski Triangle";: tn=1: mt=4 '256-1 '176-1
x(tn,1)=znw: y(tn,1)=znh: x(tn,2)=gw/2: y(tn,2)=gh: x(tn,3)=gw: y(tn,3)=znh: GOSUB FirstPoint: tn=tn+1
x(tn,1)=znw: y(tn,1)=gh: x(tn,2)=gw/2: y(tn,2)=znh: x(tn,3)=gw: y(tn,3)=gh: GOSUB FirstPoint: tn=tn+1
x(tn,1)=znw: y(tn,1)=gh: x(tn,2)=gw: y(tn,2)=gh/2: x(tn,3)=znw: y(tn,3)=znh: GOSUB FirstPoint: tn=tn+1
x(tn,1)=gw: y(tn,1)=gh: x(tn,2)=znw: y(tn,2)=gh/2: x(tn,3)=gw: y(tn,3)=znh: GOSUB FirstPoint
tn=1: PRINT AT 12,4;"Press any key to continue";
ScanAnyKey:
IF INKEY$="" THEN GO TO ScanAnyKey: END IF
RANDOMIZE : CLS
DrawTriangles:
'rn=RND*3+1
rn=CAST(UINTEGER,RND)*3+1
'rn=CAST(UINTEGER,RND*3)+1
'rn=INT (RND*3)+1
x(tn,4)=(x(tn,4)+x(tn,rn))/2
y(tn,4)=(y(tn,4)+y(tn,rn))/2
PLOT x(tn,4),y(tn,4)
tn=tn+1: IF tn>mt THEN tn=1: END IF
GO TO DrawTriangles
FirstPoint:
x(tn,4)=x(tn,2): y(tn,4)=y(tn,2): RETURN

Crashes the machine after a few pixels and the offending line is the first cast line. The assembly code:
  Code:
__LABEL__DrawTriangles:
   call RND
   call __FTOU32REG
   ld de, 3
   call __MUL16_FAST
   inc hl
   ld a, l
   ld (_rn), a


I am trying to get rid of the floating point multiplication in the RND section to gain speed.
The arrays work fine now thanks, and thanks for the DIM answer; reminds me of context within a context.
I had a feeling that those two bugs might be related.

Otherwise, I just checked into the forum as I have been really busy so sorry for the late reply to this thread.
<<

Darkstar

Posts: 60

Joined: Tue Mar 29, 2011 1:05 am

Post Mon Jul 18, 2011 8:07 pm

Re: Out of memory when using uinteger array

  Code:
@echo off
if exist TRIANGLES.TAP del TRIANGLES.TAP
if exist TRIANGLES.ASM del TRIANGLES.ASM

zxb Triangles.bas --optimize=2 --output=TRIANGLES.ASM --asm --org=24576 --array-base=1 --string-base=1 --heap-size=1024
rem --enable-break
zxbasm TRIANGLES.ASM --output=TRIANGLES --tap --BASIC --autorun

ren TRIANGLES TRIANGLES.TAP


This is the compiling BAT file.
<<

boriel

Site Admin

Posts: 1143

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue Jul 19, 2011 9:17 am

Re: Out of memory when using uinteger array

Did you download the latest version 1.2.8-s705 (linked above)? I've run your test for about 10 minutes (at x400 speed) and it does not crash on my emulator... :?:
<<

Darkstar

Posts: 60

Joined: Tue Mar 29, 2011 1:05 am

Post Tue Jul 19, 2011 11:01 am

Re: Out of memory when using uinteger array

Yes of course I did download 1.2.8-s705, even double checked it using the --version switch before proceeding. However I did not delete the old files but just copied over them thus replacing the old files with the new ones. Could that be it? I am using Spectaculator 7.51.
<<

boriel

Site Admin

Posts: 1143

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue Jul 19, 2011 11:23 am

Re: Out of memory when using uinteger array

Check the attached TRIANGLES.asm file and compare it with your one. They should be the same (try using something like gvimdiff or the like). I think it could be a good idea to place a comment in the beginning of the ASM file which read something like: ";; Generated by ZX Basic version XXXX". What do you think?

Also assemble this file and run it and tell me if it works ok.
:roll:
Attachments
TRIANGLES.ZIP
TRIANGLES.ASM compiled with ZXB 1.2.8s705
(16.17 KiB) Downloaded 61 times
<<

Darkstar

Posts: 60

Joined: Tue Mar 29, 2011 1:05 am

Post Tue Jul 19, 2011 11:49 am

Re: Out of memory when using uinteger array

I think it could be a good idea to place a comment in the beginning of the ASM file which read something like: ";; Generated by ZX Basic version XXXX". What do you think?


I would think that would be a very good idea to keep track and to avoid confusion. I have a program made for the 16K that takes up all of the lower memory and it's made with 1.2.6 and I have not recompiled it for the new version might run out of memory, so I keep 1.2.6 around.

My asm file is 65.286 bytes. Yours is 64.091 bytes.

The code for rnd in the zip file:
  Code:
__LABEL__DrawTriangles:
   call RND
   push bc
   push de
   push af
   ld a, 082h
   ld de, 00040h
   ld bc, 00000h
   call __MULF
   call __FTOU32REG
   inc hl
   ld a, l
   ld (_rn), a


Are you sure you compiled it with this statement?
"rn=CAST(UINTEGER,RND)*3+1"
<<

boriel

Site Admin

Posts: 1143

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue Jul 19, 2011 1:13 pm

Re: Out of memory when using uinteger array

Note: CAST(Uinteger, RND) is *always* 0, because RND returns a float number form 0 to 0.99999999...
So CAST(Uinteger, RND * 3) will return an Uinteger number between 0 and 2 (which is most likely what you want). I used this 2nd form, and your program works ok.

The first ASM chunk you posted corresponds to CAST(Uinteger, RND) * 3 + 1, and won't work.

If you use the 1st one your program will looks like crashed but it is not; it's only generating the same "random" (truncated) numbers once and over again (this should happen both in 1.2.6 and 1.2.8).
Check this and tell me.
<<

Darkstar

Posts: 60

Joined: Tue Mar 29, 2011 1:05 am

Post Tue Jul 19, 2011 1:45 pm

Re: Out of memory when using uinteger array

"Note: CAST(Uinteger, RND) is *always* 0, because RND returns a float number form 0 to 0.99999999..."

I was waiting for that argument somehow. However can you not do a RND that works on integers. I know it can be done :D
<<

Darkstar

Posts: 60

Joined: Tue Mar 29, 2011 1:05 am

Post Tue Jul 19, 2011 1:47 pm

Re: Out of memory when using uinteger array

"Check this and tell me."

I have checked this back and forth and the current release is stable.
<<

boriel

Site Admin

Posts: 1143

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue Jul 19, 2011 2:56 pm

Re: Out of memory when using uinteger array

Darkstar wrote:"Note: CAST(Uinteger, RND) is *always* 0, because RND returns a float number form 0 to 0.99999999..."

I was waiting for that argument somehow. However can you not do a RND that works on integers. I know it can be done :D

It is, and very easy indeed, since the main RAND routine generates a 32bits (DE, HL) integer. I can add a library <rand.bas> which contains a randint() routine, for example (it's just a simple call to RAND routine).

Try this:
  Code:
function fastcall randint as ULong
   asm
   jp RAND
   end asm
end function

#require "random.asm"
Next

Return to Bug Reports

Who is online

Users browsing this forum: No registered users and 1 guest

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.

phpBB SEO