Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 201
The_Neo Avatar asked Apr 06 '13 19:04

The_Neo


1 Answers

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.)

like image 198
Tom Blodget Avatar answered Oct 03 '22 05:10

Tom Blodget