I know this is scary for a beginner, but there is no good or wrong way to install a stack if it meets the requirements and it does not contain errors.
Before you have a fully working memory manager, you (the person) must be a memory manager.
You need to know how you use memory.
The whole concept of "memory allocation" does not yet exist! You do not have a memory manager, you just have many RW RAM addresses.
Document your assumptions
Start with the required minimum amount of memory that your bootloader can safely accept.
Since the initial program loader (loader) is 0x7c00, it is reasonable to assume that the system has 31.5KiB of memory. You can also assume that there is no memory and rely on the cache, but this is also an extended topic.
Making assumptions is vital when things go wrong.
Take into account the disadvantages
Then you should know about the reserved and used areas, this is achieved using a standard memory card .
Exposure:
00000 - 003ff IVT 00400 - 004ff BDA 00500 - 0052f Dangerous Zone (The Petch Zone :) ) 00530 - 07bff Free 07c00 - 07dff IPL
The Petch Zone is an inside joke and respect for Michael Petch .
Make a fully informed decision
Create a minimal environment by setting up a temporary stack.
In the fragment above, the area 00530 - 07bff free, you can use it as a ~ 29KiB stack.
Since the stack is fully descending, you can put the ss:sp stack pointer in 07c00 .
07c00 is a physical address that translates it to any suitable logical address ( 0000:7c00 , 0001:7bf0 , 0002:7be0 , 0003:7bd0 , ..., 07c0:0000 ), anyone will get the one you like most) and set ss:sp it in any atomic way wrt interrupts that you know / how.
EDIT Using a logical address with a small offset part leads to problems when offset overflow correction from 0 to fffe as Ped7g is correctly noted.
Although checking for static behavior (the starting address is the same) dynamic behavior fails, so it’s better to use 0000:7c00 and, of course, nothing with a segment above 0053 .
Any other area will work; setting the stack pointer to a0000 (end of normal memory) is another choice.
No, it's better to just be aware of where you are investing.
EDIT : as Michael Patch noted in the comments , the address a0000 also dangerous. A more secure address would be 9c000 .
Update memory card
Update the memory card with blocks that suit your needs.
Write down where your kernel begins and ends, where your dynamic data is located, etc.
for instance
00000 - 003ff IVT 00400 - 004ff BDA 00500 - 0052f Dangerous Zone (The Petch Zone :) ) 00530 - 07bff Stack 07c00 - 07dff IPL 07e00 - 08fff Kernel 09000 - 10000 Other kernel stuff
So far, you could leave using static blocks on a memory card, but if you want to use more than 1 megabyte of memory, you need a query BIOS to get an available memory card.
This is internally dynamic, as each system has a different amount of memory.
This map is nothing but very minimal metadata for the memory manager, so the time has come ...
Implement a simple memory manager
In the base environment that you have installed so far, enter a simple memory manager in which blocks of memory are stored.
Pitfall: The memory manager needs some “meta-memory” to store its book of allocated memory, this call is for speculation.
As soon as you can allocate and free memory, you can move the stack to a large area, load other data from the disk or its equivalent, etc.
The idea is that you can now manage memory dynamically, as in C, with malloc and mfree freeing you from the burden of mental handling of a memory card.
More advanced memory manager
A more advanced memory manager is usually written in a high-level language where data manipulation is easier (especially when working with topics such as paging).