├── .gitignore ├── Makefile ├── README.md ├── bochsrc ├── bochsrc.bxrc ├── doc └── Bochs-2.4.5.exe ├── screenshot.png └── sos.s /.gitignore: -------------------------------------------------------------------------------- 1 | bochsout.txt 2 | *.swp 3 | *.bin 4 | *.img 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -c -Wall -o 2 | 3 | all: kernel 4 | 5 | clean: 6 | -rm -f *.bin *.img 7 | 8 | kernel: 9 | nasm -fbin -o kernel.bin sos.s 10 | cp kernel.bin kernel.img 11 | 12 | disasm: 13 | ndisasm kernel.img | less 14 | 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A simple os on 8086 for my GF to learn assembly language and OS. 2 | It's very simple and special since it can do the task switch, to make it, just press any key on your keyboard. :) 3 | 4 | to run the 8086sos, you can use bochs, qemu, virtualbox, vmware, even real pc, whatever, any x86 machine can run it. 5 | 6 | ![screenshot](https://github.com/wuxx/8086sos/blob/master/screenshot.png) 7 | -------------------------------------------------------------------------------- /bochsrc: -------------------------------------------------------------------------------- 1 | ############################################################### 2 | # bochsrc file for Tinix. 3 | ############################################################### 4 | #display_library: sdl 5 | # how much memory the emulated machine will have 6 | megs: 32 7 | 8 | # filename of ROM images 9 | #romimage: file=$BXSHARE/BIOS-bochs-latest 10 | romimage: file=/mnt/hgfs/Dropbox/wuxx_learn/os_learn/bochs-2.4.6/bios/BIOS-bochs-latest 11 | #, address=0xf0000 12 | #vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest 13 | vgaromimage: file=/mnt/hgfs/Dropbox/wuxx_learn/os_learn/bochs-2.4.6/bios/VGABIOS-lgpl-latest 14 | #VGABIOS-elpin-2.40 15 | 16 | # what disk images will be used 17 | #floppya: 1_44=balder10.img, status=inserted 18 | #floppya: 1_44=b.img, status=inserted 19 | floppya: 1_44=kernel.img, status=inserted 20 | 21 | # choose the boot disk. 22 | cpu: count=1,ips=1000000 23 | boot: a 24 | 25 | # where do we send log messages? 26 | log: bochsout.txt 27 | # disable the mouse, since Tinix is text only 28 | mouse: enabled=0 29 | 30 | # enable key mapping, using US layout as default. 31 | keyboard_mapping: enabled=1, map=/mnt/hgfs/Dropbox/wuxx_learn/os_learn/bochs-2.4.6/gui/keymaps/x11-pc-us.map 32 | -------------------------------------------------------------------------------- /bochsrc.bxrc: -------------------------------------------------------------------------------- 1 | ############################################################### 2 | # bochsrc file for Tinix. 3 | ############################################################### 4 | #display_library: sdl 5 | # how much memory the emulated machine will have 6 | megs: 32 7 | 8 | # filename of ROM images 9 | romimage: file=$BXSHARE/BIOS-bochs-latest 10 | #, address=0xf0000 11 | vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest 12 | #VGABIOS-elpin-2.40 13 | 14 | # what disk images will be used 15 | #floppya: 1_44=balder10.img, status=inserted 16 | #floppya: 1_44=b.img, status=inserted 17 | floppya: 1_44=kernel.img, status=inserted 18 | 19 | # choose the boot disk. 20 | cpu: count=1,ips=1000000 21 | boot: a 22 | 23 | # where do we send log messages? 24 | log: bochsout.txt 25 | #debugger_log:bochs_debug.txt 26 | # disable the mouse, since Tinix is text only 27 | mouse: enabled=0 28 | 29 | # enable key mapping, using US layout as default. 30 | keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-us.map 31 | -------------------------------------------------------------------------------- /doc/Bochs-2.4.5.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wuxx/8086sos/85f88b37f7e5ec8dd19e8adb8456f697eb84cc43/doc/Bochs-2.4.5.exe -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wuxx/8086sos/85f88b37f7e5ec8dd19e8adb8456f697eb84cc43/screenshot.png -------------------------------------------------------------------------------- /sos.s: -------------------------------------------------------------------------------- 1 | [BITS 16] 2 | [ORG 0] 3 | 4 | jmp start 5 | 6 | start: 7 | mov ax, 0x7c0 8 | mov ds, ax 9 | mov es, ax 10 | mov ax, 0x900 11 | mov ss, ax 12 | mov ax, 0x0 13 | mov sp, ax 14 | cli ; disable irq 15 | 16 | push dword 0x7c0 ; update cs:ip to 0x7c0:ip, for some machine, the default cs is 0x0 17 | push after_update_cs 18 | retf 19 | 20 | after_update_cs: 21 | sti ; enable irq 22 | 23 | ; bakup old int9 entry 24 | push es 25 | push ds 26 | mov ax, 0x0 27 | mov ds, ax 28 | mov si, 0x9*0x4 29 | 30 | mov di, int9_origin 31 | mov cx, 0x2 32 | cld 33 | rep movsw ; ds:si -> es:di 34 | 35 | ; set new int9 entry 36 | pop ds 37 | mov si, int9_new 38 | 39 | mov ax, 0x0 40 | mov es, ax 41 | mov di, 0x9*0x4 42 | mov cx, 0x2 43 | cld 44 | rep movsw 45 | pop es 46 | 47 | jmp run 48 | 49 | run: 50 | ; 26 flag 51 | ; 24 cs 52 | ; 22 ip --- cpu auto save 53 | ; 20 ax 54 | ; 18 cx 55 | ; 16 dx 56 | ; 14 bx 57 | ; 12 sp (the sp before pusha) 58 | ; 10 bp 59 | ; 8 si 60 | ; 6 di --- inst pusha save 61 | ; 4 ds --- push ds 62 | ; 2 es --- push es 63 | ; 0 ss --- push ss 64 | ; prepare taskB 65 | mov ss, [taskB_context + 0] 66 | mov es, [taskB_context + 2] 67 | mov ds, [taskB_context + 4] 68 | mov di, [taskB_context + 6] 69 | mov si, [taskB_context + 8] 70 | mov bp, [taskB_context + 10] 71 | mov sp, [taskB_context + 12] 72 | mov bx, [taskB_context + 14] 73 | mov dx, [taskB_context + 16] 74 | mov cx, [taskB_context + 18] 75 | mov ax, [taskB_context + 20] 76 | push word [taskB_context + 26] ;flag 77 | push word [taskB_context + 24] ;cs 78 | push word [taskB_context + 22] ;ip 79 | pusha 80 | push ds 81 | push es 82 | push ss 83 | mov [next_task+0], ss 84 | mov [next_task+2], sp 85 | 86 | ; prepare taskA and run it 87 | mov ss, [taskA_context + 0] 88 | mov es, [taskA_context + 2] 89 | mov ds, [taskA_context + 4] 90 | mov di, [taskA_context + 6] 91 | mov si, [taskA_context + 8] 92 | mov bp, [taskA_context + 10] 93 | mov sp, [taskA_context + 12] 94 | mov bx, [taskA_context + 14] 95 | mov dx, [taskA_context + 16] 96 | mov cx, [taskA_context + 18] 97 | mov ax, [taskA_context + 20] 98 | 99 | push word [taskA_context + 26] ;flag 100 | push word [taskA_context + 24] ;cs 101 | push word [taskA_context + 22] ;ip 102 | 103 | mov [current_taskid], ss ; indicate taskA 104 | iret ; kick the taskA 105 | 106 | taskA: 107 | mov si, msgA 108 | call write_message 109 | call delay 110 | jmp taskA 111 | 112 | taskB: 113 | mov si, msgB 114 | call write_message 115 | call delay 116 | jmp taskB 117 | 118 | delay: 119 | push ax 120 | mov ax, 0xFFFF 121 | dloop: 122 | sub ax, 1 123 | cmp ax, 0 124 | je end_dloop 125 | pusha 126 | popa 127 | jmp dloop 128 | end_dloop: 129 | pop ax 130 | ret 131 | 132 | int9_entry: 133 | ; cpu context save 134 | pusha 135 | push ds 136 | push es 137 | push ss 138 | 139 | ; read scan code 140 | ; break code = make code | 0x80 141 | in al, 0x60 142 | and ax, 0x0080 143 | jz kmake 144 | jmp kbreak 145 | 146 | kmake: ; make code 147 | ;call print_hex 148 | mov ax, [next_task+0] 149 | mov bx, [next_task+2] 150 | 151 | mov [next_task+0], ss 152 | mov [next_task+2], sp 153 | mov ss, ax 154 | mov sp, bx 155 | 156 | mov [current_taskid], ss 157 | jmp end 158 | 159 | kbreak: ; break code 160 | ;mov ax, 0xb 161 | ;call print_hex 162 | 163 | end: 164 | ;end of irq 165 | mov al, 0x20 166 | mov dx, 0x20 167 | out dx, al 168 | 169 | ;push word [int9_origin+2] cs 170 | ;push word [int9_origin+0] ip 171 | ;retf ; origin int9 handler will iret. 172 | 173 | ; cpu context restore 174 | pop ss 175 | pop es 176 | pop ds 177 | popa 178 | iret 179 | 180 | write_message: 181 | pusha ;ax bx cx dx sp bp si di 182 | again: 183 | lodsb ; DS:[SI] is read to al 184 | cmp al, 0x0 185 | jz end_message 186 | mov ah, 0x0E ; teletype Mode 187 | mov bx, 0007 ; white on black attribute 188 | int 0x10 189 | jmp again 190 | 191 | end_message: 192 | popa 193 | ret 194 | 195 | ; print the ax register 196 | print_hex: 197 | pusha 198 | mov si, msgAX 199 | call write_message 200 | 201 | push ax 202 | ; ah 203 | mov cx, 0x4 204 | mov bl, 12 205 | mov dx, 0xf000 206 | 207 | hex_again: 208 | and ax, dx 209 | 210 | push cx 211 | mov cl, bl 212 | shr ax, cl 213 | pop cx 214 | 215 | cmp al,0xA 216 | jb N0_N9 217 | NA_NF: 218 | add al, 0x7 219 | N0_N9: 220 | add al, 0x30 221 | mov ah, 0x0E ; teletype Mode 222 | push bx 223 | mov bx, 0007 ; white on black attribute 224 | int 0x10 225 | pop bx 226 | 227 | pop ax 228 | push ax 229 | 230 | shr dx, 4 231 | sub bl, 4 232 | loop hex_again 233 | 234 | pop ax 235 | mov si, msgNL 236 | call write_message 237 | popa 238 | ret 239 | ; ------------------------------------------------------------- 240 | 241 | msgA db 'task A', 0xD, 0xA, 0 242 | msgB db 'task B', 0xD, 0xA, 0 243 | msgAX db 'AX:0X', 0 244 | msgNL db 0xD, 0xA, 0 245 | 246 | ; 0 - taskA; 1 - taskB 247 | current_taskid: 248 | dw 0x0 249 | 250 | int9_origin: 251 | dw 0 ; ip 252 | dw 0 ; cs 253 | int9_new: 254 | dw int9_entry ; ip 255 | dw 0x7c0 ; cs 256 | 257 | next_task: 258 | dw 0x0 ; ss 259 | dw 0x0 ; sp 260 | 261 | taskA_context: 262 | dw 0x1000 ; ss 0x10000 - 0x11000 263 | dw 0 ; es 264 | dw 0x7c0 ; ds 265 | dw 0 ; di 266 | dw 0 ; si 267 | dw 0 ; bp 268 | dw 0xfffc ; sp 269 | dw 0 ; bx 270 | dw 0 ; dx 271 | dw 0 ; cx 272 | dw 0 ; ax 273 | 274 | dw taskA ; ip 275 | dw 0x7c0 ; cs 276 | dw 0x200 ; flag, enable irq 277 | 278 | taskB_context: 279 | dw 0x1100 ; ss 0x11000 - 0x12000 280 | dw 0 ; es 281 | dw 0x7c0 ; ds 282 | dw 0 ; di 283 | dw 0 ; si 284 | dw 0 ; bp 285 | dw 0xfffc ; sp 286 | dw 0 ; bx 287 | dw 0 ; dx 288 | dw 0 ; cx 289 | dw 0 ; ax 290 | 291 | dw taskB ; ip 292 | dw 0x7c0 ; cs 293 | dw 0x200 ; flag, enable irq 294 | 295 | times 510-($-$$) db 0 296 | dw 0xAA55 297 | 298 | --------------------------------------------------------------------------------