Page 1 of 1

Emulation software freezes when setting stack pointer (VBox)

Posted: Wed Oct 08, 2014 5:23 am
by jrhetf4xb
Hi,

I have been trying to roll my own bootloader for the past couple of days and I'm having some problems with VirtualBox.
Everything seems to be working fine until I attempt to set the stack pointer, at which point my "kernel" freezes.
I discovered this by attempting to print characters on the screen. If I don't touch the stack pointer, they print. If I do, they don't (hence the freeze).
This is the line of code that is problematic:

Code: Select all

movw $0x9c00, %sp
And this is the big picture:

Code: Select all

.section .data
	ch: .byte 0x50

.section .text
	.globl bootloader

bootloader:
	cli

	movw $0, %ax
	movw %ax, %es
	movw $0x07c0, %ax
	movw %ax, %ds
	movw %ax, %ss
	movw $0x9c00, %sp

	sti

	movb $0x0E, %ah
	movb ch, %al
	int $0x10	
	movb $0x41, %al
	int $0x10
	movb $0x43, %al
	int $0x10
	movb $0x41, %al
	int $0x10	
  halt:
	jmp halt
Does anyone know what's causing this?

Re: Emulation software freezes when setting stack pointer (V

Posted: Wed Oct 08, 2014 6:18 am
by Combuster
.section .data
Where's the linker configuration, and where's the 16 bits code directive?

GAS can't create a bootsector without complications and the help of other apps. People often use an assembler that can write binary files directly for simplicity (yasm/nasm/fasm)

Re: Emulation software freezes when setting stack pointer (V

Posted: Wed Oct 08, 2014 8:02 am
by jrhetf4xb
This is my linker script (from a post on stackexchange.com):

Code: Select all

ENTRY(bootloader);
SECTIONS
{    
    . = 0x7C00;    
    .text : AT(0x7C00)
    {
        _text = .;
        *(.text);
        _text_end = .;
    }
    .data :
    {
        _data = .;
        *(.bss);
        *(.bss*);
        *(.data);
        *(.rodata*);
        *(COMMON)
        _data_end = .;
    }    
    .sig : AT(0x7DFE)    
    {        
        SHORT(0xaa55);
    }    
    /DISCARD/ :
    {
        *(.note*);
        *(.iplt*);
        *(.igot*);
        *(.rel*);
        *(.comment);
        /* add any unwanted sections spewed out by your version of gcc and flags here */    
    }
}


And this is my shell script to build a binary and create a floppy image so that I can load it into VirtualBox:

Code: Select all

/usr/local/cross/bin/i586-elf-gcc -c -Os -march=i386 -ffreestanding -Werror -I. -o bootloader.o bootloader.S

/usr/local/cross/bin/i586-elf-ld -Tlinker.ld -nostdlib --nmagic -o bootloader bootloader.o

objcopy -O binary bootloader bootloader.bin

dd if=/dev/zero of=floppy.img bs=1024 count=1440

dd if=bootloader.bin of=floppy.img bs=1 count=512 conv=notrunc

As for the 16bit directive, I'm not sure how to specify this one... Tried things like .code16 or .code16gcc but it gave me errors so I just left it like this...
EDIT:
Should I just switch to NASM or keep on with GAS?

Re: Emulation software freezes when setting stack pointer (V

Posted: Wed Oct 08, 2014 8:15 am
by no92
jrhetf4xb wrote:Should I just switch to NASM or keep on with GAS?
I think Combuster recommends using yasm/nasm/fasm, assuming I'm not completely misunderstanding him.

Re: Emulation software freezes when setting stack pointer (V

Posted: Wed Oct 08, 2014 8:17 am
by iocoder
jrhetf4xb wrote:Tried things like .code16 or .code16gcc but it gave me errors so I just left it like this...
What errors?! Can you post them please? It is necessary that your assembler should generate 16-bit code.

Code: Select all

    . = 0x7C00;   
    .text : AT(0x7C00)
Well, since you assume that your code is at 0x7C00, then you should set DS to $0 so that the address of "ch" after the linking stage matches with its real location in memory. I dunno why you set SS to $0x7C0 then set SP to $0x9C00. I believe it makes more sense to set SS to $0, unless you want your stack top to be at $0x11800.

Re: Emulation software freezes when setting stack pointer (V

Posted: Wed Oct 08, 2014 10:38 am
by jrhetf4xb
Alright, I put the .code16 directive and I changed the start of the segments and voila! It works, thank you!
As for the error, it might have been something with cygwin but now I'm using a Linux image so it's OK.