Machine-level Representation of C Programs

本文详细介绍了从源代码到可执行文件的过程,包括预处理、编译、汇编和链接等步骤,并深入探讨了数据移动指令及算术逻辑操作在IA-32架构中的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值