2

I'm using NASM to write a minimal OS for x86 real mode for educational purposes. I want to use the 512-byte boot sector to load a larger sector that contains the rest of the OS. I've successfully created a boot sector that loads another sector, but I cannot seem to write/read strings within the loaded sector. Here is my code:

    bits 16

    mov ax, 0x7c0
    mov ds, ax

    jmp code

    ;; Write bootStr to boot sector.
    bootStr db "AAA"

code:   

    ;; for int 0x10
    mov ah, 0x0e

    ;; Print first char of bootStr.
    mov di, bootStr
    mov BYTE al, [di]
    int 0x10    ; prints A

    ;; Load next sector.
    ;; adapted from:
    ;; https://blog.benjojo.co.uk/post/interactive-x86-bootloader-tutorial
    mov ah, 0x02
    mov al, 1   
    mov ch, 0    
    mov cl, 2    
    mov dh, 0   
    mov bx, new 
    mov es, bx  
    xor bx, bx
    int 0x13
    jmp new:0

    new equ 0x0500

    ;; Pad boot sector.
    times 510-($-$$) db 0 
    db 0x55
    db 0xaa

nextSector: 

    ;; for int 0x10
    mov ah, 0x0e

    ;; Try to print first char of nextStr (defined below).
    mov di, nextStr
    mov BYTE al, [di]
    int 0x10    ; should print B

    ;; Move 'C' into nextStr and print it.
    mov BYTE [di], 'C'
    mov BYTE al, [di]
    int 0x10    ; prints C

    ;; Print first char of bootStr again.
    mov di, bootStr
    mov BYTE al, [di]
    int 0x10    ; prints A

    hlt

    nextStr db "BBB"

When I run (on Debian Stretch):

nasm -f bin -o boot.bin boot.asm
qemu-system-x86_64 boot.bin

I get:

Booting from Hard Disk...
A CA

So it looks like some invisible character is printed where I expect to see B.

What I don't understand is why, from the loaded sector, I can write characters to nextStr with mov BYTE [di] 'C' and then print them, but I can't seem to define nextStr with db and then print its characters. After several hours I've had no luck fixing the issue.

Questions:

  • Is nextStr never written with db in the first place, or am I simply failing to read/print it?
  • How should I fix this so that I can write/read/print strings within the loaded sector?
jth
  • 369
  • 3
  • 8
  • The problem is that you have placed all the code for the first and secind sector into one assembly file. You start with an origin point of 0 in the first sector (since you didn't specify an `org` directive and 0 is the default). This means that all the labels starting at `nextSector` are not starting from an offset of 0x000 as your code expects but at an offset of 512. – Michael Petch Sep 22 '18 at 22:29
  • You could split the two assembly files up for the first and second sector so that the `org` (origin point) is 0 in each file. If you want everything in one file you can look at using sections with a `vstart` address of 0.You can read about that in the [NASM docs](https://www.nasm.us/doc/nasmdoc7.html) in the section _7.1.3 Multisection Support for the bin Format_ – Michael Petch Sep 22 '18 at 22:32
  • As well in the second stage you will need to make sure as one of the first things you do - you set _DS_ to the value `new` (or 0x500) so that you read from the right segment in memory where the code at nextSector was physically loaded. – Michael Petch Sep 22 '18 at 22:36
  • Thanks for the quick replies! Could you point me in the right direction for splitting it into one sector per file? How do I compile two files into one bootable binary? – jth Sep 22 '18 at 22:45
  • Well if you want to do it one one file as you are this may do the trick: http://www.capp-sysware.com/misc/stackoverflow/52461308/boot.asm . There are examples on Stackoverflow for bootloaders done in two files that use the `dd` program to build the two parts into a single binary. You should be able to find some with a search. The onyl problem though is if you do split them up you won't be able to easily reference the `bootStr` variable in the first file with the second. – Michael Petch Sep 22 '18 at 22:47
  • 2
    OK, thanks a lot for all the help! I'll spend some more time on this with the information you've provided. – jth Sep 22 '18 at 22:51
  • 2
    @MichaelPetch I adapted your `boot.asm` file for my actual project and it works perfectly! Thank you so much! – jth Sep 23 '18 at 01:23
  • @MichaelPetch I posted a follow-up question: https://stackoverflow.com/q/52463695/10402025 If you have the time/inclination I'd greatly appreciate you taking a look at it. Thanks again! – jth Sep 23 '18 at 06:51

0 Answers0