Showing posts with label Writing Operating System (OS). Show all posts
Showing posts with label Writing Operating System (OS). Show all posts

Friday, December 28, 2007

User Programs code of staOS

Program 1:

org 0x100
mov si, msg
call os_print_string
mov ah, 00
int 0x16
jmp 0x0050:0000


msg db "Knock the door it will open, ask we will give",0x0d,0x0a,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

Program 2
This program is for calling operating system function from user program.

org 0x100
bits 16
mov si, sys_msg
call 0x0003
mov ax, input
call 0x0007
call 0x000f
call 0x0013
jmp 0x0050:0000
input times 30 db 0
sys_msg db "Enter string: ",0x00

program 3

org 0x100
bits 16
mov si, msg
call DisplayMessage
mov cx, 10
LL:
call read
call DisplayCharacter
loop LL
jmp 0x0050:0000
read:
mov ah, 0x10
int 0x16 ret
DisplayCharacter:
mov ah, 0x0e
mov bh, 0x00
mov bl, 0x02
int 0x10
retDisplayMessage:
lodsb
or al, al
jz .DONE
mov ah, 0x0e
mov bh, 0x00
mov bl, 0x05
int 0x10
jmp DisplayMessage
.DONE:

program 4


org 0x100
mov si, msg
call os_print_string
mov ah, 00
int 0x16
jmp 0x0050:0000
msg db "Motivation is a tool for success",0x0d,0x0a,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

Program 5


org 0x100
mov si, msg
call os_print_string
mov ah, 00
int 0x16
jmp 0x0050:0000
msg db "Hello, World from staOS 1.0",0x0d,0x0a,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




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

Boot Loader code

org 0x7c00
bits 16

cli
mov ax,0
mov ds, ax

mov es,ax
mov gs,ax
mov ss,ax
mov sp,0xffff

sti


mov si,msg
call DisplayMessage

mov ax, 0x0050
mov es, ax
mov bx, 0x0000
call ReadSector

mov ax, 0x0050
mov es, ax
mov bx, 0x2200
call ReadSector1

mov si, msg1
call DisplayMessage

mov si, beforeOS
call DisplayMessage

jmp 0x0000:0x2D00

mov si, afterOS
call DisplayMessage
cli

hlt

ReadSector:
mov ah, 0x02
mov al, 0x11
mov ch, 0x00
mov cl, 0x02
mov dh, 0x00
mov dl, 0x00
int 0x13
jc ReadSector
ret

ReadSector1:
mov ah, 0x02
mov al, 0x0d
mov ch, 0x01
mov cl, 0x01
mov dh, 0x00
mov dl, 0x00
int 0x13
jc ReadSector1
ret


DisplayMessage:
lodsb
or al, al
jz .DONE
mov ah, 0x0e
mov bh, 0x00
mov bl, 0x14
int 0x10
jmp DisplayMessage
.DONE:
ret

msg db "Boot Loader searching for OS...",0x0d,0x0a,0x00
msg1 db "Stalin OS found in Floppy DISK, Loading staOS...",0x0d,0x0a,0x00
reading db " Reading sector... please wait...", 0x0d,0x0a,0x00
finished db " Finished Reading.",0x0d,0x0a,0x00
beforeOS db "Updated Before jump to 0x0050:0x2800 !", 0x0d,0x0a,0x00
afterOS db " back to loader ",0x00
sectorloaded db " after loaded sector in memory ready to move control", 0x0d,0x0a,0x00

TIMES 510-($-$$) DB 0
DW 0xAA55

Wednesday, May 30, 2007

NASM and Boot loader

NASM

Netwide assembler we can generate plain text binary using NASM. This NASM is very useful to write own os.

Download site for NASM

http://sourceforge.net/project/showfiles.php?group_id=6208

1). DOS 16-bit binaries (OBSOLETE) 0.98.39 February 27, 2005 - Download
2). DOS 32-bit binaries 0.98.39 January 15, 2005 - Download

Boot Loader program

boot.asm
========code starts here=============
[bits 16]
org 0x7c00

mov si,msg
call DisplayMessage
hlt

DisplayMessage:
lodsb
or al, al
jz .DONE
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x02
int 0x10
jmp DisplayMessage
.DONE:
ret
msg db 0x0d, 0x0a, "Boot loader test example", 0x0d, 0x0a, 0x00
TIMES 510-($-$$) DB 0
DW 0xAA55
=========code ends here==============


assemble this program as: .>nasm -o boot boot.asm

After assembled this program plain machine instruction stored in boot file.

boot file size will be 512 bytes, now we need to write these instruction into boot sector of floppy
for that we need to write one C program, otherwise we can use some other utility(debug ect...) to write these instruction into boot sector of floppy.

==========code starts here===========

#include
#include
#include
main()
{
static unsigned char inc[512],inc1[512];
int i=0;
FILE *fp;
fp=fopen("boot","rb");
if(fp==NULL)
{
printf("file openning error");
exit(0);
}
while(i<512) here="="="="="="="="="="="="">
steps to do

1. Insert floppy in floppy drive
2. write boot.asm file
3 assemble with nasm as nasm -o boot boot.asm
4. write c program to copy boot to boot sector of floppy drive.
5. reboot the system and set first bootable device to floppy in SETUP.

Now u can see the text "Boot loader test example" on screen.

Steps to write Tiny OS

1. Computer booting process
2. Boot sector process
3. Plain Text Binary Files
4. Interrupts
5. Memory map
6. Segmented memory model
7. Real mode and protected mode
8. Assembly language knowledge
9. IVT and ISR