x86 Can push/pop be less than 4 bytes? [duplicate]

2019-02-13 22:01发布

问题:

This question already has an answer here:

  • Why is it not possible to push a byte onto a stack on Pentium IA-32? 4 answers

Hi I am reading a guide on x86 by the University of Virginia and it states that pushing and popping the stack either removes or adds a 4-byte data element to the stack.

Why is this set to 4 bytes? Can this be changed, could you save memory on the stack by pushing on smaller data elements?

The guide can be found here if anyone wishes to view it: http://www.cs.virginia.edu/~evans/cs216/guides/x86.html

回答1:

Short answer: Yes, 16 or 32 bits. And, for x86-64, 64 bits.

The primary reasons for a stack are to return from nested function calls and to save/restore register values. It is also typically used to pass parameters and return function results. Except for the smallest parameters, these items usually have the same size by the design of the processor, namely, the size of the instruction pointer register. For 8088/8086, it is 16-bits. For 80386 and successors, it is 32-bits. Therefore, there is little value in having stack instructions that operate on other sizes.

There is also the consideration of the size of the data on the memory bus. It takes the same amount of time to retrieve or store a word as it does a byte. (Except for 8088 which has 16-bit registers but an 8-bit data bus.) Alignment also comes into play. The stack should be aligned on word boundaries so each value can be retrieved as one memory operation. The trade-off is usually taken to save time over saving memory. To pass one byte as a parameter, one word is usually used. (Or, depending on the optimization available to the compiler, one word-sized register would be used, avoiding the stack altogether.)