From ee0eefad66c5ad63f5399b06b07a2e17a79e1837 Mon Sep 17 00:00:00 2001
From: Carlos Fenollosa <carlos.fenollosa@gmail.com>
Date: Sun, 5 Oct 2014 20:41:18 +0200
Subject: [PATCH] lesson 6, segmentation

---
 05-bootsector-functions-strings/README.md     |  2 +-
 06-bootsector-segmentation/README.md          | 25 +++++++++++++++++
 .../boot_sect_segmentation.asm                | 27 +++++++++++++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 06-bootsector-segmentation/README.md
 create mode 100644 06-bootsector-segmentation/boot_sect_segmentation.asm

diff --git a/05-bootsector-functions-strings/README.md b/05-bootsector-functions-strings/README.md
index ff5dc79..0c57b81 100644
--- a/05-bootsector-functions-strings/README.md
+++ b/05-bootsector-functions-strings/README.md
@@ -5,7 +5,7 @@ function calling, strings*
 
 We are close to our definitive boot sector.
 
-In lesson 6 we will start reading from the disk, which is the last step before
+In lesson 7 we will start reading from the disk, which is the last step before
 loading a kernel. But first, we will write some code with control structures,
 function calling, and full strings usage. We really need to be comfortable with
 those concepts before jumping to the disk and the kernel.
diff --git a/06-bootsector-segmentation/README.md b/06-bootsector-segmentation/README.md
new file mode 100644
index 0000000..87521ab
--- /dev/null
+++ b/06-bootsector-segmentation/README.md
@@ -0,0 +1,25 @@
+*Concepts you may want to Google beforehand: segmentation*
+
+**Goal: learn how to address memory with 16-bit real mode segmentation**
+
+If you are comfortable with segmentation, skip this lesson.
+
+We did segmentation
+with `[org]` on lesson 3. Segmentation means that you can specify
+an offset to all the data you refer to.
+
+This is done by using special registers: `cs`, `ds`, `ss` and `es`, for
+Code, Data, Stack and Extra (i.e. user-defined)
+
+Beware: they are *implicitly* used by the CPU, so once you set some
+value for, say, `ds`, then all your memory access will be offset by `ds`.
+[Read more here](http://wiki.osdev.org/Segmentation)
+
+Furthermore, to compute the real address we don't just join the two
+addresses, but we *overlap* them: `segment << 4 + address`. For example,
+if `ds` is `0x4d`, then `[0x20]` actually refers to `0x4d0 + 0x20 = 0x4f0`
+
+Enough theory. Have a look at the code and play with it a bit.
+
+Hint: We cannot `mov` literals to those registers, we have to
+use a general purpose register before.
diff --git a/06-bootsector-segmentation/boot_sect_segmentation.asm b/06-bootsector-segmentation/boot_sect_segmentation.asm
new file mode 100644
index 0000000..51a9a6e
--- /dev/null
+++ b/06-bootsector-segmentation/boot_sect_segmentation.asm
@@ -0,0 +1,27 @@
+mov ah, 0x0e ; tty
+
+mov al, [the_secret]
+int 0x10 ; we already saw this doesn't work, right?
+
+mov bx, 0x7c0 ; remember, the segment is automatically <<4 for you
+mov ds, bx
+; WARNING: from now on all memory references will be offset by 'ds' implicitly
+mov al, [the_secret]
+int 0x10
+
+mov al, [es:the_secret]
+int 0x10 ; doesn't look right... isn't 'es' currently 0x000?
+
+mov bx, 0x7c0
+mov es, bx
+mov al, [es:the_secret]
+int 0x10
+
+
+jmp $
+
+the_secret:
+    db "X"
+
+times 510 - ($-$$) db 0
+dw 0xaa55