My BSOD

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Edwin
Posts: 12
Joined: Thu Dec 21, 2006 5:19 pm

Post by Edwin »

[quote]
4.2.2. Stack Alignment
The stack pointer for a stack segment should be aligned on 16-bit (word) or 32-bit (double-word)
boundaries, depending on the width of the stack segment. The D flag in the segment descriptor
for the current code segment sets the stack-segment width (see “Segment Descriptorsâ€
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

Post by Pyrofan1 »

User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post by inflater »

If a 16-bit value is pushed onto a 32-bit wide
stack, the value is automatically padded with zeros out to 32 bits.
Intel manuals say all! :D
Okay okay, I will read them more...

inflater
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
Aali
Member
Member
Posts: 58
Joined: Sat Apr 14, 2007 12:13 pm

Post by Aali »

i hate to break it to you, but thats just wrong

i have code that DEPENDS on this behaviour

the following code:

push word ax
push word 0x4040
inc esp
push 0x5b585252
push word ax

produces this stack:

0xbfe0ac73: 0xcd 0x80 0x52 0x52 0x58 0x5b 0x40 0xcd 0x80

notice the complete lack of zero padding
Edwin
Posts: 12
Joined: Thu Dec 21, 2006 5:19 pm

Post by Edwin »

perhaps removing "word" will do the trick..
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

Edwin wrote:perhaps removing "word" will do the trick..
The "word" is there to force 16-bit and prove that on his processor stack alignment is not forced with padding.
Edwin
Posts: 12
Joined: Thu Dec 21, 2006 5:19 pm

Post by Edwin »

Okay, that may be right, but there is no reason why an ordinary push bx or pop bx should screw up stack alignment.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post by inflater »

The "word" is there to force 16-bit
I'm confused.. Why to force 16-bit? The stack will become, logically, unaligned then...

Never mind that, I heard MOV EAX, [ESP - X] should do the POP operation without screwing the stack. What I need to appoint for the X when I want to pop the last dword on the stack? 8? And for word value, 4 ?

inflater
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
Aali
Member
Member
Posts: 58
Joined: Sat Apr 14, 2007 12:13 pm

Post by Aali »

Edwin wrote:Okay, that may be right, but there is no reason why an ordinary push bx or pop bx should screw up stack alignment.
the problem is, there is no "ordinary" push bx or pop bx, you have to use the 0x66 prefix (which turns it into a 16bit operation)

56 00000042 6650 push word ax
60 0000004E 6650 push ax

exactly the same opcode, the word is just there to clarify things a bit
Edwin
Posts: 12
Joined: Thu Dec 21, 2006 5:19 pm

Post by Edwin »

Hmm I tried it myself and it seems you're right Aali, it seems that successive 16-bit push and pops indeed screw up the alignment, but when I do a push ax and then a 32-bit push, stack alignment seems normal.
So I think that the CPU tries to align the stack to the size of the pushed or popped value. It's confusing because the Intel-manual is not clear about this issue (as far as i've seen).
Post Reply