Last active
February 10, 2020 15:13
-
-
Save WitherOrNot/86f375861589c91ceaa0bc10f6ba21da to your computer and use it in GitHub Desktop.
A simple real mode os in assembly. To make a bootable floppy image, type nasm -f bin -o os.bin simple_os.asm && dd conv=notrunc if=os.bin of=simple_os.img into a command line.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
org 0x7c00 ; put code in a safe location | |
bits 16 ; put in 16 bit mode | |
; setting up stack | |
mov ax, 0 | |
mov es, ax | |
mov ds, ax | |
mov ss, ax | |
mov sp, 0x7c00 | |
mov si, message ; put message in si | |
call print_string | |
main: | |
mov si, prompt ; show prompt | |
call print_string | |
mov di, buffer ; get typed command | |
call terminal | |
mov si, buffer | |
cmp byte [si], 0 | |
je main ; ignore blank lines | |
mov si, buffer | |
mov di, cmd_hi | |
call strcmp ; check what is typed against command name | |
jc .hi ; if carry flag is set, run the corrensponding code | |
mov si, buffer | |
mov di, cmd_help | |
call strcmp | |
jc .help | |
mov si, buffer | |
mov di, cmd_shutdown | |
call strcmp | |
jc .shutdown | |
mov si, buffer | |
mov di, cmd_reboot | |
call strcmp | |
jc .reboot | |
mov si, badcommand | |
call print_string | |
mov si, badcommand2 | |
call print_string | |
jmp main | |
.hi: | |
mov si, hworld ; display hello world message | |
call print_string | |
jmp main | |
.help: | |
mov si, cmdlist | |
call print_string | |
jmp main | |
.shutdown: | |
mov ax, 0x1000 | |
mov ss, ax | |
mov sp, 0xf000 | |
mov ax, 0x5307 | |
mov bx, 0x01 | |
mov cx, 0x03 | |
int 0x15 ; set power mode and shut down | |
.reboot: | |
db 0x0ea | |
dw 0x0000 | |
dw 0xffff ; move to end of memory | |
jmp main ; keep going | |
print_string: | |
mov al, byte [si] ; take next byte from si and move it to al | |
inc si | |
cmp al, 0 ; check if string has ended | |
je .done ; if so, return | |
mov ah, 0x0e ; set BIOS to teletype mode | |
int 0x10 ; print character | |
jmp print_string | |
.done: | |
ret | |
terminal: | |
mov cl, 0 | |
.loop: | |
mov ah, 0 | |
int 0x16 ; put cursor on screen | |
cmp al, 0x08 | |
je .backspace ; backspace when you hit backspace | |
cmp al, 0x0d | |
je .enter ; enter when you hit enter | |
cmp cl, 0x3F ; stop when the screen ends | |
je .loop | |
mov ah, 0x0e | |
int 0x10 ; print what was typed | |
stosb ; put character in buffer | |
inc cl ; move cursor | |
jmp .loop | |
.backspace: | |
cmp cl, 0 ; is there nothing? | |
je .loop ; then there's nothing to delete! | |
dec di | |
mov byte [di], 0 | |
dec cl ; move cursor backwards | |
mov si, backspace ; get rid of character | |
call print_string | |
jmp .loop | |
.enter: | |
mov al, 0 | |
stosb ; end string | |
mov ah, 0x0e | |
mov al, 0x0d | |
int 0x10 | |
mov al, 0x0a | |
int 0x10 ; add next line | |
ret | |
strcmp: | |
.loop: | |
mov al, [si] ;grab bytes from si and di | |
mov bl, [di] | |
cmp al, bl | |
jne .notequal ; letters are not equal, so strings are not equal! | |
cmp al, 0 | |
je .equal ; strings are equal if they get here | |
inc si | |
inc di | |
jmp .loop ; move to the next pair of letters | |
.notequal: | |
clc ; clear carry flag | |
ret | |
.equal: | |
stc ; set carry flag | |
ret | |
message db 'Welcome to WitherOS 1.0!', 0x0d, 0x0a, 0 | |
hworld db 'Hello World!', 0x0d, 0x0a, 0 | |
badcommand db 'Unknown command entered.', 0x0d, 0x0a, 0 | |
badcommand2 db 'Type help for a list of commands.', 0x0d, 0x0a, 0 | |
prompt db 'not@linux:~$ ', 0 | |
cmd_hi db 'sayhi', 0 | |
cmd_help db 'help', 0 | |
cmd_shutdown db 'shutdown', 0 | |
cmd_reboot db 'reboot', 0 | |
cmdlist db 'Commands: sayhi, help, shutdown, reboot', 0x0d, 0x0a, 0 | |
buffer times 64 db 0 | |
backspace db 0x08, ' ' , 0x08 | |
times 510 - ($-$$) db 0 ; fill unused space with zeroes | |
dw 0xaa55 ; boot signature |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment