Search code examples
assemblyincludenasmx86-16bootloader

Assembly with %include at the top - Printing Outputs Unexpected Result: just an " S"


I'm relatively new to assembly programming and was wondering why my code does not print the expected strings. This project is supposed to be a bootloader when finished. I am compiling using the command nasm -f bin boot.asm -o boot.bin. There are no errors during compilation.

boot.asm

bits 16
org 0x7C00

%include "print.asm"
%include "text.asm"

boot:
        mov si, boot_string_00
        call print
        mov si, boot_string_01
        call print

times 510 - ($-$$) db 0
dw 0xAA55

print.asm

print:
        mov ah, 0x0E

.print_loop:
        lodsb
        or al, al
        je .print_done
        int 0x10
        jmp .print_loop

.print_done:
        cli
        ret

text.asm

boot_string_00: db "Placeholder OS Title v0.0.1", 0
boot_string_01: db "Loading Operating system", 0

Expected Output:

PlaceHolder OS Title v0.0.1Loading Operating System

Actual Output:

S

Also, I was wondering how i could implement newlines in assembly so that i could just use '\n' in my strings.


Solution

  • You included stuff at the top of your bootloader, where it will executes first. Instead include extra functions where they aren't in the main path of execution and are only reached by call.


    This should work, placing the %include directives where it's safe to put extra function or data, just like if you were writing them all in one file.

    boot.asm:

    [bits 16]
    [org 0x7c00]
    
    boot:
      xor ax, ax
      mov ds, ax        ; set up DS to make sure it matches our ORG
    
      mov si, boot_string_00
      call println
    
      mov si, boot_string_01
      call println
    
    finish:       ; fall into a hlt loop to save power when we're done
      hlt
      jmp finish
     
    
    %include "printf.asm"      ; not reachable except by call to labels in this file
    %include "text.S"
    
    
    times 510-($-$$) db 0
    dw 0xaa55
    

    printf.asm:

    print:
            mov ah, 0x0E      ; call number for int 0x10 screen output
    
    print_loop:
            lodsb
            test al, al
            je print_done
            int 0x10
            jmp print_loop
    
    print_done:
            ret
               
    println:
      call print
      mov si, line_end
      call print
      ret
    

    text.S:

    boot_string_00: db "Placeholder OS Title v0.0.1", 0
    boot_string_01: db "Loading Operating system", 0
    line_end:       db 0xD, 0xA, 0