┌───────────────────────┐
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
└───────────────────────┘
Exordium Operating System Development Notes
~ CuB3y0nd
# 

 C ……
<s>_ Maste
r_</s>


便
便
……

……
…… LOL

**__** **_CSAPP_** ……

 _03/09/2025_ <s>
……</s>

GitHub: https://github.com/CuB3y0nd/Exordium

<br />

# 

##  1MB 

 1MB Intel 8086  Intel 8086  20 线
访 $$2^{20} =1048576$$  1MB  1MB 




|     |     |               |                                   
                                                                                
                           |
| ------- | ------- | ----------------- | --------------------------------------
--------------------------------------------------------------------------------
-------------------------- |
| 0xFFFF0 | 0xFFFFF | 16B               | BIOS  BIOS 
 64KB  16 
 jmp F000:E05B |
| 0xF0000 | 0xFFFEF | 64KB-16B          |  BIOS  F0000~FFFFF  64B
 16  0xFFFEF    
                           |
| 0xC8000 | 0xEFFFF | 160KB             |  ROM  I/O 
                                                                                
                           |
| 0xC0000 | 0xC7FFF | 32KB              |  BIOS                       
                                                                                
                           |
| 0xB8000 | 0xBFFFF | 32KB              |                 
                                                                                
                           |
| 0xB0000 | 0xB7FFF | 32KB              |                     
                                                                                
                           |
| 0xA0000 | 0xAFFFF | 64KB              |                     
                                                                                
                           |
| 0x9FC00 | 0x9FFFF | 1KB               | EDBA (Extended BIOS Data Area)        
                                                                                
                           |
| 0x7E00  | 0x9FBFF | 622080B 608KB |                               
                                                                                
                           |
| 0x7C00  | 0x7DFF  | 512B              | MBR  BIOS                 
                                                                                
                           |
| 0x500   | 0x7BFF  | 30464B 30KB   |                               
                                                                                
                           |
| 0x400   | 0x4FF   | 256B              | BIOS Data Area                        
                                                                                
                           |
| 0x00    | 0x3FF   | 1KB               | Interrupt Vector Table                
                                                                                
                           |

## 

 Power CPU  `CS:IP`  `0xF000:0xFFF0`. 
 0xFFFF0
 `BIOS (Basic Input/Output System)` 
 BIOS. BIOS 
 ` (Interrupt Vector Table, IVT)` 
BIOS  64KB  I/O 

 I/O 


> [!TIP]
>  BIOS 
 ROM 
>
> BIOS  ROM  ROM  1MB 
 0xF0000~0xFFFFF 

访 1MB  0xFFFF0  1MB  16 
 `jmp F000:E05
B`  0xFE05B  BIOS  0xFE05B 

 BIOS 便
便 0x00~0x03FF 

BIOS 使
使

BIOS ……


BIOS  `0  0  1 ` 
 `0x55`  `0xAA`BIOS 便 `
 MBR (Main Boot Record)` `0x7c00` 


 0  0  1 
便 BIOS  MBRBIOS 
 0x55 
 0xAA MBR.  MBR BIOS 
MBR 
 BIOS 
 512 
……

 0  0  1 MBR  BIOS 
 BIOS 

西0xAA55 
/……

 0x7c00 0x7c00  1981  8 IBM 
 PC 5150  ROM BIOS  INT 19H PC 5150 
 x86 

 DOS 1.0
 16KB  32KB PC 5150 BIOS  32KB 
 BIOS  32KB 

MBR 便
MBR 
MBR  0x7
c00  MBR  MBR
 MBR 

使

8086 CPU  0x00~0x03FF 
 DOS 1.0  32KB MBR 
 MBR  32KB 

MBR  MBR  512 
 512  1K
B 

 32KB  1KB 32KB  0x8000
 1KB (0x400)  0x7c00 0x7c00 

###  MBR





```plaintext
.
├── boot
│   └── mbr.s
└── Makefile
```

```asm title="boot/mbr.asm" wrap=false
section mbr vstart=0x7c00
  mov ax, 0x0600 ; clear screen
  mov bh, 0x07   ; color attribute 0x07
  xor cx, cx     ; upper left corner
  mov dx, 0x184f ; bottom right corner
  int 0x10

  mov ah, 0x03   ; get cursor position
  xor bh, bh     ; video page 0
  int 0x10

  mov cx, 0x03   ; length of string
  mov ax, 0x1301 ; write string, move cursor
  mov bx, 0x07   ; video page 0, color attribute 0x07
  lea bp, [msg]  ; ES:BP is the pointer to string
  int 0x10

  jmp $

  msg db "MBR"

  times 510-($-$$) db 0
boot_flag:
  dw 0xAA55
```

 `int 0x10`  [INT 10 - Video BIOS Services](http
s://stanislavs.org/helppc/int_10.html).

AT&T  🐴 ……



> Intel Syntax Support
>
> Up until v2.10 of binutils, GAS supported only the AT&T syntax for x86 and x86
-64, which differs significantly from the Intel syntax used by virtually every o
ther assembler. Today, GAS supports both syntax sets (.intel_syntax and the defa
ult .att_syntax), and even allows disabling the otherwise mandatory operand pref
ixes '%' or '$' (..._syntax noprefix). There are some pitfalls - several FP opco
des suffer from a reverse operand ordering that is bound to stay in there for co
mpatibility reasons, .intel_syntax generates less optimized opcodes on occasion 
(try mov'ing to %si...).
>
> `It is generally discouraged to use the support for Intel Syntax because it ca
n subtly and surprisingly different than the real Intel Syntax found in other as
semblers.` A different assembler should be considered if Intel Syntax is desired
.

…… GAS  nasm nasm  GAS……
 GAS……
 GAS ……Linux ……
……

```plaintext title="Makefile" wrap=false
AS = nasm
DD = dd bs=512 conv=notrunc
IMG = exordium.img
IMG_SIZE = 60M

all: boot/mbr create_img write_mbr

boot/mbr: boot/mbr.asm
 $(AS) -I boot -o $@ $<

create_img:
 qemu-img create -f raw $(IMG) $(IMG_SIZE)

write_mbr: boot/mbr
 $(DD) if=$< of=$(IMG) count=1

clean:
 rm -rf boot/mbr
 rm -f $(IMG)
```

使 `make clean && make` 

使 `start.sh` 

```shell title="start.sh" wrap=false
#!/bin/bash

IMG="exordium.img"

qemu-system-i386 -drive file=$IMG,format=raw,if=ide,index=0 -s -S -monitor stdio
```

使 `debug.sh`  gdb 

```shell title="debug.sh" wrap=false
#!/bin/sh

gdb -ix gdb/.gdbinit 
  -ex 'set tdesc filename gdb/target.xml' 
  -ex 'target remote localhost:1234'
```

 gdb  `(c) continue` MBR 
 MBR 

> [!TIP]
>  gdb 
 gdb  32-bit  16-bit 
>
>  `-m
onitor stdio`  qemu 使 `x/10i $cs*16+$eip` 
>
> 
>
> ```asm showLineNumbers=false wrap=false ins={2}
> (qemu) x/10i $cs*16+$eip
> 0x000ffff0:  ea 5b e0 00 f0           ljmpw    $0xf000:$0xe05b
> 0x000ffff5:  30 36 2f 32              xorb     %dh, 0x322f
> 0x000ffff9:  33 2f                    xorw     (%bx), %bp
> 0x000ffffb:  39 39                    cmpw     %di, (%bx, %di)
> 0x000ffffd:  00 fc                    addb     %bh, %ah
> 0x000fffff:  00 00                    addb     %al, (%bx, %si)
> 0x00100001:  00 00                    addb     %al, (%bx, %si)
> 0x00100003:  00 00                    addb     %al, (%bx, %si)
> 0x00100005:  00 00                    addb     %al, (%bx, %si)
> 0x00100007:  00 00                    addb     %al, (%bx, %si)
> ```
>
>  bochs 16-bit 
 patch qemu qemu + gdb 
 `gdb`  16-bit 
 32-bit 
>
> [The only proper way to debug 16-bit code on Qemu+GDB](https://gist.gith
ub.com/Theldus/4e1efc07ec13fb84fa10c2f3d054dccd).

### Loader

 MBR  512 
 Loader


Loader  MBR  Loader 
 MBR  0 LBA  0  
1 ……
 2 
 1MB 0x0500~0x7BFF  0x7E00~0x9FBFF 
 Loader  GDT
使 Loader 

 Loader ……仿 Linux 
 0x90000 

 [AT Attachment with Packet Interface](http://ebook.pldw
orld.com/_eBook/ATA%20spec/ATA7_Spec.pdf).

……
 source code.  [](#) 


### 




 32-bit CPU  16-bit CPU8086 
 32-bit CPU 
 16-bit 32-bit.


 8086



1. 
2. 

3. 访
4. 访 64KB 
5. 
6.  20 线 1MB使

使
 1MB
 

CPU  32-bit 线线 32-bit $$2^
{32} =4294967296$$  4GB 
: 16-bit  4GB 

 16-bit  32-bit
访 4GB  0
便

 `
 (Global Description Table, GDT)` 
 8 
CPU GDT 
 `GDTR` 
 ` (Selector)` 西
 GDT 


- `Index`  GDT  13
-bits $$2^{13} =8192$$ 
- `TI (Table Indicator)` 使 0  GDT 
 1  LDT 
- `RPL (Requested Privilege Level)`  0123 

 GDT  `L (Long Mode)`  32-
bit  64-bit.
 [Global Descriptor Table](https://en.wikipedia.org/wi
ki/Segment_descriptor)


 CPU ……访
CPU 
 ` (Segment Descriptor Cache)` hidde
n part CPU CPU 
访


> [!TIP]
> 
 4 使

 CPU 




```asm
mov ax, 0x10
mov ds, ax
```



```asm
mov ax, ds
mov ds, ax
```

 CPU 使使 GDT/LDT 



 BXBP 32-bit  SIDI
 ESP  32-bit  16-bit  32-bit
 12
48

<s
>__</s>



-  GDT
-  A20 Gate
-  CR0  PE (Protection Enable)  1

 GitHub 
……/

# 

- **Mar 12, 2025** Yeeee!  Exordium <s>
……</s> BIOS  MBR 
- **Mar 17, 2025** TNND 使 GAS 
- **Mar 19, 2025**  Loader.
- **Mar 20, 2025** 使 I/O  in/out 
- **Mar 24, 2025** 使 NASM ……GDB 
- **Apr 2, 2025**  RAM 
- **Apr 13, 2025** 

# 

 **_2022.10 _**



##  0 

- **0.2 **

 $$4times 4times 4$$  $$4+4+4$$

- **0.15 **

>  esp  esp  esp 





##  1 

- **1.3 宿**

>  -lpthread  vim  makefilevim  Linux 
vim Makefile 

 typo vim  makefile vim  makefile

##  2  MBR 

- **2.2 BIOS**

 typo

|   |   |  |                                                    
                                                                                
           |
| ----- | ----- | ---- | -------------------------------------------------------
--------------------------------------------------------------------------------
---------- |
| FFFF0 | FFFFF | 16B  | BIOS  BIOS  6
40KB  16  jmp 
F000:E05B |

 64KB  640KB

|   |   |  |                                                    
                                                                                
          |
| ----- | ----- | ---- | -------------------------------------------------------
--------------------------------------------------------------------------------
--------- |
| FFFF0 | FFFFF | 16B  | BIOS  BIOS  6
4KB  16  jmp F
000:E05B |

##  3  MBR

- **3.1.3  vstart**

code..startsection..start

- **3.2.2 **

 typoIP CS 
 CPU  CPU CPU 


- **3.2.4  CPU **

使 fs0x5678  gs 
 gs  fs 

- **3.2.7  call - 16 **


 call near prog_na
meprog_nameproc_name

 bochs 

 `> (markdown ci
te syntax)` 



- **3.3.1 CPU ——IO **

CPU ……


- **3.5.3 **

 Fea ture Fe
a tureFeature

- **3.6.1  MBR**

 MBR  512 ……

 eax  eax  LOADER_START_ S
ECTOR 0x2LOADER_START_ SECTORLOADER_STAR
T_SECTOR

 16 访 64KB  16 
 64KB访 1MB 


- **3.6.2 **

……
 xD

##  4 

- **4.2.1 **

 64  64 
 64 8 


使CPU 访
 16 
 4 
访 GDT
 [](#) 

- **4.3.1 **

访:……


- **4.3.5 **

 4-2  27 

```asm wrap=false showLineNumbers=false
DESC_VIDEO_HIGH4 equ (0x00 << 24) + DESC_G_4K + DESC_D_32 + 
DESC_L + DESC_AVL + DESC_LIMIT_VIDEO2 + DESC_P + 
DESC_DPL_0 + DESC_S_DATA + DESC_TYPE_D ATA + 0x00
```

`DESC_TYPE_D ATA`  `DESC_TYPE_DATA`.

 4-2 13 

```asm wrap=false showLineNumbers=false
DESC_LIMIT_VIDEO2 equ 0000_000000000000000b
```

 `0xb8000`

```asm wrap=false showLineNumbers=false
DESC_LIMIT_VIDEO2 equ 0000_0000000000001011b
```

##  5 

- **5.2.5 **

 5-4  152~153  GDT