step 2: load 2nd sector and jump into it
authorEleni Maria Stea <estea@igalia.com>
Fri, 4 Dec 2020 16:50:17 +0000 (18:50 +0200)
committerEleni Maria Stea <estea@igalia.com>
Fri, 4 Dec 2020 16:51:50 +0000 (18:51 +0200)
.gdbinit [new file with mode: 0644]
Makefile
bb.asm

diff --git a/.gdbinit b/.gdbinit
new file mode 100644 (file)
index 0000000..a3fac16
--- /dev/null
+++ b/.gdbinit
@@ -0,0 +1,4 @@
+target remote localhost:1234
+set architecture i8086
+set disassembly-flavor intel
+disp/i $pc
index e52a97e..ea23f67 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,6 +8,10 @@ floppy.img: $(bin)
        dd if=/dev/zero of=$@ bs=1024 count=1440
        dd if=$< of=$@ bs=512 conv=notrunc
 
        dd if=/dev/zero of=$@ bs=1024 count=1440
        dd if=$< of=$@ bs=512 conv=notrunc
 
+.PHONY: disasm
+disasm: $(bin)
+       ndisasm -b16 -o 0x7c00 $< > dis
+
 .PHONY: clean
 clean:
        rm -f $(bin)
 .PHONY: clean
 clean:
        rm -f $(bin)
@@ -16,3 +20,7 @@ clean:
 .PHONY: run
 run: floppy.img
        qemu-system-i386 -fda $<
 .PHONY: run
 run: floppy.img
        qemu-system-i386 -fda $<
+
+.PHONY: debug
+debug: floppy.img
+       qemu-system-i386 -S -s -fda $<
diff --git a/bb.asm b/bb.asm
index f5c84af..b3ef814 100644 (file)
--- a/bb.asm
+++ b/bb.asm
 start:
        mov sp, 7c00h
 
 start:
        mov sp, 7c00h
 
+; initialize segment registers
+       mov ax, 0
+       mov ds, ax
+       mov es, ax
+       mov ss, ax
+
+; save dx value
+       mov [saved_drive_num], dl
+
 ; service 0: set videomode ah
 ; param: which video mode (13h: 320x200, 256 colors) al
 ; ax: ah (high 8 bits), al (low)
 ; service 0: set videomode ah
 ; param: which video mode (13h: 320x200, 256 colors) al
 ; ax: ah (high 8 bits), al (low)
@@ -19,11 +28,26 @@ start:
 ; calling video bios
 ; software interrupt 10h
        int 10h
 ; calling video bios
 ; software interrupt 10h
        int 10h
+       mov ax, 3
        call clearscreen
 
        call clearscreen
 
-; infinite loop
-end:
-       jmp end
+; load 2nd sector from boot device and jump to it
+; bios helpers, 13h = disk io
+       mov ax, 0
+       mov ds, ax
+       mov ah, 02h                     ; call 2: read sectors into memory
+       mov al, 1                               ; number of sectors to read
+       mov ch, 0                               ; low 8 bits of cylinder number
+       mov cl, 2                               ; sector number that starts from 1
+       mov dh, 0                               ; head number
+       mov dl, [saved_drive_num]       ; 8bits
+       mov bx, sector_2
+       int 13h
+; error check: if carry flag isn't set jump to loaded code
+       jnc     sector_2
+.inf_loop:
+       jmp .inf_loop
+
 
 clearscreen:
 ; video ram is mapped in a0000
 
 clearscreen:
 ; video ram is mapped in a0000
@@ -37,12 +61,14 @@ clearscreen:
 ; automatically set to 7c0 (7c00h)
 ; offset can be paired with any register
 ; pair the registers
 ; automatically set to 7c0 (7c00h)
 ; offset can be paired with any register
 ; pair the registers
+       push ax
        mov ax, 0a000h
        mov ds, ax
        mov di, 0
        mov ax, 0a000h
        mov ds, ax
        mov di, 0
+       pop ax
 ; counter cx
        mov cx, 64000
 ; counter cx
        mov cx, 64000
-       mov ax, 3
+
 .loop_begin:
 ; dereferrence[] address
        mov [di], al
 .loop_begin:
 ; dereferrence[] address
        mov [di], al
@@ -52,7 +78,16 @@ clearscreen:
        jnz .loop_begin
        ret
 
        jnz .loop_begin
        ret
 
+saved_drive_num:
+       db 0 ; declare byte 0
+
 ; assembler trick: write as many 0 needed to fill 510 bytes
 ; $ <- means here
        times 510-($-$$) db 0
        dw 0aa55h
 ; assembler trick: write as many 0 needed to fill 510 bytes
 ; $ <- means here
        times 510-($-$$) db 0
        dw 0aa55h
+
+sector_2:
+       mov ax, 5
+       call clearscreen
+.inf_loop:
+       jmp .inf_loop