Skip to content

Commit 8211b95

Browse files
committed
lesson 3, boot sector with memory addressing
1 parent 3a8b908 commit 8211b95

File tree

3 files changed

+77
-11
lines changed

3 files changed

+77
-11
lines changed

03-bootsector-memory/README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
*Concepts you may want to Google beforehand: memory offsets, pointers*
22

3-
The only goal of this lesson is to learn where the boot sector is stored
4-
53
Please open page 14 [of this document](
64
http://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf)<sup>1</sup>
75
and look at the figure with the memory layout.
86

9-
I could just go ahead and tell you that it starts at `0x7C00`, but it's
10-
better with an example.
7+
The only goal of this lesson is to learn where the boot sector is stored
8+
9+
I could just go ahead and tell you that the BIOS places it at `0x7C00`, but an
10+
example with wrong solutions will make things clearer.
1111

1212
We want to print an X on screen. We will try 4 different strategies
1313
and see which ones work and why.
@@ -23,7 +23,7 @@ Then we will try to access `the_secret` in many different ways:
2323
1. `mov al, the_secret`
2424
2. `mov al, [the_secret]`
2525
3. `mov al, the_secret + 0x7C00`
26-
4. `mov al, 2d + 0x7C00`, where `2d` is the actual position of the X in the binary
26+
4. `mov al, 2d + 0x7C00`, where `2d` is the actual position of the 'X' byte in the binary
2727

2828
Take a look at the code and read the comments.
2929

@@ -33,6 +33,21 @@ the bytes following 1 and 2 are just random garbage.
3333
If you add or remove instructions, remember to compute the new offset of the X
3434
by counting the bytes, and replace `0x2d` with the new one.
3535

36+
Please don't continue onto the next file unless you have 100% understood
37+
the boot sector offset and memory addressing.
38+
39+
Now, since offsetting `0x7c00` everywhere is very inconvenient, assemblers let
40+
us define a "global offset" for every memory location, with the `org` command:
41+
42+
```nasm
43+
[org 0x7c00]
44+
```
45+
46+
Go ahead and open `boot_sect_memory_org.asm` and you will see the canonical
47+
way to print data with the boot sector, which is now attempt 2. Compile the code
48+
and run it, and you will see how the `org` command affects each previous solution.
49+
50+
Read the comments for a full explanation of the changes with and without `org`
3651

3752
-----
3853

03-bootsector-memory/boot_sect_memory.asm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int 0x10
1010

1111
; attempt 2
1212
; It tries to print the memory address of 'the_secret' which is the correct approach.
13-
; However, BIOS starts loading at address 0x7c00
13+
; However, BIOS places our bootsector binary at address 0x7c00
1414
; so we need to add that padding beforehand. We'll do that in attempt 3
1515
mov al, "2"
1616
int 0x10
@@ -19,7 +19,9 @@ int 0x10
1919

2020
; attempt 3
2121
; Add the BIOS starting offset 0x7c00 to the memory address of the X
22-
; and then dereference the contents of that pointer
22+
; and then dereference the contents of that pointer.
23+
; We need the help of a different register 'bx' because 'mov al, [ax]' is illegal.
24+
; A register can't be used as source and destination for the same command.
2325
mov al, "3"
2426
int 0x10
2527
mov bx, the_secret
@@ -29,19 +31,21 @@ int 0x10
2931

3032
; attempt 4
3133
; We try a shortcut since we know that the X is stored at byte 0x2d in our binary
34+
; That's smart but ineffective, we don't want to be recounting label offsets
35+
; every time we change the code
3236
mov al, "4"
3337
int 0x10
3438
mov al, [0x7c2d]
3539
int 0x10
3640

3741

38-
jmp $
39-
42+
jmp $ ; infinite loop
4043

4144
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)
45+
; ASCII code 0x58 ('X') is stored just before the zero-padding.
46+
; On this code that is at byte 0x2d (check it out using 'xdd file.bin')
4447
db "X"
4548

49+
; zero padding and magic bios number
4650
times 510-($-$$) db 0
4751
dw 0xaa55
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[org 0x7c00]
2+
mov ah, 0x0e
3+
4+
; attempt 1
5+
; Will fail again regardless of 'org' because we are still addressing the pointer
6+
; and not the data it points to
7+
mov al, "1"
8+
int 0x10
9+
mov al, the_secret
10+
int 0x10
11+
12+
; attempt 2
13+
; Having solved the memory offset problem with 'org', this is now the correct answer
14+
mov al, "2"
15+
int 0x10
16+
mov al, [the_secret]
17+
int 0x10
18+
19+
; attempt 3
20+
; As you expected, we are adding 0x7c00 twice, so this is not going to work
21+
mov al, "3"
22+
int 0x10
23+
mov bx, the_secret
24+
add bx, 0x7c00
25+
mov al, [bx]
26+
int 0x10
27+
28+
; attempt 4
29+
; This still works because there are no memory references to pointers, so
30+
; the 'org' mode never applies. Directly addressing memory by counting bytes
31+
; is always going to work, but it's inconvenient
32+
mov al, "4"
33+
int 0x10
34+
mov al, [0x7c2d]
35+
int 0x10
36+
37+
38+
jmp $ ; infinite loop
39+
40+
the_secret:
41+
; ASCII code 0x58 ('X') is stored just before the zero-padding.
42+
; On this code that is at byte 0x2d (check it out using 'xdd file.bin')
43+
db "X"
44+
45+
; zero padding and magic bios number
46+
times 510-($-$$) db 0
47+
dw 0xaa55

0 commit comments

Comments
 (0)