Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 3. BOOT SECTOR PROGRAMMING (IN 16-BIT REAL


MODE) 16


us why labels are useful, since without labels we would have to count offsets from the
compiled code, and then update these when changes in code cause these offsets to change.
So now we have seen how BIOS does indeed load our boot sector to the address
0x7c00, and we have also seen how addressing and assembly code labels are related.
It is inconvenient to always have to account for this label--memory offset in your
code, so many assemblers will correct label references during assemblege if you include
the following instruction at the top of your code, telling it exactly where you expect the
code to loaded in memory:


[org 0x7c00]

Question 1


What do you expect will be printed now, when thisorgdirective is added to this boot-
sector program? For good marks, explain why this is so.


3.4.3 Defining Strings


Supposing you wanted to print a pre-defined message (e.g. “Booting OS”) to the screen
at some point; how would you define such a string in your assembly program? We have
to remind ourselves that our computer knows nothing about strings, and that a string
is merely a sequence of data units (e.g. bytes, words, etc.) held somewhere in memory.
In the assembler we can define a string as follows:
my_string:
db ’Booting OS’


We’ve actually already seendb, which translates to “declare byte(s) of data”, which tells
the assembler to write the subsequent bytes directly to the binary output file (i.e. do not
interpret them as processor instructions). Since we surrounded our data with quotes,
the assembler knows to convert each character to its ASCII byte code. Note that, we
often use a label (e.g. mystring) to mark the start of our data, otherwise we would
have no easy way of referencing it within our code.
One thing we have overlooked in this example is that knowing howlonga string
is equally important as to knowing where it is. Since it is us that has to write all the
code that handles strings, it is important to have a consistent strategy for knowing how
long a string is. There are a few possibilities, but the convention is to declare strings
asnull-terminating, which means we always declare the last byte of the string as 0 , as
follows:


my_string:
db ’Booting OS’,

When later iterating through a string, perhaps to print each of its characters in turn, we
can easily determine when we have reached the end.

Free download pdf