How to address offsets in dynamically alloc'd memory

Started by BigDaddy, February 02, 2005, 06:28:05 PM

Previous topic - Next topic

BigDaddy

Let's say I want to dynamically alloc 1000 bytes in my program, and I want to address certain offsets in it.  If I use a regular segment scheme and db dw, etc, to name the offsets, then the assembler builds that 1000h bytes into my program - but I don't know that I'll need that space until runtime. 

Now, I know I can use AT to prevent code generation for a segment, but that's just tricking the assembler.  Is there a proper way to do this?  I'd like to define the segment and use its offsets, but I don't want all that space built into my exe.

Thanks

MichaelW

Any (uninitialized) data you place in an uninitialized data segment (.DATA? or. FARDATA?) can be accessed at run time just like initialized data (.DATA or.FARDATA), but unlike initialized data, uninitialized data is not added to the EXE.

Dynamically allocated memory generally means memory allocated from the OS. DOS provides Interrupt 21h, function 48h (Allocate Memory), and function 49h (Free Allocated Memory) for this purpose. One complication that you are likely to encounter is that COM files and many/most EXE files are allocated all available memory when they are loaded, so before you can allocate memory you must free memory. The common method of dealing with this is to use function 4Ah (Set Memory Block Size) to resize the program's memory block, freeing all of the unused memory above the program.
eschew obfuscation

BigDaddy

Happy to hear from you, Michael. 

For my situation, yours is a good solution, though I ordinarily avoid the simplified directives.  If you know the longhand version, let me know.  You wouldn't want me to have to look at a manual, would you? :wink

Still, I'm curious about a different possibility, and I've thought up a good example that might have a revealing solution. 

Here it is: let's say that you want to copy the command line that starts at 80h in the PSP.  Now, rather than specify the offset, you'd like to give that location a fancy name, like "Cmd_Line". 

You don't know where DOS will load your program, so you can't use AT, and any other segment definition that comes to mind will result in an actual output of bytes to your EXE. 

How could you define Cmd_Line?

MichaelW

AFAIK anything that can be done with the simplified segment definitions can also be done with full segment definitions. Segments of class BSS and STACK are not added to the EXE, so you could specify an uninitialized data segment with something like:

uninit SEGMENT 'BSS'

For an EXE file the PSP is in a separate segment, set up by the loader, and the ES and DS segment registers are set at load time to the segment address of the PSP. For a COM file all of the segment registers are set at load time to the segment address of the PSP.
eschew obfuscation

BigDaddy

Thanks!  I had actually wondered if BSS didn't have some role.  Outside of some unsavory nicknames that programmers have given to it, I haven't been able to learn alot. 

Thanks again!