[OS]miniOS-7(2)

👉️ 코드설명 /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
CSCode Segment실행 중인 코드
Running code
DSData Segment일반 데이터
General Data
ESExtra Segment추가 데이터(문자열 명령 등)
Additional data (string commands, etc.)
SSStack Segment스택 / Stack
FSExtra Segment운영체제가 자유롭게 사용
The operating system is free to use
GSExtra 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비트/32bit16비트/16bit8비트/8bit
EAXAXAH / AL
EBXBXBH / BL
ECXCXCH / CL
EDXDXDH / DL
ESISI
EDIDI
EBPBP
ESPSP

— 빨간색 픽셀을 반복해서 채웁니다.
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.

Leave a Reply