CALL needs a stack?

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
Eric

CALL needs a stack?

Post 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?
pini

Re:CALL needs a stack?

Post 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....
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:CALL needs a stack?

Post 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...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:CALL needs a stack?

Post 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 = .;
    }
Every good solution is obvious once you've found it.
Post Reply