weird 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
DruG5t0r3

weird stack

Post by DruG5t0r3 »

I have something odd going on here.

I pushed this onto the stack. Compiled with nasm

push ds
push es
push fs
push gs


but looking at the stack, it actually pushed double words (4 bytes). Not 2 bytes as a segment register should be. Any thoughts?
AR

Re:weird stack

Post by AR »

In 32bit mode, stack operations are always 32bits (4 bytes). When you "pop ds", it will remove 32bits from the stack but only loads the first 16 into DS.

As a side note: Do not prefix in the instructions with o16, if you do you will make a mess of the C handler:

Code: Select all

void ISR(..., unsigned int FSandGS, unsigned int DSandES, ...)
or

Code: Select all

struct SegRegs { unsigned short FS, GS, DS, ES; } __attribute__((__packed__));
void ISR(..., SegRegs sr, ...)
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:weird stack

Post by Pype.Clicker »

Get a look at intel manuals ... that's exactly what's supposed to happen (for performance, alignment and another couple of GoodThings)
DruG5t0r3

Re:weird stack

Post by DruG5t0r3 »

thanks guys...its just thats its a little ugly for my reg struct...

struct regs { unsigned short ds, unsigned short pading...

you get the idea...
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:weird stack

Post by Pype.Clicker »

you have the option of declaring 'pure-padding' fields too!

Code: Select all

struct regs {
     unsigned short ds;
     unsigned :16;
     unsigned short es;
     unsigned :16;
     ...
};
iirc, that's even ANSI C :)
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:weird stack

Post by Colonel Kernel »

Pype.Clicker wrote: you have the option of declaring 'pure-padding' fields too!
Anonymous bitfields... hadn't thought of trying that. :) I wonder if Doxygen will choke on them though...
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
zyp

Re:weird stack

Post by zyp »

What if you just use unsigned int?
Are the upper two bytes zeroed, or may they contain garbage?
DruG5t0r3

Re:weird stack

Post by DruG5t0r3 »

i've tried, it doesn't work, padding does.
Post Reply