Hi guys,

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?

This is a common trick: it rounds up the result of an integer division. See these examples for division by 10:

Rounding down:
X / Y
0 / 10 = 0
1 / 10 = 0
[...]
9 / 10 = 0
10 / 10 = 1
[...]

Rounding half up:
(X + Y / 2) / 2
0 / 10 -> (0 + 5) / 10 = 0
1 / 10 ~= 0
[...]
4 / 10 ~= 0
5 / 10 ~= 1
[...]
9 / 10 ~= 1
10 / 10 = 1
[...]

Rounding up:
(X + (Y - 1)) / Y
0 / 10 -> (0 + 9) / 10 = 0
1 / 10 -> (1 + 9) / 10 = 1
[...]
9 / 10 ~= 1
10 / 10 = 1
[...]

In the code above, you calculate the number of paragraphs - an integer multiple of 16 bytes - from the number of bytes. The number of bytes is the offset of label "init_end" plus 15. The plus 15 is the "+ (Y - 1)" part in the rounding up example above.

Rounding up makes sure that the number of paragraphs includes all paragraphs, even if the last one is partial: it contains only 1-15 bytes, not the full 16; that is, dividing the offset of label "init_end" by 16 gives you a non-zero remainder (the remainder is the length of the partial last paragraph).

Paragraphs are a feature of the segmented memory model of x86 CPU's.

Joe
--
KOVÁCS Balázs aka Joe Forster/STA; [email protected]; http://sta.c64.org
Don't E-mail spam, HTML or uncompressed files! More contacts on homepage
_______________________________________________
Freedos-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freedos-devel

Reply via email to