⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s03_07.htm

📁 Programmer s Reference Manual is an improtant book on Intel processor architecture and programming.
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><HTML><HEAD><TITLE>80386 Programmer's Reference Manual -- Section 3.7</TITLE></HEAD><BODY><B>up:</B> <A HREF="c03.htm">Chapter 3 -- Applications Instruction Set</A><BR><B>prev:</B> <A HREF="s03_06.htm">3.6  String and Character Translation Instructions</A><BR><B>next:</B> <A HREF="s03_08.htm">3.8  Flag Control Instructions</A><P><HR><P><H1>3.7  Instructions for Block-Structured Languages</H1>The instructions in this section provide machine-language support forfunctions normally found in high-level languages. These instructions include<A HREF="ENTER.htm">ENTER</A> and <A HREF="LEAVE.htm">LEAVE</A>, which simplify the programming of procedures.<P><A HREF="ENTER.htm">ENTER</A> (Enter Procedure) creates a stack frame that may be used to implementthe scope rules of block-structured high-level languages. A <A HREF="LEAVE.htm">LEAVE</A>instruction at the end of a procedure complements an <A HREF="ENTER.htm">ENTER</A> at the beginningof the procedure to simplify stack management and to control access tovariables for nested procedures.<P>The <A HREF="ENTER.htm">ENTER</A> instruction includes two parameters. The first parameterspecifies the number of bytes of dynamic storage to be allocated on thestack for the routine being entered. The second parameter corresponds to thelexical nesting level (0-31) of the routine. (Note that the lexical levelhas no relationship to either the protection privilege levels or to the I/Oprivilege level.)<P>The specified lexical level determines how many sets of stack framepointers the CPU copies into the new stack frame from the preceding frame.This list of stack frame pointers is sometimes called the display. The firstword of the display is a pointer to the last stack frame. This pointerenables a <A HREF="LEAVE.htm">LEAVE</A> instruction to reverse the action of the previous <A HREF="ENTER.htm">ENTER</A>instruction by effectively discarding the last stack frame.Example:<PRE><A HREF="ENTER.htm">ENTER</A> 2048,3</PRE>Allocates 2048 bytes of dynamic storage on the stack and sets up pointersto two previous stack frames in the stack frame that <A HREF="ENTER.htm">ENTER</A> creates forthis procedure.After <A HREF="ENTER.htm">ENTER</A> creates the new display for a procedure, it allocates thedynamic storage space for that procedure by decrementing ESP by the numberof bytes specified in the first parameter. This new value of ESP serves as astarting point for all <A HREF="PUSH.htm">PUSH</A> and <A HREF="POP.htm">POP</A> operations within that procedure.<P>To enable a procedure to address its display, <A HREF="ENTER.htm">ENTER</A> leaves EBP pointing tothe beginning of the new stack frame. Data manipulation instructions thatspecify EBP as a base register implicitly address locations within the stacksegment instead of the data segment.<P>The <A HREF="ENTER.htm">ENTER</A> instruction can be used in two ways: nested and non-nested. Ifthe lexical level is 0, the non-nested form is used. Since the secondoperand is 0, <A HREF="ENTER.htm">ENTER</A> pushes EBP, copies ESP to EBP and then subtracts thefirst operand from ESP. The nested form of <A HREF="ENTER.htm">ENTER</A> occurs when the secondparameter (lexical level) is not 0.<P><A HREF="#fig3-16">Figure 3-16</A>  gives the formal definition of <A HREF="ENTER.htm">ENTER</A>.<P>The main procedure (with other procedures nested within) operates at thehighest lexical level, level 1. The first procedure it calls operates at thenext deeper lexical level, level 2. A level 2 procedure can access thevariables of the main program which are at fixed locations specified by thecompiler. In the case of level 1, <A HREF="ENTER.htm">ENTER</A> allocates only the requesteddynamic storage on the stack because there is no previous display to copy.<P>A program operating at a higher lexical level calling a program at a lowerlexical level requires that the called procedure should have access to thevariables of the calling program. <A HREF="ENTER.htm">ENTER</A> provides this access through adisplay that provides addressability to the calling program's stack frame.<P>A procedure calling another procedure at the same lexical level impliesthat they are parallel procedures and that the called procedure should nothave access to the variables of the calling procedure. In this case, <A HREF="ENTER.htm">ENTER</A>copies only that portion of the display from the calling procedure whichrefers to previously nested procedures operating at higher lexical levels.The new stack frame does not include the pointer for addressing the callingprocedure's stack frame.<P><A HREF="ENTER.htm">ENTER</A> treats a reentrant procedure as a procedure calling another procedureat the same lexical level. In this case, each succeeding iteration of thereentrant procedure can address only its own variables and the variables ofthe calling procedures at higher lexical levels. A reentrant procedure canalways address its own variables; it does not require pointers to the stackframes of previous iterations.<P>By copying only the stack frame pointers of procedures at higher lexicallevels, <A HREF="ENTER.htm">ENTER</A> makes sure that procedures access only those variables ofhigher lexical levels, not those at parallel lexical levels (see <A HREF="#fig3-17">Figure 3-17</A>  ).<A HREF="#fig3-18">Figures 3-18</A>  through 3-21 demonstrate the actions of the <A HREF="ENTER.htm">ENTER</A>instruction if the modules shown in <A HREF="#fig3-17">Figure 3-17</A>  were to call one another inalphabetic order.<P>Block-structured high-level languages can use the lexical levels defined by<A HREF="ENTER.htm">ENTER</A> to control access to the variables of previously nested procedures.Referring to <A HREF="#fig3-17">Figure 3-17</A>  for example, if PROCEDURE A calls PROCEDURE Bwhich, in turn, calls PROCEDURE C, then PROCEDURE C will have access to thevariables of MAIN and PROCEDURE A, but not PROCEDURE B because they operateat the same lexical level. Following is the complete definition of access tovariables for <A HREF="#fig3-17">Figure 3-17</A>  .<OL><LI> MAIN PROGRAM has variables at fixed locations.<LI> PROCEDURE A can access only the fixed variables of MAIN.<LI> PROCEDURE B can access only the variables of PROCEDURE A and MAIN.PROCEDURE B cannot access the variables of PROCEDURE C or PROCEDURE D.<LI> PROCEDURE C can access only the variables of PROCEDURE A and MAIN.PROCEDURE C cannot access the variables of PROCEDURE B or PROCEDURE D.<LI> PROCEDURE D can access the variables of PROCEDURE C, PROCEDURE A, andMAIN. PROCEDURE D cannot access the variables of PROCEDURE B.</OL><A HREF="ENTER.htm">ENTER</A> at the beginning of the MAIN PROGRAM creates dynamic storage spacefor MAIN but copies no pointers. The first and only word in the displaypoints to itself because there is no previous value for <A HREF="LEAVE.htm">LEAVE</A> to return toEBP. See <A HREF="#fig3-18">Figure 3-18</A>  .<P>After MAIN calls PROCEDURE A, <A HREF="ENTER.htm">ENTER</A> creates a new display for PROCEDURE Awith the first word pointing to the previous value of EBP (BPM for <A HREF="LEAVE.htm">LEAVE</A> toreturn to the MAIN stack frame) and the second word pointing to the currentvalue of EBP. Procedure A can access variables in MAIN since MAIN is atlevel 1. Therefore the base for the dynamic storage for MAIN is at [EBP-2].All dynamic variables for MAIN are at a fixed offset from this value. See<A HREF="#fig3-19">Figure 3-19</A>  .<P>After PROCEDURE A calls PROCEDURE B, <A HREF="ENTER.htm">ENTER</A> creates a new display forPROCEDURE B with the first word pointing to the previous value of EBP, thesecond word pointing to the value of EBP for MAIN, and the third wordpointing to the value of EBP for A and the last word pointing to the currentEBP. B can access variables in A and MAIN by fetching from the display thebase addresses of the respective dynamic storage areas. See <A HREF="#fig3-20">Figure 3-20</A>  .After PROCEDURE B calls PROCEDURE C, <A HREF="ENTER.htm">ENTER</A> creates a new display forPROCEDURE C with the first word pointing to the previous value of EBP, thesecond word pointing to the value of EBP for MAIN, and the third wordpointing to the EBP value for A and the third word pointing to the currentvalue of EBP. Because PROCEDURE B and PROCEDURE C have the same lexicallevel, PROCEDURE C is not allowed access to variables in B and thereforedoes not receive a pointer to the beginning of PROCEDURE B's stack frame.See <A HREF="#fig3-21">Figure 3-21</A>  .<P><A HREF="LEAVE.htm">LEAVE</A> (Leave Procedure) reverses the action of the previous <A HREF="ENTER.htm">ENTER</A>instruction. The <A HREF="LEAVE.htm">LEAVE</A> instruction does not include any operands. <A HREF="LEAVE.htm">LEAVE</A>copies EBP to ESP to release all stack space allocated to the procedure bythe most recent <A HREF="ENTER.htm">ENTER</A> instruction. Then <A HREF="LEAVE.htm">LEAVE</A> pops the old value of EBP fromthe stack. A subsequent <A HREF="RET.htm">RET</A> instruction can then remove any arguments thatwere pushed on the stack by the calling program for use by the calledprocedure.<P><A NAME="fig3-16"><IMG align=center SRC="fig3-16.gif" border=0><P><HR><P><A NAME="fig3-17"><IMG align=center SRC="fig3-17.gif" border=0><P><HR><P><A NAME="fig3-18"><IMG align=center SRC="fig3-18.gif" border=0><P><HR><P><A NAME="fig3-19"><IMG align=center SRC="fig3-19.gif" border=0><P><HR><P><A NAME="fig3-20"><IMG align=center SRC="fig3-20.gif" border=0><P><HR><P><A NAME="fig3-21"><IMG align=center SRC="fig3-21.gif" border=0><P><HR><P><B>up:</B> <A HREF="c03.htm">Chapter 3 -- Applications Instruction Set</A><BR><B>prev:</B> <A HREF="s03_06.htm">3.6  String and Character Translation Instructions</A><BR><B>next:</B> <A HREF="s03_08.htm">3.8  Flag Control Instructions</A></BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -