relocating bootloader

Programming, for all ages and all languages.
Post Reply
schilds
Member
Member
Posts: 32
Joined: Sat May 07, 2011 8:21 am

relocating bootloader

Post by schilds »

My bootloader appears to relocate itself and run correctly, is anyone able (and willing, thanks in advance ;)) to verify that the code does what I think it does?

https://github.com/schilds/Serial-BootL ... loader.asm

It should relocate some of itself (the main loop and some functions) to just before where it used to be (i.e. just before the boot sector, 0x7c00). The function of the relocated main loop should be to read and parse bytes from the serial port, store them to the (now free to overwrite) boot sector and then execute them.

Basically I've written the relocation as if it were position independent (i.e. works with just a simple copy), but I'm not actually sure that it is. How do I know whether the assembler is creating absolute or relative jumps/calls?
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: relocating bootloader

Post by Combuster »

You can always disassemble the output and check the jump opcodes to see if they are absolute (FF) or relative (EB/E9). Most assemblers default to the latter though because it's always the shorter encoding.
"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 ]
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: relocating bootloader

Post by Brendan »

Hi,
schilds wrote:My bootloader appears to relocate itself and run correctly, is anyone able (and willing, thanks in advance ;)) to verify that the code does what I think it does?

https://github.com/schilds/Serial-BootL ... loader.asm
You forgot to make sure that the "direction" flag is clear. If the BIOS left it set, then the copy will work backwards and copy the wrong thing.
schilds wrote:Basically I've written the relocation as if it were position independent (i.e. works with just a simple copy), but I'm not actually sure that it is. How do I know whether the assembler is creating absolute or relative jumps/calls?
It's much easier to do the reverse. For example, use "org 0x7A00" at the start so that after relocation everything is as the assembler expects, and do the relocation first so that you don't need to care about "position independent" for almost all of your code.

For example:

Code: Select all

    org 0x7A00
    bits 16

start:
    jmp .initialise

;Put a BPB here, if necessary

.initialise:
    xor ax,ax
    mov es,ax
    mov ds,ax
    cli
    mov ss,ax
    mov sp,0x7A00
    sti
    cld
    mov cx,512/2
    mov si,0x7C00
    mov di,0x7A00
    rep movsw
    jmp 0x0000:main

main:
Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
schilds
Member
Member
Posts: 32
Joined: Sat May 07, 2011 8:21 am

Re: relocating bootloader

Post by schilds »

Oooh, neat.

Thanks guys.
Post Reply