What are the ESP and the EBP registers?

前端 未结 4 914
心在旅途
心在旅途 2020-12-04 19:11

I found that the ESP register is the current stack pointer and EBP is the base pointer for the current stack frame. However, I don\'t understand these definitions (I am just

4条回答
  •  执念已碎
    2020-12-04 19:49

    EBP and ESP are remnants of the era, where compilers didn't e.g. have static analysis to detect how many bytes of a stack is needed in a function call. Also the stack was supposed to dynamically grow and shrink during the execution of a function, interrupts would have allowed to trash all the stack from 0 to SP, and spaghetti code was the de facto standard. Actually interrupts (and passing parameters through registers alone) were the designed method to call kernel functions.

    In these surroundings one needs to have a fixed point of the stack, where the return address to the caller, local variables and the arguments of a function is always found. Thus the bp register was justified. In this architecture bp was allowed to be indexed ([bp - 300h]), but sp wasn't. Those opcodes/instruction encodings which could have been interpreted as mov ax, [sp + 1111h] were reused for other purposes.

    In 386+ and via the introduction of the 'E', ESP gained the property of offset. At this time EBP was freed from the sole purpose, as esp was able to handle both tasks.

    Note, that even now EBP points to memory through the stack segment (SS), just like ESP. Other addressing modes (without ESP/EBP as the base) default to the DS segment. (absolute, DI, SI, and/or BX in 16-bit mode, and in 32-bit addressing modes any register can be a base in an addressing mode).

提交回复
热议问题