PDA

View Full Version : Memory allocating in DOS



per
October 31st, 2008, 01:10 AM
I am currently trying to modify "Animator" to allocate buffers instead of keeping them in the file (program size reduced from 64K to 6K). However, I've never used the allocation commands before, and I need some help to make routines that works (the ones I got now just seems to work).

The first is to free the not-in-use memory allocated for my program. I decided to use DOS call 4Ah.


mov ah,4Ah
mov bx,0140h ;rounded-up program size (should I include DOS' file prefix too?)
mov dx,cs
mov es,dx
int 21h

This does seems to work, but I won't guarantee if I'm safe. After this, I uses DOS call 48h to allocate just the memory I need.

Is my 4Ah routine safe enough?

mbbrutman
October 31st, 2008, 03:19 PM
I had to look this up, and I learned a little bit while doing it.

The book I have suggests making the call right after the program receives control from DOS. The ES register should point at the PSP and the number of paragraphs necessary to run should be in BX. You should leave room for the PSP in the allocation size and round up to the nearest paragraph boundary.

I think the only thing that I might change in your code is if you are using an assembler use offsets and some arithmetic instead of the hard coded constant that describes the size of the allocation you need.

If you want to dynamically allocate buffers after this then use function 48.

per
November 1st, 2008, 12:35 AM
I had to look this up, and I learned a little bit while doing it.

The book I have suggests making the call right after the program receives control from DOS. The ES register should point at the PSP and the number of paragraphs necessary to run should be in BX. You should leave room for the PSP in the allocation size and round up to the nearest paragraph boundary.

I think the only thing that I might change in your code is if you are using an assembler use offsets and some arithmetic instead of the hard coded constant that describes the size of the allocation you need.

If you want to dynamically allocate buffers after this then use function 48.

I figured about the same, and it works. However, my second problem is how much memory should I allocate for the program (where is the stack by the way?). Sould I allocate the full 64Kb, or just the 4Kb the program uses?

mbbrutman
November 1st, 2008, 06:03 AM
I thought that in a COM program the stack is included in the initial memory allocation. So besides allocating room for the code and PSP, don't forget the stack space.

It probably doesn't hurt to allocate 64KB, as most systems have more than enough memory. It only matters if you are on a 64KB or 128KB system and you have DOS loaded.

per
November 1st, 2008, 07:37 AM
I thought that in a COM program the stack is included in the initial memory allocation. So besides allocating room for the code and PSP, don't forget the stack space.

I know, but the question is where? If it's in the start of the segement, it's OK, because the only thing that's there is the run-once init-code. However, if it's in the end of the segement, I have no choice. I'll try to look it up in my DOS TechRef (BTW, my XT has only 256Kb of RAM).

*Edit*
As of you need DOS 2.0 to run the program, DOS needs to be loaded.

per
November 1st, 2008, 08:47 AM
Aww... I just figured that the stack IS at the end of the segement.

The choices I now have is to:
1. Keep it as it is
2. Move the stack to a more reasonable place

The second option will, as you by now problably have guessed, use a lot of not-recommed quick'n-dirty code that MOVSB the entire stack and manually alters SP.

*Edit*
I figured how DOS sets up the stack; on initalazion, a near adress to a Int 20h instruction (in the PSP) is pushed on the stack by DOS (this is why a near "ret" instruction is used to take you back to the command line).

With the following code at the start of the program;


pop ax
mov sp,1000h
push ax

I got it to free the 60Kb non-used memory safely.

The memory usage now is exactly 4Kb for the program, and the filesize is 3.75Kb (4Kb - 256b. 256b is the size of the PSP, which is allways added to the program by DOS on initalazion). The stack I have reserved is 80 words.