assembly – GAS syntax bootloader load the next sector from floppy

I’m playing with the Real mode and was trying to code something when suddenly I exceeded the 510 boundary (.org 510) and as warning popped out. So I started reading about how to split the code into multiple sectors and load them from the disk using the floppy for simplicity.

I’m probably missing a lot of stuff one of which would be manual stack init, though I’m not sure if that would affect the loading as it’s coded here. I’m using -monitor stdio to check the values in registers if necessary with info registers. Seems sufficient enough for now, but feel free to suggest better debugging tools that work with Qemu.

Feel free not to hold back, I’d like to learn more. 🙂

Building:

#!/bin/sh -xe
as -o code.o code.s -g --statistics --warn --fatal-warnings
ld -o code.bin --oformat=binary -Ttext=0x7c00 --build-id=none code.o
qemu-system-i386 -monitor stdio -drive file=code.bin,index=0,if=floppy,format=raw

Code:

.att_syntax
.code16
.global _start

.text
// sector 1 begin
_start:
    mov $0x0e, %ah
    mov $'a', %al
    int $0x10

    jmp load_next_sector

load_next_sector:
    // set Buffer Address Pointer to 0
    mov $0x0, %bx
    // service 02h: Read Sectors From Drive
    mov $0x2, %ah
    // Sectors To Read Count
    mov $0x1, %al
    // Cylinder/Track number 1 (zero-based)
    mov $0x0, %ch
    // Sector number 2 (one-based)
    mov $0x2, %cl
    // Head 1 (zero-based)
    mov $0x0, %dh
    int $0x13

    // fail, AL codes: http://www.oocities.org/wangxuancong/int13h.html
    jc load_next_sector

    // success
    jmp sector_2

.org 510
.word 0xAA55
// sector 1 end

// sector 2 begin
sector_2:
    // dummy
    xor %cx, %cx
    mov $0x13, %cl

    mov $0x0e, %ah
    mov $'b', %al
    int $0x10
    jmp pause

pause:
    jmp pause

.org 1024
// sector 2 end
.end
```