程序的机器级表示(二)

Data Formats 

如图:

Accessing Information 

如图:

The low-order 2 bytes of the first four registers can be independently read or written by the byte operation instructions.

When a byte instruction updates one of these single-byte “register elements,” the remaining 3 bytes of the register do not change. Similarly, the low-order 16 bits of each register can be read or written by word operation instructions. 

Operand Specifiers 

Source values can be given as constants or read from registers or memory. Results can be stored in either registers or memory.  

 Data Movement Instructions 

IA32 imposes the restriction that a move instruction cannot have both operands refer to memory locations. (do this via a register)

With sign expansion, the upper bits of the destination are filled in with copies of the most significant bit of the source value. With zero expansion, the upper bits are filled with zeros.

With IA32, the program stack is stored in some region of memory. 

As illustrated in Figure 3.5, the stack grows downward such that the top element of the stack has the lowest address of all stack elements.  

The stack pointer %esp holds the address of the top stack element

图示:

指令的等价

pushl %ebp    ---->

subl $4,%esp       Decrement stack pointer

movl %ebp,(%esp)      Store %ebp on stack 

popl %eax    ---->

movl (%esp),%eax    Read %eax from stack

addl $4,%esp           Increment stack pointer 

Since the stack is contained in the same memory as the program code and other forms of program data, programs can access arbitrary positions within the stack using the standard memory addressing methods. For example, assuming the topmost element of the stack is a double word, the instruction movl 4(%esp),%edx will copy the second double word from the stack to register %edx. 

在下面的示例里,

We omit the portion of the assembly code that allocates space on the run-time stack on procedure entry and deallocates it prior to return. The details of this set-up and completion code will be covered when we discuss procedure linkage

note:(b) 1. save  address (xp) to register edx 2. save the value(*xp) corressponding to the address(xp) to eax

              3. save y to ecx 4. save ecx(y's value) to main memory edx 5. return eax

再次强调:Local variables such as x are often kept in registers rather than stored in memory locations. 

一道练习:

You are given the following information. A function with prototype
void decode1(int *xp, int *yp, int *zp);
is compiled into assembly code. The body of the code is as follows:

xp at %ebp+8, yp at %ebp+12, zp at %ebp+16
1 movl   8(%ebp), %edi  // edi: xp
2 movl 12(%ebp), %edx  // edx: yp
3 movl 16(%ebp), %ecx   // ecx: zp
4 movl (%edx), %ebx  // ebx: *yp
5 movl (%ecx), %esi  // esi: *zp
6 movl (%edi), %eax  // eax: *xp
7 movl %eax, (%edx)  // *yp = *xp
8 movl %ebx, (%ecx)  // *zp = *yp
9 movl %esi, (%edi)  // *xp = *zp

void decode1(int *xp, int *yp, int *zp)
{
int* di = xp;
int* dx = yp;
int* cx = zp;

int ty = *dx; //*yp
int tz = *cx; //*zp
int tx = *di; //*xp

*dx = tx; // *yp = tx
*cx = ty; // *zp = ty
*di = tz; // *xp = tz
}

Load Effective Address 

Its first operand appears to be a memory refer- ence, but instead of reading from the designated location, the instruction copies the effective address to the destination. We indicate this computation in Figure 3.7 using the C address operator &S. This instruction can be used to generate point- ers for later memory references. In addition, it can be used to compactly describe common arithmetic operations. For example, if register %edx contains value x, then the instruction leal 7(%edx,%edx,4), %eax will set register %eax to 5x + 7. Compilers often find clever uses of leal that have nothing to do with effective address computations. The destination operand must be a register. 

instruction     effect      description
leal  S,D       D<- &S    load effective address

猜你喜欢

转载自www.cnblogs.com/geeklove01/p/9129125.html