jin-chen
Uploaded by
13 SLIDES
255 VUES
130LIKES

Understanding C Program Compilation: From Source Code to Executable

DESCRIPTION

This session explores the stages involved in compiling a C program using the GCC compiler. It explains the role of the preprocessor, compiler, assembler, and linker in transforming C source code into machine-level representations. Key concepts such as generating assembly code, object files, and different compilation flags are covered. Furthermore, it delves into control flow in programming, including sequential and non-sequential operations, and jump instructions. Gain insights into how C programming translates into executable instructions.

1 / 13

Télécharger la présentation

Understanding C Program Compilation: From Source Code to Executable

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recitation 1 Session 5 Ishani Chakraborty

  2. Machine Level Representation • What happens to a C program when we compile it ? $ gcc prog.c • Preprocessor (expands code) • Compiler (source2asm) • Assembler (asm2object) • Linker (Combines objects to form a single executable)

  3. Assembly code • Generated by –s option $ gcc –O2 –S code.c • Using GCC compiler, the assembly code is generated in GAS format (GNU Assembler)

  4. #include<stdio.h> int accum = 0; int sum(int x, int y) { int t = x+y; accum = accum + t; return t; } add.c • $ gcc –O2 –S add.c (generate asm .s code) • vi add.s • $ gcc –O2 –c add.c (generate binary machine .o code) • $ gcc add.s • $ gcc add.o

  5. $ vi add.s

  6. $ objdump –d add.o

  7. Example: asm => c movl 16(%ebp), %eax movl 12(%ebp), %edx subl %eax, %edx movl %edx, %eax imull 8(%ebp),%edx sall $31, %eax sarl $31, %eax xorl %edx, %eax

  8. int asm2c(int x, int y, int z) { int t1 = y - z; int t2 = x * t1; int t3 = (t1 << 31) >> 31; int t4 = t3 ˆ t2; return t4; }

  9. Control Flow • Control the sequence of operations. • Default: Sequential • Non sequential through conditionals/loops/switches etc. • Condition Codes: To perform non-sequential jumps. CF, ZF, SF, OF. • Jump instructions: jmp, je,jne,jg (signed greater), jge, ja (unsigned greater),…

  10. Jump targets are using PC – relative ie., the difference between address of target instruction and instruction following the jump. • eg., what is the jump target for instr 1 and 2 ? (1) 0x08: 7e 11 jle 1b …. …. (2) 0x19: 7f f5 jg 10 ….

  11. int absdiff2(int x, int y) { int result; if (x< y) result = y-x; else result = x-y; return result; }

More Related