โ๏ธ check_command: ๋ถ๋ถ์ด์ด์ ์ค๋ช
ํฉ๋๋ค.
I am explaining this because it relates to the check_command: section.
— ์
๋ ฅ๋ ๊ธ์๊ฐ ์์ ์์ผ๋ฉด ํ๋กฌํํธ ๋ผ๋ฒจ๋ก ์ด๋ํฉ๋๋ค.
If no text has been entered, the focus moves to the prompt label.
— cmp cx, 0 ์
๋ ฅ๋๊ธ์๊ฐ 0๊ฐ์ธ ๊ฒฝ์ฐ promt๋ก ์ ํํฉ๋๋ค.cmp cx, 0: If the number of entered characters is zero, the program jumps to the prompt.
cmp cx, 0
je prompt
— help๋ช ๋ น์ด / help command
1) ์์ค ํฌ์ธํฐ ๋ ์ง์คํฐ(SI)์ ์ฌ์ฉ์๊ฐ ํค๋ณด๋๋ก ์
๋ ฅํ ๋ฌธ์์ด์ด ๋ด๊ธด ๋ฒํผ(cmd_buffer)์ ์์ ์ฃผ์๋ฅผ ๋ฃ์ต๋๋ค.
Load the starting address of the buffer (cmd_buffer)โwhich contains the string entered by the user via the keyboardโinto the source pointer register (SI).
mov si, cmd_buffer
2) ๋ชฉ์ ์ง ํฌ์ธํฐ ๋ ์ง์คํฐ(DI)์ cmd_help๋ณ์์ ์์ ์ฃผ์๋ฅผ ๋ฃ์ต๋๋ค.
Load the starting address of the cmd_help variable into the destination pointer register (DI).
mov di, cmd_help
3)cmd_buffer์ ์ฃผ์๊ฐ๊ณผ cmd_help์ ์ฃผ์๊ฐ์ ๋น๊ตํฉ๋๋ค.
Compare the address of cmd_buffer with the address of cmd_help.
call compare_string
4)๊ฐ์ผ๋ฉด do_help๋ก ์ ํํฉ๋๋ค.
If they match, it jumps to do_help.
je do_help
5)compare_string ํจ์ / compare_string function
compare_string:
.loop:
mov al, [si]
mov bl, [di]
cmp al, bl ; ๋ ๊ธ์๊ฐ ๊ฐ์์ง ๋น๊ต
; Compare if two characters are equal
jne .not_equal ; ๋ค๋ฅด๋ฉด ํ์ถ
; If not equal, escape
cmp al, 0 ; ๋ฌธ์์ด์ด ๋๋ฌ๋์ง ํ์ธ (๋ ๋ค 0์ธ ์ํฉ)
; Check if the string ended (Both are 0 case)
je .equal ; ๋๋ฌ์ผ๋ฉด ์๋ฒฝํ ์ผ์น
; If ended, perfect match
inc si
inc di
jmp .loop
.not_equal:
clc ; Carry Flag ํด๋ฆฌ์ด (๊ฐ์ง ์์ ํ์, ์ต์ง flag ์ธํ
์ฉ์ผ๋ก cmp ํ์ฉ)
; Clear Carry Flag (Indicate not equal, use cmp for forced flag setting)
mov al, 1
cmp al, 0 ; Zero Flag๋ฅผ 0์ผ๋ก ๋ง๋ค์ด '๋ค๋ฆ'์ ์๋ฆผ
; Set Zero Flag to 0 to notify 'Not Equal'
ret
.equal:
cmp al, 0 ; Zero Flag๋ฅผ 1๋ก ๋ง๋ค์ด '๊ฐ์'์ ์๋ฆผ (AL์ด 0์ด๋ฏ๋ก)
; Set Zero Flag to 1 to notify 'Equal' (Since AL is 0)
ret
5-1) ๊ฐ์ ๋น๊ต ๋ฃจํด / Value comparison routine
1.si์ ์
๋ ฅ ๋ฌธ์ ์์น ์ ์ฅ: ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๋ฌธ์์ด ๋ฒํผ(cmd_buffer)์ ์์ ์ฃผ์๋ฅผ si์ ๋ฃ์ต๋๋ค.
Store the input character location in SI: Load the starting address of the user-input string buffer (cmd_buffer) into SI.
โ
si(sourcd index)๋ ์ง์คํฐ๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ค๋ฅธ ๋ ์ง์คํฐ๋ฅผ ์ฌ์ฉํด๋ ๋์ง๋ง
๊ทธ๋ผ์๋ ๋ถ๊ตฌํ๊ณ si๋ฅผ ๊ตณ์ด ์ ํํด์ ์ด ์ด์ ๋ ๋์ค์ ์ฝ๋๋ฅผ lodsb ํํ๋ก ์
๊ทธ๋ ์ด๋ํ๊ธฐ ํธํ๊ฒ ๋ง๋ค๊ธฐ ์ํด์์
๋๋ค.
You could use a different register instead of the SI (Source Index) register, but the reason I specifically chose SI is to make it easier to upgrade the code to use the `lodsb` instruction later.
lodsb ๋ช
๋ น์ด๋ CPU๊ฐ ๋ฌด์กฐ๊ฑด si ๋ ์ง์คํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ณณ์์ ๊ฐ์ ์ฝ์ด์ค๋๋ก ํ๋ ๋ช
๋ น์ด ์
๋๋ค.
The `lodsb` instruction causes the CPU to unconditionally read a value from the memory location pointed to by the `si` register.
2.di์ ๋น๊ต ๋์ ์ฃผ์ ์ ์ฅ: ๋น๊ตํ ๋ช
๋ น์ด ๋ณ์(cmd_help)์ ์์ ์ฃผ์๋ฅผ di์ ๋ฃ์ต๋๋ค.
Store the address of the comparison target in DI: Load the starting address of the command variable to be compared (cmd_help) into DI.
3.al๊ณผ bl๋ก ๊ฐ ๋ถ๋ฌ์ค๊ธฐ:si๊ฐ ๊ฐ๋ฆฌํค๋ ์์น์ ๊ฐ(์
๋ ฅํ ์ฒซ ๊ธ์)์ al๋ก ๊ฐ์ ธ์ต๋๋ค.
Loading values โโinto AL and BL: The value at the location pointed to by SI (the first character entered) is loaded into AL.
4.di๊ฐ ๊ฐ๋ฆฌํค๋ ์์น์ ๊ฐ(์ ๋ต ์ฒซ ๊ธ์)์ bl๋ก ๊ฐ์ ธ์ต๋๋ค.
Load the value at the location pointed to by di (the first character of the correct answer) into bl.
5.cmp al, bl๋ก ์ง์ง ๋น๊ต: ๋ ๋ ์ง์คํฐ์ ๋ด๊ธด ์์ํ ๋ฐ์ดํฐ(๊ธ์)๋ฅผ ๋น๊ตํฉ๋๋ค.
A true comparison using `cmp al, bl`: This compares the raw data (characters) contained in the two registers.
5-2) ๊ฐ์ด ์๋ก ๋ค๋ฅด๋ฉด ํ์ถํ๊ธฐ
Exit if the values โโare different.
5-2-1.jne๋ Jump if Not Equal์ ์ฝ์๋ก ๋ฐ๋ก ์์ค์ ์๋ cmp al, bl ์ฐ์ฐ ๊ฒฐ๊ณผ ๋ ๊ฐ์ด ์๋ก ๋ค๋ฅผ ๋ .not_equal ๋ ์ด๋ธ๋ก ์ ํ(์ด๋)ํ๋ผ๋ ๋ช
๋ น์ด์
๋๋ค.jne stands for “Jump if Not Equal”; it is an instruction that jumps (moves) to the .not_equal label if the two values โโdiffer, based on the result of the cmp al, bl operation performed in the line immediately preceding it.
jne .not_equal
5-3) al๋ ์ง์คํฐ๋ฅผ 1๋ก ์
ํ
ํ๊ณ cmp๋ก ๋น๊ต๊ฒฐ๊ณผ 0์ด ์๋๋ฉด ์ ์ฅ๋ ๋ฉ์ธ๋ก์ง(do_help)๋ก ์ด๋ํฉ๋๋ค.
Set the AL register to 1, and if the comparison result from the cmp instruction is non-zero, jump to the stored main logic (do_help).
1์ ์
ํ
ํ๋ ์ด์ ๋ 1๋ก ์ด๊ธฐํํ์ง ์์ผ๋ฉด ๊ธฐ์กด์ ๊ฐ์ด ์ ์ง๋๊ณ ์๊ธฐ ๋๋ฌธ์ ํ๋ก๊ทธ๋จ ์ค๋์ ๋ฐฉ์ง๋ฅผ ์ํด์ ๊ฐ์ ๋ช
ํํ ์
ํ
ํฉ๋๋ค.
The reason for setting the value to 1 is to explicitly initialize it; without doing so, the existing value would remain, potentially causing the program to malfunction.
.not_equal:
clc ; Carry Flag ํด๋ฆฌ์ด (๊ฐ์ง ์์ ํ์, ์ต์ง flag ์ธํ
์ฉ์ผ๋ก cmp ํ์ฉ)
; Clear Carry Flag (Indicate not equal, use cmp for forced flag setting)
mov al, 1
cmp al, 0 ; Zero Flag๋ฅผ 0์ผ๋ก ๋ง๋ค์ด '๋ค๋ฆ'์ ์๋ฆผ
; Set Zero Flag to 0 to notify 'Not Equal'
ret
5-4) al๋ ์ง์คํฐ์ ๊ฐ์ ์ด๋ฏธ 0์ด๋ฏ๋ก ์ด์ ์ ์ ์ฅ๋ ๋ฉ์ธ๋ก์ง do_help๋ก ์ด๋ํฉ๋๋ค.
Since the value of the AL register is already 0, the execution jumps to the previously stored main logic, do_help.
zero flag๋ ๋น๊ต๊ฐ์ด 0๊ณผ ๊ฐ์ผ๋ฉด cpu ๋ด๋ถ์zero flag๋ ๊ฐ์ด 1์ด ๋ฉ๋๋ค.
The zero flag is a CPU flag that is set to 1 when the comparison value is equal to 0.
.equal:
cmp al, 0 ; Zero Flag๋ฅผ 1๋ก ๋ง๋ค์ด '๊ฐ์'์ ์๋ฆผ (AL์ด 0์ด๋ฏ๋ก)
; Set Zero Flag to 1 to notify 'Equal' (Since AL is 0)
ret
6) do_clear ํจ์ / do_clear function
6-1.16๋นํธ ๋ฐ์ด์ค์ค๋ ํ๋ฉด์ ์ง์ฐ๋ ๊ธฐ๋ฅ์ด ์์ด์ ํ๋ฉด์ ๋ฐ์ด ์ฌ๋ฆฌ๋ ๋ฐฉ์์ ์ฌ์ฉํฉ๋๋ค. ์๋๋ ์คํฌ๋กค ์
๊ธฐ๋ฅ ์ ํ์
๋๋ค.
Since the 16-bit BIOS lacks a screen-clearing function, it uses a method of shifting the screen upward. Shown below is the selection for the scroll-up function.
mov ah, 0x06 ; ์คํฌ๋กค ์
๊ธฐ๋ฅ
; Scroll up function
6-2. ๋ช ์ค์ ์ฌ๋ฆด์ง ์ง์ ํ์ง ์๊ณ 0์ ์ง์ ํ๋ฉด ๋ฌดํ๋๋ก ํ๋ฉด์ ์๋ก ๋ฐ์ด์ฌ๋ฆฝ๋๋ค.
If you specify 0 instead of a specific number of lines, the screen scrolls upward indefinitely.
mov al, 0 ; 0 = ํ๋ฉด ์ ์ฒด ์ง์ฐ๊ธฐ
; 0 = Clear entire screen
6-3. ๋น๊ณต๊ฐ์ CLI๊ธฐ๋ณธ ์์์ธ ๋ฐํ์๊ณผ ๊ธ์์์ ์ง์ ํฉ๋๋ค.
Specify the default CLI background and text colors for the empty space.
์ด ๋ถ๋ถ์ด ์๋ค๋ฉด bh์ ์ด์ ์ ๋ค๋ฅธ ๊ฐ์ด ๋ค์ด์๋ค๋ฉด ํ๋ฉด์ ๋ค๋ฅธ ์์์ด ๋ํ ๋ ์ ์์ต๋๋ค.
Without this part, if bh already contains a different value, a different color might appear on the screen.
๊ทธ๋์ ์์์ ํ์คํ ์ง์ ํ๋ ์์ ์ฅ์น ์
๋๋ค.
So, it serves as a safeguard to explicitly specify the color.
mov bh, 0x07 ; ๋ฐํ์ ๊ฒ์ , ๊ธ์์ ํฐ์
; Background black, Foreground white
6-4.์ง์ฐ๊ณ ์ถ์ ํ๋ฉด์ ๋ค๋ชจ๋ ์์ญ(๋ฒ์)์ ์ง์ ํฉ๋๋ค.
Select the rectangular area (range) on the screen that you want to erase.
์๋ ๋ชจ๋ํฐ์ ํ
์คํธ ๋ชจ๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ๋ก 80์นธ(0~79), ์ธ๋ก 25์นธ(0~24)์ ๊ธ์ํ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค. ๋ฐ๋ผ์ (0,0)๋ถํฐ (24,79)๊น์ง ์ง์ ํ๋ค๋ ๊ฒ์ ๋ชจ๋ํฐ ์ ์ฒด ํ๋ฉด์ ๊ฝ ์ฐจ๊ฒ ์ง์ ํ๋ค๋ ๋ป์
๋๋ค.
The text mode of older monitors essentially consists of a character grid with 80 columns (0โ79) and 25 rows (0โ24). Therefore, specifying the range from (0, 0) to (24, 79) means designating the entire screen area.
mov ch, 0 ; ์ข์๋จ ํ (Top-left Row)
mov cl, 0 ; ์ข์๋จ ์ด (Top-left Column)
mov dh, 24 ; ์ฐํ๋จ ํ (Bottom-right row)
mov dl, 79 ; ์ฐํ๋จ ์ด (Bottom-right column)
6-5.๋ฐ์ด์ค์ค ์คํ / Run BIOS
int 0x10
6-6.์ปค์ ์์น ๋ณ๊ฒฝ๊ธฐ๋ฅ์ ์ง์ ์ ์ค์ ํฉ๋๋ค.
Configure the cursor position change function.
mov ah, 0x02
6-7.ํ์ด์ง ๋ฒํธ ์ง์ ,0๋ฒ ๋น๋์ค ํ์ด์ง๋ฅผ ์ง์ ํฉ๋๋ค.
Page number assignment: Specifies video page 0.
mov bh, 0
6-8.์ปค์๋ฅผ ์ด๋ํ ๋ชฉ์ ์ง ์ฃผ์๋ฅผ ์ง์ ํฉ๋๋ค. (0,0)์ ๋ชจ๋ํฐ ํ๋ฉด์ ๋งจ ์ผ์ชฝ ์ ๊ตฌ์์ ์๋ฏธํฉ๋๋ค.
Specify the destination address for moving the cursor. (0,0) refers to the top-left corner of the monitor screen.
mov dh,0์ ์ฒซ๋ฒ์งธ ํ์ ์๋ฏธํ๊ณ mov dl,0์ ์ข์ธก ์ฒซ๋ฒ์งธ ์ด์ ์๋ฏธํฉ๋๋ค.mov dh, 0 refers to the first row, and mov dl, 0 refers to the first column on the left.
mov dh, 0
mov dl, 0
6-9.promt๋ผ๋ฒจ๋ก ์ ํํฉ๋๋ค.
Jump to the ‘prompt’ label.
jmp prompt
โ๏ธ ๋๋จธ์ง ์ฝ๋๋ ๋ณ์์ค์ ๋ถ๋ถ๊ณผ ์ด๊ธฐํ ๋ถ๋ถ์
๋๋ค.
The remaining code consists of the variable declarations and the initialization section.
; ----------------------------------------------------
; ๋ฐ์ดํฐ ๋ฐ ๋ณ์ ์ ์ธ ์์ญ (์ ์ญ ๋ณ์)
; Data and variable declaration area (Global variables)
; ----------------------------------------------------
msg_welcome db '3. Hello from Sector 2! miniOS Shell Started.', 13, 10, 0
msg_prompt db 'miniOS> ', 0
msg_unknown db 'Unknown command! Type "help".', 13, 10, 0
msg_help_text db 'Available commands: help, clear', 13, 10, 0
; ๋น๊ตํ ๋ช
๋ น์ด ๊ธฐ์ค ๋ฌธ์์ด (์์)
; Reference command strings for comparison (Constants)
cmd_help db 'help', 0
cmd_clear db 'clear', 0
; ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๊ธ์๋ค์ ๋ด์ '๋น ๋ณ์ ๊ณต๊ฐ' (๋ฒํผ)
; 'Empty variable space' to hold characters entered by the user (Buffer)
; 16๋ฐ์ดํธ ํฌ๊ธฐ๋งํผ ๊ณต๊ฐ์ ๋ฏธ๋ฆฌ ํ๋ณดํด ๋ก๋๋ค.
; Pre-allocate space of 16 bytes.
cmd_buffer times 16 db 0
; 512๋ฐ์ดํธ ํฌ๊ธฐ ๋ง์ถ๊ธฐ
; Fill up to 512 bytes
times 512 - ($ - $$) db 0