[ORG 0x00]
[BITS 16]
SECTION .text
jmp 0x1000:START ;; right after bootloader sector
SECTORCOUNT: dw 0x0000 ; currently executing sector
TOTALSECTORCOUNT equ 1024 ; virtual OS's num of sectors
;; code section
START:
; segment setting for each sector
mov ax, cs ; ex: 0x0000, 0x0020, ...
mov ds, ax
mov ax, 0xB800;
mov es, ax;
; print number for current memory addr and jump to next memory addr
; which is multiply of 512 bytes
%assign i 0
%rep TOTALSECTORCOUNT ; repeat as many as TOTALSECTORCOUNT
%assign i i+1
mov ax, 2
mul word [SECTORCOUNT]
mov si, ax ; si(video memory offset) = char_size * sector_count
; 160 * 2 + es:si == third line + offset
mov byte [es:si + (160 * 2)], '0' + (i % 10)
add word [SECTORCOUNT], 1
%if i == TOTALSECTORCOUNT
jmp $ ; end of virtual OS
%else
jmp (0x1000 + i * 0x20):0x0000 ; jump to next memory addr
%endif
; $$ == SECTION .code == 0x00
; $ == cur addr that can be over 0x10000 + 512 bytes
times (512 - ($ - $$) % 512) db 0x00
%endrep- Start code from
0x10000, start address of OS in memory - Print number to screen
- jump to next segment whose address is multiply of 512 bytes
- If jump reaches last segment(last part of OS), infinite loop
- Otherwise, repeat from 2
-
TOTALSECTORCOUNT equ 1024is like preprocessor. The result machine code does not have space for the value of 1024. However, you can access the variable from different asm file because the variable exists in object file -
Preprocessor
%assign i 0defines i = 0; re-assignable%rep TOTALSECTORCOUNTrepeats as many as TOTALSECTORCOUNT%endrep%if i == TOTALSECTORCOUNT%elif%else%endif
-
Virtual OS code is for test purpose. Later, the code is replaced with complex operating system code that can handle signal, thread, process, multi-cores, and so on
-
One thing to notice is that you cannot set
TOTALSECTORCOUNTover1152. If image occupies 1153 sectors, OS invades0xA0000in memory which is reserved for graphic mode video memory. If you change TOTALSECTORCOUNT in VirtualOS.asm and TOTALSECTORCOUNT in BootLoader.asm to 1153, then you will see that total number of printed string is just 1152 (80 * 14 + 32). The OS size is 1024 sectors, but you can set it up to 1152. No special reason to have 1024 sectors -
Now you can see a new directory named
01.Kernel32. This directory contains the virtual OS assembly code and Makefile. Also there is some change in Makefile in root directory -
Memory Layout up to CH05
- start(inclusive) ~ end(exclusive)
- 0x00000 ~ 0x00400 (Interrupt Vector Table)
- 0x07C00 ~ 0x07E00 (Bootloader)
- 0x07E00 ~ 0x10000 (Stack)
- 0x10000 ~ 0x90000 (Operating System; 1024 sectors)
- 0xA0000 ~ ... (video memory for graphic mode)
- 0xB8000 ~ ... (video memory for text mode)