I am trying to write a bootloader on my own using the NASM Assembler. I am trying to print two lines of text, wait for a keypress, and reboot if the key pressed is 'r' and continue booting if the key pressed is 'b'. However, that is not the subject of my question (I will implement those functions later, hence the nop
instructions in the code at the moment). Rather, I would like to ask about why my newline character 0xA
prints in such a strange way? I have done all three items in my TODO at one point, but I think this is important as the loader must not look bad.
Here is my code:
BITS 16 ; tell nasm we're working in 16 bit real mode
start: ; entry symbol for the os
mov ax, 0x07C0 ; 4 kilobyte stack space set up
add ax, 288 ; (4096+512) / 16 bytes per paragraph
mov ss, ax
mov sp, 4096
mov ax, 0x07C0 ; set data segment to where we've loaded
mov ds, ax
mov si, text_string ; put our string position into SI
call write_string ; call our string-printing function
mov si, text_string_tutorial
cli ; jump here for an infinite loop
hlt
text_string db 'Mao Mao Loader 0.01 written by Safal Aryal', 0xA
text_string_tutorial db 'Press r to reboot or b to continue booting', 0x9
write_string: ; output string located in si
mov ah, 0xE ; the 'print char' function of int 0x10
.repeat:
lodsb ; get character from the string
cmp al, 0 ; check if it is zero
je .done ; if it is, jump to .done and finish the function
int 10h ; call interrupt 10h
jmp .repeat ; repeat for every char in the string
.done:
ret
read_input:
mov ah, 0x00
int 16h
cmp al, 'r'
nop
cmp al, 'b'
nop
times 510-($-$$) db 0 ; pad boot sector with zeroes
dw 0xAA55 ; standard bootsig
;TODO: read key pressed, change background colour and start writing kernel
It looks to me like the newline ( 0xa
) is doing exactly what I would expect--printing a newline. However, you didn't also print a carriage return ( 0xd
), so therefore your next sentence starts in the middle of the screen. Add a carriage return and you should get the result you're looking for.
Instruction cmp al, 0 check if working char is 0. In this case - ends routine. Your strings must end with 0. As in example:
... Aryal', 0x0D, 0xA, 0
...booting', 0x0D, 0xA, 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.