System Software: Assignment On
System Software: Assignment On
SYSTEM SOFTWARE
Topics :
- ANSII C Macros - The C preprocessor
- MASM Assembler
- Microsoft object file format
ANSII C Macros - The C preprocessor
The C preprocessor provides four separate facilities that you can use as you see fit :
Inclusion of header files. These are files of declarations that can be substituted into
your program.
Macro expansion. You can define macros, which are abbreviations for arbitrary
fragments of C code, and then the C preprocessor will replace the macros with
their definitions throughout the program.
Conditional compilation. Using special preprocessing directives, you can include or
exclude parts of the program according to various conditions.
Line control. If you use a program to combine or rearrange source files into an
intermediate file which is then compiled, you can use line control to inform the
compiler of where each source line originally came from.
The C preprocessor is designed for C-like languages; you may run into problems if
you apply it to other kinds of languages, because it assumes that it is dealing with C.
For example, the C preprocessor sometimes outputs extra white space to avoid
inadvertent C token concatenation, and this may cause problems with other languages.
Most C preprocessor features are inactive unless you give specific directives to
request their use. But there are three transformations that the preprocessor always
makes on all the input it receives, even in the absence of directives.
/*
*/ # /*
*/ defi\
ne FO\
O 10\
20
is equivalent into `#define FOO 1020'. You can split even an escape sequence
with Backslash-Newline. For example, you can split "foo\bar" between the `\' and the
`b' to get
"foo\\
bar"
- Preprocessing Directives :
Most preprocessor features are active only if you use preprocessing directives to
request their use.
Preprocessing directives are lines in your program that start with `#'. The `#' is
followed by an identifier that is the directive name. For example, `#define' is the
directive that defines a macro. Whitespace is also allowed before and after the `#'.
The set of valid directive names is fixed. Programs cannot define new preprocessing
directives.
Some directive names require arguments; these make up the rest of the directive
line and must be separated from the directive name by whitespace. For example,
`#define' must be followed by a macro name and the intended expansion of the
macro.
A preprocessing directive cannot be more than one line in normal circumstances. It
may be split cosmetically with Backslash-Newline, but that has no effect on its
meaning. Comments containing Newlines can also divide the directive into multiple
lines, but the comments are changed to Spaces before the directive is interpreted. The
only way a significant Newline can occur in a preprocessing directive is within a string
constant or character constant.
Note that most C compilers that might be applied to the output from the
preprocessor do not accept string or character constants containing Newlines.The `#'
and the directive name cannot come from a macro expansion. For example, if `foo' is
defined as a macro expanding to `define', that does not make `#foo' a valid
preprocessing directive.
- Header Files :
System header files declare the interfaces to parts of the operating system. You
include them in your program to supply the definitions and declarations you need
to invoke system calls and libraries.
Your own header files contain declarations for interfaces between the source files
of your program. Each time you have a group of related declarations and macro
definitions all or most of which are needed in several different source files, it is a
good idea to create a header file for them.
Including a header file produces the same results in C compilation as copying the
header file into each source file that needs it. But such copying would be time-
consuming and error-prone. With a header file, the related declarations appear in only
one place. If they need to be changed, they can be changed in one place, and
programs that include the header file will automatically use the new version when next
recompiled. The header file eliminates the labor of finding and changing all the copies
as well as the risk that a failure to find one copy will result in inconsistencies within a
program.
The usual convention is to give header files names that end with `.h'. Avoid unusual
characters in header file names, as they reduce portability.
- Macros :
A macro is a sort of abbreviation which you can define once and then use later.
There are many complicated features associated with macros in the C preprocessor.
then the C preprocessor will recognize and expand the macro `BUFFER_SIZE', resulting
in
The use of all upper case for macro names is a standard convention. Programs are
easier to read when it is possible to tell at a glance which names are macros.
MASM Assembler
The Microsoft Macro Assembler (MASM) is an x86 assembler that uses the Intel
syntax for MS-DOS and Microsoft Windows. Beginning with MASM 8.0 there are two
versions of the assembler - one for 16-bit and 32-bit assembly sources, and another
(ML64) for 64-bit sources only.
MASM is maintained by Microsoft, but since version 6.12 has not been sold as a
separate product, it is instead supplied with various Microsoft SDKs and C compilers.
Recent versions of MASM are included with Microsoft Visual Studio.
Early versions of MASM generated object modules using the OMF format, which
was used to create binaries for MS-DOS or OS/2. Since version 6.1, MASM is able to
produce object modules in the Portable Executable (PE/COFF) format. PE/COFF is
compatible with recent Microsoft C compilers, and object modules produced by either
MASM or the C compiler can be routinely intermixed and linked into Win32 and
Win64 binaries.
Some other assemblers can assemble most code written for MASM, with the exception
of more complex macros.
The maximum size of the entire record (unless otherwise noted for specific record
types) is 1024 bytes.
The contents of each record are determined by the record type, but certain subfields
appear frequently enough to be explained separately. The format of such fields is
below.
Names :
Indexed References :
Certain items are ordered by occurrence and are referenced by index. The first
occurrence of the item has index number 1. Index fields may contain 0 (indicating that
they are not present) or values from 1 through 7FFF. The index number field in an
object record can be either 1 or 2 bytes long. If the number is in the range 0-7FH, the
high-order bit (bit 7) is 0 and the low-order bits contain the index number, so the field
is only 1 byte long. If the index number is in the range 80- 7FFFH, the field is 2 bytes
long.
Type Indexes :
Type Index fields occupy 1 or 2 bytes and occur in PUBDEF, LPUBDEF, COMDEF,
LCOMDEF, EXTDEF, and LEXTDEF records. They are encoded as described above for
indexed references, but the interpretation of the values stored is governed by whether
the module has the "new" or "old" object module format.
Ordered Collections :
Certain records and record groups are ordered so that the records may be referred
to with indexes (the format of indexes is described in the "Indexed References" section
of this document). The same format is used whether an index refers to names, logical
segments, or other items.
The overall ordering is obtained from the order of the records within the file
together with the ordering of repeated fields within these records. Such ordered
collections are referenced by index, counting from 1 (index 0 indicates unknown or not
specified).
For example, there may be many LNAMES records within a module, and each of those
records may contain many names. The names are indexed starting at 1 for the first
name in the first LNAMES record encountered while reading the file, 2 for the second
name in the first record, and so forth, with the highest index for the last name in the
last LNAMES record encountered.
- When the least-significant bit of the record type byte is set (that
is, the record type is an odd number), the numeric fields are 4
bytes.