FreeBasic Boot Loader Problem

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
nimday
Posts: 4
Joined: Thu May 29, 2014 4:29 pm

FreeBasic Boot Loader Problem

Post by nimday »

Hello , newbie here , please remove this if not the right place
I made this simple boot loaderwith FB , but there are some problems
I can see the character with qemu , but will crash if i use the procedure(prints)
Also the final size is more than 512 bytes(544 bytes)
Can somebody help me ?

Code: Select all

declare sub main()
declare sub prints(src AS Byte )
sub start()
 asm
  .code16
  call main
  cli
  hlt
 end asm
end sub

sub main()
  asm
    mov al,65
    mov ah,&h0e
    int &H10
  end asm
 'prints 65
end sub

sub prints(src AS Byte )
  dim ch as byte
  ch = src
  asm
    mov al,[ch]
    mov ah,&h0e
    int &H10
  end asm
end sub

sub sig
 asm
 .org 510
 .word 0xAA55
 end asm
end sub

Code: Select all

fbc boot.bas -c
bin\win32\ld -Ttext 0x7c00 -o boot.bin boot.o
objcopy -O binary -j .text boot.bin boot1.bin
objdump -S --disassemble boot.o > boot.dump
pause
I can see some code after boot signature

Code: Select all

00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA 66 5F 66 5E 66 5B C3 90 90 90 90 90 90 90 90 90 FF FF FF FF 00 00 00 00 FF FF FF FF 00 00 00 00
Sorry for my bad english
azblue
Member
Member
Posts: 147
Joined: Sat Feb 27, 2010 8:55 pm

Re: FreeBasic Boot Loader Problem

Post by azblue »

nimday wrote:... but will crash if i use the procedure(prints)
Also the final size is more than 512 bytes(544 bytes)
That's why your print is crashing. You probably want to write a boot loader in assembly and then load your Basic program.


nimday wrote:

Code: Select all

declare sub main()
declare sub prints(src AS Byte )
sub start()
 asm
  .code16
  call main
  cli
  hlt
 end asm
end sub
Why not just put main in here rather than calling it? That'lll save a few bytes. Look at the assembly listing your compiler gives you; it's pushing 3 registers before calls and poping them after. That's a lot of wasted code when you're trying to hit a 512 limit.
nimday wrote:

Code: Select all

sub prints(src AS Byte )
  dim ch as byte
  ch = src
  asm
    mov al,[ch]
Why not just do:

Code: Select all

mov al, [src]
Look at your assembly listing; there's quite a bit of wasted space adding that extra variable. Also, see if there's some option that does not initialize variables to zero; you waste some space with that.
nimday wrote: Sorry for my bad english
Your English seems fine to me :)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: FreeBasic Boot Loader Problem

Post by Combuster »

You can't write naked functions with FreeBasic. You'll always start off with the 32-bit function prologue and the ASM prologue before the content of the ASM block gets executed. All of this is garbage code that gets executed before what you actually wrote as bootloader code does.

Write the whole thing in assembly. There's no Basic code to speak of anyway.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
nimday
Posts: 4
Joined: Thu May 29, 2014 4:29 pm

Re: FreeBasic Boot Loader Problem

Post by nimday »

Thank you both for the comments :)

I think should start with pure assembly for the boot loader.
nimday
Posts: 4
Joined: Thu May 29, 2014 4:29 pm

Re: FreeBasic Boot Loader Problem

Post by nimday »

Ok , this is my solution , write the bootloader with assembly then load FB

Code: Select all

    dim shared vidmem as BYTE PTR = CAST(BYTE PTR, &hB8000)
    declare sub Clr ()
    declare SUB PrintString(src AS Byte Ptr)

    sub main()  

     const s = "Hello World!"
     
     Clr
     
     PrintString CPtr(Byte Ptr, @s)
     
     hang:
      goto hang
     
    end sub

    sub Clr ()  
        dim i as uinteger
        for i = 0 to 80*25
            vidmem[i*2]=32
            Vidmem[i*2+1]=0
        next
    end sub

    SUB PrintString(src AS Byte Ptr)
        dim i as uinteger
        while (src[i] <> 0 )
          vidmem[i*2]=src[i]
          vidmem[i*2+1]=7
          i += 1
        wend
    end sub

Code: Select all

    ;nasm -f bin boot.asm -o boot.bin
    [BITS 16]       ; 16 bit instructions
    [ORG 0x7C00]

    load:
            mov ah, 0               ; floppy drive reset command
            int 13h                 ; bios floppy interrupt
            or ah, ah               ; check for error code
            jnz load                ; repeat if error

            mov ax, 0
            mov es, ax
            mov bx, 0x1000          ; destination address of kernel = 0000:1000
            mov ax ,200h + 10
            mov cx,02h
            mov dh, 0               ; head
            int 13h                 ; bios floppy interrupt
            or ah, ah               ; check for error code
            jnz load                ; repeat if error

            cli                     ; Disable interrupts

            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:kernel_seg      ; Jump to code segment, offset clear_pipe
           
    [BITS 32]
    kernel_seg:
            mov ax, 10h             ; save data segment
            mov ds, ax              ; setup data segment
            mov ss, ax              ; setup stack segment
            mov esp, 090000h        ; move the stack pointer to 090000h

            jmp 08h:01000h          ; jump to our kernel

    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:

    gdt_desc:
            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 identifier

Code: Select all

    /*STARTUP("kernel.o");*/
    INPUT("kernel.o");
    OUTPUT("kernel.bin");
    OUTPUT_FORMAT("binary")
    SECTIONS
    {
        . = 0x1000;
    }

Code: Select all

    fbc -nodeflibs -R -c kernel1.bas -o kernel.o
    ld -Tkernel.ld
    copy /b boot.bin + kernel.bin os.img
    pause
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

Re: FreeBasic Boot Loader Problem

Post by Bender »

I don't have much experience with FB but don't you need a cross compiler?
Like, i686-elf-fbc or at least a cross-linker?
BTW You'll run out of space quickly better implement an FS.
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: FreeBasic Boot Loader Problem

Post by Combuster »

There's no formal assumption-free crosscompiler when it comes to FreeBasic, only hacks. The binary itself can be informed of the requested ABI on the command line (using -target, -arch, -fpu, -gen), after which you'll have to potentially supply it with the additional toolchain binaries it will try to use.
BTW You'll run out of space quickly better implement an FS.
So a filesystem takes negative disk space by definition? :mrgreen:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
nimday
Posts: 4
Joined: Thu May 29, 2014 4:29 pm

Re: FreeBasic Boot Loader Problem

Post by nimday »

Bender wrote:I don't have much experience with FB but don't you need a cross compiler?
Like, i686-elf-fbc or at least a cross-linker?
BTW You'll run out of space quickly better implement an FS.
Thanks , do you think i should use FB-win32-linux-gcc-4.7.3.zip ?
I used ld from FB-win32-dos-binutils-2.23.2.zip for the linker because ld from bin\win32 folder is not very friendly with binary ( cannot perform ...etc)
Post Reply