CS429 Computer Architecture Topics Simple C program Basic structure, functions, separate files Compilation Phases, options Assembler GNU style, byte ordering,

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 1 Engineering Problem Solving.
Advertisements

Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
UBC104 Embedded Systems Variables, Structures & Pointers.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Computer Architecture and Assembly Languages Course’s web site: Teaching Assistant: Or Peri Office Hours: Thursday 37/-108.
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
CS 101 Problem Solving and Structured Programming in C Sami Rollins Spring 2003.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Introduction to C. A Brief History Created by Dennis Ritchie at AT&T Labs in 1972 Originally created to design and support the Unix operating system.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Computer Science 210 Computer Organization Introduction to C.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Introduction to C Programming. A Brief History u Created by Dennis Ritchie at AT&T Labs in 1972 u Originally created to design and support the Unix operating.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Programming With C.
ECE 103 Engineering Programming Chapter 1 Introduction Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
Recitation 6 – 2/26/01 Outline Linking Exam Review –Topics Covered –Your Questions Shaheen Gandhi Office Hours: Wednesday.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to Programming
Lec 4Systems Architecture1 Systems Architecture Lecture 4: Compilers, Assemblers, Linkers & Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
(language, compilation and debugging) David 09/16/2011.
Bits and Bytes September 1, F’05 class02.ppt “The Class That Gives CMU Its Zip!”
Introduction to Computer Organization & Systems Topics: Command Line Bitwise operators COMP Spring 2014 C Part V.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.
C is a high level language (HLL)
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Practical Session 3.
Content Coverity Static Analysis Use cases of Coverity Examples
Chapter 3 General-Purpose Processors: Software
The Machine Model Memory
Computer Science 210 Computer Organization
C Primer.
A bit of C programming Lecture 3 Uli Raich.
Computer Architecture and Assembly Language
Command Line Arguments
Understand argc and argv
C Language By Sra Sontisirikit
Debugging with gdb gdb is the GNU debugger on our CS machines.
Homework Reading Machine Projects Labs PAL, pp ,
Computer Architecture and Assembly Language
CSE 351 Section 1: HW 0 + Intro to C
Introduction to C Programming Language
Programmazione I a.a. 2017/2018.
Choice of Programming Language
Computer Science 210 Computer Organization
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Assembly Language Programming II: C Compiler Calling Sequences
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Lecture 2 SCOPE – Local and Global variables
Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan
Comp Org & Assembly Lang
Introduction to C Topics Compilation Using the gcc Compiler
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
15213 C Primer 17 September 2002.
System and Cyber Security
Administrative things
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Introduction to C CS 3410.
Presentation transcript:

CS429 Computer Architecture Topics Simple C program Basic structure, functions, separate files Compilation Phases, options Assembler GNU style, byte ordering, code and data segments Tools for inspecting binary Programs: od, objdump CS429, F’12 Class-C-intro.ppt Introduction to C September 5, 2012

– 2 – CS429, F’12 A Simple C Program A first program is to just print a short message. We assume our target is a 32-bit, X86-compatible machine. This program prints “Hello!” to its “standard output”. We will use “gcc” to compile this program. /* Simple Program */ #include "stdio.h" int main ( ) { printf( "Hello!\n" ); }

– 3 – CS429, F’12 Simple C Program with Return Status This program returns a status code using exit( n ); We compile with “gcc -O2 -o ” Be wary of the PowerPoint fonts. /* Simple Program */ #include “stdio.h” // For the printf command #include “stdlib.h” // For the exit command int main ( ) { printf( "Hello!\n" ); exit( 11 ); }

– 4 – CS429, F’12 Program with Environment Variables This program has environment input arguments Variables argc and argv reflect the command line. Variable env reflects the environment variables. /* Simple Program */ #include “stdio.h” // For the printf command #include “stdlib.h” // For the exit command int main ( int argc, char *argv[], char *env[] ) { printf("Status: number of command-line args.\n" ); exit( argc ); }

– 5 – CS429, F’12 The Command Line Arguments #include "stdio.h" #include "stdlib.h" int main (int argc, char *argv[], char *env[] ) { int i; if( argc == 1 ) printf( "The command line argument is:\n" ); else printf( "The %d command line arguments are:\n", argc ); for( i = 0; i < argc; i++ ) printf( "Arg %3d: %s\n", i, argv[ i ] ); exit( argc ); }

– 6 – CS429, F’12 The Command Line Arguments #include "stdio.h" #include "stdlib.h” int main (int argc, char *argv[], char *env[] ) { int i; printf( "The environment strings are:\n" ); i = 0; while( env[i] != NULL ) { printf( "Arg %3d: %s\n", i, env[ i ] ); i++; } exit( i ); }

– 7 – CS429, F’12 The GNU GCC Compiler GCC is a cross compiler It runs on many machines Input languages: C, C++, Fortran, Java, and others Many target languages: X86, PowerPC, ARM, MC680x0, … Documentation available on-line GCC works in phases: gcc -v -O2 -o.c GCC can be used to print assembler gcc -S -O2.c

– 8 – CS429, F’12 Assembler Output From gcc Produces assembler output, doesn’t run “gas” assembler sum.s gcc -S -O2 -c sum.c ==> sum.c int sum( int x, int y ) { int t = x + y; return t; }.file"sum.c".text.p2align 4,,15.globl sum: pushl%ebp movl%esp, %ebp movl12(%ebp), %eax addl8(%ebp), %eax popl%ebp ret

– 9 – CS429, F’12 Assembler Output From binary “objdump” can be used to view the binary. sum.o: file format elf32-i386 Disassembly of section.text: : 0:55 push %ebp 1:89 e5 mov %esp,%ebp 3:8b 45 0c mov 0xc(%ebp),%eax 6: add 0x8(%ebp),%eax 9:5d pop %ebp a:c3 ret

– 10 – CS429, F’12 Show Bytes Program #include typedef unsigned char *byte_pointer; void show_bytes( byte_pointer start, int len ) { int i; for( i =0; i < len ; i++ ) printf(" %.2x", start[i] ); printf("\n"); } int main(int argc, char *argv[], char *env[] ) { int i = 15213; float f = ; double d = ; int *p = &i; show_bytes( (byte_pointer) &i, sizeof(i) ); show_bytes( (byte_pointer) &f, sizeof(f) ); show_bytes( (byte_pointer) &f, sizeof(d) ); show_bytes( (byte_pointer) &p, sizeof(p) ); }

– 11 – CS429, F’12 C Tutorials Available on the Web Use a search engine, and type “C tutorial” and you will get a lot of options. I thought this next reference was good. A local reference by Christian Miller (UTCS grad student) fall/cs429/lectures/Miller-C-Intro.pdf “The C Programming Language” Brian Kernighan and Dennis Ritchie is a standard guide. Prentice-Hall Publisher, I suggest the 2 nd Edition A modern architecture is not an ISA alone All developments target a system Hardware and software combined Benchmarks are widely used to illustrate performance Be careful -- your experience may be different