[OS]miniOS-Text-1

๐Ÿ‘‰๐Ÿป ์ด ๋ถ€๋ถ„์€ ๋ฏธ๋‹ˆOS ํ…์ŠคํŠธ ๋ชจ๋“œ ์‹œ์ž‘ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
This section marks the beginning of the MiniOS text mode.

๐Ÿ‘‰๐Ÿป 32๋น„ํŠธ ๋ณดํ˜ธ ๋ชจ๋“œ ์ง„์ž… ๋ฐ C์–ธ์–ด kernel_maiin()ํ•จ์ˆ˜๋กœ๋“œ ๊นŒ์ง€ ์ž…๋‹ˆ๋‹ค.
This covers the transition into 32-bit protected mode and the loading of the C-language kernel_main() function.

๐Ÿ‘‰๐Ÿป32๋น„ํŠธ ๋ณดํ˜ธ ๋ชจ๋“œ์—์„œ๋Š” ๋žจ์„ 4๊ธฐ๊ฐ€๊นŒ์ง€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
In 32-bit protected mode, up to 4 GB of RAM can be used.

๐Ÿ“boot.asm

โœ”๏ธ ๋””์Šคํฌ์ฝ๊ธฐ / Disk read

    mov ah, 0x02        ; ๋””์Šคํฌ ์ฝ๊ธฐ ๊ธฐ๋Šฅ / Disk reading capability
    mov al, 12          ; ์ฝ์–ด์˜ฌ ์„นํ„ฐ ๊ฐœ์ˆ˜(12๊ฐœ) / Number of sectors to read (12)
    mov ch, 0           ; ์‹ค๋ฆฐ๋” 0 / Cylinder 0
    mov cl, 2           ; ์‹œ์ž‘ ์„นํ„ฐ 2 / Starting sector 2
    mov dh, 0           ; ํ—ค๋“œ 0 / Head 0
    mov bx, 0x8000      ; ๋ฉ”๋ชจ๋ฆฌ 0x8000์— ๋กœ๋“œ / Loaded into memory 0x8000
    int 0x13

1)์‹ค์ œ๋กœ ํ”Œ๋กœํ”ผ ๋””์Šคํฌ๋ฅผ ์ฝ์–ด ๋ณด๋Š” ๋ถ€๋ถ„์ž…์ด๋‹ค.
This is the part where the floppy disk is actually read.

โœ”๏ธ sector2.asm์œผ๋กœ ์ ํ”„ํ•ด์„œ ์ฝ”๋“œ ์‹คํ–‰
Jump to sector2.asm and execute the code

 jmp 0x0000:0x8000

โœ”๏ธ ๋ถ€ํŠธ ์‹œ๊ทธ๋‹ˆ์ฒ˜
Boot signature

; 1๋ฒˆ ์„นํ„ฐ(MBR) ๋นˆ ๊ณต๊ฐ„์„ 0์œผ๋กœ ์ฑ„์šฐ๊ณ  ๋ถ€ํŠธ ์‹œ๊ทธ๋‹ˆ์ฒ˜ ์ถ”๊ฐ€
; Fill remaining space of sector 1 (MBR) with 0 and add boot signature
times 510 - ($ - $$) db 0
dw 0xaa55

1)์ด ์ฝ”๋“œ๊ฐ€ ์—†์œผ๋ฉด ๋ถ€ํŒ…ํ•  ์ˆ˜ ์—†์Œ / Without this code, it cannot boot.

๐Ÿ“ sector2.asm

โœ”๏ธ sector2.asm์ด 0x8000ย ์ด ์ฃผ์†Œ์—์„œ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” boot.asm์—์„œ ์ ํ”„ํ•˜๊ณ  ์ปดํŒŒ์ผ์‹œ ๋ง์ปค์˜ต์…˜์„ ํ†ตํ•ด์„œ ์ฃผ์†Œ๋ฅผ ์ง€์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
The reason sector2.asm can execute at the address 0x8000 is that the code jumps there from boot.asm, and the address was specified using linker options during compilation.

# 1. boot.asm์˜ ์ ํ”„ ์ฝ”๋“œ / Jump code in boot.asm
jmp 0x0000:0x8000 

# 2.์ปดํŒŒ์ผ์‹œ ๋‹จ์ผ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ๊ฒฐํ•ฉ์‹œ ์˜ต์…˜์œผ๋กœ ์ฃผ์†Œ๋ฅผ ์ง€์ •
#   Specify addresses as an option when combining into a single binary during compilation.
i686-elf-ld -m elf_i386 -Ttext 0x8000 -e start sector2.o kernel.o -o kernel.bin --oformat binary

1) -e start๋Š” sector2.asm์˜ global start ์œ„์น˜๋ฅผ ์˜๋ฏธํ•จ.
-e start refers to the global start location in sector2.asm.

โœ”๏ธ A20 ๊ฒŒ์ดํŠธ ํ™œ์„ฑํ™” (๋ฉ”๋ชจ๋ฆฌ 1MB ์ด์ƒ ์ ‘๊ทผ ํ—ˆ์šฉ)
A20 gate activation (enables access to memory beyond 1MB)

    in al, 0x92
    or al, 2
    out 0x92, al

1) in al, 0x92 :
๋งˆ๋”๋ณด๋“œ์˜ ์‹œ์Šคํ…œ ์ œ์–ด ํฌํŠธ ์ค‘ ํ•˜๋‚˜์ธ 0x92๋ฒˆ ํฌํŠธ(System Control Port A)์˜ ํ˜„์žฌ ํ•˜๋“œ์›จ์–ด ์ƒํƒœ ๊ฐ’์„ ์ฝ์–ด์™€์„œ al ๋ ˆ์ง€์Šคํ„ฐ์— ์ €์žฅ
Read the current hardware status value of port 0x92 (System Control Port A)โ€”one of the motherboard’s system control portsโ€”and store it in the AL register.

2)or al, 2:
al ๋ ˆ์ง€์Šคํ„ฐ์— ๋“ค์–ด ์žˆ๋Š” ๊ฐ’๊ณผ ์ˆซ์ž 2(์ด์ง„์ˆ˜๋กœ 00000010)๋ฅผ ๋น„ํŠธ OR ์—ฐ์‚ฐ
Perform a bitwise OR operation between the value in the al register and the number 2 (00000010 in binary).
0x92 ํฌํŠธ์˜ 2๋ฒˆ ๋น„ํŠธ๊ฐ€ ๋ฐ”๋กœ A20 ๊ฒŒ์ดํŠธ๋ฅผ ์ผœ๊ณ  ๋„๋Š” ์Šค์œ„์น˜ ์—ญํ• ์„ ํ•จ.
Bit 2 of port 0x92 acts as a switch for enabling and disabling the A20 gate.
or al, 2๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๊ธฐ์กด์— ์–ด๋–ค ๊ฐ’์ด ๋“ค์–ด ์žˆ์—ˆ๋“  ์ƒ๊ด€์—†์ด ๋‹ค๋ฅธ ๋น„ํŠธ๋Š” ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•œ ์ฑ„ 2๋ฒˆ ๋น„ํŠธ๋งŒ ๋ฌด์กฐ๊ฑด 1(์ฐธ)๋กœ ๋ฐ”๋€Œ๊ฒŒ ๋จ.
Performing an OR operation with 2 will force the 2nd bit to become 1 (true), regardless of its previous value, while leaving all other bits unchanged.

3)out 0x92, al
2๋ฒˆ ๋น„ํŠธ๊ฐ€ 1๋กœ ์กฐ์ž‘๋œ al ๋ ˆ์ง€์Šคํ„ฐ์˜ ๊ฐ’์„ ๋‹ค์‹œ 0x92๋ฒˆ ํฌํŠธ์— ์ถœ๋ ฅ(์ €์žฅ)
Output (store) the value of the AL registerโ€”with its 2nd bit set to 1โ€”back to port 0x92.
์ด ๋ช…๋ น์–ด๊ฐ€ ์‹คํ–‰๋˜๋Š” ์ˆœ๊ฐ„ ๋งˆ๋”๋ณด๋“œ์˜ ํ•˜๋“œ์›จ์–ด ํšŒ๋กœ๊ฐ€ ๋ฐ˜์‘ํ•˜์—ฌ A20 ๋ผ์ธ(์ฃผ์†Œ์„ )์ด ๊ฐ•์ œ๋กœ ์—ด๋ฆฌ๊ฒŒ ๋จ.
The moment this instruction is executed, the motherboard’s hardware circuitry responds, forcing the A20 line (address line) to open.

โœ”๏ธ GDT ๋กœ๋“œ
Loading the GDT

lgdt [gdt_pointer]

1)lgdt [gdt_pointer] (Load Global Descriptor Table)
๋ฉ”๋ชจ๋ฆฌ ์ ‘๊ทผ ๊ทœ์น™๊ณผ ๊ถŒํ•œ์„ ์ •์˜ํ•ด ๋‘” GDT(Global Descriptor Table)์˜ ์œ„์น˜์™€ ํฌ๊ธฐ ์ •๋ณด๋ฅผ CPU์—๊ฒŒ ์•Œ๋ ค์ฃผ๊ณ  ๋“ฑ๋กํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
This is an instruction that informs the CPU of the location and size of the Global Descriptor Table (GDT)โ€”which defines memory access rules and permissionsโ€”and registers it.

16๋น„ํŠธ ๋ชจ๋“œ์—์„œ๋Š” ๋‹จ์ˆœํžˆ ์„ธ๊ทธ๋จผํŠธ ร— 16 + ์˜คํ”„์…‹ ๊ณ„์‚ฐ์œผ๋กœ ์‹ค์ œ ๋ฌผ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ(RAM)์— ์ง์ ‘ ์ ‘๊ทผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์€ ๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•œ ๋ณดํ˜ธ์ฒด๊ณ„๊ฐ€ ์—†๊ณ  ์šด์˜์ฒด์ œ ์˜์—ญ์ด๋“  ์•„๋ฌด๊ณณ์—๋‚˜ ๋ฉ”๋ชจ๋ฆด ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์‹œ์Šคํ…œ์ด ๋‹ค์šด๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
n 16-bit mode, a method was used to access actual physical memory (RAM) directly by simply calculating “segment ร— 16 + offset.”
This approach lacks a memory protection mechanism, allowing memory to be accessed anywhereโ€”including within the operating system’s own spaceโ€”which can cause the system to crash.

32๋น„ํŠธ ๋ณดํ˜ธ๋ชจ๋“œ์—์„œ๋Š” ์ฒ ์ €ํ•œ ๊ทœ์น™๊ณผ ๊ฒ€์‚ฌ์ฒด๊ณ„๋ฅผ ๋„์ž…ํ•จ์œผ๋กœ CPU๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉ์‹œ ๋งค๋ฒˆ ์—„๊ฒฉํ•˜๊ฒŒ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.
In 32-bit protected mode, a system of rigorous rules and checks is implemented, ensuring that every instance of CPU memory access is strictly verified.

โœ”๏ธ GDT ํ…Œ์ด๋ธ” / GDT Table

; GDT(Global Descriptor Table) ์ •์˜
; Definition of Global Descriptor Table (GDT)
align 4
gdt_start:
    dd 0, 0                     ; 1. ๋„ ๋””์Šคํฌ๋ฆฝํ„ฐ / Null descriptor
    dw 0xFFFF, 0x0000           ; 2. ์ฝ”๋“œ ์„ธ๊ทธ๋จผํŠธ (0x08) / Code segment (0x08)
    db 0x00, 0x9A, 0xCF, 0x00
    dw 0xFFFF, 0x0000           ; 3. ๋ฐ์ดํ„ฐ ์„ธ๊ทธ๋จผํŠธ (0x10) / Data Segment (0x10)
    db 0x00, 0x92, 0xCF, 0x00
gdt_end:

gdt_pointer:
    dw gdt_end - gdt_start - 1
    dd gdt_start

1)๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ ๋ฐฐ์น˜ / Memory address layout
๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ 4๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์œ„์น˜์— GDT์ฃผ์†Œ ๋ฐฐ์น˜(๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„๋•Œ 4๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์ฝ์Œ)
CPU๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํšจ์œจ์ ์ด๊ณ  ๋น ๋ฅด๊ฒŒ ์ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ์ฃผ์†Œ๋ฐฐ์น˜
Place the GDT address at a memory location that is a multiple of 4 (since data is read in 4-byte units).
Address arrangement to enable the CPU to read memory efficiently and quickly

align 4

2)๋„ ๋””์Šคํฌ๋ฆฝํ„ฐ / Null descriptor
GDT์˜ ๋งจ ์ฒซ ๋ฒˆ์งธ ์นธ(์ธ๋ฑ์Šค 0๋ฒˆ)์€ ๋ฐ˜๋“œ์‹œ 0์œผ๋กœ ์ฑ„์›Œ์ง„ ๋นˆ ์นธ(Null)์ด์–ด์•ผ ํ•œ๋‹ค๋Š” CPU ๊ธฐ์ค€
CPU specification requiring the very first entry (index 0) of the GDT to be a null entry (filled with zeros).

dd 0, 0 

3)์ฝ”๋“œ ์„ธ๊ทธ๋จผํŠธ (์ด 8๋ฐ”์ดํŠธ) โ€” ์•„๋ž˜ ๋‘ ์ค„์ด ํ•ฉ์ณ์ ธ์„œ 1๊ฐœ ์„ธ๊ทธ๋จผํŠธ
Code segment (8 bytes total) โ€” the two lines below combine to form a single segment

— 0xFFFF = 65535 ,์ปดํ“จํ„ฐ๊ฐ€ ์‹คํ–‰ํ•  ๋ช…๋ น์–ด(์ฝ”๋“œ)๊ฐ€ ๋‹ด๊ธด ์˜์—ญ์˜ ๊ทœ์น™์„ ์ •์˜
Define the rules for the area containing the instructions (code) that a computer will execute.

— dw(Define word) 2๋ฐ”์ดํŠธ(16๋น„ํŠธ) ํฌ๊ธฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น
Allocate 2 bytes (16 bits) of data in memory

— db(Define byte) 1๋ฐ”์ดํŠธ(8๋น„ํŠธ) ํฌ๊ธฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น
Allocate 1 byte (8 bits) of data in memory

dw 0xFFFF, 0x0000           ; ์•ž 4๋ฐ”์ดํŠธ (๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ ๋ฐ ์‹œ์ž‘ ์ฃผ์†Œ ์ผ๋ถ€)
                            ; First 4 bytes (memory size and part of the start address)
db 0x00, 0x9A, 0xCF, 0x00   ; ๋’ค 4๋ฐ”์ดํŠธ (๊ถŒํ•œ ์†์„ฑ ๋ฐ ๋‚˜๋จธ์ง€ ์ฃผ์†Œ)
                            ; Last 4 bytes (permission attributes and remaining address)

4)๋ฐ์ดํ„ฐ ์„ธ๊ทธ๋จผํŠธ (์ด 8๋ฐ”์ดํŠธ) โ€” ์•„๋ž˜ ๋‘ ์ค„์ด ํ•ฉ์ณ์ ธ์„œ ๋˜ ๋‹ค๋ฅธ 1๊ฐœ ์„ธ๊ทธ๋จผํŠธ
Data segment (8 bytes total) โ€” the two lines below combine to form another single segment

— ์•ž 4๋ฐ”์ดํŠธ๋Š” ๊ฐ™๊ณ  ๋’ค 4๋ฐ”์ดํŠธ์ค‘ 0x92 ์ด๊ฒƒ๋งŒ ๋‹ค๋ฆ„.
The first 4 bytes are the same, and among the last 4 bytes, only the 0x92 value differs.

dw 0xFFFF, 0x0000           ; ์•ž 4๋ฐ”์ดํŠธ (๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ ๋ฐ ์‹œ์ž‘ ์ฃผ์†Œ ์ผ๋ถ€)
                            ; ; First 4 bytes (memory size and part of the start address)
db 0x00, 0x92, 0xCF, 0x00   ; ๋’ค 4๋ฐ”์ดํŠธ (๊ถŒํ•œ ์†์„ฑ ๋ฐ ๋‚˜๋จธ์ง€ ์ฃผ์†Œ)
                            ; Last 4 bytes (permission attributes and remaining address)
๐Ÿ’ก
์ฝ”๋“œ ์„ธ๊ทธ๋จผํŠธ : ๊ธฐ๊ณ„์–ด ์ฝ”๋“œ ์ €์žฅ๊ตฌ์—ญ(์–ด์…ˆ๋ธ”๋Ÿฌ + C์–ธ์–ด)
Code segment: A storage area for machine code (assembler + C language)

๋ฐ์ดํ„ฐ ์„ธ๊ทธ๋จผํŠธ(์Šคํƒ ์„ธ๊ทธ๋จผํŠธ) : ๋ณ€์ˆ˜์ €์žฅ๊ตฌ์—ญ
Data segment (stack segment): Storage area for variables

5)gdt_pointer

— GDTํ…Œ์ด๋ธ”์ด ์œ„์น˜ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์˜ ์‹œ์ž‘๊ณผ ๋์„ ๊ธฐ๋กํ•จ.
Records the start and end memory addresses where the GDT table is located.

— dd(Define Doubleword) 4๋ฐ”์ดํŠธ(32๋น„ํŠธ) ํฌ๊ธฐ์˜ ๋ฐ์ดํ„ฐ(์ •์ˆ˜๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ)๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น
Allocate 4-byte (32-bit) data (integers or memory addresses) in memory

gdt_pointer:
    dw gdt_end - gdt_start - 1
    dd gdt_start

1)gdt_end,gdt_start์™€ ๊ฐ™์€ ๋ผ๋ฒจ์„ ๋งŒ๋“ค๋ฉด ๋ผ๋ฒจ์— ํ˜„์žฌ ๋ฉ”๋ชจ๋ฆฌ ์œ„์น˜ ๊ฐ’์ด ์ €์žฅ๋จ
Creating labels such as gdt_end and gdt_start causes the current memory location value to be stored in those labels.

2)dw gdt_end – gdt_start – 1 ์ด์œ ๋Š” ์ฃผ์†Œ๊ฐ€ 1๋ฒˆ์ด ์•„๋‹Œ 0๋ฒˆ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋–„๋ฌธ์— -1์„ ํ•ด์ฃผ์–ด์•ผ ์ •ํ™•ํ•œ ์ฃผ์†Œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•จ
The reason for dw gdt_end - gdt_start - 1 is that addresses begin at 0 rather than 1; therefore, subtracting 1 is necessary to obtain the correct address.

# ์ •ํ™•ํ•œ ์ฃผ์†Œ๊ฐ€ ์•„๋‹˜ / Not an accurate address
(0x8000 + gtd_end) - (gdt_start) = 24

# ์ •ํ™•ํ•œ ์ฃผ์†Œ / Exact address
(0x8000 + gtd_end) - (gdt_start) - 1 = 23
๐Ÿ’ก 
sector2.asm์€ ๋ฉ”๋ชจ๋ฆฌ์˜ 0x8000๋ฒˆ์ง€ ์ž…๋‹ˆ๋‹ค.(๋ง์ปค์—์„œ ์ง€์ •ํ•จ.)
sector2.asm is located at memory address 0x8000 (as specified in the linker).

์ด๋•Œ GDTํ…Œ์ด๋ธ”์€ ์œ„์˜ ์ฃผ์†Œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ˆซ์ž ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
At this point, the GDT table records numerical data into memory, starting from the aforementioned address and proceeding sequentially.

๊ธฐ๋ก์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
The order of the records is as follows.

๋„๋””์Šคํฌ๋ฆฝํ„ฐ,์ฝ”๋“œ์„ธ๊ทธ๋จผํŠธ,๋ฐ์ดํ„ฐ์„ธ๊ทธ๋จผํŠธ gdt_pointer์˜ dw,dd๋ถ€๋ถ„
The `dw` and `dd` components of the `gdt_pointer` for the null descriptor, code segment, and data segment.

โœ”๏ธ32 ๋น„ํŠธ ๋ณดํ˜ธ ๋ชจ๋“œ ์ง„์ž… / Entering 32-bit protected mode

jmp 0x08:protected_start

โœ”๏ธC์–ธ์–ด์‹คํ–‰ / Executing C code

— C์–ธ์–ด ํ•จ์ˆ˜๋ฅผ ์–ด์…ˆ๋ธ”๋Ÿฌ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ / Importing C Functions into Assembly

extern kernel_main 

— ๋ถˆ๋Ÿฌ์˜จ C์–ธ์–ด ํ•จ์ˆ˜ ์‹คํ–‰ / Execute the imported C function

call kernel_main

โœ”๏ธ ๋ฐ์ดํ„ฐ ์„ธ๊ทธ๋จผํŠธ ๋ ˆ์ง€์Šคํ„ฐ ์ดˆ๊ธฐํ™” / Initialization of data segment registers

    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax

1)GDTํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ์„ธ๊ทธ๋จผํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ ˆ์ง€์Šคํ„ฐ ์ดˆ๊ธฐํ™” 0x10(=16)
Initializing registers to use data segments from the GDT table

2)์ฝ”๋“œ ์„ธ๊ทธ๋จผํŠธ๋Š” 0x08
The code segment is 0x08

โœ”๏ธ 32๋น„ํŠธ CPU์˜ ์Šคํƒ ํฌ์ธํ„ฐ ๋ ˆ์ง€์Šคํ„ฐ(esp)์— ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ 0x90000์„ ๋Œ€์ž…
Assign memory address 0x90000 to the stack pointer register (esp) of a 32-bit CPU

mov esp, 0x90000

1)์•ž์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ(ํŠนํžˆ C์–ธ์–ด ์ปค๋„)์ด ์‚ฌ์šฉํ•  ์Šคํƒ(Stack)์˜ ์œ„์น˜๋ฅผ ์ง€์ •
Specifies the location of the stack that the program (especially the C language kernel) will use in the future.

2)์Šคํƒ์˜ ์ฃผ์†Œ๋ฅผ 0x90000๋กœ ์ง€์ •ํ•ด์„œ ์ปค๋„ ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š” 0x8000์™€ ๊ฒน์น˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉ๊ฐ€๋Šฅ
By setting the stack address to 0x90000, it can be used without overlapping with the 0x8000 address where the kernel code resides.

3)C์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ ์Šคํƒ๊ณต๊ฐ„์„ ์„ค์ •ํ•ด์•ผํ•จ
To use the C language, you must configure stack space.

๐Ÿ“kernel.c

โœ”๏ธ ๋น„๋””์˜ค ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ ์ง€์ • / Video Memory Addressing (0xB8000)

 volatile char *vram = (volatile char *)0xB8000;

1)VGA ํ…์ŠคํŠธ ๋ชจ๋“œ ๋น„๋””์˜ค ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ
VGA text mode video memory address

0xB8000

2)volatile
์™ธ๋ถ€ ์š”์ธ(ํ•˜๋“œ์›จ์–ด ๋“ฑ)์— ์˜ํ•ด ๊ฐ’์ด ๋ณ€ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋งค๋ฒˆ ์ง„์งœ ๋ฉ”๋ชจ๋ฆฌ์— ์ง์ ‘ ์ ‘๊ทผ ํ•˜๋„๋กํ•˜๋Š” ์ตœ์ ํ™” ๋ฐฉ์ง€ ๊ฒฝ๊ณ 
A warning to prevent optimization, ensuring that the value is accessed directly from actual memory every time, as it may be altered by external factors (such as hardware).

ํ”„๋กœ๊ทธ๋žจ ๋ฐ”๊นฅ์˜ ํ•˜๋“œ์›จ์–ด์™€ ์ง์ ‘ ์†Œํ†ตํ•˜๋Š” ๋ชจ๋“  ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉ
Essentially used in all memory spaces that communicate directly with hardware outside the program

โœ”๏ธ ํ™”๋ฉด์— ๊ธ€์ž ์ถœ๋ ฅ
Display text on the screen

    const char *msg = "Shell Version = 1.0.2!";
    int i = 0;
    
    while (msg[i] != '\0') {
        vram[i * 2]     = msg[i];       // ์ง์ˆ˜ ๋ฐ”์ดํŠธ: ASCII ๋ฌธ์ž / Even-numbered bytes: ASCII characters
        vram[i * 2 + 1] = 0x0F;         // ํ™€์ˆ˜ ๋ฐ”์ดํŠธ: ํฐ์ƒ‰ ๊ธ€์ž, ๊ฒ€์€ ๋ฐฐ๊ฒฝ ์†์„ฑ / Odd-numbered bytes: white text, black background attribute
        i++;
    }

1)*msg๋Š” ๋ฌธ์ž์—ด ํฌ์ธํ„ฐ์ด์ง€๋งŒ C์–ธ์–ด์—์„œ ๋ฐฐ์—ด๊ณผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ(๋‚ด๋ถ€์ ์œผ๋กœ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•จ)
*Although msg is a string pointer, it can be used in the same way as an array in C (it functions identically under the hood).

2)๋น„๋””์˜ค ๋ฉ”๋ชจ๋ฆฌ ํ•˜๋“œ์›จ์–ด ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ์—์„œ ์ง์ˆ˜ ๋ฒˆ์ง€์—๋Š” ๊ธ€์ž,ํ™€์ˆ˜๋ฒˆ์ง€์—๋Š” ์ƒ‰์ƒ ์†์„ฑ์„ ์ €์žฅํ•จ.
In the video memory hardware architecture, even-numbered addresses store characters, while odd-numbered addresses store color attributes.

        vram[i * 2] = msg[i];     
        vram[i * 2 + 1] = 0x0F;   

3)VGAํ…์ŠคํŠธ ๋ชจ๋“œ์˜ ๊ธฐ๋ณธ์„ค์ •์€ ๊ฒ€์€ ๋ฐ”ํƒ•์— ํฐ์ƒ‰ ๊ธ€์ž
The default setting for VGA Text Mode is white text on a black background.

๐Ÿ“ ํ…Œ์ŠคํŠธ / Test

โœ”๏ธ ์ปดํŒŒ์ผ

% rm -f *.bin *.o *.img temp.bin sector2.bin
nasm -f bin boot.asm -o boot.bin
nasm -f elf32 sector2.asm -o sector2.o
i686-elf-gcc -m32 -c kernel.c -o kernel.o -nostdlib -fno-builtin -fno-stack-protector -fno-pic -fno-PIE
i686-elf-ld -m elf_i386 -Ttext 0x8000 -e start -o sector2.bin sector2.o kernel.o --oformat binary
cat boot.bin sector2.bin > temp.bin
dd if=temp.bin of=os.img bs=512 count=2880 conv=sync
qemu-system-x86_64 -drive file=os.img,format=raw,if=floppy -boot a

โœ”๏ธ ์Šคํฌ๋ฆฐ ์ƒท / ScreenShot

์Šคํฌ๋ฆฐ์ƒท/Screenshot

๋งˆํƒœ๋ณต์Œ 21์žฅ 22์ ˆ / Matthew 21: 22

“๋„ˆํฌ๊ฐ€ ๊ธฐ๋„ํ•  ๋•Œ์— ๋ฌด์—‡์ด๋“ ์ง€ ๋ฏฟ๊ณ  ๊ตฌํ•˜๋Š” ๊ฒƒ์€ ๋‹ค ๋ฐ›์œผ๋ฆฌ๋ผ ํ•˜์‹œ๋‹ˆ๋ผ”

“And everything you ask in prayer, believing, you will receive.”

Leave a Reply