Halp w/ assembly

dank69

Lifer
Oct 6, 2009
32,860
22,723
136
I've been messing around with assembly (for fun!) because I wanted to understand some of the fundamentals that go into making an OS.

First hit was a PDF I thought was a guide but turns out is a textbook, and an unfinished version at that. I've been able to get most of it to work so far, but I am stuck switching to 32-bit protected mode. Google shows a few results with people struggling with the exact same code from this textbook but their solutions are cryptic to a noob like me.

I'm using nasm and qemu.

asm16.asm:

Code:
org 0x7c00
mov bp, 0x9000
mov sp, bp
mov si, MSG_REAL_MODE
call print_line
call switch_to_pm
jmp $

%include "print_string.asm"
%include "gdt.asm"
%include "print_string_pm.asm"
%include "switch_to_pm.asm"

use32
BEGIN_PM:
mov esi, MSG_PROT_MODE
call print_string_pm
jmp $

HEX_OUT db '0x0000', 0
MSG_REAL_MODE db "Started in 16-bit Real Mode", 0
MSG_PROT_MODE db "Successfully landed in 32-bit Protected Mode", 0
times 510-($-$$) db 0
dw 0xaa55
switch_to_pm.asm:

Code:
use16
switch_to_pm:
mov si, MSG_SWITCHING
call print_line
mov dx, CODE_SEG
call print_hex
mov dx, init_pm
call print_hex
cli
lgdt [gdt_descriptor]
mov eax, cr0
or eax, 0x1
mov cr0, eax
jmp CODE_SEG:init_pm
use32
init_pm:
mov esi, MSG_1
call print_string_pm
mov ax, DATA_SEG
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ebp, 0x90000
mov esp, ebp
call BEGIN_PM
MSG_SWITCHING: db "Switching to 32-bit protected mode...", 0
MSG_1: db "Far jump completed", 0
gdt.asm:

Code:
gdt_start:
gdt_null:
dd 0x0
dd 0x0
gdt_code:
dw 0xffff
dw 0x0
db 0x0
db 10011010b
db 11001111b
db 0x0
gdt_data:
dw 0xffff
dw 0x0
db 0x0
db 10010010b
db 11001111b
db 0x0
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start
I am able to print in 16-bit mode and the print_hex calls produce 0x0008 and 0x7CC3 for CODE_SEG and init_pm respectively. The call to print_string_pm does not work. Nothing seems to work after mov cr0, eax.

Any ideas? The Google results talk about org 0x7c00 causing problems but I can't decipher the solutions.
 

dank69

Lifer
Oct 6, 2009
32,860
22,723
136
Turns out there was nothing wrong with my code. It was making the far jump just fine. There was a problem in the print_string_pm.asm code which I finally figured out.

However, if anyone here has experience with assembly, I am having a problem with a different part of the tutorial. The tutorial gives an example of how to access a mechanical drive with sector/cylinder/head but I don't have a mechanical drive. I need to know how to adjust the code to read from a flash drive instead.
 
Last edited:

ASK THE COMMUNITY