Problem with xbasic compiler

Page 2/4
1 | | 3 | 4

By Kai Magazine

Paragon (1416)

Kai Magazine's picture

06-07-2021, 13:45

If you are going to use a lot of strings you will see you run out of memory very fast.
If you are using msx2 you can store them on vram (convert each character to its ascii value and vpoke them) then bsave the vram area.
When you run the program/game you just bload the image with the values in vram and read them (with vpeek, for example) when you need to print them. This will save you a lot of ram, and you will learn that ram is the most scarce resource when working with xbasic.

By freedom711

Rookie (27)

freedom711's picture

06-07-2021, 16:47

'#I 42,Z,17,A$,126,18,79,35,126,35,102,111,19,6,0,237,176

What's is this?

oO

By Kai Magazine

Paragon (1416)

Kai Magazine's picture

06-07-2021, 17:06

this '#I special xbasic instruction allows you to code on assembly. Nyyrikki, who is a master in both assembly and xbasic created an assembly code that reads a particular string and includes it inside the turbo-block.
In order to actually make use of this you really need to know assembly, so my suggestion at this moment is just to leave it for when you have more experience.

There are other easier ways to do it for your level, one example is the one I explained before.
But the easiest way is to define the string inside the turboblock ^_^

By freedom711

Rookie (27)

freedom711's picture

06-07-2021, 17:07

NYYRIKKI wrote:

Yes, strings are bit of a pain as they work very differently inside a turbo block... I don't suggest trying to transfer them, but if you really must...

10 DEFINT Z
20 A$="I don't know why I'm doing this"
30 Z=VARPTR(A$)
40 _TURBO ON (Z)
45 '#I 42,Z,17,A$,126,18,79,35,126,35,102,111,19,6,0,237,176
50 PRINT A$

YEAH!!!! Running Naked in a Field of Flowers

By NYYRIKKI

Enlighted (5873)

NYYRIKKI's picture

06-07-2021, 21:23

freedom711 wrote:

What's is this?

It is just a simple machine language program... Here is assembly version & explanation of how it works:

42,Z	LD HL,(Z)	;Load BASIC A$ string descriptor we located with VARPTR to HL
17,A$	LD DE,A$	;Load X-BASIC A$ string location to DE
126	LD A,(HL)	;Read BASIC string length
18	LD (DE),A	;Save X-BASIC string length as first on string
79	LD C,A		;Save string length for LDIR
35	INC HL		;Next byte of string descriptor
126	LD A,(HL)	;Load low byte of string location to A
35	INC HL		;Next byte of string descriptor
102	LD H,(HL)	;Load high byte of string location to H
111	LD L,A		;Load low byte of string location L making HL now the complete string location
19	INC DE		;Move destination to byte after length
6,0	LD B,0		;Reset high byte of length to 0
237,176	LDIR		;Copy all of the string (HL=Source, DE=Destination, BC=Length)

By freedom711

Rookie (27)

freedom711's picture

06-07-2021, 21:31

I understand better now....
thanks.

I years ago I programmed in assermbly the commodore 64....

You are making me want to learn the z80 Smile2

By NYYRIKKI

Enlighted (5873)

NYYRIKKI's picture

06-07-2021, 21:42

freedom711 wrote:

You are making me want to learn the z80 Smile2

Always on my secret agenda. Wink
This is not bad place to start: https://www.msx.org/wiki/Z80_Assembler_for_Dummies

By thegeps

Paladin (860)

thegeps's picture

06-07-2021, 22:03

NYYRIKKI wrote:

This is not bad place to start: https://www.msx.org/wiki/Z80_Assembler_for_Dummies

And this forum too... I learned a lot here :)

By freedom711

Rookie (27)

freedom711's picture

06-07-2021, 23:27

help me.... oO

10 defstr a-b
20 dim ab(100)
30 for i-1 to 100
40 read ab(i)
50 next i
60 data a,b,f,t,h,u,i,j,k,o
61 data a,b,f,t,h,u,i,j,k,o
62 data a,b,f,t,h,u,i,j,k,o
63 data a,b,f,t,h,u,i,j,k,o
64 data a,b,f,t,h,u,i,j,k,o
65 data a,b,f,t,h,u,i,j,k,o
66 data a,b,f,t,h,u,i,j,k,o
67 data a,b,f,t,h,u,i,j,k,o
68 data a,b,f,t,h,u,i,j,k,o
69 data a,b,f,t,h,u,i,j,k,o
70 for i=1 to 100
80 print ab(i); " ";
90 next i

if i run normally it's run without errors...

IF _run take error Out of Memory 20

Array 100 elements it's OUT OF MEMORY ?????

Help me please oO

By NYYRIKKI

Enlighted (5873)

NYYRIKKI's picture

07-07-2021, 10:36

Yes, let's take a look... There is typically something like 23414 bytes for you to waste. The tokenized program source takes ~360 bytes, next there are BASIC variables (that in this case don't take space), then there is your compiled program that takes about 235 bytes and then your table that takes 101*256 = 25856 bytes -> Yes, you are out of space. This is the very fundamental difference. while MSX-BASIC reserves the space when it is used, X-BASIC reserves all at the beginning of the program. This makes it very fast as it does not need to do any trash collection, book keeping or other house keeping tasks. On the other hand the memory is then used and you just need to live with it.

As Kai already pointed out strings will eat up your space very quickly (in your case each 8bit character takes 256 bytes of memory), but usually you don't really need more than few at a time, so in practice rest is just about solving where to keep them. ie. in your case you can do:

10 DEFSTR A-B
20 FOR I=1 TO 100
30 READ AB:PRINTAB;" ";
40 NEXT I
50 DATA a,b,f,t,h,u,i,j,k,o
60 DATA a,b,f,t,h,u,i,j,k,o
70 DATA a,b,f,t,h,u,i,j,k,o
80 DATA a,b,f,t,h,u,i,j,k,o
90 DATA a,b,f,t,h,u,i,j,k,o
100 DATA a,b,f,t,h,u,i,j,k,o
110 DATA a,b,f,t,h,u,i,j,k,o
120 DATA a,b,f,t,h,u,i,j,k,o
130 DATA a,b,f,t,h,u,i,j,k,o
140 DATA a,b,f,t,h,u,i,j,k,o

... and only 256 bytes is used for AB.

Page 2/4
1 | | 3 | 4