[OS] miniOS-4(2)

โœ”๏ธ 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

Leave a Reply