[OS]mimiOS-1

๐Ÿ‘‰๐Ÿป ๋ฆฌ๋ˆ…์Šค๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ„๋‹จํ•œ OS๋ฅผ ๋งŒ๋“œ๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
To understand Linux, I am starting a project to build a simple operating system.

๐Ÿ‘‰๐Ÿป OS๊ฐœ๋ฐœ์„ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜๋Š” ์šด์˜์ฒด์ œ๋Š” ์šฐ๋ถ„ํˆฌ ๊ณ„์—ด์ธ Xubuntu๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
Xubuntu, a variant of Ubuntu, was used as the operating system for OS development.

๐Ÿ‘‰๐Ÿป CPU๋Š” x86 cpu์ž…๋‹ˆ๋‹ค.
The CPU is an x86 CPU.

๐Ÿ‘‰๐Ÿป1.ํ•„์ˆ˜ ๋„๊ตฌ ๋ฐ ๊ฐ€์ƒ ๋จธ์‹  ์„ค์น˜
Installing Essential Tools and Virtual Machines

โœ”๏ธ ํ„ฐ๋ฏธ๋„์„ ์—ด๊ณ  ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ปดํŒŒ์ผ๋Ÿฌ์™€ ์—๋ฎฌ๋ ˆ์ดํ„ฐ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
Open the terminal and enter the command below to install the compiler and emulator.

sudo apt update
sudo apt install nasm qemu-system-x86 build-essential gdb
  • nasm:
  • — ์–ด์…ˆ๋ธ”๋ฆฌ์–ด ์ฝ”๋“œ๋ฅผ ๊ธฐ๊ณ„์–ด๋กœ ๋ฒˆ์—ญํ•ด ์ฃผ๋Š” ์–ด์…ˆ๋ธ”๋Ÿฌ์ž…๋‹ˆ๋‹ค.
    — It is an assembler that translates assembly language code into machine code.
  • qemu-system-x86:
  • — ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  OS ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์‹คํ–‰ํ•ด ๋ณผ ๊ฐ€์ƒ ๋จธ์‹ (์—๋ฎฌ๋ ˆ์ดํ„ฐ)์ž…๋‹ˆ๋‹ค.
    — This is a virtual machine (emulator) where we can safely run the OS binary we created.
  • build-essential:
  • — ๋‚˜์ค‘์— C ์–ธ์–ด ์ปค๋„์„ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ gcc, make ๋“ฑ์˜ ์ปดํŒŒ์ผ ๋„๊ตฌ ๋ชจ์Œ์ž…๋‹ˆ๋‹ค.
    — This is a collection of compilation tools, such as gcc and make, that will be needed later when creating a C-language kernel.

๐Ÿ‘‰๐Ÿป2.์ฒซ ๋ฒˆ์งธ ๋ถ€ํŠธ๋กœ๋” ์ฝ”๋“œ ์ž‘์„ฑ
Writing the first bootloader code

โœ”๏ธ ์›ํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ์— ์ž‘์—… ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ , ํ…์ŠคํŠธ ์—๋””ํ„ฐ(Mousepad, VS Code ๋“ฑ)๋กœ boot.asm ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
Create a working folder in your desired directory and create a boot.asm file using a text editor (such as Mousepad or VS Code).

mkdir ~/miniOS && cd ~/miniOS
nano boot.asm

โœ”๏ธ ์—ด๋ฆฐ ํŒŒ์ผ์— ์•„๋ž˜์˜ 16๋น„ํŠธ x86 ๋ถ€ํŒ… ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ๋ถ™์—ฌ๋„ฃ๊ณ  ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
Copy and paste the 16-bit x86 boot code below into the open file and save it.

; BIOS๊ฐ€ ์ด ์ฝ”๋“œ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ 0x7C00์— ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
; The BIOS loads this code into memory at 0x7C00.
[org 0x7c00]

; 16๋น„ํŠธ ๋ฆฌ์–ผ ๋ชจ๋“œ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
; Executes in 16-bit real mode.
bits 16

start:
    ; BIOS์˜ 'ํ•œ ๊ธ€์ž ์ถœ๋ ฅ' ๊ธฐ๋Šฅ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
    ; Select the BIOS 'print single character' function.
    mov ah, 0x0e

    ; ํ™”๋ฉด์— ์ฐ์„ ๋ฌธ์ž 'X'
    ; Character 'X' to display on the screen
    mov al, 'X'

    ; BIOS ๋น„๋””์˜ค ์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
    ; Calls the BIOS video interrupt to output a character.
    int 0x10

    ; ํ˜„์žฌ ์œ„์น˜($)์—์„œ ๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ๋Œ๋ฉฐ ๋Œ€๊ธฐํ•ฉ๋‹ˆ๋‹ค.
    ; Waits in an infinite loop at the current location ($).
    jmp $

; 512๋ฐ”์ดํŠธ ์ค‘ ๋นˆ ๊ณต๊ฐ„์„ ๋ชจ๋‘ 0์œผ๋กœ ์ฑ„์›๋‹ˆ๋‹ค.
; Fill all remaining space within the 512 bytes with zeros.
times 510 - ($ - $$) db 0

; ์ด ๋””์Šคํฌ๊ฐ€ ๋ถ€ํŒ… ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ฆฌ๋Š” ๊ฐ’์„ ์ €์žฅ.
; Stores a value indicating that this disk is bootable.
dw 0xaa55

๐Ÿ‘‰๐Ÿป3.๋นŒ๋“œ ๋ฐ QEMU๋กœ ๋ถ€ํŒ… ์‹คํ–‰
Build and boot using QEMU

โœ”๏ธ ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜์˜ ๋‘ ์ค„์„ ์‹คํ–‰ํ•˜๋ฉด ์ฆ‰์‹œ OS๊ฐ€ ๊ฐ€์ƒ ๋จธ์‹ ์—์„œ ๊ตฌ๋™๋ฉ๋‹ˆ๋‹ค.
Running the two lines below in the terminal will immediately launch the OS on the virtual machine.

# 1. ์–ด์…ˆ๋ธ”๋ฆฌ์–ด ์ฝ”๋“œ๋ฅผ ์ˆœ์ˆ˜ํ•œ ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ(.bin)๋กœ ์ปดํŒŒ์ผ
# Compile assembly code into a pure binary file (.bin)
nasm -f bin boot.asm -o boot.bin

# 2. QEMU ์—๋ฎฌ๋ ˆ์ดํ„ฐ์— ์ด ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ๋””์Šคํฌ๋กœ ๋„ฃ์–ด ๊ฐ€์ƒ ์ปดํ“จํ„ฐ ๋ถ€ํŒ…
# Put this binary on a disk and boot the virtual machine in the QEMU emulator
qemu-system-x86_64 -drive format=raw,file=boot.bin

โœ”๏ธ ์‹คํ–‰ํ™”๋ฉด / Execution Screen

๐Ÿ‘‰๐Ÿป3.์ข…๋ฃŒํ•˜๊ธฐ / Exit

— ์ƒˆ๋กœ ๋งŒ๋“  ์šด์˜์ฒด์ œ๊ฐ€ ์‹คํ–‰๋˜๋ฉด QEMU๊ฐ€ ๋งˆ์šฐ์Šค ์ œ์–ด๊ถŒ์„ ๋…์ ํ•ด์„œ ๋งˆ์šฐ์Šค๊ฐ€ ์ž‘๋™ ์•ˆ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.(์ •์ƒ๋™์ž‘)
When the newly created operating system runs, QEMU may capture exclusive control of the mouse, causing it to stop working (this is normal behavior).

— ์ด๋Ÿฐ ๊ฒฝ์šฐ ํ‚ค๋ณด๋“œ์—์„œ ์•„๋ž˜ํ‚ค๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
In this case, you can exit the program by pressing the Down arrow key on your keyboard.

Ctrl+Alt+G

๐Ÿ‘‰๐Ÿป ์ฝ”๋“œ์„ค๋ช… / Code Explanation

โœ”๏ธ [org 0x7c00]

[org 0x7c00]์—์„œ [ ]์˜ ์˜๋ฏธ์™€ org ๋œป

1)org (Origin)๋Š” ์‹œ์ž‘์ ์ด๋ผ๋Š” ๋œป์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ(RAM)์— ๋ฐฐ์น˜๋  ํ”„๋กœ๊ทธ๋žจ์˜ ์ฃผ์†Œ์œ„์น˜๋ฅผ ์ปดํŒŒ์ผ๋Ÿฌ(nasm)์—๊ฒŒ ์•Œ๋ ค์ฃผ๋Š” ์ง€์‹œ์–ด์ž…๋‹ˆ๋‹ค.
org (Origin) signifies a starting point; it is a directive that informs the compiler (nasm) of the memory address where the program is to be placed.

2)NASM(Netwide Assembler)์—์„œ๋Š”[org 0x7c00]๋ฅผ org 0x7c00 ์ด๋ ‡๊ฒŒ ์จ๋„ ๋ฉ๋‹ˆ๋‹ค.
In NASM (Netwide Assembler), you can write [org 0x7c00] simply as org 0x7c00.

3)0x7c00๋Š” ๋ถ€ํŒ…์ „์šฉ ์ฃผ์†Œ์ž…๋‹ˆ๋‹ค.1981๋…„ IBM์ด ์ตœ์ดˆ์˜ ๊ฐœ์ธ์šฉ ์ปดํ“จํ„ฐ(IBM PC 5150)๋ฅผ ๋งŒ๋“ค ๋•Œ ๊ฐœ๋ฐœ์ž๋“ค์ด “๋ถ€ํŒ… ์ „์šฉ ์ฝ”๋“œ๋Š” ๋ฌด์กฐ๊ฑด RAM์˜ 0x7C00 ์ฃผ์†Œ์— ์˜ฌ๋ ค๋†“๊ณ  ์‹คํ–‰ํ•˜์ž”๊ณ  ๊ทœ์น™์„ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.
0x7c00 is a boot-specific address. When IBM created its first personal computer (the IBM PC 5150) in 1981, developers established a rule to always load and execute boot-specific code at the 0x7C00 address in RAM.

4)BIOS๋Š” ๋””์Šคํฌ(SSD, HDD, USB ๋“ฑ)์˜ ๋งจ ์ฒซ ๋ฒˆ์งธ ์„นํ„ฐ(512๋ฐ”์ดํŠธ)๋ฅผ ์ฝ์–ด์„œ ๋ฐฉ๊ธˆ ๋งํ•œ RAM์˜ 0x7C00 ์ฃผ์†Œ๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.(์—ฌ๊ธฐ์„œ๋Š” QEMU์•ˆ์˜ BIOS๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.)
The BIOS reads the very first sector (512 bytes) of a disk (such as an SSD, HDD, or USB drive) and copies it to address 0x7C00 in RAM (referring here to the BIOS within QEMU).

โœ”๏ธ bits 16

— ์ฒ˜์Œ์—๋Š” ๋ฌด์กฐ๊ฑด 16๋น„ํŠธ ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ C์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” bits 32๋กœ 32๋น„ํŠธ ๋ชจ๋“œ์ „ํ™˜์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
You start in 16-bit mode by default, and to use C, you must switch to 32-bit mode using the bits 32 directive.

โœ”๏ธ start:

— ๋ผ๋ฒจ์ž…๋‹ˆ๋‹ค. begin:์ด๋‚˜ main:๋“ฑ์œผ๋กœ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
This is a label. You can assign it a different name, such as begin: or main:.

— ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๊ณ  ๋ฐ˜๋ณต,์กฐ๊ฑด,์ ํ”„๋ฅผ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
It is used when code becomes lengthy and requires loops, conditional statements, and jumps.

โœ”๏ธ mov ah, 0x0e

— mov ah, 0x0e๋Š” AH ๋ ˆ์ง€์Šคํ„ฐ์— 0x0e๋ผ๋Š” ๊ฐ’์„ ๋„ฃ๋Š”(๋Œ€์ž…ํ•˜๋Š”) ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
mov ah, 0x0e is an instruction that loads (assigns) the value 0x0e into the AH register.

— AH๋Š” 16๋น„ํŠธ CPU์—์„œ ์‚ฌ์šฉํ•˜๋Š” AX ๋ ˆ์ง€์Šคํ„ฐ์˜ ์ƒ์œ„ 8๋น„ํŠธ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.

— AX = [AH : AL] (์ƒ์œ„ 8๋น„ํŠธ AH, ํ•˜์œ„ 8๋น„ํŠธ AL) –> 16๋น„ํŠธ
AH is the upper 8-bit portion of the AX register used in 16-bit CPUs.

— AH๋Š” CPU ๋ช…๋ น์–ด์— ๋”ฐ๋ผ ํŠน์ • ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ, ๊ธฐ๋Šฅ ๋ฒˆํ˜ธ ํ˜น์€ ์ƒํƒœ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ์šฉ๋„๋กœ ๋งŽ์ด ์“ฐ์ž…๋‹ˆ๋‹ค.
AH is frequently used to store function numbers or status values โ€‹โ€‹when performing specific functions based on CPU instructions.

mov ah, 0x0e(10์ง„์ˆ˜๋กœ 14๋ฒˆ)๋Š” BIOS ๋น„๋””์˜ค ์ธํ„ฐ๋ŸฝํŠธ์˜ ๋ฌธ์ž ์ถœ๋ ฅ ๊ธฐ๋Šฅ์„ ์„ ํƒํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
mov ah, 0x0e (decimal 14) is the code that selects the character output function of the BIOS video interrupt.

— ์ฆ‰, AH = 0x0e๋Š” BIOS ๋น„๋””์˜ค ์„œ๋น„์Šค ์ค‘์—์„œ ๋ฌธ์ž ์ถœ๋ ฅ ๊ธฐ๋Šฅ์„ ์ง€์ •ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
In other words, AH = 0x0e is the code that specifies the character output function within the BIOS video services.

— ์ดํ›„ AL ๋ ˆ์ง€์Šคํ„ฐ์— ์ถœ๋ ฅํ•  ๋ฌธ์ž๋ฅผ ๋‹ด๊ณ , int 0x10์„ ํ˜ธ์ถœํ•˜๋ฉด ์ปค์„œ ์œ„์น˜์— ๊ทธ ๋ฌธ์ž๊ฐ€ ํ™”๋ฉด์— ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
Afterward, if you place the character to be displayed into the AL register and call int 0x10, that character is displayed on the screen at the cursor’s position.

โœ”๏ธ mov al, ‘X’

— AL ๋ ˆ์ง€์Šคํ„ฐ์— ๋ฌธ์ž ‘X’๋ฅผ 1๋ฐ”์ดํŠธ ํฌ๊ธฐ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
Store the character ‘X’ in the AL register as a 1-byte value.

— ‘X’์˜ ASCII ์ฝ”๋“œ(16์ง„์ˆ˜ 0x58)๋ฅผ AL์— ์ €์žฅํ•˜์—ฌ ์ดํ›„ ๋ช…๋ น์–ด์—์„œ ์ด ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
Store the ASCII code for ‘X’ (hexadecimal 0x58) in AL so that this value can be used in subsequent instructions.

โœ”๏ธ int 0x10

— int์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
It refers to the ‘int’ interrupt.

— ์ปดํ“จํ„ฐ์—์„œ ์ธํ„ฐ๋ŸฝํŠธ๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„์„ ์ž ์‹œ ๋ฉˆ์ถ”๊ณ , ํŠน์ •ํ•œ ํ•˜๋“œ์›จ์–ด๋‚˜ ์†Œํ”„ํŠธ์›จ์–ด๊ฐ€ ๋ฏธ๋ฆฌ ์ •ํ•ด์ง„ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ž…๋‹ˆ๋‹ค.
In computing, an interrupt is a mechanism that temporarily pauses the flow of a program, allowing specific hardware or software to perform a predefined task.

— ์‹ค์ œ๋กœ ๋น„๋””์˜ค ์„œ๋น„์Šค๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‹ค์ œ๋กœ ‘X’๋ผ๋Š” ๋ฌธ์ž๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
It actually calls the video service and displays the character ‘X’ on the screen.

— ์ธํ„ฐ๋ŸฝํŠธ 0x10๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
Interrupt 0x10 handles the following functions.

ํ™”๋ฉด ๋ชจ๋“œ ์„ค์ • (ํ…์ŠคํŠธ/๊ทธ๋ž˜ํ”ฝ ๋ชจ๋“œ ์ „ํ™˜)
Screen Mode Setting (Switching between Text and Graphics Modes)

๋ฌธ์ž ๋˜๋Š” ๊ทธ๋ž˜ํ”ฝ ์ถœ๋ ฅ
Text or graphic output

ํ™”๋ฉด ํด๋ฆฌ์–ด, ์ปค์„œ ์œ„์น˜ ์กฐ์ ˆ
Clear screen, adjust cursor position

ํฐํŠธ ์„ค์ •, ํŒ”๋ ˆํŠธ ์กฐ์ • ๋“ฑ
Font settings, palette adjustments, etc.

โญ๏ธ mov ahmov alint ๋Š” ํ•˜๋‚˜์˜ ๋กœ์ง์œผ๋กœ ์ดํ•ดํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค.
You can understand mov ah, mov al, and int as a single piece of logic.

โœ”๏ธ jmp $

$๋Š” ์–ด์…ˆ๋ธ”๋ฆฌ์–ด์—์„œ ํ˜„์žฌ ๋ช…๋ น์–ด ์ฃผ์†Œ ๋˜๋Š” ํ˜„์žฌ ์œ„์น˜(์ฃผ์†Œ ๋ ˆ์ด๋ธ”)๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธฐํ˜ธ์ž…๋‹ˆ๋‹ค.
In assembly language, $ is a symbol representing the current instruction address or the current location (address label).

jmp $๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜์ง€ ์•Š๊ณ  ํŠน์ • ์œ„์น˜์—์„œ ๋ฌดํ•œํžˆ ๋จธ๋ฌด๋ฅด๋ฉด์„œ ๊ธฐ๋‹ค๋ฆฌ๋„๋ก ๋งŒ๋“œ๋Š” ์šฉ๋„๋กœ ์“ฐ์ž…๋‹ˆ๋‹ค.
jmp $ is used to make the program wait by remaining indefinitely at a specific location instead of terminating.

โœ”๏ธ times 510 – ($ – $$) db 0

— ๊ณ„์‚ฐ๋œ ํšŸ์ˆ˜๋งŒํผ ๋ฐ”์ดํŠธ(db) ๊ณต๊ฐ„์„ ์ „๋ถ€ 0์œผ๋กœ ์ฑ„์šฐ๋ผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.
This command fills the specified number of bytes (db) with zeros.

— 512๋ฐ”์ดํŠธ์—์„œ 510๋ฐ”์ดํŠธ๋Š” ๋ถ€ํŠธ๋กœ๋” ๋ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„์ž…๋‹ˆ๋‹ค.
Of the 512 bytes, 510 bytes are used to store the bootloader and data.

— ๋‚˜๋จธ์ง€ ๋งˆ์ง€๋ง‰ 2๋ฐ”์ดํŠธ๋Š” ๋ถ€ํŒ…๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํŒ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ์‹œ๊ทธ๋‹ˆ์ฒ˜๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ ๋ณดํ†ต 16์ง„์ˆ˜ 0xAA55๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.
The final two bytes serve as a signature to determine bootability and typically contain the hexadecimal value 0xAA55.

— ์ดˆ๊ธฐ IBM PC ํ˜ธํ™˜ ์ปดํ“จํ„ฐ์˜ ํ•˜๋“œ๋””์Šคํฌ ๋ฐ ํ”Œ๋กœํ”ผ๋””์Šคํฌ ๋“œ๋ผ์ด๋ธŒ๋Š” ์„นํ„ฐ ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์ผ์Šต๋‹ˆ๋‹ค.
Hard disk and floppy disk drives in early IBM PC-compatible computers read and wrote data in sector units.

— $๋Š” ํ˜„์žฌ ์œ„์น˜์˜ ์ฃผ์†Œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
$ represents the address of the current location.

— $$๋Š” ์‹œ์ž‘ ์œ„์น˜์˜์ฃผ์†Œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
$$ refers to the address of the starting position.

— ($ – $$)๋Š” (ํ˜„์žฌ์œ„์น˜ – ์‹œ์ž‘์œ„์น˜์ž…๋‹ˆ๋‹ค.)
($ – $$) represents (current position – starting position).

— ๊ฐ„๋‹จํ•œ ์˜ˆ๋กœ 10๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ–ˆ๋‹ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค.
As a simple example, if 10 bytes of data are stored, the calculation is performed as shown below.

0x7C10 โˆ’ 0x7C00 = 0x10(10์ง„์ˆ˜๋กœ 10 / 10 in decimal)

times N db 0์€ N๋งŒํผ 0 ๋ฐ”์ดํŠธ(๋นˆ ๊ณต๊ฐ„)๋ฅผ ์ฑ„์šฐ๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค.
times N db 0 means to fill N bytes with zeros (empty space).

โœ”๏ธ dw 0xaa55

— dw๋Š” “define word”์˜ ์•ฝ์ž๋กœ, 2๋ฐ”์ดํŠธ(16๋น„ํŠธ) ๋ฐ์ดํ„ฐ๋ฅผ ์„ ์–ธํ•˜๊ฑฐ๋‚˜ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
dw stands for “define word” and is used to declare or store 2-byte (16-bit) data.

— ๋ถ€ํŠธ ์„นํ„ฐ์˜ 0xaa55๊ฐ’์„ ๋งˆ์ง€๋ง‰์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
Write the value 0xaa55 to the end of the boot sector.

— ์ด ๊ฐ’์ด ์žˆ์–ด์•ผ BIOS๊ฐ€ ํ•ด๋‹น ์„นํ„ฐ๋ฅผ ๋ถ€ํŒ… ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค.
The BIOS requires this value to recognize the sector as bootable.

Leave a Reply