Skip to content

Commit bdca5e6

Browse files
committed
lesson 3, boot sector with memory addressing
1 parent 81e9f2c commit bdca5e6

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed
12 KB
Binary file not shown.

03-bootsector-memory/README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
1-
TBD
1+
*Concepts you may want to Google beforehand: memory offsets, pointers*
22

3-
Our new boot sector will refer to memory addresses and labels
3+
The only goal of this lesson is to learn where the boot sector is stored
4+
5+
Please open page 14 [of this document](
6+
http://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf)<sup>*</sup>
7+
and look at the figure with the memory layout.
8+
9+
I could just go ahead and tell you that it starts at `0x7C00`, but it's
10+
better with an example.
11+
12+
We want to print an X on screen. We will try 4 different strategies
13+
and see which ones work and why.
14+
15+
First, we will define the X as data, with a label:
16+
```nasm
17+
the_secret:
18+
db "X"
19+
```
20+
21+
Then we will try to access `the_secret` in many different ways:
22+
23+
1. `mov al, the_secret`
24+
2. `mov al, [the_secret]`
25+
3. `mov al, the_secret + 0x7C00`
26+
4. `mov al, 2d + 0x7C00`, where `2d` is the actual position of the X in the binary
27+
28+
Take a look at the code and read the comments.
29+
30+
Compile and run the code. You should see a string similar to `1[2¢3X4X`, where
31+
the bytes following 1 and 2 are just random garbage.
32+
33+
If you add or remove instructions, remember to compute the new offset of the X
34+
by counting the bytes, and replace `0x2d` with the new one.
35+
36+
~~~~~
37+
This whole tutorial is heavily inspired on that document. Please read the
38+
root-level README for more information on that.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
mov ah, 0x0e
2+
3+
; attempt 1
4+
; Fails because it tries to print the memory address (i.e. pointer)
5+
; not its actual contents
6+
mov al, "1"
7+
int 0x10
8+
mov al, the_secret
9+
int 0x10
10+
11+
; attempt 2
12+
; It tries to print the memory address of 'the_secret' which is the correct approach.
13+
; However, BIOS starts loading at address 0x7c00
14+
; so we need to add that padding beforehand. We'll do that in attempt 3
15+
mov al, "2"
16+
int 0x10
17+
mov al, [the_secret]
18+
int 0x10
19+
20+
; attempt 3
21+
; Add the BIOS starting offset 0x7c00 to the memory address of the X
22+
; and then dereference the contents of that pointer
23+
mov al, "3"
24+
int 0x10
25+
mov bx, the_secret
26+
add bx, 0x7c00
27+
mov al, [bx]
28+
int 0x10
29+
30+
; attempt 4
31+
; We try a shortcut since we know that the X is stored at byte 0x2d in our binary
32+
mov al, "4"
33+
int 0x10
34+
mov al, [0x7c2d]
35+
int 0x10
36+
37+
38+
jmp $
39+
40+
41+
the_secret:
42+
; ASCII code 0x58 is stored just before the zero-padding
43+
; on this code that is at byte 0x2d (check it out using xdd)
44+
db "X"
45+
46+
times 510-($-$$) db 0
47+
dw 0xaa55

0 commit comments

Comments
 (0)