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

Posts: 62

Joined: Tue Mar 29, 2011 1:05 am

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 FLOATtn=0: mt=0: rn=0: znw=0: znh=16gw=255: gh=175+znh: BORDER 1: CLS: PRINT AT 10,5;"The Sierpinski Triangle";: tn=1: mt=4 '256-1 '176-1x(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+1x(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+1x(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+1x(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 FirstPointtn=1: PRINT AT 12,4;"Press any key to continue";ScanAnyKey:IF INKEY\$="" THEN GO TO ScanAnyKey: END IFRANDOMIZE : CLSDrawTriangles:rn=INT (RND*3)+1x(tn,4)=(x(tn,4)+x(tn,rn))/2y(tn,4)=(y(tn,4)+y(tn,rn))/2PLOT x(tn,4),y(tn,4)tn=tn+1: IF tn>mt THEN tn=1: END IFGO TO DrawTrianglesFirstPoint: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.

Posts: 62

Joined: Tue Mar 29, 2011 1:05 am

Re: Out of memory when using uinteger array

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

Crashes the machine, UBYTE as well.

Posts: 1194

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

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.

Posts: 1194

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

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.

Posts: 1194

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

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.

Posts: 62

Joined: Tue Mar 29, 2011 1:05 am

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 UINTEGERtn=0: mt=0: rn=0: znw=0: znh=16gw=255: gh=175+znh: BORDER 1: CLS: PRINT AT 10,5;"The Sierpinski Triangle";: tn=1: mt=4 '256-1 '176-1x(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+1x(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+1x(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+1x(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 FirstPointtn=1: PRINT AT 12,4;"Press any key to continue";ScanAnyKey:IF INKEY\$="" THEN GO TO ScanAnyKey: END IFRANDOMIZE : CLSDrawTriangles:'rn=RND*3+1rn=CAST(UINTEGER,RND)*3+1'rn=CAST(UINTEGER,RND*3)+1'rn=INT (RND*3)+1x(tn,4)=(x(tn,4)+x(tn,rn))/2y(tn,4)=(y(tn,4)+y(tn,rn))/2PLOT x(tn,4),y(tn,4)tn=tn+1: IF tn>mt THEN tn=1: END IFGO TO DrawTrianglesFirstPoint: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.

Posts: 62

Joined: Tue Mar 29, 2011 1:05 am

Re: Out of memory when using uinteger array

Code:
`@echo offif exist TRIANGLES.TAP del TRIANGLES.TAPif exist TRIANGLES.ASM del TRIANGLES.ASMzxb Triangles.bas --optimize=2 --output=TRIANGLES.ASM --asm --org=24576 --array-base=1 --string-base=1 --heap-size=1024rem --enable-breakzxbasm TRIANGLES.ASM --output=TRIANGLES --tap --BASIC --autorunren TRIANGLES TRIANGLES.TAP`

This is the compiling BAT file.

Posts: 1194

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Re: Out of memory when using uinteger array

Posts: 62

Joined: Tue Mar 29, 2011 1:05 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.

Posts: 1194

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

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.
Attachments
TRIANGLES.ZIP
TRIANGLES.ASM compiled with ZXB 1.2.8s705

Posts: 62

Joined: Tue Mar 29, 2011 1:05 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"

Posts: 1194

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

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..
Check this and tell me.

Posts: 62

Joined: Tue Mar 29, 2011 1:05 am

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

Posts: 62

Joined: Tue Mar 29, 2011 1:05 am

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.

Posts: 1194

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

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

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 asmend function#require "random.asm"`
Next