Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Can Memblocks store strings?

Author
Message
Harvester
20
Years of Service
User Offline
Joined: 17th Sep 2004
Location: Infront of my computer
Posted: 22nd Sep 2004 12:31
I was wondering if it is possible to store trings inside a memblock.
I wanted to make a memblock that held information about items for my game(kinda like this)

MAKE MEMBLOCK 1, Size
Item_Name$
Item_Cost#
Item_Power#

of course, the variables(item_Name$ and such) would be replaced by the memblock commands. The help section doesn't have anything for strings in memblocks, and I couldn't find it in forums, so if you
can help, thanks using dbc enhanced, v1.12
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 22nd Sep 2004 21:41
Quote: "dbc enhanced, v1.12"

You mean 1.13

And, yes, it can take strings, use the Write MemBlock Byte command to write each Ascii value of the string to the memblock.
I would suggest that at the start of the string, you put a single byte value telling you how long the string is, then you can read it in a For...Loop easier.

Jess.


Team EOD :: Programmer/All-Round Nice Guy
Aust. Convention!
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 22nd Sep 2004 23:22
The answer to that is; "yes & no."

What I mean is, yes, you can "can" save strings into memblocks, providing you save each character individually as a byte value into the memblock. This means you have to "tear the strings apart" to save them and "paste them together" when you fetch them, because there are no automatic commands to do that for you. You simply have to take it one piece at a time, and fudge...

And for housekeeping, as Jessticular said, it's a good idea to use one byte to save the size of the string at the start of it, so you can read them easier and know when to stop reading the bytes.
S.

Any truly great code should be indisguishable from magic.
Harvester
20
Years of Service
User Offline
Joined: 17th Sep 2004
Location: Infront of my computer
Posted: 23rd Sep 2004 07:58
I was afraid it was going to be single characters in ascii
I think i will just store array slot numbers that hold the strings I
want instead. Thanks for your help

By the way, I haven't downloaded the update yet, i'm still using 1.12
Harvester
20
Years of Service
User Offline
Joined: 17th Sep 2004
Location: Infront of my computer
Posted: 23rd Sep 2004 08:25
sry about posting so soon again, won't let me edit
Found another problom, maybe you guys can help.
When I run the following code

MAKE MEMBLOCK 1, 120
WRITE MEMBLOCK WORD 1, 1, 1
WRITE MEMBLOCK WORD 1, 2, 2
WRITE MEMBLOCK WORD 1, 3, 3
WRITE MEMBLOCK WORD 1, 4, 4
WRITE MEMBLOCK WORD 1, 5, 5
WRITE MEMBLOCK WORD 1, 6, 6

FOR T=1 TO 6
PRINT MEMBLOCK WORD(1, T)
NEXT T
DELETE MEMBLOCK 1
DO
IF INKEY$() = "a" THEN GOTO Try_Again_Lable
IF INKEY$() = "e" THEN END
LOOP

the printed numbers are
513
770
1027
1284
1541
6

changing the
write memblock word 1, 1, 1
to
write memblock word 1, 1, 7
gives output like
519
changing the
write memblock word 1, 6, 6
to another number, it prints that number
Do I have to clear memblock memory before I use it? if so how?
Any help is appreciated, thanks
Torrey
20
Years of Service
User Offline
Joined: 20th Aug 2004
Location: New Jersey
Posted: 23rd Sep 2004 16:06 Edited at: 24th Sep 2004 06:34
Within the next few hours I should have uploaded an updated test dll for you to use if you'd like. The test dll is one I create and test out commands with before I put them to use in a more professional commerical version. The version of this command set is still raw and fresh, so it's going to be missing the command to delete the string from memory, but overall still very usable. I'll give you more details in a little bit.

edit: Just realize that I wandered in to the DarkBASIC Classic forum again, so this command set would be useless. If you own DarkBASIC Pro and want to help test out the memory<->string commands head on over to the DLL forum. Look in the thread labeled, "DLLs in ASM"
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 24th Sep 2004 02:47


Harvester,

Of course you're getting those values back... And no, you don't need to clear a memblock before using it. The trouble is you see, memblocks are a very low level access to the raw system memory, so you have to do a lot of the book keeping you'd normally expect the computer to do for you. Take for example, your situation;

WRITE MEMBLOCK WORD 1, 1, 1
WRITE MEMBLOCK WORD 1, 2, 2
WRITE MEMBLOCK WORD 1, 3, 3
WRITE MEMBLOCK WORD 1, 4, 4
WRITE MEMBLOCK WORD 1, 5, 5
WRITE MEMBLOCK WORD 1, 6, 6

These commands are "right and wrong" at the same time, in that they are correct in the way they are formatted but wrong in their use of the position values. Let's go on to the next protion of your code the reason;

FOR T=1 TO 6
PRINT MEMBLOCK WORD(1, T)
NEXT T

This code will NEVER produce the results of the write commands up above, because the position value is incorrect in each instance save the first and last. (As was true of the write commands, but only the last was a correct location.) Here's why;

In a memblock, the array of RAM is dealt with as BYTE VALUES for any addressing, then can be "fetched" or "written" as byte, word, or dword values. So, to write WORDS as you attempted the commands must be;


WRITE MEMBLOCK WORD 1, 1, 1
WRITE MEMBLOCK WORD 1, 3, 2
WRITE MEMBLOCK WORD 1, 5, 3
WRITE MEMBLOCK WORD 1, 7, 4
WRITE MEMBLOCK WORD 1, 9, 5
WRITE MEMBLOCK WORD 1, 11, 6

so we can read them back as;

FOR T=1 TO 13 step 2
PRINT MEMBLOCK WORD(1, T)
NEXT T

remember: byte counts for positions, then size of operation for read/write.

S.

Any truly great code should be indisguishable from magic.
Rknight
21
Years of Service
User Offline
Joined: 25th Sep 2003
Location: NJ
Posted: 24th Sep 2004 06:58 Edited at: 24th Sep 2004 07:00
This is a way to store it byte by byte if you ever change your mind and decide to do it that way, here's one clumsy way to do it:

To store:

`Get the length, and restrict it to a maximum.
TLEN=LEN(TText$)
IF TLEN>98 then TLEN=98
WRITE MEMBLOCK BYTE MBN,1500+ADJ,TLEN
`Get each char, and write it byte by byte.
FOR MBPOS=1501 to 1501+TLEN
TChar=ASC(MID$(TText$,MBPOS-1500))
WRITE MEMBLOCK BYTE MBN,MBPOS+ADJ,TChar
NEXT MBPOS

To Retrieve:

`retrieve the length
TLEN=MEMBLOCK BYTE(MBN,1500+ADJ)
TText$=""
`retrieve the string byte by byte
FOR MBPOS=1501 to 1501+TLEN
TText$=TText$+CHR$(MEMBLOCK BYTE (MBN,MBPOS+ADJ))
NEXT MBPOS

-----

1500 would be the starting locaiton. +ADJ, would be an adjustment variable you could set to make entries, spaced by say 100 byte increments. ADJ=EntryNumber*Entrysize could allow you to refer to multiple strings stored at precise intervals.

There're better ways to do it, but I haven't attempted any of them yet.
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 24th Sep 2004 08:08
Harvester,

To make a final, definitive statement about memblocks; for myself, I consider the array a variation of what is termed "virtual memory" in some other compiler programs, in that you have a handle which points to it (the block number) and a pointer into the block from which to store and retrieve values. Using this definition, it is possible to "stream" the data contained in the block using the following definitions;

rem vptr is the input or output
rem pointer to the block array
dim vptr(0)
vptr(0) = 0

rem read bytes in sequence..
function read_byte(memblock)
a = memblock byte(memblock, vptr(0))
vptr(0) = vptr(0) + 1
endfunction a

rem read integers in sequence
function read_word(memblock)
a = memblock word(memblock, vptr(0))
vptr(0) = vptr(0) + 2
rem adjust A to have the proper word sign...
if a > 32767 then a = 32767 - a
endfunction a

rem read dwords in sequence
function read_dword(memblock)
a = memblock dword(memblock, vptr(0))
vptr(0) = vptr(0) + 4
endfunction a

rem read stored strings...
function read_string(memblock)
rem a = string length as stored...
a = memblock byte(memblock, vptr(0))
vptr(0) = vptr(0) + 1
a$ = ""
for x = 1 to a
a$ = a$ + chr$(memblock byte(memblock, vptr(0)))
vptr(0) = vptr(0) + 1
next x
endfunction a$

The inverse is of course true when writing your data, giving a workable read/write file where you can save and retrieve values in the order desired. This is highly useful feature since DBC doesn't allow read and write access to a file at the same time, but does allow read-write to memblocks. So we can read a file, change it as desired, and then write changes when finished before saving.

Using the above, we can simply make the block by reading a file, then read the values in the order needed or saved at the time the file or block was created;

a = read_byte(memblock)
b = read_word(memblock)
c$ = read_string(memblock)
...
and so on. This becomes quite transparent in use as in the above, using the pointer to define the location being accessed.

And also; note the change in the read_word function to correct the sign of the number returned, because read will return a 65535 for -1, and a write will not allow negative values. (So they must be adjusted too before writing a program integer.)

good luck!
S.

Any truly great code should be indisguishable from magic.
Harvester
20
Years of Service
User Offline
Joined: 17th Sep 2004
Location: Infront of my computer
Posted: 24th Sep 2004 08:16
Thank you for all your help It is working now, and I don't have to re-write much of it(atleast, I don't think I will) Thanks again for helping out
BearCDPOLD
21
Years of Service
User Offline
Joined: 16th Oct 2003
Location: AZ,USA
Posted: 24th Sep 2004 09:19
I'm a little iffy on memblocks myself, to the best of my knowledge you can:

Store specific types of media into memblocks for later use (images, meshes, etc.)
Write bytes, words, dwords, (any others???) to memblocks.

You define the size of a memblock in bytes, and when writing different pieces of data to the memblock you must keep track of how many bytes a particular piece of data is.

Seems like a dumb post, but I posted this to make sure I've got everything straight, because the only thing I haven't learned in DB is them darned memblocks(undocumented commands don't count!).

Crazy Donut Productions, Current Project: Project Starbuks
Sony stole our name!
Harvester
20
Years of Service
User Offline
Joined: 17th Sep 2004
Location: Infront of my computer
Posted: 24th Sep 2004 10:45
You can also use the WRITE MEMBLOCK FLOAT command to write floats(takes up 4 bytes of space, or so my help files say) I think you have averything else right, but keep in mind that I just started using them myself, so what do I know
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 24th Sep 2004 13:32
Yes, that is correct, you can save Floats and bitmaps, sounds and meshes inside a memblock. Each of these have their own basic sizes and parameters which you can modify by reading and writing to the block itself. Some of these structures are quite complex, so read the documentation close before you attempt them. A memblock is a raw section of memory, which can be formatted in most any manner desired, but you have to take care of any "shuffling" that takes place...

For example; if you had a block with a string of say 15 bytes in size, and then want to write 20 bytes as the new value, you must move each byte after the string down 5 locations before you can write the new string to the block. For that reason, many times I'll assign a string space as say, 50 or 80 bytes in size no matter what length of the string I intend to put there, so that if the string does grow or shrink, I don't have to re-arrange all the other bytes in the block. If it helps, think of the pointer to a memblock's position as the text cursor on the screen, where you write bytes, numbers, images, or whatever to fill up the lines as you go along...

Like I said, you have to manage the extra book keeping, but the process can be very flexible and even fun!
S.

Any truly great code should be indisguishable from magic.

Login to post a reply

Server time is: 2025-05-24 18:57:45
Your offset time is: 2025-05-24 18:57:45