Skip to content

Commit ee0eefa

Browse files
committed
lesson 6, segmentation
1 parent 9bc3cd3 commit ee0eefa

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

Diff for: 05-bootsector-functions-strings/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function calling, strings*
55

66
We are close to our definitive boot sector.
77

8-
In lesson 6 we will start reading from the disk, which is the last step before
8+
In lesson 7 we will start reading from the disk, which is the last step before
99
loading a kernel. But first, we will write some code with control structures,
1010
function calling, and full strings usage. We really need to be comfortable with
1111
those concepts before jumping to the disk and the kernel.

Diff for: 06-bootsector-segmentation/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
*Concepts you may want to Google beforehand: segmentation*
2+
3+
**Goal: learn how to address memory with 16-bit real mode segmentation**
4+
5+
If you are comfortable with segmentation, skip this lesson.
6+
7+
We did segmentation
8+
with `[org]` on lesson 3. Segmentation means that you can specify
9+
an offset to all the data you refer to.
10+
11+
This is done by using special registers: `cs`, `ds`, `ss` and `es`, for
12+
Code, Data, Stack and Extra (i.e. user-defined)
13+
14+
Beware: they are *implicitly* used by the CPU, so once you set some
15+
value for, say, `ds`, then all your memory access will be offset by `ds`.
16+
[Read more here](http://wiki.osdev.org/Segmentation)
17+
18+
Furthermore, to compute the real address we don't just join the two
19+
addresses, but we *overlap* them: `segment << 4 + address`. For example,
20+
if `ds` is `0x4d`, then `[0x20]` actually refers to `0x4d0 + 0x20 = 0x4f0`
21+
22+
Enough theory. Have a look at the code and play with it a bit.
23+
24+
Hint: We cannot `mov` literals to those registers, we have to
25+
use a general purpose register before.
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
mov ah, 0x0e ; tty
2+
3+
mov al, [the_secret]
4+
int 0x10 ; we already saw this doesn't work, right?
5+
6+
mov bx, 0x7c0 ; remember, the segment is automatically <<4 for you
7+
mov ds, bx
8+
; WARNING: from now on all memory references will be offset by 'ds' implicitly
9+
mov al, [the_secret]
10+
int 0x10
11+
12+
mov al, [es:the_secret]
13+
int 0x10 ; doesn't look right... isn't 'es' currently 0x000?
14+
15+
mov bx, 0x7c0
16+
mov es, bx
17+
mov al, [es:the_secret]
18+
int 0x10
19+
20+
21+
jmp $
22+
23+
the_secret:
24+
db "X"
25+
26+
times 510 - ($-$$) db 0
27+
dw 0xaa55

0 commit comments

Comments
 (0)