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.
iansjack wrote:When pushing values to the stack the stack counter is pre-decremented. The code is correct.
Thanks for answering.
On [this site] it writes that push first decrements ESP by 4 (one word). This would mean that if we set up a stack of size X bytes, we can only ever use X-3 bytes, with the current code.
I am considering to use the code below. Am I going too far to fix a non-issue?
- If you push something in 32-bit mode, you write 32 bits = 4 bytes to the stack.
- A word is not 4 bytes. For intel, it's two, and it can be anything else depending on the platform. Therefore it's a horrible term to use.
- A stack always has the values stored adjacent in memory, so it always uses exactly a multiple of 4 bytes. It makes no sense to add two bytes because they can't hold a full entry and would be wasted.
- A stack should be aligned. Anything else is a bug.
A 32-bit push decrements ESP by four, then writes four bytes at ESP (that is, ESP+0, ESP+1, ESP+2 and ESP+3). If you would still want to manually write a byte at ESP+4 you'd write after stack_end, and therefore not into any memory you've defined. If you point ESP at stack_end - 1 then the last byte of the stack doesn't get used (and you lose three at the other end)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
If you don't understand what various instructions do to the registers and memory then the easiest thing to do is to single-step code in a debugger. Write a simple program that sets up a stack and pushes a value to it. Step that program in your debugger and watch what happens to the stack pointer and to the stack itself; then you will see how it works.
Again, the Wiki is correct - you set SP to stack_end not stack_end - 1.
Thank you for your posts. I misunderstood what stack_end stands for.
I thought stack_end points to an imaginary 4097'th byte, but instead it really does point to the last byte, 4096'th.
It does point after the last byte. It also points at 4096.
stack_begin points to the 0th byte.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]