0

I am writing a simple OS which prints all stdin (keyboard input) to stdout (the screen). I want to print a welcome string before the input. I'm using NASM since it is readable and easy to use.

I've tried two ways to print the string:

(Assume welcome is defined as db "Welcome to TextEditOS! Type whatever you want below.",0dh,0ah,0dh,0ah,0. Remove the 0 for #2.)

  1. Using a loop similar to a while loop. Originally I used bx instead of si since a few tutorials did it that way, but this answer suggested using si since bx is used for the page number and color in TTY mode.
mov si, welcome
mov ah, 0eh

printWelcome:
    cmp [si], byte 0
    je mainLoop
    mov al, [si]
    int 0x10
    inc si
    jmp printWelcome
  1. Using int 10h where ah=13h. The code I used is something like this, though I believe I have the original code on my home computer (I'm away right now, will update this question once I get back).
    xor ax, ax
    mov es, ax
    xor bh, bh
    mov bp, welcome

    mov ah, 0x13
    mov bl, 0ah ;this was something else I think
    mov al, 1   ;write mode
    mov cx, 56  ;length
    mov dh, 0   ;x
    mov dl, 0   ;y

    int 0x10

Both of these work just fine in QEMU. However, when I burn the full code onto a USB flash drive and boot it, the first way prints around 1-2 screens of invisible characters before stopping in the middle of the screen, and the other seems to print an @ and nothing else. The rest of my code, which is the keyboard input, works fine both ways.

Here is the full code for my operating system:

[org 0x7c00]

jmp short start

db 90h,90h,"TEXTOS",20h,20h

section .text

start:

; First, let's set up the stack
mov bp, 7c00h
mov sp, bp

; Insert either way I used to print the string below

; end print func

mainLoop:
    mov ah, 0    ; get keyboard input
    int 16h
    mov ah, 0eh  ; print char
    int 10h      ; al already has the char to print
    cmp al, 13   ; is char 0dh?
    je newLine
    cmp al, 8    ; is char 08h?
    je backSpace
    jmp mainLoop ; loop

newLine:
    mov al, 10   ; complete \n with 0ah
    int 10h
    jmp mainLoop

backSpace:
    mov al, 32   ; insert space over last char
    int 10h
    mov al, 8    ; then another backspace to go back
    int 10h
    jmp mainLoop

welcome: ; welcome message
    db "Welcome to TextEditOS! Type whatever you want below.", 0dh, 0ah, 0dh, 0ah, 0

times 510-($-$$) db 0
; Boot signature
dw 0xaa55

Commands used to compile and run:

nasm boot.asm -fbin -oboot.img
qemu-system-x86_64 -drive file=boot.img,format=raw

As said above, I will find the code for #2 and edit this question with it. I will also try to upload some GIFs of what I get on real hardware.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • Any chance this is related? https://stackoverflow.com/a/47320115/3857942 – Michael Petch Jun 01 '22 at 15:39
  • 1
    @MichaelPetch I added the BPB and the same issue occurs, though I noticed my USB drive appears _first_ in the boot drive list. I confirmed my BPB is not being overwritten by the BIOS as well. – OnixIsThePewterGod Jun 01 '22 at 16:03
  • One possible problem I guess is that you set up SP but don't set SS to 0 (SS could be something other than 0 when the BIOS jumps to bootloader code). I assume you mean to have the stack grow down beneath the bootloader (0x0000:0x7c00) which why I suggest ensuring SS=0x0000 – Michael Petch Jun 01 '22 at 16:18
  • 1
    I see you say you have code that does `xor ax, ax` `mov es, ax` . Is there a reason you aren't setting DS to 0 rather than ES? The values in DS, ES, SS can't be assumed to be 0 when your bootloader is reached. It's unclear why you set ES to 0 unless you have code you aren't showing that relied on it. Your code clearly expects DS to be 0 though. Try setting DS to 0 and see what happens. This could explain why it prints garbage as it is referencing the wrong memory location for the string. – Michael Petch Jun 01 '22 at 16:34
  • I set `ss` to 0 but it still does not work. – OnixIsThePewterGod Jun 01 '22 at 16:56
  • 2
    Here's the thing. The complete code of your bootloader doesn't do what the rest of the question is about. It gets a key and prints it out where as the rest of what you wrote is about printing strings but it is just code snippets. I see now why you used ES:BP because in some code you used Int 10h/AH-13h (BIOS write string). What you really should do is show the exact code that fails and a screenshot of the output would be nice, – Michael Petch Jun 01 '22 at 17:36
  • 1
    I understand, but I wanted to post my code just in case there was a bug somewhere else. I will also post screenshots soon. I haven't had time to do it, sorry. – OnixIsThePewterGod Jun 04 '22 at 16:33
  • I still don't understand... How am I supposed to correctly print strings? – OnixIsThePewterGod Jun 13 '22 at 15:03

0 Answers0