Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the 80x87 instruction set use a "stack-based" design?

Back when Intel first designed the 8087, why did they choose to organize the floating-point registers as a stack? What possible advantage could be gained from such a design? It seems much less flexible and harder to work with than allowing arbitrary registers to be used as source and destination operands.

like image 269
Alex D Avatar asked Oct 18 '14 20:10

Alex D


1 Answers

The article "On the Advantages of the 8087's Stack", shared in the comments by @Jester, explains the thinking of the designers. A summary of why they organized the floating-point registers as a stack:

  1. Potentially, it could have made procedure calls more efficient, since (in theory) neither callers nor callees would have to explicitly save and restore FP registers. Callees who needed to do FP calculations would simply push their operands on the register stack, do their calculations, and pop the results off the stack when they were done, automatically restoring the caller's x87 state. (This is essentially the same as how the machine stack is used for function parameters, return values and local variables.)

  2. Given the way instructions were already encoded on the 8086/8088, and the number of opcodes already in use, they could only provide 1-operand instructions for the 8087, not 2-operand. That would not have worked well with a flat register file.

  3. They thought that providing the FXCH instruction would make it simple enough to rearrange the x87 register stack at will (so that arbitrary pairs of values could be used as operands when needed). Further, the FXCH operation is cheap.

like image 169
Alex D Avatar answered Oct 05 '22 23:10

Alex D