Search code examples
assemblyx86bootloader

Boot looping when trying to switch to 32 bit mode


When switching from 16 bit to 32 bit mode using my bootloader it boot loops, I know this is whats causing the issue as all code before causes the expected results. Any suggestions as to what may be wrong?

My code:

[bits 16]
[org 0x7c00]

mov [bootDrive], dl

mov bp, 0x7c00
mov sp, bp

mov bx, welcomeMsg
call print

call inputStart

in al, 0x92
or al, 2
out 0x92, al

cli
lgdt [descriptorGDT]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp GDTCodeSeg:startProtectedMode

jmp $

%include "src/print.asm"
%include "src/BIOSKeyboardInput.asm"
%include "src/GDT.asm"

bootDrive: db 0

welcomeMsg: db "Hello there! Successfully entered bootloader.", 0

[bits 32]
startProtectedMode:
    mov ax, GDTDataSeg
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    mov ax, 0x0F41
    mov [0xb8000], ax

    jmp $

times 510-($-$$) db 0
db 0x55, 0xaa

GDT.asm:

GDTNullSeg:  
    dd 0x0  
    dd 0x0  
  
GDTCodeSeg:  
    dw 0xFFFF  
    dw 0x0000  
    db 0x00  
    db 0b10011010  
    db 0b11001111  
    db 0x00  
  
GDTDataSeg:  
    dw 0xFFFF  
    dw 0x0000  
    db 0x00  
    db 0b10010010  
    db 0b11001111  
    db 0x00  
  
GDTEnd:  
  
descriptorGDT:  
    GDTSize:  
        dw GDTEnd - GDTNullSeg - 1  
        dd GDTNullSeg  
  
GDTCodeSegSize equ GDTCodeSeg - GDTNullSeg  
GDTDataSegSize equ GDTDataSeg - GDTNullSeg 

EDIT: print.asm:

print:
    mov ah, 0x0e

    loop:
        mov al, [bx]
        cmp al, 0
        je exit
            int 0x10
            inc bx
            jmp loop

    exit:
        ret 

BIOSKeyboardInput.asm:

inputStart:
    mov bx, inputInfo
    call print

    call input
    ret

input:
    mov ah, 0x00
    int 0x16

    cmp al, 0x1B
    je exitInput

    cmp ah, 0x4B
    je leftArrowInput

    cmp ah, 0x4D
    je rightArrowInput

    cmp ah, 0x48
    je upArrowInput

    cmp ah, 0x50
    je downArrowInput

    cmp ah, 0x47
    je homeInput

    mov ah, 0x0e
    int 0x10

    cmp al, 0x8
    je backspaceInput

    jmp input

leftArrowInput:
    mov al, 0x8
    mov ah, 0x0e
    int 0x10
    jmp input

rightArrowInput:
    mov al, 0x20
    mov ah, 0x0e
    int 0x10
    jmp input

upArrowInput:
    jmp input

downArrowInput:
    jmp input

backspaceInput:
    mov al, 0x20
    int 0x10
    mov al, 0x8
    int 0x10
    jmp input

homeInput:
    mov al, 0x17
    mov ah, 0x0e
    int 0x10
    jmp input

exitInput:
    ret

inputInfo: db 0xA, 0xD, "To exit input mode click ESC.", 0

Solution

  • Answer thanks to Jester in comments:

    Issue is:

    jmp GDTCodeSeg:startProtectedMode
    

    and

    mov ax, GDTDataSeg
    

    You need to supply a segment selector.
    Instead should be:

    jmp 8:startProtectedMode
    

    and

    mov ax, 16