Wednesday, December 26, 2007

16-bit staOS code

org 0x00
BITS 16
jmp near MainLoop
call os_print_string
ret
call os_input_string
ret
call os_string_compare
ret
call os_wait_for_key
ret
call New_Line
ret

times 10240-($-$$) db 0


cli
mov ax,0x0050

mov ds, ax
mov es,ax
mov gs,ax
mov ss,ax

mov sp,0xffff
sti


mov si, before_draw
call os_print_string

mov si, before_input
call os_print_string
call os_clear_screen

call os_draw_background


MainLoop:
mov si, .prompt
call os_print_string

mov ax, .input
call os_input_string


call New_Line

call os_cursor_end_line

mov si, .input
cmp byte [si], 0
je MainLoop


mov si, .input
mov di, .exit_string
call os_string_compare
jc near ExitPgm

mov si, .input
mov di, .helpcmd
call os_string_compare
jc near helpprocess

mov si, .input
mov di, .clscmd
call os_string_compare
jc near clsprocess

mov si, .input
mov di, .dircmd
call os_string_compare
jc near dirprocess

mov si, .input
call check_runcmd
jc near runprocess


mov si, .cmdnotfound
call os_print_string

call os_cursor_end_line

jmp MainLoop
.dircmd db "dir",0x00
.runhellocmd db "run hello.com",0x00
.input times 255 db 0
.exit_string db 'exit',0x00
.tmp_buf dw 0
.prompt db "staOS>",0x00
.cmdnotfound db " 10 kb for user pgm : Commmand Not Found",0x0d,0x0a,0x00
.helpcmd db "help",0x00
.clscmd db "cls",0x00
.loadhello db "run hello.com",0x00
before_draw db 'before draw',0x0d,0x0a,0x00
before_input db 'before input',0x0d,0x0a,0x00

New_Line:
mov si, .newLine
call os_print_string
ret
.newLine db 0x0d,0x0a,0x00

runprocess:
add si, 4
jmp near LOAD_display_files_in_disk

dirprocess:
call display_files_in_disk
jmp near MainLoop

loadhello:
mov bx, 0x100
mov ah, 0x02
mov al, 0x01
mov ch, 0x03
mov cl, 0x01
mov dh, 0x00
mov dl, 0x00
int 0x13
jc loadhello
jmp near 0x100
jmp near MainLoop

LoadProcess:
mov bx, 0x100
mov ah, 0x02
mov al, 0x05
mov cl, 0x01
mov dh, 0x00
mov dl, 0x00
int 0x13
jc LoadProcess
jmp near 0x100
clsprocess:

call os_draw_background
jmp near MainLoop

.prompt1 db "staOS>",0x00

helpprocess:

mov si, .help
call os_print_string
call os_cursor_end_line
jmp near MainLoop

.help db "run dir help cls exit ",0x0d,0x0a,"This Tiny OS is only for understanding how operating system works",0x0d,0x0a,0x00


os_string_compare:
pusha

.more:
mov ax, [si]
mov bx, [di]

cmp byte [si], 0
je .terminated

cmp ax, bx
jne .not_same

inc si
inc di
jmp .more


.not_same:
popa
clc
ret


.terminated:
cmp byte [di], 0
jne .not_same

popa
stc
ret



os_input_string:
pusha

mov bx, ax

;mov ax, 0x2000
;mov ds, ax
;mov es, ax

mov di, bx
mov cx, 0

mov ax, 0

.loop_blank:
stosb

cmp byte [di], 0
je .blank_done

jmp .loop_blank

.blank_done:
mov di, bx
mov cx, 0


.more:
call os_wait_for_key

cmp al, 13
je .done

cmp al, 8
je .backspace

cmp al, 32
jl .more

cmp al, 126
jg .more

jmp .nobackspace


.backspace:
cmp cx, 0
je .more

pusha
mov ah, 0Eh
mov al, 8
mov bl, 8
int 10h
mov al, 32
int 10h
mov al, 8
int 10h
popa

mov byte [di], 0

dec cx
dec di

jmp .more


.nobackspace:
pusha
mov ah, 0Eh
int 10h
popa

inc cx
cmp cx, 250
je near .done

stosb

jmp near .more


.done:
popa
ret

os_wait_for_key:
pusha

xor ax, ax
mov ah, 00
int 16h

mov [.tmp_buf], ax

popa
mov ax, [.tmp_buf]
ret
.tmp_buf dw 0

os_move_cursor:
pusha

xor bh, bh
mov ah, 2
int 10h

popa
ret
os_show_cursor:
pusha

mov ch, 0
mov cl, 7
mov ah, 1
mov al, 3
int 10h

popa
ret
os_hide_cursor:
pusha

mov ch, 32
mov ah, 1
mov al, 3
int 10h

popa
ret
os_cursor_end_line:
pusha

mov ah, 3
mov bh, 0
int 10h
cmp dh, 23
jl no_scroll

mov dh, 23
mov dl, 0
call os_move_cursor
call os_scroll_up_oneline

no_scroll:

popa
ret

os_draw_background:
pusha
;push ax
;push bx
;push cx

call os_clear_screen

mov ah, 09h
xor bh, bh
mov cx, 80
mov bl, 01010000b
mov al, ' '
int 10h
mov si, .screenmsg
call os_print_string

mov dh, 1
mov dl, 0
call os_move_cursor

mov ah, 09h
xor bh, bh
mov cx, 1840
mov bl, 00000110b
xor bh, bh
mov al, ' '
int 10h

mov dh, 24
mov dl, 0
call os_move_cursor

mov ah, 09h
xor bh, bh
mov cx, 80
mov bl, 01010000b
mov al, ' '
int 10h

mov dh, 24
mov dl, 1
call os_move_cursor


mov dh, 0
mov dl, 1
call os_move_cursor

mov dh, 1
mov dl, 0
call os_move_cursor
popa
ret
.screenmsg db " 16 Bit Demo staOS 1.0",0x00

os_print_string:
pusha

mov ah, 0Eh
mov bl, 12

.repeat:
lodsb
cmp al, 0
je .done
int 10h
jmp .repeat

.done:
popa
ret

os_scroll_up_oneline:
pusha

mov ah, 6
mov al, 1
mov bh, 6
mov ch, 1
mov cl, 0
mov dh, 23
mov dl, 79
int 10h

popa
ret

os_clear_screen:
pusha

mov dx, 0
call os_move_cursor

mov ah, 6
mov al, 0
mov bh, 12
mov ch, 0
mov cl, 0
mov dh, 24
mov dl, 79
int 10h

popa
ret

ReadSector:
pusha
mov ah, 0x02
mov al, 0x03
mov ch, 0x02
mov cl, 0x01
mov dh, 0x00
mov dl, 0x00
int 0x13
jc ReadSector
popa
ret

display_files_in_disk:
pusha
mov bx, .dir_buff
call ReadSector
mov si, .dir_buff
call display_files

popa
ret
.dir_buff times 1536 db 0xff

display_files:
pusha
NEXT:
cmp byte [si], 0xFF
jz end_display_files

mov di, .file_name

call FILE
call FILE_EXT
mov byte [di],0

push si
mov si, .file_name
call STA_print_string
pop si

inc si
inc si
inc si
push si
call New_Line
pop si
jmp NEXT




.file_name times 17 db 0


FILE:
mov cx, 10
FILE_name:
mov al, [si]
cmp al, ' '
jz bypass
mov [di], al
inc di
bypass:
inc si
loop FILE_name

mov al, '.'
mov [di], al
inc di
ret




FILE_EXT:
mov cx, 3
FILE_EXT_name:
mov al, [si]
mov [di], al
inc si
inc di
loop FILE_EXT_name
ret

end_display_files:
popa
ret

STA_print_string:
pusha

mov ah, 0Eh
mov bl, 12

.repeat:
lodsb
cmp al, 0
je .done
int 10h
jmp .repeat

.done:
popa
ret

convert_to_decimal:
pusha
push si
mov cx, 3
EOS:
mov al, [si]
sub al, 48
mov [si],al
inc si
loop EOS
pop si
mov al, [si]
mov bl, 10
mul bl
add al, [si+1]
mov [si], al
popa
ret

check_runcmd:
pusha
CRCMD:
mov al, [si]
cmp al, 'r'
jnz RCexit
mov al, [si+1]
cmp al, 'u'
jnz RCexit
mov al, [si+2]
cmp al, 'n'
jnz RCexit

popa
stc
ret
RCexit
popa
clc
ret


LOAD_display_files_in_disk:


mov [compareFile], si
mov bx, dir_buff
call ReadSector
mov si, dir_buff
jmp LOAD_display_files



dir_buff times 1536 db 0xff

LOAD_display_files:

LOAD_NEXT:
cmp byte [si], 0xFF
jz near MainLoop
mov di, LOAD_file_name
call LOAD_FILE
call LOAD_FILE_EXT
mov byte [di],0

push di
push si
mov di, LOAD_file_name
mov si, [compareFile]
call os_string_compare
jc near matchFound

RR:
pop si
pop di
add si, 3
jmp LOAD_NEXT
jmp near MainLoop

compareFile dw 0
LOAD_file_name times 17 db 0



matchFound:
pop si
pop di
mov byte [si+3], 0
call convert_to_decimal
mov ch, [si]
jmp near LoadProcess

matchFound1:
mov si, notfound
call os_print_string
jmp RR

notfound db "not found",0x0d,0x0a,0x00
matchStr db "match found ",0x00,0x0d,0x0a

LOAD_FILE:
push cx
push ax

mov cx, 10
LOAD_FILE_name:
mov al, [si]
cmp al, ' '
jz LOAD_bypass
mov [di], al
inc di
LOAD_bypass:
inc si
loop LOAD_FILE_name

mov al, '.'
mov [di], al
inc di
pop ax
pop cx

ret




LOAD_FILE_EXT:
push cx
push ax
mov cx, 3
LOAD_FILE_EXT_name:
mov al, [si]
mov [di], al
inc si
inc di
loop LOAD_FILE_EXT_name
pop ax
pop cx
ret


ExitPgm:
int 0x19

No comments: