Basics: Command | Index | Basics: ECHO

Write Your Own OS from Scratch - Chapter 2. Basics

Basics: CLS

Now we are going to add another command to clear the screen.

The first thing we start with is the function to clear the screen. In demoos.asm add the following

;-------------------------------------------------------
; Clear Screen Command
; Input:
;      None
; Output:
;      AX is Destroyed
;-------------------------------------------------------
cls:
    mov    ah, 00Fh                                     ; Get video mode
    int    010h
    mov    ah, 000h                                     ; Set video mode
    int    010h                                         ;  this automatically clears the screen
    ret
		

Next we need the string to match it to.

clsstr:         db      "CLS", 0
		

Finally we test for the command in our main function

    mov    si, clsstr                                   ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdncls                                      ; Carry set if not equal
    call   cls                                          ; Call function
    jmp    main                                         ; We're done checking
cmdncls:
		

Then change:

version:        db      00Dh, 00Ah, "DEMO OS V0.05"     ; Message to be printed
                db      00Dh, 00Ah, '$'
		

to

version:        db      00Dh, 00Ah, "DEMO OS V0.06"     ; Message to be printed
                db      00Dh, 00Ah, '$'
		

demoos.asm

;=======================================================
; Demo OS - demoos.asm
; -----------------------------------------------------
; Written in NASM
; Written by Marcus Kelly
;=======================================================

    org     00000h                                      ; Assemble code for this offset

start:
    cli                                                 ; Turn off interrupts
    cld							; Make sure we are going forward
    mov     ax, cs                                      ; Set all segments to code segment
    mov     ds, ax                                      ; Make sure data segment is zero
    mov     es, ax                                      ; Make sure extra segment is zero
    mov     ss, ax                                      ; Make sure stack segment is zero
    mov     sp, 07C00h                                  ; Place stack ptr before boot sector
    sti                                                 ; Turn on interrupts

    call   intinit                                      ; Initialize interrupts
    call   ver

main:
    call   crlf                                         ; Print carriage return / line feed
    mov    ah, 002h                                     ; Service standard output
    mov    dl, '>'                                      ; Print '>'
    int    021h
    mov    dl, ' '                                      ; Print Space
    int    021h
    mov    ah, 00Ah                                     ; Service string input
    mov    dx, buffer                                   ; Set dx to our buffer
    int    021h
    call   crlf                                         ; Print carriage return / line feed

    mov    di, buffer+2                                 ; Point to the string we typed

    mov    si, verstr                                   ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdnver                                      ; Carry set if not equal
    call   ver                                          ; Call function
    jmp    main                                         ; We're done checking
cmdnver:

    mov    si, clsstr                                   ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdncls                                      ; Carry set if not equal
    call   cls                                          ; Call function
    jmp    main                                         ; We're done checking
cmdncls:

    mov    ah, 009h                                     ; Service Print String
    mov    dx, invalid                                  ; Invalid command
    int    021h                                         ; Our Service
    call   crlf                                         ; New line
    jmp    main                                         ; Repeat forever

;-------------------------------------------------------
; Compare Command
; Input:
;      DS:SI = Command String
;      ES:DI = Buffer String
; Output:
;      Carry set if not equal
;      Carry clear if equal
;-------------------------------------------------------
cmpcmd:
    push   ax                                           ; Save registers
    push   si
    push   di
cmpcmdnext:
    mov    al, [es:di]                                  ; Load char from command line 
    inc    di                                           ; Point to next character
    call   toup                                         ; Convert to uppercase
    mov    ah, [ds:si]                                  ; Load char to test against
    inc    si                                           ; Point to next character
    cmp    ah, al                                       ; Compare both characters
    jz     cmpcmdnext                                   ; If they are the same check next
    dec    si                                           ; Adjust to point to last char
    dec    di
    cmp    [ds:si], byte 0                              ; Our test command end should be 0
    jnz    cmpcmdne                                     ; If it isn't not our command
    cmp    [es:di], byte ' '                            ; Our input cmd might be space
    jz     cmpcmde                                      ; If it is this is our command
    cmp    [es:di], byte 00Dh                           ; Our input cmd might be CR
    jz     cmpcmde                                      ; If it is this is our command
    jmp    cmpcmdne                                     ; Otherwise not our command
cmpcmde:
    pop    di                                           ; Restore registers
    pop    si
    pop    ax
    clc                                                 ; Clear carry for match
    ret                                                 ; Return from subroutine
cmpcmdne:
    pop    di                                           ; Restore registers
    pop    si
    pop    ax
    stc                                                 ; Set carry if not a match
    ret                                                 ; Return from subroutine

;-------------------------------------------------------
; Version Command
; Input:
;      None
; Output:
;      AX, DX are Destroyed
;-------------------------------------------------------
ver:
    mov    ah, 009h                                     ; Service string output
    mov    dx, version                                  ; dx is set to offset version
    int    021h                                         ; Call the service
    ret

;-------------------------------------------------------
; Clear Screen Command
; Input:
;      None
; Output:
;      AX is Destroyed
;-------------------------------------------------------
cls:
    mov    ah, 00Fh                                     ; Get video mode
    int    010h
    mov    ah, 000h                                     ; Set video mode
    int    010h                                         ;  this automatically clears the screen
    ret

%include "io.asm"
%include "int.asm"

;-------------------------------------------------------
; Data Area
;-------------------------------------------------------

verstr:         db      "VER", 0
clsstr:         db      "CLS", 0

version:        db      00Dh, 00Ah, "DEMO OS V0.06"     ; Message to be printed
                db      00Dh, 00Ah, '$'

invalid:        db      00Dh, 00Ah, "Invalid Command"   ; Message to be printed
                db      00Dh, 00Ah, '$'

buffer:         db      64                              ; Buffer for string input
                db      0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
		

Modify the disk image

Now lets build our disk. If you are running the previous tutorial in VirtualBox Remember to remove the disk or close virtual box before assembling the disk image.

> nasm demoos.asm -o demoos.bin
> nasm osdisk.asm -o osdisk.img
		

Your osdisk.img can now be loaded with VirtualBox. The result should be as follows:

>
		
Basics: Command | Index | Basics: ECHO

Support This Project On Patreon

Copyright © 2021 Marcus Kelly