views:

179

answers:

4

Hi,

I've come to learn that you cannot push a byte directly onto the Intel Pentium's stack, can anyone explain this to me please?

The reason that I've been given is because the esp register is word-addressable (or, that is the assumption in our model) and it must be an "even address". I would have assumed decrementing the value of some 32-bit binary number wouldn't mess with the alignment of the register, but apparently I don't understand enough.

I have tried some NASM tests and come up that if I declare a variable (bite db 123) and push it on to the stack, esp is decremented by 4 (indicating that it pushed 32-bits?). But, "push byte bite" (sorry for my choice of variable names) will result in a kind error:

test.asm:10: error: Unsupported non-32-bit ELF relocation

Any words of wisdom would be greatly appreciated during this troubled time. I am first year undergraduate so sorry for my naivety in any of this.

Tim

A: 

The stack pointer must be (for some optimalization reasons) 4B aligned -> it should be divisible by four (and, therefore, have last 2 bits zero).

Yossarian
While this does make sense, Yully's answer explains it from the bottom up. Thanks anyway :)
Tim Green
+1  A: 

It'll make the stack pointer not able to do its job in some cases. for instance, lets say you had a function which pushed a byte onto the stack and then calls another function. The call will end up trying to write a misaligned return address onto the stack, resulting in an error.

Yuliy
Perfect, thank you :)
Tim Green
A: 

what you want to do is use the bit rotation opcodes to rotate through each 32-bit memory location, placing 8 bits at a time into the register until you have rotated back to the starting bit positions. now you should have 4 8-bit quantities lined up side by side in your 32 bit register. now push that onto the stack and you're done.

Guy
Sorry, that doesn't make much sense to me.
Tim Green
+3  A: 

Its based on how the stack was created:

The address-size attribute of the stack segment determines the stack pointer size (16, 32 or 64 bits). The operand-size attribute of the current code segment determines the amount the stack pointer is decremented (2, 4 or 8 bytes).

In non-64-bit modes: if the address-size and operand-size attributes are 32, the 32-bit ESP register (stack pointer) is decremented by 4. If both attributes are 16, the 16-bit SP register (stack pointer) is decremented by 2.

Source: http://www.intel.com/Assets/PDF/manual/253667.pdf

pg. 4-320 Vol. 2B

Edit

Just wanted to point out also that an interesting read is the section on stacks in the manual, it will explain creating a stack segment further.

http://www.intel.com/Assets/PDF/manual/253665.pdf

Chapter 6.2

Very informative, but unfortunately still above what I can grasp with my current knowledge.
Tim Green
@Tim - Understandable. Perhaps still worth a quick read.
Wait, wait. Each memory location is 32-bits wide, so if you push a value, it will decrement the stack by 4 bytes, because 4*8 is 32-bits, and if it decremented it by 2, then it would try and make an unaligned read, imploding the universe?Is that right? If so, you get the points, that PDF was awesome (2nd one).
Tim Green
@Tim Green - Correct!
Thank you :D Your answer is clearly the better one!
Tim Green