Real mode bare bones and the stack

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
Post Reply
YanivSB
Posts: 3
Joined: Tue May 06, 2014 7:56 am

Real mode bare bones and the stack

Post by YanivSB »

Hi,
I'm new to this forum and I hope I'm posting according to the rules.
I tried finding an answer for this but couldn't see one.
In the real mode assembly bear bones:
http://wiki.osdev.org/Real_mode_assembly_bare_bones
there is a use of the call instruction, meaning it is using the stack.
However, in the original bare bones tutorial:
http://wiki.osdev.org/Bare_bones
It says that the esp points at anything and using it may cause harm. So why is it ok to use it in the first tutorial without setting it up first? Does it have something to do with GRUB being present in Bare bones?

Thanks!
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Real mode bare bones and the stack

Post by sortie »

These two tutorials are fully unrelated and take place in two different environments (the real mode tutorial is basically a bootloader, while the regular bare bones tutorial is a multiboot kernel loaded by GRUB). Things are different in those two environments. You need to think critically.

It may well be a bug in the Real Mode Bare Bones tutorial that it doesn't set up its own stack pointer and trusts the BIOS to set up a stack.
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: Real mode bare bones and the stack

Post by Octocontrabass »

Indeed, that is a bug in the tutorial. The BIOS stack is not guaranteed to be large enough for anything but the default IRQ handlers. In particular, extended int 0x13 functions are likely to cause problems if the stack is too small.
YanivSB
Posts: 3
Joined: Tue May 06, 2014 7:56 am

Re: Real mode bare bones and the stack

Post by YanivSB »

Thanks!
Just to be sure I get it - both tutorials run in real mode. The difference is that on of them runs after the BIOS, which leaves the system in a certain state, while the other runs after GRUB, which leaves the system in an other state. Both of them have no guarantee regarding the stack state. Is this correct? Say I want to find out about how the BIOS leaves the system, how will I go about it?
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: Real mode bare bones and the stack

Post by Octocontrabass »

GRUB leaves the CPU in protected mode, not real mode.

The BIOS is much less predictable than GRUB, but most will leave the system in real mode with a small stack set up, the drive number for int 0x13 in DL, and CS:IP pointing to either 0000:7C00 or 07C0:0000.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Real mode bare bones and the stack

Post by sortie »

Octocontrabass: The multiboot specification leaves the stack unspecified.
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: Real mode bare bones and the stack

Post by Octocontrabass »

Sortie: Yes, I forgot to mention that part. Proofreading is difficult from my phone. #-o

GRUB leaves the system according to the multiboot specification. Multiboot doesn't specify how the stack should be set up, so GRUB doesn't set one up for you.
YanivSB
Posts: 3
Joined: Tue May 06, 2014 7:56 am

Re: Real mode bare bones and the stack

Post by YanivSB »

Oh, got it. Thank you all!
Post Reply