Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Out of memory when using uinteger array (*solved*)
#1
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.
Reply
#2
"rn=CAST(UINTEGER,RND)*3+1"

Crashes the machine, UBYTE as well.
Reply
#3
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.

Quote: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: <!-- m --><a class="postlink" href="http://www.boriel.com/wiki/en/index.php/ZX_BASIC:DIM">http://www.boriel.com/wiki/en/index.php/ZX_BASIC:DIM</a><!-- m -->
Reply
#4
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.
Reply
#5
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.
Reply
#6
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.
Reply
#7
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.
Reply
#8
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... :?:
Reply
#9
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.
Reply
#10
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:


Attached Files
.zip   TRIANGLES.ZIP (Size: 16.17 KB / Downloads: 567)
Reply
#11
Quote: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"
Reply
#12
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.
Reply
#13
"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 Big Grin
Reply
#14
"Check this and tell me."

I have checked this back and forth and the current release is stable.
Reply
#15
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 Big Grin
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"
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)