0

I'm trying to switch my code over to use GDT and protected mode, however I'm having an issue to where the new print.asm file isn't printing anything anymore, any help would be appreciated

I've tried debugging this for about 30 minutes and I can't really figure it out

My code: boot.asm -

[ORG 0x7C00]

jmp start

%include "boot/disk.asm"
%include "boot/print.asm"
%include "boot/gdt.asm"
%include "boot/switch.asm"

hang:
    jmp hang

start:
    xor ecx, ecx

    mov bp, 0x9000
    mov sp, bp
    mov ecx, realmsg
    call print

    xor ecx, ecx

    mov ecx, loadmsg
    call print

    xor ecx, ecx

    mov ecx, protmsg
    call print

    call switchPM

    xor ecx, ecx

    mov ecx, diskmsg
    call print

    call diskLoad
    jc diskErr

    jmp hang

done:
    ret

    realmsg db "Loading real mode...", 0x0D, 0x0A, 0
    loadmsg db "Loading OS...", 0x0D, 0x0A, 0
    diskmsg db "Loading disk sectors...", 0x0D, 0x0A, 0
    diskmsgdone db "Disk sectors loaded", 0x0D, 0x0A, 0
    diskerr db "Error loading disk sectors", 0x0D, 0x0A, 0
    protmsg db "Loading protected mode...", 0x0D, 0x0A, 0
    protmsgdone db "Protected mode loaded", 0x0D, 0x0A, 0

    times 510-($-$$) db 0
    db 0x55
    db 0xAA

[bits 32]
beginPM:
    xor ecx, ecx

    mov ecx, protmsgdone
    call print

gdt.asm -

gdtStart:
    dd 0x0
    dd 0x0

gdtCode:
    dw 0xFFFF
    dw 0x0
    db 0x0
    db 10011010b
    db 11001111b
    db 0x0

gdtData:
    dw 0xFFFF
    dw 0x0
    db 0x0
    db 10010010b
    db 11001111b
    db 0x0

gdtEnd:

gdtDescriptor:
    dw gdtEnd - gdtStart - 1
    dd gdtStart

codeSeg equ gdtCode - gdtStart
dataSeg equ gdtData - gdtStart

switch.asm -

[bits 16]
switchPM:
    cli
    lgdt [gdtDescriptor]
    mov eax, cr0
    or eax, 0x1
    mov cr0, eax
    jmp codeSeg:initPM

[bits 32]
initPM:
    mov ax, dataSeg
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    mov ebp, 0x90000
    mov esp, ebp

    call beginPM

print.asm -

[bits 32]

videoMemory equ 0xb8000
whiteOnBlack equ 0x0f

print:
    pusha
    mov edx, videoMemory
    mov ecx, 0
    mov ebx, whiteOnBlack

    call printString
    popa
    ret

printString:
    pusha
    mov ah, 0x0e

    .loop:
        lodsb
        cmp al, 0
        je .done
        mov [edx], ax
        add edx, 2
        jmp .loop

    .done:
        popa
        ret

disk.asm -

diskLoad:
    mov bx, 0x8000
    mov dh, 0x00
    mov dl, 0x80
    mov ch, 0x00
    mov cl, 0x02
    mov al, 0x01
    mov ah, 0x01
    int 0x13
    jc diskLoad
    ret
    
diskErr:
    xor si, si

    mov si, diskerr
    call print
    jmp hang
Pigioty
  • 11
  • 6
  • 1
    You are invoking `print` from real mode. That of course won't work. Then your `switchPM` references `beginPM` but that is in the second sector and has not been loaded yet (if at all, you did not include the code for `diskLoad`). – Jester Jun 28 '23 at 21:53
  • Sorry, I've edited the message to include disk.asm – Pigioty Jun 28 '23 at 22:05
  • But the `call diskLoad` is **after** the `call switchPM` so the the `beginPM` is not yet loaded. Not to mention that `call switchPM` doesn't return so any code afterwards is pointless. – Jester Jun 28 '23 at 22:13
  • I had tried to move the code, didn't work, and thanks I didn't think about that – Pigioty Jun 28 '23 at 22:15
  • 3
    Also you don't initialize real mode segments (which I have already told you in one of your previous questions). Furthermore your includes mess up the `bits`, your real mode code will be assembled as 32 bit as far as I can tell. – Jester Jun 28 '23 at 22:20
  • 3
    You can single-step your boot sector in Bochs to see exactly what happens, and whether it matches what you expect. (Including showing disassembly of the next instructions in the CPUs current mode, so you can debug the switch to 32-bit protected mode, and whether you assembled things the right way.) Setting up a good debugging environment will save you *way* more time in the long run, and teach you way more about what's going on. – Peter Cordes Jun 28 '23 at 22:37
  • @Jester, do you mind giving me some direction on how I could initialize real mode segments? – Pigioty Jun 29 '23 at 00:21
  • 1
    You should zero at least `ds`, `es` and `ss`. Also note that your `diskLoad` does not load anything because you used the wrong function code and even if it did, it would load to `0x8000` which is not what the rest of the program expects. – Jester Jun 29 '23 at 01:04

0 Answers0