dos memory allocation (ASM)

Started by
9 comments, last by Ravyne 10 years, 6 months ago

Hi, im new to asm. I decided to improve my skills by programming for dos. So,now im stuck with 48h function(int 21h) - memory allocation. What is its internal implementation like? I mean, how does dos choose segment address for memory block having been allocated? How can it get to know if any block isn't free?
The only thing i know about memory allocation is 'new' from cpp which returns pointer to object located in memory sad.png

[spoiler]sorry for my English[/spoiler]

Sorry for my English.

Advertisement
Programming against 16-bit segmented memory models may be educational and fun, and perhaps even slightly useful if you want to do retro-computing work, but for the most part if you want to learn modern assembly language it's a total waste of time.

What's your end goal with learning ASM?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Hi, im new to asm. I decided to improve my skills by programming for dos. So,now im stuck with 48h function(int 21h) - memory allocation. What is its internal implementation like? I mean, how does dos choose segment address for memory block having been allocated? How can it get to know if any block isn't free?
The only thing i know about memory allocation is 'new' from cpp which returns pointer to object located in memory sad.png

[spoiler]sorry for my English[/spoiler]

So, there are a few things here:

1. For learning modern assembly, or even embedded assembly, 16 bit DOS is not the way to go. If you really want to learn some embedded assembly, pickup an arduino/propeller/arm educational board and get started there. If your goal is to learn modern assembly, then you're in for a rocky road, but you should get started on the x64 end of things, as it gives you significantly more registers to play with.

2. Regarding int21h/48h: Depending on how the exe/com file is built, you may need to first free all the memory your application is not using (depending on header flags, etc. DOS can allocate the entire remaining free chunk to you already). As for how DOS does its allocations, it has a lot of tools available to it, one simple way would be a linked list or similar behavior as your typical heap allocator. Another would be to use a bitmask, etc. Microsoft doesn't specify, mainly because it is not an important implementation detail to know about.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Programming against 16-bit segmented memory models may be educational and fun, and perhaps even slightly useful if you want to do retro-computing work, but for the most part if you want to learn modern assembly language it's a total waste of time.

i think it can be useful even so. Since i don't know asm well yet, it would be nice to start with something not so complicated. Other reason is that i'll have to use api calls for some of my demands, since windows runs in protected mode. Can i be wrong?

What's your end goal with learning ASM?

hmm, actually i haven't ever thought about it. Maybe it's just my interest?

So, there are a few things here:

1. For learning modern assembly, or even embedded assembly, 16 bit DOS is not the way to go. If you really want to learn some embedded assembly, pickup an arduino/propeller/arm educational board and get started there. If your goal is to learn modern assembly, then you're in for a rocky road, but you should get started on the x64 end of things, as it gives you significantly more registers to play with.

thanks for your answer, i hope i'll made it to x64 mode eventually, but i want to know any basics of asm.

2. Regarding int21h/48h: Depending on how the exe/com file is built, you may need to first free all the memory your application is not using (depending on header flags, etc. DOS can allocate the entire remaining free chunk to you already). As for how DOS does its allocations, it has a lot of tools available to it, one simple way would be a linked list or similar behavior as your typical heap allocator. Another would be to use a bitmask, etc. Microsoft doesn't specify, mainly because it is not an important implementation detail to know about.

well, but how does dos search free mem block? Does it allocate whole segment to mem block? I mean, why is mem block located at AX:0000?

Sorry for my English.

could you give some links with dos custom memory allocation samples? That few ones i found is full with linux/windows/dos internal api calls...

Sorry for my English.

Seriously, save yourself some major headaches and either learn x64 assembly or get a dedicated microcontroller kit that's designed to be hacked on, like an Arduino or whatever.

Learning segmented memory models in this day and age is an utterly useless exercise and will serve to do nothing but confuse you more. Learning to write good assembly code is hard enough, don't make it worse ;-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

1. For learning modern assembly, or even embedded assembly, 16 bit DOS is not the way to go. If you really want to learn some embedded assembly, pickup an arduino/propeller/arm educational board and get started there. If your goal is to learn modern assembly, then you're in for a rocky road, but you should get started on the x64 end of things, as it gives you significantly more registers to play with.

Nintendo GBA is another excellent ARM based platform, and of course better optimized for games than the more generic arduino and such. Not much activity on the old forum.gbadev.org these days, but a few of us still check in regularly, and I'm currently working on a NES/SNES style RPG for it smile.png

ARM assembly is much more fun to write than x86, plus you can do accurate cycle counting and run on real "period" hardware... if you can still find flash carts these days.


well, but how does dos search free mem block? Does it allocate whole segment to mem block? I mean, why is mem block located at AX:0000?

As I mentioned, DOS maintains a table of information, be it a bit set, or linked list,etc. Of blocks of memory that it has not allocated. In real mode there is NO SUCH THING as free memory. There is memory you have used, and memory you have not used. The DOS memory allocation functions are simply there to TRY and keep your applications from trampling all over DOS.

DOS is NOT the platform you should be learning. DOS does not reflect ANY modern machines, embedded or other. If your goal is to LEARN x86 ASSEMBLY, then its time to whip out either the 32 bit or 64 bit assembler and get going. Modern operating systems don't even have to ENTER real mode anymore to boot. So its not even useful there. (If you end up writing your own operating system, then you probably will have to touch realmode for all of 2 seconds, if you don't use grub or another bootloader. But by then you will know enough about assembly to be able to understand what is going on).


Nintendo GBA is another excellent ARM based platform, and of course better optimized for games than the more generic arduino and such. Not much activity on the old forum.gbadev.org these days, but a few of us still check in regularly, and I'm currently working on a NES/SNES style RPG for it

ARM assembly is much more fun to write than x86, plus you can do accurate cycle counting and run on real "period" hardware... if you can still find flash carts these days.

Yes, although you can also get one of the propeller kits and do some low level development on a multicore board. Pretty fun shit.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Yes, although you can also get one of the propeller kits and do some low level development on a multicore board. Pretty fun shit.

I don't doubt it :) I've dabbled in robotics, although using the AVR microcontrollers rather than ARM because they're cheap and easy to work with physically. Lots of fun seeing your code affect things in the real world rather than on a screen. But without my previous GBA experience, it would have been really hard to learn embedded stuff. As it was, I pretty much knew how everything was supposed to work already, so it was just a matter of figuring out the specifics rather than stumbling around cluelessly at first.

And being gamedev.net, I assume the OP is going toward game development rather than physical side of electronics, and GBA is one of the most well balanced and fun to program systems I know.

Although for really retro stuff, NES is great too.

Seriously listen to these guys and do not mess around with 16 bit asm + dos. If you want to start simple pick up a AVR 8bit chip. There is also PIC but pic uses a banked/paged memory system on a lot of their chips which makes things dumb. With AVR 8 bit you have about 130 instructions or so and properly aligned memory that is not split off into chunks. After you get bored with that which you probably won't go an move to ARM.

I started with PIC but after getting tired of all the bank switch and crap I moved to AVR and it is much nicer to work with at the asm level. Eventually I plan to move to ARM and mess about there at some point maybe.

I use bare avr chips not the arduino platform because arduino makes it a nasty hack to do any asm with it.

The only issue with working with the micro controllers is to really do anything interesting you are going to have to learn some electronics. I found this the most difficult part is the actual electronics not writing of the firmware which even in asm is relatively easy.

Right now I am working on a sound activated trigger switch using a AVR and a Microphone. Still trying to understand the electronics behind it already understand the firmware part.

This topic is closed to new replies.

Advertisement