2014-10-05 09:11:03 +00:00
|
|
|
*Concepts you may want to Google beforehand: memory offsets, pointers*
|
2014-09-29 09:43:20 +00:00
|
|
|
|
2014-10-05 16:39:45 +00:00
|
|
|
**Goal: Learn how the computer memory is organized**
|
|
|
|
|
2014-10-05 09:11:03 +00:00
|
|
|
Please open page 14 [of this document](
|
2014-10-05 09:12:19 +00:00
|
|
|
http://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf)<sup>1</sup>
|
2014-10-05 09:11:03 +00:00
|
|
|
and look at the figure with the memory layout.
|
|
|
|
|
2014-10-05 09:28:07 +00:00
|
|
|
The only goal of this lesson is to learn where the boot sector is stored
|
|
|
|
|
2014-10-05 09:30:57 +00:00
|
|
|
I could just bluntly tell you that the BIOS places it at `0x7C00` and
|
|
|
|
get it done with, but an example with wrong solutions will make things clearer.
|
2014-10-05 09:11:03 +00:00
|
|
|
|
|
|
|
We want to print an X on screen. We will try 4 different strategies
|
|
|
|
and see which ones work and why.
|
|
|
|
|
2014-10-05 09:30:57 +00:00
|
|
|
**Open the file `boot_sect_memory.asm`**
|
|
|
|
|
2014-10-05 09:11:03 +00:00
|
|
|
First, we will define the X as data, with a label:
|
|
|
|
```nasm
|
|
|
|
the_secret:
|
|
|
|
db "X"
|
|
|
|
```
|
|
|
|
|
|
|
|
Then we will try to access `the_secret` in many different ways:
|
|
|
|
|
|
|
|
1. `mov al, the_secret`
|
|
|
|
2. `mov al, [the_secret]`
|
|
|
|
3. `mov al, the_secret + 0x7C00`
|
2014-10-05 09:28:07 +00:00
|
|
|
4. `mov al, 2d + 0x7C00`, where `2d` is the actual position of the 'X' byte in the binary
|
2014-10-05 09:11:03 +00:00
|
|
|
|
|
|
|
Take a look at the code and read the comments.
|
|
|
|
|
|
|
|
Compile and run the code. You should see a string similar to `1[2¢3X4X`, where
|
|
|
|
the bytes following 1 and 2 are just random garbage.
|
|
|
|
|
|
|
|
If you add or remove instructions, remember to compute the new offset of the X
|
|
|
|
by counting the bytes, and replace `0x2d` with the new one.
|
|
|
|
|
2014-10-05 09:30:57 +00:00
|
|
|
Please don't continue onto the next section unless you have 100% understood
|
2014-10-05 09:28:07 +00:00
|
|
|
the boot sector offset and memory addressing.
|
|
|
|
|
2014-10-05 09:28:42 +00:00
|
|
|
|
|
|
|
The global offset
|
|
|
|
-----------------
|
|
|
|
|
2014-10-05 09:28:07 +00:00
|
|
|
Now, since offsetting `0x7c00` everywhere is very inconvenient, assemblers let
|
|
|
|
us define a "global offset" for every memory location, with the `org` command:
|
|
|
|
|
|
|
|
```nasm
|
|
|
|
[org 0x7c00]
|
|
|
|
```
|
|
|
|
|
2014-10-05 09:30:57 +00:00
|
|
|
Go ahead and **open `boot_sect_memory_org.asm`** and you will see the canonical
|
2014-10-05 09:28:07 +00:00
|
|
|
way to print data with the boot sector, which is now attempt 2. Compile the code
|
|
|
|
and run it, and you will see how the `org` command affects each previous solution.
|
|
|
|
|
|
|
|
Read the comments for a full explanation of the changes with and without `org`
|
2014-10-05 09:13:32 +00:00
|
|
|
|
|
|
|
-----
|
2014-10-05 09:12:54 +00:00
|
|
|
|
2014-10-05 09:12:19 +00:00
|
|
|
[1] This whole tutorial is heavily inspired on that document. Please read the
|
2014-10-05 09:11:03 +00:00
|
|
|
root-level README for more information on that.
|