DEV Community

Jaipal001
Jaipal001

Posted on

64-bit bootloader from scratch in AT&T assembly, Part-2

we will continue from disable_int

    cli                 # disables CPU intrupts
    lgdt gdtp          # A data structure to manage memory
    lidt idt           # IDK what does this do but we need it too
                            # All the variables we be defined later

    mov %cr0, %eax    # This works in 16-bit mode!
    or $0x1, %eax     # Modify the 32-bit register
    mov %eax, %cr0    # Write it back

    movw $(gdt_data_segment - gdt_start), %ax  # GDT data segment size, copy to all segments
    movw %ax, %ds
    movw %ax, %es
    movw %ax, %es
    movw %ax, %fs
    movw %ax, %gs
    movw %ax, %ss

    ljmp $(gdt_code_segment - gdt_start), $entry32   # Now we are ready to jump to 32-bit mode
Enter fullscreen mode Exit fullscreen mode

But lets define print and println first as they are called while kernel is being loaded from disk in intrupt mode

It will also be in 16-bit mode that's why we are defining it before going to 32-bit protected mode

print:
    lodsb        # Not explaining, check it: https://www.felixcloutier.com/x86/lods:lodsb:lodsw:lodsd:lodsq

print_loop:
    inb (%dx), %al  # characters, tried `movb` but didn't work
    cmp $0, %al      # check if its null terminator
    je print_end
    mov $0x0e, %ah  # color ig?
    mov $0x07, %bl  # and some config, I forgot
    int $0x10        # write to bios screen
    add $1, %dx      # next letter pointer
    jmp print_loop
print_end:
    ret

println:
    mov $0x0e, %ah
    mov $0xa, %al
    int $0x10
    mov $0x20, %al
    int $0x10
    ret
Enter fullscreen mode Exit fullscreen mode

With this 16-bit mode ends, next will be 32-bit mode where we will load our kernel

Top comments (0)