Bona Fide OS Developer
View unanswered posts | View active topics It is currently Sun Feb 25, 2018 4:34 am

Post new topic Reply to topic  [ 3 posts ] 
 Problems with tutorial. 
Author Message

Joined: Fri Mar 18, 2011 3:49 pm
Posts: 2
Post Problems with tutorial.
Hi everybody.

I have some questions about tutorials. More precisely those which are on bootloader running kernel written in c.

I followed step by step it but i don't have the same result : i boot but the c kernel doesn't seem to be read...

My computer runs with WinXP SP3. I use Notepad++ as text editor. I compile with the last version of nasm and DJGPP/GCC. I make my floppy images with makeboot and I run them with bochs. I tried with the partcopy, dd and vfd combo but without any success. I tried with Mingw/GCC... no more success. There is something i didn't see ?

I write below the code i used (which can be found on this website):
const char *tutorial3;

void main()


const char *tutorial3 = "MuOS Tutorial 3";

void clrscr()
  unsigned char *vidmem = (unsigned char *)0xB8000;
  const long size = 80*25;
  long loop;

  // Clear visible video memory
  for (loop=0; loop<size; loop++) {
    *vidmem++ = 0;
    *vidmem++ = 0xF;

  // Set cursor position to 0,0
  out(0x3D4, 14);
  out(0x3D5, 0);
  out(0x3D4, 15);
  out(0x3D5, 0);

void print(const char *_message)
  unsigned short offset;
  unsigned long i;
  unsigned char *vidmem = (unsigned char *)0xB8000;

  // Read cursor position
  out(0x3D4, 14);
  offset = in(0x3D5) << 8;
  out(0x3D4, 15);
  offset |= in(0x3D5);

  // Start at writing at cursor position
  vidmem += offset*2;

  // Continue until we reach null character
  i = 0;
  while (_message[i] != 0) {
    *vidmem = _message[i++];
   vidmem += 2;

  // Set new cursor position
  offset += i;
  out(0x3D5, (unsigned char)(offset));
  out(0x3D4, 14);
  out(0x3D5, (unsigned char)(offset >> 8));

unsigned char in(unsigned short _port)
  // "=a" (result) means: put AL register in variable result when finished
  // "d" (_port) means: load EDX with _port
  unsigned char result;
  __asm__  ("in %%dx, %%al" : "=a" (result) : "d" (_port));
  return result;

void out(unsigned short _port, unsigned char _data)
  // "a" (_data) means: load EAX with _data
  // "d" (_port) means: load EDX with _port
  __asm__ ("out %%al, %%dx" : :"a" (_data), "d" (_port));

[BITS 16]       ; We need 16-bit intructions for Real mode

[ORG 0x7C00]    ; The BIOS loads the boot sector into memory location 0x7C00

mov ah, 0Eh      ; Display 'A'
mov al, 'A'
mov bh, 0Fh
mov bl, 0
int 10h

        mov ah, 0               ; RESET-command
        int 13h                 ; Call interrupt 13h
        or ah, ah               ; Check for error code
        jnz reset_drive         ; Try again if ah != 0

        mov ax, 0
        mov es, ax
        mov bx, 0x1000          ; Destination address = 0000:1000

        mov ah, 02h             ; READ SECTOR-command
        mov al, 02h             ; Number of sectors to read = 1
        mov ch, 0               ; Cylinder = 0
        mov cl, 02h             ; Sector = 2
        mov dh, 0               ; Head = 0
        int 13h                 ; Call interrupt 13h
        or ah, ah               ; Check for error code
        jnz reset_drive         ; Try again if ah != 0

        cli                     ; Disable interrupts, we want to be alone

        xor ax, ax
        mov ds, ax              ; Set DS-register to 0 - used by lgdt

        lgdt [gdt_desc]         ; Load the GDT descriptor

        mov eax, cr0            ; Copy the contents of CR0 into EAX
        or eax, 1               ; Set bit 0
        mov cr0, eax            ; Copy the contents of EAX into CR0

        jmp 08h:clear_pipe      ; Jump to code segment, offset clear_pipe

[BITS 32]                       ; We now need 32-bit instructions
        mov ax, 10h             ; Save data segment identifyer
        mov ds, ax              ; Move a valid data segment into the data segment register
        mov ss, ax              ; Move a valid data segment into the stack segment register
        mov esp, 090000h        ; Move the stack pointer to 090000h

        jmp 08h:01000h          ; Jump to section 08h (code), offset 01000h

gdt:                    ; Address for the GDT

gdt_null:               ; Null Segment
        dd 0
        dd 0

gdt_code:               ; Code segment, read/execute, nonconforming
        dw 0FFFFh
        dw 0
        db 0
        db 10011010b
        db 11001111b
        db 0

gdt_data:               ; Data segment, read/write, expand down
        dw 0FFFFh
        dw 0
        db 0
        db 10010010b
        db 11001111b
        db 0

gdt_end:                ; Used to calculate the size of the GDT

gdt_desc:                       ; The GDT descriptor
        dw gdt_end - gdt - 1    ; Limit (size)
        dd gdt                  ; Address of the GDT

times 510-($-$$) db 0           ; Fill up the file with zeros

        dw 0AA55h                ; Boot sector identifyer

@echo on
nasm -f bin loader.asm -o loader.bin

gcc -ffreestanding -c main.c -o main.o
gcc -c video.c -o video.o
gcc -c ports.c -o ports.o

ld -e _main -Ttext 0x1000 -o kernel.o main.o video.o ports.o

ld -i -e _main -Ttext 0x1000 -o kernel.o main.o video.o ports.o
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin

makeboot floppy.img loader.bin kernel.bin
bochs -f bochsrc.txt

So, does this code run for you? Could you display code dump for kernel.bin, loader.bin and floppy.img, and their size in bytes? Or tell me what software do you use and with which parameters?

Thanks you.

Fri Mar 18, 2011 4:17 pm

Joined: Fri Mar 18, 2011 3:49 pm
Posts: 2
Post Re: Problems with tutorial.
That's ok.
I solved my problem by using boil plate and linker script.

Sun Mar 20, 2011 10:13 am
Site Admin

Joined: Fri Jul 24, 2009 10:02 pm
Posts: 247
Location: Las Vegas, NV, US
Post Re: Problems with tutorial.
So what exactly was the issue and how did you fix it?

It may be the compiler tools you are using. Some react differently than others.

Tue Aug 23, 2011 1:15 am
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin and tweaked by the BF Team.