Machine-level Representation of C Programs

3.2 Program Encodings

Unix> gcc –O2 –o p p1.c p2.c

This command actually  invokes a sequence of programs to turn the source code into executable code:

1.       the C preprocessor expands the source code to include any files specified with #include commands and to expand any macros.

2.        The compiler generates assembly code versions of the two source files having names p1.s and p2.s

3.       The assembler converts the assembly code into binary object code files p1.o and p2.o.

4.       the linker merges these two object files along with code implementing standard Unix library functions and generates the final executable file.

3.2.1 Machine-Level Code

3.4.2 Data Movement Instructions

1. Among the most heavily used instructions are those that perform data movement.

2. The source operand designates a value that is immediate, stored in a register, or stored in memory.

3. The destination operand designates a location that is either a register or a memory address.

4. IA32 imposes the restriction that a move instruction can not have both operands refer to memory location. Copying a value from one memory location to another requires two instructions – the first to load the source value into a register, and the second to write this register value to the destination.

The final two data movement operations are used to push data onto and pop data from the program stack. As we will see, the stack plays a vital role in the handling of procedure calls.

The program stack is stored in some region of memory. 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 this lowest stack element. \

Pushing a double-word value onto the stack therefore involves first decrementing the stack pointer by 4 and then writing the value at the new top of stack address. Therefore, the instruction pushl %ebp has equivalent behavior to the following pair of instructions:

  Subl $4,%esp

     Movl %ebp, (%esp)

3.4.3 Data Movement Example

Two features about this assembly code are worth noting.

1.       We see that what we call “pointer” in C are simply address. Dereferencing a pointer involves putting that pointer in a register, and then using this register in an indirect memory reference.

2.       Local variable are often kept in registers rather than stored in memory locations. Register access is much faster than memory access.

3.5 Arithmetic and Logical Operations  (NEXT  P106)

你可能感兴趣的:(C++,c,unix,C#,Access)