Summary

This document is a primer on the C programming language, covering compilation, libraries, and linking. It includes code examples, and basic explanations related to programming concepts.

Full Transcript

C primer, Compilation, Libraries and Linking #include int main () { printf (“Hello, World\n”); return 0; } Plan A basic Introduction to the C programming language Compilation of a C program on an Intel architecture How this is different fro...

C primer, Compilation, Libraries and Linking #include int main () { printf (“Hello, World\n”); return 0; } Plan A basic Introduction to the C programming language Compilation of a C program on an Intel architecture How this is different from java Libraries Linking Run time environments The C programming language The Syntax is similar to Java But we do not have any Object-Oriented idea No Classes, No Objects We only have functions (kind of like methods) We have the same basic data types (int, float, double, char) No String class any more You need to deal with arrays of characters Arrays are declared the same way The C programming language Functions are declared in a similar way to methods from java ( just no public, private, in front ) All local variables must be declared at the top of a function Loops and conditionals are the same (if, else if, else ) Big Difference Memory management. If you allocate memory ( using malloc which is similar to the keyword new in java you need to dislocate it, using free) You made it you! You Remove it! File Structure - Java Each class in a single file The file name matches the name of the class exactly File extension/tag:.java A full program is built up of multiple files File Structure - C All code can be in a single file or multiple files File name is not important for the language/compilation The file name is important to a programmer as a way to organize code File extension/tag of.c for code File extension /tag.h for header definition files Only needed if placing code in multiple files A full program is built up of a single file or multiple files that are compiled and linked together to create a single executable. The C programming language The simplest program possible (one that does nothing) File: Example.java (Java program) public class Example File: Test.c (C program) { int main() public static void main(String[]args) { { } return 0; } } Main Method in C It is the starting point for a C program ( you always need one) All C programs must have one main function, that should return an int. Braces enclose the int main() function body. { Always need a return at the end of the main. It gives back a code to the return 0; operating system about the exit status of the } program 0 = exiting with no error Other codes are based upon the operating system it is on Hello World Program File: Test.c (C program) #include int main() { printf(“Hello World\n”); return 0; } File: Example.java (Java program) I/O not part of C language, public class Example { it comes from a library called public static void main(String[]args) standard io {  must #include the header System.out.println(“Hello World”); } } C - Variables Can be declared globally or locally Globally – a variable declared out side of a function. It can be accessed by any function that is declared below the declaration of the global variable Locally – a variable declared inside of a function. All local variables must be declared at the top of a function before any other code. Variables #include int x = 0;  Global Variable - should avoid if possible int main() { int y = 999;  Local Variable printf(“Hello World\n”); return 0; } #include int hitStrength = 0; int attack(int level); void defend(float dfLevel, int attack); int main() { int attackerLevel = 999; int result = 0; printf(“You have dishonored my family!!!!\n”); result = attack( 100 ); if(result > 0) defend( 0.5, attackerLevel ); return 0; } int attack(int level) { } void defend(float dfLevel, int attack) { } Write Code and then compile High Level Read y READ Assembly STORE A Y HALT Y dv 010 Machine Code 0x0000 0x0803 0x3000 Run on Architecture 0x0000 Compilation – Simple Computer Write Code and then compile High Level Preprocessing is the first pass of any C Compilation Process Preprocessing compilation. It processes Compilation include-files, conditional Assembly compilation instructions Linker and macros. Executable Compile: gcc on Run on Architecture Linux Compilation – C Write Code and then compile High Level Compilation is the second pass. It takes the Compilation Process Preprocessing output of the Compilation preprocessor, and the Assembly source code, and generates Linker assembler source code. (.S files ) Executable Compile: gcc on Run on Architecture Linux Compilation – C Write Code and then compile Assembly is the third High Level stage of compilation. It takes the assembly source Compilation Process Preprocessing code and produces an Compilation assembly listing with Assembly offsets. The assembler Linker output is stored in an object file. (.O files ) Executable Compile: gcc on Run on Architecture Linux Compilation – C Write Code and Linking is the final stage of then compile compilation. It takes one or High Level more object files or libraries as input and combines them to Compilation Process produce a single (usually Preprocessing executable) file. In doing so, it Compilation resolves references to external Assembly symbols, assigns final addresses Linker to procedures/functions and variables, and revises code and data to reflect new addresses (a Executable process called relocation). Run on Architecture Compile: gcc on Compilation – C Linux Compilation – C Note: a C program compiled on one architecture will not run on a different architecture. Java programs are compiled in a different fashion so that they will be more portable to run on multiple architectures This is due to the JVM (the Java Virtual Machine) design Compilation – C through the Linux shell Linux command to compile a C program: gcc By default it produces a file called a.out. This is your executable file To run the program you type./a.out or a.out Command Compile: gcc.c Command Run:./a.out Compilation – C through the Linux shell Compilation – C through the Linux shell To compile and get a better name for an executable use the following Command Compile: gcc.c -O Command Run:./ Compilation – C through the Linux shell Compilation – C through the Linux shell To see assembly code produced by the compiler before the Assembler phase Command Compile: gcc –S.c You will now see a file called.S in your directory Examination of the assembly of the simple program File: Test.s (intel assembly) File: Test.c (C program) int main( ) { return 0; } Intel Assembly Examples ( From C Code) File: abc.s (intel assembly) %rsp = stack pointer %rbp = base pointer %eax = general purpose register A Stack Setup for main Return in main instead Stack clean up and of halt exit Data returned through program %eax Examination of the assembly of a global variable int x = 3; int main() { x = x + 3; return 0; } rip - Instruction pointer relative data access http://en.wikipedia.org/wiki/X86-64 Call to sub sub(int a) File: HelloWorldExample.c include int main() { printf(“Hello World\n”); return 0; } Notice “puts i.e. put string” instead of “printf ” We did not write it. Its definition is not in our program. Libraries For the printf in the last code we got a puts subroutine call in the assembly. Where did puts come from? Compiler used it as an optimization step when making the assembly The definition of the subroutine is not in our code so where does it reside? Another programmer wrote it and included it in a standard library Libraries a library is a collection of implementations of behavior, written in terms of a language, that has a well-defined interface by which the behavior is invoked. In addition, the behavior is provided for reuse by multiple independent programs. http://en.wikipedia.org/wiki/Programming_library Libraries Consider: gcc –c hello.c This provides a.O file (Preprocessing, Compilation and Assembly steps done) hello.o with unresolved external Call puts references At a different location of the computer is a compile library that needs to be connected puts:. file with compiled version of library return functions Linking When you run: gcc –o hello hello.c gcc does the following: 1. Compiles to assembly (.s file ) 2. Runs assembler to produce.o machine code/executable with possible unresolved references 3. Runs linker to produce the executable ( connects together all the chunks and resolves any external references) Could link together multiple.o files into a single executable. Run time environments Label Command start ------------- ------------- Set up of environment ------------- call main ------------- ------------- clean up of environment ------------- HALT puts: ------------- Return control to the operating ------------- system (this is done through RETURN libfunction2 ------------- interrupts) RETURN Libraries provide a support environment for applications. Run time environments Run-time environments includes Support for I/O ( Input/Output) Support for dynamic memory allocation (not on the stack) Heap data is not on the stack so it FF16 does not get destroyed with The Stack subroutine returns. Can allocate new memory ( new command or malloc function ) Can deallocate memory ( free Main Memory function in C) The Heap (pool of available memory) Static Data HALT Code 0016 Run time environments

Use Quizgecko on...
Browser
Browser