Page 1 of 1

CALL needs a stack?

Posted: Tue Jun 22, 2004 11:35 am
by Eric
Im writing a bootsector..in some sample source I looked at, they have written some "functions" and use CALL to reach them. Just a question. CALL pops the IP to the stack. But how can they use a stack, cause all this was done BEFORE they set the stackpointer and all that. So my question is, how can they use commands that uses a stack, like CALL, when there is no stack yet?

Re:CALL needs a stack?

Posted: Tue Jun 22, 2004 1:26 pm
by pini
1) There is always a stack, because ESP always has a value.

2) CALL pushes IP onto the stack, and RET pops it.

3) The initial value of ESP is 0xfffe when the BIOS gives control to the booloader, so stack is operational....

Maybe you should read back some basic documentation on stack working....

Re:CALL needs a stack?

Posted: Wed Jun 23, 2004 2:06 am
by Pype.Clicker
it's just unsafe to do so because noone ever enforced BIOS vendors to have a specific value in SP so you cannot take for granted that the stack will be large enough for *your* needs...

Re:CALL needs a stack?

Posted: Wed Jun 23, 2004 7:31 am
by Solar
Somewhat related, and with my repository going offline these days... ;-)

I somewhat optimistically assumed that GRUB actually sets up a BSS section appropriately, and tweaked linker script and early startup ASM to set aside a 16k BSS section, which I used as preliminary kernel stack. I never verified this to work correctly, though...

The ASM fragment:

Code: Select all

.asciz "$Id: kernel_loader.asm 10 2003-08-09 12:10:24Z solar $"

.global kernel_loader

# Multiboot header
.set ALIGN,     1<<0             # request loaded modules to be page-aligned
.set MEMINFO,   1<<1             # request bootloader to provide memory map
.set FLAGS,     ALIGN | MEMINFO  # combine flags to flag field
.set MAGIC,     0x1BADB002       # 'magic number' bootloader is looking for
.set CHECKSUM,  -(MAGIC + FLAGS) # checksum required

.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM

.set STACKSIZE, 0x4000           # 16k kernel stack
.comm stack, STACKSIZE, 32       # reserve quadword-aligned stack in .bss

kernel_loader:
               # setting stack
               mov  $(stack + STACKSIZE), %esp
               # preparing void kernel_main(multiboot_data* mbd, int magic) entry

               push %eax                       # pushing magic number
               push %ebx                       # pushing Multiboot data
...
The linker script fragment:

Code: Select all

    .bss :
    {
        _sbss = .;
        *(COMMON)
        *(.bss)
        _ebss = .;
    }