Pushing to stack when SP is 0

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
JuEeHa
Member
Member
Posts: 30
Joined: Thu Mar 10, 2011 4:24 am

Pushing to stack when SP is 0

Post by JuEeHa »

What happens if you do it in real mode? Is interrupt generated? I couldn't find any info with google.
Using 700MHz Pentium III machine with 64MB of RAM because I feel like it.
ed implementation in C: main(a){for(;;;){read(0,&a,1);if(a=='\n')write(1,"?\n",2);}}
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Pushing to stack when SP is 0

Post by bluemoon »

In true real mode, it's pushed on FFFE. You could do an experiment on it.
It may be different on unreal or v86 mode.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Pushing to stack when SP is 0

Post by Solar »

bluemoon wrote:You could do an experiment on it.
A shudder runs down my spine. Verily, evil words should not be spoken in jest...

Ash nazg durbatulûk, ash nazg gimbatul, ash nazg thrakatulûk agh burzum-ishi krimpatul...
Every good solution is obvious once you've found it.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Pushing to stack when SP is 0

Post by xenos »

Solar wrote:Ash nazg durbatulûk, ash nazg gimbatul, ash nazg thrakatulûk agh burzum-ishi krimpatul...
In the context of a wrapping stack pointer, this probably translates as "One ring buffer to rule them all..."
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Pushing to stack when SP is 0

Post by Solar »

I was more thinking along the lines of "let's try this, see what it does, and then extrapolate from there. Gee, it makes me invisible, that's a good thing to have". :twisted:

(I.e., speechless and somewhat perversely curious smiling at the concept of figuring out stuff like this by trial & error. If you had seen the things I have seen under comment lines like "I tried this and it seems to work"...)
Every good solution is obvious once you've found it.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Pushing to stack when SP is 0

Post by gerryg400 »

It does wrap around from $0 to $fffe on a 16 bit push, but the same does not apply for $1. If %sp is $1 then a push will cause a shutdown.
If a trainstation is where trains stop, what is a workstation ?
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: Pushing to stack when SP is 0

Post by Rudster816 »

gerryg400 wrote:It does wrap around from $0 to $fffe on a 16 bit push, but the same does not apply for $1. If %sp is $1 then a push will cause a shutdown.
No, it just wraps to 0xFFFF. There is zero alignment checking in real mode.

Code: Select all

mov esp, 1
push 0xDEAD
Executing that snippet in Qemu moves the word 0xDEAD to 0xFFFF, and sets SP to 0xFFFF. Memory looks like this:
0x0FFFF: 0xAD
0x10000: 0xDE
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Pushing to stack when SP is 0

Post by gerryg400 »

Qemu is an emulator. Real machines shut down. Or at least they are supposed to.
If a trainstation is where trains stop, what is a workstation ?
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Pushing to stack when SP is 0

Post by iansjack »

According to the Intel manual:
If the ESP or SP register is 1 when the PUSH instruction is executed in real-address mode, a stack-fault exception (#SS) is generated (because the limit of the stack segment is violated). Its delivery encounters a second stack-fault exception (for the same reason), causing generation of a double-fault exception (#DF). Delivery of the double-fault exception encounters a third stack-fault exception, and the logical processor enters shutdown mode. See the discussion of the double-fault exception in Chapter 6 of the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 3A.
rdos
Member
Member
Posts: 3308
Joined: Wed Oct 01, 2008 1:55 pm

Re: Pushing to stack when SP is 0

Post by rdos »

The fault in protected mode is probably also related to limit violations, and not to "wrap around". IOW, if you have a flat stack selector, and wrap it around, there will be no faults.
rdos
Member
Member
Posts: 3308
Joined: Wed Oct 01, 2008 1:55 pm

Re: Pushing to stack when SP is 0

Post by rdos »

Rudster816 wrote:Executing that snippet in Qemu moves the word 0xDEAD to 0xFFFF, and sets SP to 0xFFFF. Memory looks like this:
0x0FFFF: 0xAD
0x10000: 0xDE
Send a bug-report to them. This is clearly wrong. One might wonder if they do proper limit-checking in other cases as well. #-o
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Pushing to stack when SP is 0

Post by gerryg400 »

rdos wrote:The fault in protected mode is probably also related to limit violations, and not to "wrap around". IOW, if you have a flat stack selector, and wrap it around, there will be no faults.
That's true. However my recollection is that if for example you set ESP to $2 and push 32 bits then ESP will end up being $fffffffe but half of the pushed value will be lost. I should warn that my recollections are not always 100%.
If a trainstation is where trains stop, what is a workstation ?
Post Reply