Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 5. WRITING, BUILDING, AND LOADING YOUR


KERNEL 48


total = a + b;, some more space will be reserved for the variabletotal, and in it will
be stored the addition of thecontents of addressespointed to by the labelsaandb.
Now, suppose that we’d like to store a value at a specific address of memory; for
example, like we have done in assembly, to write characters directly to the video memory
at address0xb8000when BIOS was no longer available. How would we do that in C,
when it seems that any value we wish to store must be in an address that has been
determined by the compiler? Indeed, some high-level languages do not allow us to access
memory in this way at all, which essentially is breaking the fluffy abstraction of the
language. Luckily, C allows us to usepointer variables, that are datatypes used for
storing addresses (rather than values), and which we candereferenceto read or write
data to wherever they point.
Now, technically, all pointer variables are the same datatype (e.g. a 32-bit memory
address), but usually we plan to read and write specific datatypes from and to the address
pointed to by a pointer, so we tell the compiler that, say,thisis a pointer to acharand
thatis a pointer to anint. This is really a convenience, so that we do not always have
to tell the compiler how many bytes it should read and write from the address held in a
certain pointer. The syntax for defining and using pointers is shown in Figure XXX.


// Here , the star following the type means that this is not a variable to hold
// a char (i.e. a single byte) but a pointer to the ADDRESS of a char ,
// which , being an address , will actually require the allocation of at least
// 32 bits.
char* video_address = 0xb8000;

// If we’d like to store a character at the address pointed to, we make the
// assignment with a star -prefixed pointer variable. This is known as
// dereferencing a pointer , because we are not changing the address held by
// the pointer variable but the contents of that address.
*video_address = ’X’;

// Just to emphasise the purpose of the star , an ommision of it, such as:
video_address = ’X’;
// would erroneously store the ASCII code of ’X’ in the pointer variable ,
// such that it may later be interpretted as an address.

In C code we often seechar*variables used for strings. Let’s think about why this
is. If we’d like to store a singleintorchar, then we know that they are both fixed
sized datatypes (i.e. we know how many bytes they will use), but a string is anarray
of datatypes (usually ofchar), which may be of any length. So, since a single datatype
cannot hold an entire string, only one element of it, we can use a pointer to achar, and
set it to the memory address of the first character of the string. This is actually what
we did in our assembly routines, such asprintstring, where we allocated a string
of characters (e.g. ‘‘Hello, World’’) somewhere within our code, then, to print a
particular string, we passed the first character’s address via thebxregister.
Let’s look at an example of what the compiler does when we set up a string variable.
In Figure XXX, we define a simple function that does nothing else other than allocate a
string to a variable.

Free download pdf