[ Pobierz całość w formacie PDF ]
each succeeding iteration \ bp" is incremented
this character were moved to a line on its own,
to point to the next \map" structure.
as is done on line 2394.)
Note that the continuation condition
Depending on your point of view, this state-
\bp->m size" is an expression, which becomes zero
ment demonstrates either the power or the
with the sentinel is referenced. This expression
obscurity of the \C" language. Try writing
could have been written equivalently but more
equivalent code to this statement in another
transparently as \bp->m size>0".
language such as Pascal or PL/1.
Note also that no explicit test for the end of the
Step \bp" through the list until an element
array is made. (It can be shown that this latter is
is encountered either with an address greater
not necessary provided CMAPSIZ, SMAPSIZ 2
than the address of the area being returned.
* NPROC !)
i.e. not \ bp->m addr a"
2535: If the list element de nes an area at least as
or which indicates the end of the list
large as that requested, then ...
i.e. not \ bp->m size != 0"
2536: Remember the address of the rst unit of
2565: We have now located the element in front of
the area
which we should insert the new list element.
The question is: Will the list grow larger by
2537: Increment the address stored in the array
one element or will amalgamation keep the
element
number of elements the same or even reduce
it by one?
2538: Decrement the size stored in the element and
compare the result with zero (i.e. was it an
If \bp > mp" we are not trying to insert at
exact t?)
the beginning of the list. If
(bp-l)->m addr+(bp-l)->m size==a
2539: In the case of an exact t, move all the re-
then the area being return abuts the previous
maining list elements (up to and including the
element in the list
sentinel) down one place.
Note that \ (bp-l)" points to the structure be- 2566: Increase the size of the previous list element
fore the one referenced by \ bp" by the size of the area being returned
2567: Does the area being returned also abut the panic deverror
next element of the list? If so | |
| prdev
2568: Add the size of the next element of the list
| |
to the size of the previous element
\ /
printf
2569: Move all the remaining list elements (up to
|
the one containing the nal zero size) down
printn
one place.
|
Note that if the test on line 2567 fortuitously
putchar
gives a true result when \bp->m size" is zero
no harm is done
5.7 printf (2340)
2576: This statement is reached if the test on line
The procedure \printf" provides a direct, unsophis-
2565 failed i.e. the area being returned cannot
ticated low-level, unbu ered way for the operating
be amalgamated with the previous element on
system to send messages to the system console ter-
the list.
minal. It is used during initialisation and to report
Can it be amalgamated with the next ele-
hardware errors or the imminent collapse of the sys-
ment? Note the check that the next element
tem.
is not null
(These versions of \ printf" and \putchar" run
in kernel mode and are similar to, but not the same
2579: Provided the area being returned is gen-
as, the versions invoked by a \ C" program which
uinely non-null (perhaps this test should have
runs in user mode. The latter versions of \printf"
been made sooner?) add a new element to
and \putchar" live in the library \/lib/libc.a".
the list and push all the remaining elements
You may still nd it useful to read the sections
up one place.
\PRINTF(III)" and \PUTCHAR(III)" of the UPM
at this point.)
5.5 In conclusion ...
2340: The programmer must have been carried
The code for these two procedures has been written
away when he declared all the parameters for
very tightly. There is little, if any, \ fat" which could
this procedure. In fact the procedure body
be removed to improve run time e ciency. However
only contains references to \xl" and \fmt".
it would be possible to write these procedures in a
more transparent fashion.
This serves to reveal one of the facts of \C"
If you feel strongly on this point, then as an
programming. The rules for matching parameters
exercise, you should rewrite \mfree" to make its
in procedure calls and procedure declarations are
function more easily discernible.
not enforced, not even with respect to the numbers
Note also that the correct functioning of \mal-
of parameters.
loc" and \mfree" depends on correct initialisation
Parameters are placed on the stack in reverse
of \coremap" and \swapmap". The code to do this
order. Thus when \printf" is called \ fmt" will be
occurs in the procedure \main" at lines 1568, 1583.
nearer to the \top of stack" than \xl", etc.
| . |
5.6 The File `prf.c'
---------
This le is found on Sheets 23 and 24, and contains
| . |
the following procedures:
---------
| . | stack grows down
printf (2340) panic (2416)
---------
printn (2369) prdev (2433)
| . |
putchar (2386) deverror (2447)
---------
| x2 |
The calling relationship between these proce-
---------
dures is illustrated below:
| xl |
---------
| fmt |
---------
| . | top of stack
---------
\xl" has a higher address then \fmt" but a lower 5.8 printn (2369)
address then \x2", because stacks growdownwards
This procedure calls itself recursively in order to
on the PDP11.
generate the required digits in the required order.
It might be possible to code this procedure more
2341: \fmt" maybe interpreted as a constant char-
e ciently but not more completely. (Anyway, in
acter pointer. This declaration is (almost)
view of the implementation of \ putchar", e ciency
equivalent to
is hardly a consideration here.)
\char *fmt "
Suppose n = A*b + B where A = ldiv(n,b) and
The di erence is that here the value of \fmt" where B = lrem(n,b) satis es 0 B
cannot be changed order to display the value for n, we need to display
the value for A followed by the value for B.
2346: \adx" is set to point to \ xl". The expression
The latter is easy for b = 8 or 10: it consists of
\&xl" is the address of \xl". Note that since
a single character. The former is easy if A = 0. It
\xl" is a stack location, this expression cannot
is also easy if \printn" is called recursively. Since
be evaluated at compile time.
A
(Many of the expressions you will nd else- [ Pobierz całość w formacie PDF ]
zanotowane.pl doc.pisz.pl pdf.pisz.pl wyciskamy.pev.pl
each succeeding iteration \ bp" is incremented
this character were moved to a line on its own,
to point to the next \map" structure.
as is done on line 2394.)
Note that the continuation condition
Depending on your point of view, this state-
\bp->m size" is an expression, which becomes zero
ment demonstrates either the power or the
with the sentinel is referenced. This expression
obscurity of the \C" language. Try writing
could have been written equivalently but more
equivalent code to this statement in another
transparently as \bp->m size>0".
language such as Pascal or PL/1.
Note also that no explicit test for the end of the
Step \bp" through the list until an element
array is made. (It can be shown that this latter is
is encountered either with an address greater
not necessary provided CMAPSIZ, SMAPSIZ 2
than the address of the area being returned.
* NPROC !)
i.e. not \ bp->m addr a"
2535: If the list element de nes an area at least as
or which indicates the end of the list
large as that requested, then ...
i.e. not \ bp->m size != 0"
2536: Remember the address of the rst unit of
2565: We have now located the element in front of
the area
which we should insert the new list element.
The question is: Will the list grow larger by
2537: Increment the address stored in the array
one element or will amalgamation keep the
element
number of elements the same or even reduce
it by one?
2538: Decrement the size stored in the element and
compare the result with zero (i.e. was it an
If \bp > mp" we are not trying to insert at
exact t?)
the beginning of the list. If
(bp-l)->m addr+(bp-l)->m size==a
2539: In the case of an exact t, move all the re-
then the area being return abuts the previous
maining list elements (up to and including the
element in the list
sentinel) down one place.
Note that \ (bp-l)" points to the structure be- 2566: Increase the size of the previous list element
fore the one referenced by \ bp" by the size of the area being returned
2567: Does the area being returned also abut the panic deverror
next element of the list? If so | |
| prdev
2568: Add the size of the next element of the list
| |
to the size of the previous element
\ /
printf
2569: Move all the remaining list elements (up to
|
the one containing the nal zero size) down
printn
one place.
|
Note that if the test on line 2567 fortuitously
putchar
gives a true result when \bp->m size" is zero
no harm is done
5.7 printf (2340)
2576: This statement is reached if the test on line
The procedure \printf" provides a direct, unsophis-
2565 failed i.e. the area being returned cannot
ticated low-level, unbu ered way for the operating
be amalgamated with the previous element on
system to send messages to the system console ter-
the list.
minal. It is used during initialisation and to report
Can it be amalgamated with the next ele-
hardware errors or the imminent collapse of the sys-
ment? Note the check that the next element
tem.
is not null
(These versions of \ printf" and \putchar" run
in kernel mode and are similar to, but not the same
2579: Provided the area being returned is gen-
as, the versions invoked by a \ C" program which
uinely non-null (perhaps this test should have
runs in user mode. The latter versions of \printf"
been made sooner?) add a new element to
and \putchar" live in the library \/lib/libc.a".
the list and push all the remaining elements
You may still nd it useful to read the sections
up one place.
\PRINTF(III)" and \PUTCHAR(III)" of the UPM
at this point.)
5.5 In conclusion ...
2340: The programmer must have been carried
The code for these two procedures has been written
away when he declared all the parameters for
very tightly. There is little, if any, \ fat" which could
this procedure. In fact the procedure body
be removed to improve run time e ciency. However
only contains references to \xl" and \fmt".
it would be possible to write these procedures in a
more transparent fashion.
This serves to reveal one of the facts of \C"
If you feel strongly on this point, then as an
programming. The rules for matching parameters
exercise, you should rewrite \mfree" to make its
in procedure calls and procedure declarations are
function more easily discernible.
not enforced, not even with respect to the numbers
Note also that the correct functioning of \mal-
of parameters.
loc" and \mfree" depends on correct initialisation
Parameters are placed on the stack in reverse
of \coremap" and \swapmap". The code to do this
order. Thus when \printf" is called \ fmt" will be
occurs in the procedure \main" at lines 1568, 1583.
nearer to the \top of stack" than \xl", etc.
| . |
5.6 The File `prf.c'
---------
This le is found on Sheets 23 and 24, and contains
| . |
the following procedures:
---------
| . | stack grows down
printf (2340) panic (2416)
---------
printn (2369) prdev (2433)
| . |
putchar (2386) deverror (2447)
---------
| x2 |
The calling relationship between these proce-
---------
dures is illustrated below:
| xl |
---------
| fmt |
---------
| . | top of stack
---------
\xl" has a higher address then \fmt" but a lower 5.8 printn (2369)
address then \x2", because stacks growdownwards
This procedure calls itself recursively in order to
on the PDP11.
generate the required digits in the required order.
It might be possible to code this procedure more
2341: \fmt" maybe interpreted as a constant char-
e ciently but not more completely. (Anyway, in
acter pointer. This declaration is (almost)
view of the implementation of \ putchar", e ciency
equivalent to
is hardly a consideration here.)
\char *fmt "
Suppose n = A*b + B where A = ldiv(n,b) and
The di erence is that here the value of \fmt" where B = lrem(n,b) satis es 0 B
cannot be changed order to display the value for n, we need to display
the value for A followed by the value for B.
2346: \adx" is set to point to \ xl". The expression
The latter is easy for b = 8 or 10: it consists of
\&xl" is the address of \xl". Note that since
a single character. The former is easy if A = 0. It
\xl" is a stack location, this expression cannot
is also easy if \printn" is called recursively. Since
be evaluated at compile time.
A
(Many of the expressions you will nd else- [ Pobierz całość w formacie PDF ]