News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

.data vs .data?

Started by MaynardG_Krebs, March 23, 2007, 06:11:47 PM

Previous topic - Next topic

MaynardG_Krebs

I have seen some code examples that use the .data? directive to declare uninitialized variables and such. While other source code I've looked at declares initialized and unitialized data both using the .data directive and doesn't use  data?. I haven't been able to find any information why this is so.

I am wondering what is the difference, and if there are any advantages or disadvanteges of doing it either way other than producing more readable code?

I apologize in advance for such a dumb question.

Dasar

the variables in .data section will have their values once your program is run and will take size in the executable file, while the variables in .data? section won't have values when your program runs, and won't take size in the executable file.

anyway, you can use just .data? section, and put all your variables there, and assign the appropriate values to them at the beginning of your code (once your program is run, the variables will have their values as if they were in .data section ) .

If you care about the size of your application, then its better to use .data? in your program as much as you can.

this is what I know, maybe someone correct my answer if I'm mistaken.

u

And also:

.data
var1 dd ?

is absolutely identical to

.data
var1 dd 0

Please use a smaller graphic in your signature.

hutch--

Bill,

It does not matter much in a small test program but in something larger the difference is in the build size. If you don't need initialised data just allocate space in .DATA? as it barely increases the file size where any data you allocate in the initialised data section adds to the file size when finished.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MaynardG_Krebs

Thanks to all for the response.

I have been working through a text book on assembly, and the only information it gives is that the .DATA? directive is used to declare uninitialized data but no explation as to why. Most of the examples in the book just use .DATA for both initialized and unitialized data.

Now that I know I guess I'll get into the habit of using .DATA? from now on. :bg

raymond

My experience has been that anything you declare in the .data section will increase you app size by a minimum of 2kb even if you declare only a single byte. Thus, if I do need to use the .data section (such as for a null-terminated file name), I would not bother to start a .data? section unless I would have the need for more than 2kb of uninitialized variables. If I would really care to minimize the app size to a minimum and need only a few bytes of declared data which would not need to be modified, I could always include it in the code section.

The usefulness of the .data? section is when you would need numerous sections of memory and you don't want to bother allocating/deallocating such memory from the heap. This also provides you with accessible variable names for those sections (with easier addressing modes) instead of having to retrieve the address of allocated memory each time you need it.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

MaynardG_Krebs

Quote from: raymond on March 24, 2007, 04:47:20 AM

The usefulness of the .data? section is when you would need numerous sections of memory and you don't want to bother allocating/deallocating such memory from the heap. This also provides you with accessible variable names for those sections (with easier addressing modes) instead of having to retrieve the address of allocated memory each time you need it.

Raymond


Thanks Raymond,
So then the .data? section will save time and trouble if large amounts of unitialized data is needed, rather than appropriating it in the code.

CoR

One nice thing about .DATA is that it stores variables and data linearly.

.DATA
Position1 POINT {10,10}
Position2 POINT {200,200}
Position3 POINT {300,300}

.CODE

Change Position values...
... and save it to file:
invoke WriteFile, PositionFileHandle,\ ;handle of file to write to
OFFSET Position1,\            ;pointer to data to write to file
3*2*4,\                     ;number of bytes to write to file: 3 structures with 2 variables of 4 bytes each.
OFFSET SizeReadWrite,\      ;pointer to number of bytes written
NULL               

...or read it from file:
invoke ReadFile, PositionFileHandle,\
OFFSET Position1,\ ;...and put it directly in mem ;)
3*2*4,\
OFFSET SizeReadWrite,NULL


zooba

Quote from: BillF on March 24, 2007, 01:34:45 PM
Quote from: raymond on March 24, 2007, 04:47:20 AM

The usefulness of the .data? section is when you would need numerous sections of memory and you don't want to bother allocating/deallocating such memory from the heap. This also provides you with accessible variable names for those sections (with easier addressing modes) instead of having to retrieve the address of allocated memory each time you need it.

Raymond


Thanks Raymond,
So then the .data? section will save time and trouble if large amounts of unitialized data is needed, rather than appropriating it in the code.

Large numbers of small amounts is a better application. If you are allocating 4 bytes from the heap and keeping it around the whole time, you're probably better off using the .data? section (or as Raymond said earlier, the .data section if you've already got one).

Large allocations in the .data? section can really slow compilation down. Dynamic allocation is generally accepted to be a better alternative.

Cheers,

Zooba :U