๐๏ธ ์ฝ๋์ค๋ช /Code Explanation
โ๏ธ 16๋นํธ๋ชจ๋์์ ์ฌ์ฉํ๋ ๋ ์ง์คํฐ๋ฅผ GDT์ ๋ฐ์ดํฐ๋ ์ง์คํฐ(0x10)๋ก ์ด๊ธฐํํ๋ ๋ถ๋ถ์
๋๋ค.
This section initializes the registers used in 16-bit mode with the GDT data segment selector (0x10).
bits 32
protected_start:
; 32๋นํธ์ฉ ๋ฐ์ดํฐ ์ธ๊ทธ๋จผํธ ๋ ์ง์คํฐ๋ค ์ด๊ธฐํ (GDT์ 0x10 ์คํ์
์ฌ์ฉ)
; Initialize 32-bit data segment registers (Using 0x10 offset from GDT)
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
— 32๋นํธ ๋ชจ๋์์ ๋ฒ์ฉ๋ ์ง์คํฐ๋ 32๋นํธ์ฉ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
In 32-bit mode, 32-bit general-purpose registers can be used.
— ํ์ง๋ง ์ธ๊ทธ๋จผํธ ๋ ์ง์คํฐ(CS, DS, ES, FS, GS, SS)๋ ํญ์ 16๋นํธ์
๋๋ค.
However, the segment registers (CS, DS, ES, FS, GS, SS) are always 16-bit.
— 32๋นํธ๋ชจ๋์์ ์ธ๊ทธ๋จผํธ ๋ ์ง์คํฐ๋ ์ฃผ์๊ฐ ์๋๋ผ GDT์ ์ ํ์๊ฐ ์ ์ฅ๋ฉ๋๋ค.
In 32-bit mode, segment registers store GDT selectors rather than addresses.
— GDT์ ์ ํ์๋ 16๋นํธ์
๋๋ค. ๊ทธ๋์ ์ธ๊ทธ๋จผํธ ๋ ์ง์คํฐ๋ 16๋นํธ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
The GDT selector is 16 bits; therefore, segment registers also use 16 bits.
— ax๋ ๋ฒ์ฉ๋ ์ง์คํฐ(General purpose register)์
๋๋ค.
ax is a general-purpose register.
— ๊ฐ ์ธ๊ทธ๋จผํธ ๋ ์ง์คํฐ์ ์ญํ
The role of each segment register
| ๋ ์ง์คํฐ / register | ์๋ฏธ / meaning | ์ผ๋ฐ์ ์ธ ์ฉ๋ / General Use |
|---|---|---|
| CS | Code Segment | ์คํ ์ค์ธ ์ฝ๋ Running code |
| DS | Data Segment | ์ผ๋ฐ ๋ฐ์ดํฐ General Data |
| ES | Extra Segment | ์ถ๊ฐ ๋ฐ์ดํฐ(๋ฌธ์์ด ๋ช
๋ น ๋ฑ) Additional data (string commands, etc.) |
| SS | Stack Segment | ์คํ / Stack |
| FS | Extra Segment | ์ด์์ฒด์ ๊ฐ ์์ ๋กญ๊ฒ ์ฌ์ฉ The operating system is free to use |
| GS | Extra Segment | ์ด์์ฒด์ ๊ฐ ์์ ๋กญ๊ฒ ์ฌ์ฉ The operating system is free to use |
— ๋์ค์ ์ด์์ฒด์ ๊ฐ ๋ฐ์ ํ๋ฉด FS๋ GS๋ฅผ ์ค๋ ๋ ์ ๋ณด๋ CPU๋ณ ๋ฐ์ดํฐ ๋ฑ ํน์ํ ์ฉ๋๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
As the operating system evolves, FS or GS can be used for special purposes, such as thread information or CPU-specific data.
— ์ง๊ธ์ FS,GS๊ฐ ํน๋ณํ ๋ชฉ์ ์ด ์๊ธฐ ๋๋ฌธ์ FS์ GS๋ ๋ชจ๋ 0x10(๋ฐ์ดํฐ ์ธ๊ทธ๋จผํธ) ์ผ๋ก ์ด๊ธฐํ ํด๋ก๋๋ค.
Since FS and GS currently serve no specific purpose, they are both initialized to 0x10 (data segment).
โ๏ธ ํ๋ฉด์ ๋นจ๊ฐ์ ์ฌ๊ฐํ์ ๊ทธ๋ฆฝ๋๋ค.
Draw a red rectangle on the screen.
— ๋น๋์ค ์นด๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
It uses video card memory.
mov edi, 0xA0000 ; ๋น๋์ค ๋ฉ๋ชจ๋ฆฌ ์์ ๋ฌผ๋ฆฌ ์ฃผ์
; Video memory starting physical address
1) 0xA0000๋ ๋น๋์ค ์นด๋ ๋ฉ๋ชจ๋ฆฌ(Video RAM, VRAM)์ ์ฃผ์์
๋๋ค.
0xA0000 is the address for video card memory (Video RAM, VRAM).
2)์ด ๋ชจ๋์์๋ ๋น๋์ค ๋ฉ๋ชจ๋ฆฌ๊ฐ 0xA0000๋ถํฐ ์์ํ๋๋ก ์ ํด์ ธ ์์ต๋๋ค.
In this mode, the video memory is set to start at 0xA0000.
3)mov edi, 0xA0000์ฒ๋ผ edi์ ๋น๋์ค๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ์ ์ฅํ๋ ์ด์ ๋ edi๋ฅผ ํฌ์ธํฐ ์ฒ๋ผ ์ฌ์ฉํ๊ธฐ ์ํด์์
๋๋ค.
The reason for storing the video memory address in ediโsuch as in mov edi, 0xA0000โis to use edi as a pointer.
4) ์๋์ ์ฝ๋๋ฅผ ์คํํ๋ฉด cpu๋ 0xA0000 ์ฃผ์์ 12๋ฅผ ๊ธฐ๋ก ํฉ๋๋ค.
When the code below is executed, the CPU writes 12 to the address 0xA0000.
4-1) 1ํฝ์
์ 1๋ฐ์ดํธ์
๋๋ค.ํ๋ฉด์ ์ฒซ๋ฒ์งธ ํฝ์
์ด ๋นจ๊ฐ์์ผ๋ก ๋ฐ๋๋๋ค.
One pixel is one byte. The first pixel on the screen changes to red.
4-2) 32๋นํธ์์๋ ํด์๋๊ฐ ๊ทธ๋๋ก์ธ ์ด์ ๋ ๋ถํธ๋ก๋์์ BIOS๊ฐ VGA๋ฅผ ์ด๋ฏธ Mode 13h(320ร200, 256์)๋ก ์ค์ ํด ๋์๊ธฐ ๋๋ฌธ์
๋๋ค.(mov al, 0x13)
The reason the resolution remains unchanged in 32-bit mode is that the BIOS had already set the VGA to Mode 13h (320ร200, 256 colors) during the bootloader stage (mov al, 0x13).
boot.asm
mov ah, 0x00
mov al, 0x13
int 0x10
4-3) ์ด์์ฒด์ ๊ฐ ์์ ํ ์์ฑ๋ ํ์ ๊ทธ๋ํฝ์นด๋์ ๋ ์ง์คํฐ๋ฅผ ํตํด์ ํด์๋๋ฅผ ์์ ํ 1080p๋ฑ์ผ๋ก ์ค์ ํ ์ ์์ต๋๋ค.
Once the operating system has fully loaded, you can configure the resolutionโsuch as 1080pโvia the graphics card’s registers.
mov edi, 0xA0000
mov byte [edi], 12
— ํ๋ฉด์ ํ์๋ ์ฌ๊ฐํ์ ์์ ์์น๋ 16100์
๋๋ค.
The starting position of the rectangle to be displayed on the screen is 16100.
add edi, 16100
1)๋น๋์ค ๋ฉ๋ชจ๋ฆฌ๋ 1์ฐจ์(์ผ๋ ฌ)๋ก ์ ์ฅ๋์ด ์๊ณ , ์ฐ๋ฆฌ๊ฐ 320ํฝ์
์ ํ ์ค์ด๋ผ๊ณ ์ฝ์ํด์ ๊ณ์ฐํฉ๋๋ค.
Video memory is stored in a one-dimensional (linear) format, and calculations are performed based on the convention that a single line consists of 320 pixels.
2) VGA๊ฐ 320๊ฐ์ฉ ๋์ด์ ํ๋ฉด์ ํ์ํฉ๋๋ค.
The VGA displays the image on the screen in chunks of 320.
3) ๊ทธ๋์ ๋ค์๊ณผ ๊ฐ์ด ๊ณ์ฐ๋ฉ๋๋ค.
Therefore, it is calculated as follows.
edi = 0xA0000 + (50 ร 320) + 100 = 0xA0000 + 16100
VRAM ์ฃผ์/Address = 0xA0000 + (Y ร 320) + X
3-1) ์์ ์์น๋ Y=50,X=100์
๋๋ค.
The starting position is Y=50, X=100.
3-2) edx์ ecx๋ ๋ฒ์ฉ๋ ์ง์คํฐ๋ก Y์ X์ ๋ฃจํ ์นด์ดํฐ๋ก ์ฌ์ฉ๋ฉ๋๋ค.
EDX and ECX are general-purpose registers used as loop counters for Y and X.
mov edx, 0 ; Y ๋ฃจํ ์นด์ดํฐ (์ธ๋ก 30์ค)
; Y loop counter (30 rows)
draw_32bit_row:
mov ecx, 0 ; X ๋ฃจํ ์นด์ดํฐ (๊ฐ๋ก 50์นธ)
; X loop counter (50 columns)
โ
x86์ 32๋นํธ ๋ฒ์ฉ ๋ ์ง์คํฐ
x86 32-bit general-purpose registers
๋ํ์ ์ผ๋ก 8๊ฐ์ ๋ฒ์ฉ ๋ ์ง์คํฐ๊ฐ ์์ต๋๋ค.
There are eight general-purpose registers.
| 32๋นํธ/32bit | 16๋นํธ/16bit | 8๋นํธ/8bit |
|---|---|---|
| EAX | AX | AH / AL |
| EBX | BX | BH / BL |
| ECX | CX | CH / CL |
| EDX | DX | DH / DL |
| ESI | SI | – |
| EDI | DI | – |
| EBP | BP | – |
| ESP | SP | – |
— ๋นจ๊ฐ์ ํฝ์
์ ๋ฐ๋ณตํด์ ์ฑ์๋๋ค.
Repeatedly fill with red pixels.
draw_32bit_col:
mov byte [edi], 12 ; ์์ ๋ฒํธ ๋์
(12 = ๋นจ๊ฐ์)
; Assign color number (12 = Red)
inc edi ; ๋ค์ ํฝ์
์ฃผ์๋ก ์ด๋
; Move to the next pixel address
inc ecx
cmp ecx, 50
jl draw_32bit_col
1) jl์ Jump if Less์
๋๋ค.
jl stands for “Jump if Less.”
2) ecx๊ฐ์ด 50 ๋ณด๋ค ์์ผ๋ฉด draw_32bit_col๋ก ์ ํํฉ๋๋ค.
If the ecx value is less than 50, it jumps to draw_32bit_col.
3) ๊ฐ๋ก ๋ฐฉํฅ์ผ๋ก 50๊ฐ์ ํฝ์
์ ๊ทธ๋ฆด ๋๊น์ง ๋ฐ๋ณตํ๋ ์กฐ๊ฑด๋ฌธ์
๋๋ค.
This is a conditional statement that repeats until 50 pixels are drawn horizontally.
— ์ค ๋ฐ๊ฟํด์ ์ธ๋ก ๋ฐฉํฅ์ผ๋ก 30๊ฐ์ ํฝ์
์ ๊ทธ๋ฆด๋ ๊น์ง ๋ฐ๋ณตํ๋ ์กฐ๊ฑด๋ฌธ์
๋๋ค.
This is a conditional statement that repeats the process, inserting line breaks and drawing 30 pixels vertically.
; ๊ฐ๋ก ํ ์ค์ ๋ค ๊ทธ๋ ธ์ผ๋ฉด, ๋ค์ ์ค์ ์์์ ์ผ๋ก EDI ํฌ์ธํฐ ๊ฑด๋๋ฐ๊ธฐ
; After drawing one row, skip EDI pointer to the start of the next row
; ํ ์ค์ด 320ํฝ์
์ธ๋ฐ ๋ฐฉ๊ธ 50ํฝ์
์ ์ ์งํ์ผ๋ฏ๋ก, ๋จ์ 270ํฝ์
๋งํผ ๋ํด์ค๋๋ค.
; Since a single line is 320 pixels and we have just advanced 50 pixels, we add the remaining 270 pixels.
add edi, 270
inc edx
cmp edx, 30
jl draw_32bit_row
; ์์
์๋ฃ ํ ๋ฌดํ ๋๊ธฐ
; Infinite loop after work completion
jmp $
โ
๋ค์ ํฌ์คํธ์์ ๊ณ์ ๋ฉ๋๋ค.
To be continued in the next post.