Hello Quinton, hello Mark,

I am looking at (I believe) a slightly more recent version of the
FreeDOS kernel source code (https://github.com/FDOS/kernel), but I think
the same reasoning applies.

Basically the kernel is trying at this point to move its IGROUP
(initialization code), I_GROUP (initialization data), and (I think)
HMA_TEXT, to the top of conventional memory.  Before doing that, it
needs to calculate a memory address where it can move IGROUP, I_GROUP,
and HMA_TEXT to.

        int     12h             ; move init text+data to higher memory
        mov     cl,6
        shl     ax,cl           ; convert kb to para

The BIOS's int 0x12 returns the amount of conventional memory in ax, as
a Kbyte count (e.g. 640).  By multiplying by 1,024 / 16 = 64, we can
converts this to a count of 16-byte paragraphs.  For 640 KiB of
conventional memory, this will be 0xa000.

Conventional memory starts at real mode address 0:0, so the paragraph
count also gives the real mode segment base of the _top_ of conventional
memory --- 0xa000:0.

        mov     dx,15 + INITSIZE
        mov     cl,4
        shr     dx,cl

This tries to calculate the number of paragraphs needed by the I_GROUP
(initialization data) segment.

The init_end symbol marks the end of the whole I_GROUP segment.
However, it may not be placed at a 16-byte-aligned address --- it is
defined in the IB_E segment, and the linker will place IB_E's contents
into I_GROUP output after other input segments that contribute to
I_GROUP (ID_B, ID, ID_E, IC, etc.).

So to get the correct number of paragraphs, the kernel needs to round
_up_ the address of init_end when dividing it by 16, which is why it
adds 15 first.  Without adding 15, a `shr' by 4 will instead round down
the address of init_end, which will not be correct.

        sub     ax,dx

ax from the earlier `int 12h' holds the address of the top of
conventional memory.  By subtracting the number of paragraphs needed by
the I_GROUP segment, the kernel can get the address of a memory area
near the conventional memory top where it can move I_GROUP to.

I hope this helps a bit.  Thank you!

On 9/21/19 10:52, Mark Olesen wrote:
Howdy,

Your aligning by 16 byte paragraph

On Fri, Sep 20, 2019 at 7:45 PM Quinton Cook <[email protected]> wrote:

Hello all,

I have recently taken an academic interest in the freedos project. My
current goal is to understand how the kernel was implemented.

While reading the kernel.asm file located
in freedos-svn/kernel/branches/jhall/kernel I have inferred that the
routine "kernel_start" is calculating the overall memory available to the
system. but I came across a snippet of code that confused me. Starting on
line 128
it goes

mov dx,init_end+15
mov cl,4
shr dx,cl
sub ax,dx

why is 15 added to the end of the address of the symbol init_end if you
just shift the value right by 4, doesn't that just undo the addition? Is it
related to memory addresses having to be divisible by 16? Segmentation or
something like that?

Also is that last sub calculating the amount of room needed for the kernel?

I hope all is well and get back to me when it is convenient for you, thanks
--
*Quinton Cook*
_______________________________________________
Freedos-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freedos-devel




_______________________________________________
Freedos-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freedos-devel




_______________________________________________
Freedos-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freedos-devel

Reply via email to