Reputation: 11
I try to write my mini OS. I learn from other projects, so I truly have no idea if my code is 'ok' for a bootloader and a kernel. I use assembly 16 bit, NASM, and load the bootloader and the kernel as .bin files to a floppy image. I run the OS with qemu. I came to a point where adding more code lines erase the displayed strings that appear when I start the OS. For example, if I start the os and it says 'Welcome to my OS!', after adding some code lines it would appear like this 'Welcome to m' (the end disappears).
here is my code:
bootloader.asm
[bits 16]
[org 0x7c00]
; Use the boot drive number passed to us by BIOS in register DL
start:
xor ax,ax ; We want a segment of 0 for DS
mov ds,ax ; Set AX to appropriate segment value
mov es,ax ; In this case we'll default to ES=DS
mov bx,0x8000 ; Stack segment can be any usable memory
mov ss,bx ; This places it with the top of the stack @ 0x80000.
mov sp,ax ; Set SP=0 so the bottom of stack will be @ 0x8FFFF
cld ; Set the direction flag to be positive direction
mov si, welcome_msg
call print_string
mov si, kernel_load
call print_string
pushf
stc
mov ah,00h ; Reset Disk Drive
int 13h
read_sector:
mov ax, 0x0
mov es, ax ; ES = 0
mov bx, 0x1000 ; BX = 0x1000. ES:BX=0x0:0x1000
; ES:BX = starting address to read sector(s) into
mov ah, 02 ; Int 13h/AH=2 = Read Sectors From Drive
mov al, 01 ; Sectors to read = 1
mov ch, 00 ; CH=Cylinder. Second sector of disk
; is at Cylinder 0 not 1
mov cl, 02 ; Sector to read = 2
mov dh, 00 ; Head to read = 0
; DL hasn't been destroyed by our bootloader code and still
; contains boot drive # passed to our bootloader by the BIOS
int 13h ; Read Sectors From Drive
jc error_kernel_load ; error loading kernel
popf
jmp 0x0:0x1000 ; jmp to kernel offset
cli ; Disable interrupts to circumvent bug on early 8088 CPUs
hlt ; halts the central processing unit (CPU) until the next external interrupt
error_kernel_load:
mov si, error_msg
call print_string
mov si, restart_msg
call print_string
mov ah,00 ; wait for key press
int 16h
xor ax,ax
int 19h ; reboot the computer
print_string: ; Routine: output string in SI to screen
lodsb ; Get character from string
or al,al
jz exit
mov ah,0x0e
int 10h ; int 10h 'print char' function
jmp print_string
exit:
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Messages to print
welcome_msg db 'Welcome to Bootloader!!!',0x0D,0x0A,0
kernel_load db 'Loading kernel....',0x0D,0x0A,0
error_msg db 'Kernel.bin not found!',0x0D,0x0A,0
restart_msg db 'Press any key to restart..',0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
times 510-($-$$) db 0 ; Create padding to fill out to 510 bytes
dw 0xaa55 ; Magic number in the trailer of a boot secto
kernel.asm
[bits 16]
[org 0x1000]
section .data:
cursor_col: db 0 ; cursor column
cursor_row: db 0 ; cursor row
line_col: dw 0 ; line column
line_row: dw 0 ; line row
color: db 3Fh ; background and forground color (at start set random the formal way > number+letter)
mode: db 0
start:
mov ax, 07C0h ; Set up 4K stack space after this bootloader
add ax, 288 ; (4096 + 512) / 16 bytes per paragraph
mov ss, ax
mov sp, 4096
mov byte [color], 3Fh
call clear_screen ; clear the screen and color it
; VVV print "Minerald welcome" message VVV
mov byte [cursor_col], 08
mov byte [cursor_row], 28
call set_cursor ; print at the requested position of the screen
mov si, welcome_string ; Put string position into SI
call print_string ; Call our string-printing routine
; VVV print "Press Space" message VVV
mov byte [cursor_col], 14
mov byte [cursor_row], 26
call set_cursor ; print at the requested position of the screen
mov si, press_key_string ; Put string position into SI
call print_string ; Call our string-printing routine
mov ah, 0h ; wait for key press
int 16h
mov byte [color], 1Fh
call clear_screen ; clear the screen and color it
mov byte [cursor_col], 2
mov byte [cursor_row], 0
call set_cursor ; print at the requested position of the screen
mov si, basic_background ; Put string position into SI
call print_string
mov byte [cursor_col], 22
mov byte [cursor_row], 0
call set_cursor ; print at the requested position of the screen
mov si, basic_background ; Put string position into SI
call print_string ; Call our string-printing routine
mov ah, 0 ; set display mode function.
mov al, 13h ; mode 13h = 320x200 pixels, 256 colors.
int 10h ; set it!
;================================= ; rectangles
mov byte [line_col], 80
mov byte [line_row], 165
call print_rectangle ; print rectangle
mov byte [line_col], 80
mov byte [line_row], 135
call print_rectangle ; print rectangle
;==================================
end:
jmp $ ; Jump here - infinite loop!
;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;
clear_screen:
mov ah, 06h ; Scroll up function
xor al, al ; Clear entire screen
xor cx, cx ; Upper left corner CH=row, CL=column
mov dx, 184Fh ; lower right corner DH=row, DL=column
mov bh, byte [color] ; set background and foreground color
int 10h
ret
;;;;;;;;;;
;;;;;;;;;;
print_string: ; Routine: output string in SI to screen
lodsb ; Get character from string
or al,al
jz exit
mov ah,0x0e ; int 10h 'print char' function
int 10h
jmp print_string
exit:
ret
;;;;;;;;;;
;;;;;;;;;;
set_cursor:
mov dh, byte [cursor_col] ; cursor col
mov dl, byte [cursor_row] ; cursor row
mov ah, 02h ; move cursor to the right place
xor bh, bh ; video page 0
int 10h ; call bios service
ret
;;;;;;;;;;
;;;;;;;;;;
print_rectangle:
%INCLUDE "print_rectangle.asm"
;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Messages to print
welcome_string db 'Welcome to my OS!', 0
press_key_string db 'Press any key to continue...', 0
enter_message_string db 'Enter your message below',0
APM_ERROR_string db 'APM Error...'
basic_background db '--------------------------------------------------------------------------------'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;END OF KERNEL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
this are the load commaneds:
nasm -f bin -o kernel.bin kernel.asm
nasm -f bin -o bootloader.bin bootloader.asm
dd if=bootloader.bin of=floppy.flp bs=512 seek=0 conv=notrunc
dd if=kernel.bin of=floppy.flp bs=512 seek=1 conv=notrunc
qemu-system-i386 -fda floppy.flp
edit:
from listing file of nasm before the messages are truncated:
300 ; Messages to print
301 00000191 57656C636F6D652074- welcome_string db 'Welcome to my OS!', 0
302 0000019A 6F204D696E6572616C-
303 000001A3 644F532100
304 000001A8 507265737320616E79- press_key_string db 'Press any key to continue...', 0
305 000001B1 206B657920746F2063-
306 000001BA 6F6E74696E75652E2E-
307 000001C3 2E00
308 000001C5 456E74657220796F75- enter_message_string db 'Enter your message below',0
309 000001CE 72206D657373616765-
310 000001D7 2062656C6F7700
311 000001DE 41504D204572726F72- APM_ERROR_string db 'APM Error...'
312 000001E7 2E2E2E
313 000001EA 2D2D2D2D2D2D2D2D2D- basic_background db '--------------------------------------------------------------------------------'
from listing file of nasm after the messages are truncated:
332 ; Messages to print
333 000001E8 57656C636F6D652074- welcome_string db 'Welcome to MineraldOS!', 0
334 000001F1 6F204D696E6572616C-
335 000001FA 644F532100
336 000001FF 507265737320616E79- press_key_string db 'Press any key to continue...', 0
337 00000208 206B657920746F2063-
338 00000211 6F6E74696E75652E2E-
339 0000021A 2E00
340 0000021C 456E74657220796F75- enter_message_string db 'Enter your message below',0
341 00000225 72206D657373616765-
342 0000022E 2062656C6F7700
343 00000235 41504D204572726F72- APM_ERROR_string db 'APM Error...'
344 0000023E 2E2E2E
345 00000241 2D2D2D2D2D2D2D2D2D- basic_background db '--------------------------------------------------------------------------------'
346 0000024A 2D2D2D2D2D2D2D2D2D-
Upvotes: 0
Views: 178
Reputation: 16606
Answer to your original question:
That's because your kernel gets longer, and it is more than one disk sector size in total, but you keep loading only one sector (512 bytes) of it into memory, so the rest of the string stays on the disk only, never entering the memory.
There're also several other problems (some mentioned in my comments), but those are not really relevant to the string truncation, and I'm not in mood+time to review all of your bootloader+kernel and fix all of the bugs you have there, so I will stop here (answer-wise).
Upvotes: 1