Why is a frame pointer set as an offset from the stack pointer? [duplicate]

I’m trying to understand how stack frames are constructed and have run into this description on wikipedia:

The locations of all other fields in the frame can be defined relative
either to the top of the frame, as negative offsets of the stack
pointer, or relative to the top of the frame below, as positive
offsets of the frame pointer. The location of the frame pointer itself
must inherently be defined as a negative offset of the stack pointer. — https://en.wikipedia.org/wiki/Call_stack

The description in the quote above implies that the frame pointer’s address being an offset from the stack pointer is trivially obvious from their respective definitions.

I’m confused about why the location of the frame pointer must inherently be defined as a function of the stack pointer.

As I understand the situation, the stack pointer is incremented when new memory is allocated to the frame. However the location of the frame pointer is fixed from the point where the procedure’s prologue has finished.

Since all memory accesses within the frame must either be addressed as offsets from the frame pointer or as memory offsets from the stack pointer, and one of the frame pointer and stack pointer is a fixed location and the other one is inherently mutable, surely it makes sense to make the mutable pointer an offset from the fixed pointer?

As I understand it, in the above scheme, you’d need to update the frame pointer reference every time you incremented the stack pointer, which seems excessive. What have I misunderstood?

0

Well, you are right, this is all very confusing. First of all, the frame pointer is not even necessary. Everything is addressable using only the stack pointer.

The main reason for the existence of the frame pointer is that the value of the stack pointer may change during the execution of the function, (namely, as you are pushing into the stack parameters of other functions that you are willing to call,) meaning that the offset of each argument and each local will vary. This is not a problem at all to a compiler, which can simply keep track of how the stack pointer varies and use adjusted offsets accordingly, but it can become a nightmare to a human who is trying to write the function or debug it later.

Another reason for the existence of the frame pointer is historical: the Intel x86 architecture used to be somewhat inconvenienced with respect to addressing memory relative to the stack pointer. The only registers that could be used as index registers were BX,BP,SI and DI, note how SP is missing from the list. So, the BP register was used as the frame pointer. However, it is kind of strange that the Wikipedia article should be influenced by the 8086, because it is describing a stack which grows upwards, while on the x86 architecture the stack grows downwards. Go figure.

Note that this has all been fixed in x64. If you look at the System V Application Binary Interface AMD64 Architecture Processor Supplement, on page 16 there is a footnote which says:

The conventional use of %rbp as a frame pointer for the stack frame may be avoided by using %rsp (the stack pointer) to index into the stack frame. This technique saves two instructions in the prologue and epilogue and makes one additional general-purpose register (%rbp) available.

So, in a nutshell, the conventional use of the frame pointer is as follows: upon entry to a function, the frame pointer receives a copy of the value of the stack pointer, and the stack pointer is adjusted to make room for the locals. Then, during the execution of the function, the stack pointer is allowed to vary, but the frame pointer remains constant, so arguments and locals are still addressable at fixed offsets from the frame pointer.

To return in your question, the location of the frame pointer is not exactly defined as a function of the stack pointer, in the sense that the value of the stack pointer is copied into the frame pointer only once, in the beginning of the function. From that point on, the frame pointer stays constant, while the stack pointer varies.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật