topics cs429 computer organization and architecture
play

Topics CS429: Computer Organization and Architecture Intro to C - PowerPoint PPT Presentation

Topics CS429: Computer Organization and Architecture Intro to C Simple C programs: basic structure, functions, separate files Compilation: phases, options Dr. Bill Young Department of Computer Sciences Assembler: GNU style, byte University


  1. Topics CS429: Computer Organization and Architecture Intro to C Simple C programs: basic structure, functions, separate files Compilation: phases, options Dr. Bill Young Department of Computer Sciences Assembler: GNU style, byte University of Texas at Austin ordering Tools for inspecting binary: od, objdump Last updated: January 27, 2020 at 13:38 CS429 Slideset C: 1 Intro to C CS429 Slideset C: 2 Intro to C A Simple C Program A Simple C Program What program should we write first? What program should we write first? Hello World: program to print a short message. We use the gcc compiler driver to compile the program. We assume our target is an x86-compatible machine. This program prints “Hello, world!” to its standard output. The program text is in file hello.c . How’d it get there? /* Hello World program in C */ #include "stdio.h" int main () { printf("Hello , world !\n"); } CS429 Slideset C: 3 Intro to C CS429 Slideset C: 4 Intro to C

  2. File Inclusion Running the Program A directive of the form: Several steps are necessary to run the program. Invoke the gcc compiler driver to transform your text file (in #include "filename" this case called hello.c ) into an executable image. or Then ask the operating system to run the executable. #include <filename> > gcc hello.c is replaced (by the preprocessor) with the contents of the file > a.out filename . Hello , world! > If the filename is quoted, searching for the file begins in the local directory; if it is not found there, or if the name is enclosed in The single call to gcc actually invokes: the preprocessor, the braces, seaching follows an implementation-defined rule to find the compiler, the assembler, and the linker. file. CS429 Slideset C: 5 Intro to C CS429 Slideset C: 6 Intro to C A More Complex Program Running the Temperature Program #include <stdio.h> > gcc -O2 temperature.c > a.out /* print Fahrenheit to Celsius [C = 5/9(F -32)] for fahr = 0, 20, ..., 300 */ 0 -17 20 -6 40 4 main () 60 15 { 80 26 int fahr , celsius; 100 37 int lower , upper , step; 120 48 140 60 lower = 0; /* low limit of table */ 160 71 upper = 300; /* high limit of table */ 180 82 step = 20; /* step size */ 200 93 fahr = lower; 220 104 while (fahr <= upper) { 240 115 celsius = 5 * (fahr -32) / 9; 260 126 printf("%d\t%d\n", fahr , celsius); 280 137 fahr = fahr + step; 300 148 } } CS429 Slideset C: 7 Intro to C CS429 Slideset C: 8 Intro to C

  3. About Optimization Levels Specifying an Output Filename Optimization cannot change the functional behavior of your > gcc -O2 -o tempConvert temperature.c program. It offers a tradeoff between execution efficiency, > tempConvert compilation time, and code size. 0 -17 20 -6 -O0: fast compilation time, straightforward code (default) 40 4 -O1: OK code size, slightly faster execution time 60 15 80 26 -O: same as -O1 100 37 -O2: code may be bigger, faster execution time 120 48 140 60 -O3: code may be bigger, even faster execution time 160 71 -Os: smallest code size 180 82 200 93 -Ofast: same as -O3, with fast math calculations 220 104 -Og: same as -O1, but better for debugging 240 115 260 126 The faster the execution the bigger and more obscure the code 280 137 may be. 300 148 CS429 Slideset C: 9 Intro to C CS429 Slideset C: 10 Intro to C TempConvert with For Loop Running TempConvert2 #include <stdio.h> > gcc -o tempConvert2 temp2.c > tempConvert2 #define LOWER 0 /* low limit of table */ 0 -17.8 #define UPPER 300 /* high limit of table */ 20 -6.7 #define STEP 20 /* step size */ 40 4.4 60 15.6 /* print Fahrenheit to Celsius table 80 26.7 for fahr = 0, 20, ..., 300 */ 100 37.8 120 48.9 main () 140 60.0 { 160 71.1 int fahr; 180 82.2 double celsius; 200 93.3 220 104.4 for (fahr = LOWER; fahr <= UPPER; fahr += STEP) { 240 115.6 celsius = (5.0 / 9.0) * (fahr - 32); 260 126.7 printf("%3d %6.1f\n", fahr , celsius); 280 137.8 } 300 148.9 } CS429 Slideset C: 11 Intro to C CS429 Slideset C: 12 Intro to C

  4. Program with Environment Variables Command Line Arguments Sometimes you’d like to get data from the command line, or from the operating environment. argc is the argument count, including the name of the program. argv is an array of those strings. Those names are conventional. This program has environment input variables. Variables argc and argv reflect the command line. #include <stdio.h> #include <stdio.h> // for the printf command main( int argc , char *argv [] ) { main( int argc , char *argv []) int i; { if( argc == 1 ) printf("Program has %d command line args .\n", argc); printf( "The command line argument is:\n" ); } else printf( "The %d command line arguments are:\n", argc ); > gcc countargs.c for( i = 0; i < argc; i++ ) > a.out 3 "hello" "why me?" 5 printf( "Arg %3d: %s\n", i, argv[i] ); Program has 5 command line args. } > a.out 3 "hello" "why me?" 5 * Program has 196 command line args. CS429 Slideset C: 13 Intro to C CS429 Slideset C: 14 Intro to C Running the Program Environment Variables Variable env reflects the environment variables. It holds an array of strings maintained by the OS. #include <stdio.h> > gcc -o commargs commargs.c #include <stdlib.h> > commargs "string with blanks" 1 a b 7.45 The 6 command line arguments are: main( int argc , char *argv[], char *env []) Arg 0: commargs { Arg 1: string with blanks int i; Arg 2: 1 printf( "The environment strings are:\n" ); Arg 3: a Arg 4: b i = 0; Arg 5: 7.45 while( env[i] != NULL ) { Note: Some command line arguments are treated specially by the printf( "Arg %3d: %s\n", i, env[i] ); i++; OS. E.g., “*” expands to a list of files in the current directory. } } Note that the env parameter is not in the standard, but is widely supported. To include env , you also must have argc and argv . CS429 Slideset C: 15 Intro to C CS429 Slideset C: 16 Intro to C

  5. Running the Program Accessing Environment Variables Once you know the names of Environment variables, you can access them using the getenv function from stdlib . You can use > gcc -o envargs envargs.c getenv even without the env parameter. > envargs The environment strings are: #include <stdio.h> Arg 0: PWD=/u/byoung/cs429/c #include <stdlib.h> Arg 1: TERM=dumb Arg 2: TERMCAP= int main () { Arg 3: COLUMNS =80 char* user = getenv( "USER" ); Arg 4: EMACS=t if( user != NULL ) Arg 5: INSIDE_EMACS =23.3.1 , comint printf( "USER = %s\n", user ); Arg 6: SHELL =/ lusr/bin/tcsh return 0; Arg 7: GROUP=prof } Arg 8: GPG_AGENT_INFO =/tmp/keyring -hZHfuV/gpg :0:1 # <lots more , 49 in all > > gcc testgetenv.c > a.out USER = byoung CS429 Slideset C: 17 Intro to C CS429 Slideset C: 18 Intro to C The GNU gcc Compiler Assembler Output from gcc You can stop at assembly without producing machine code or gcc is a cross compiler linking. It runs on many machines gcc -S -O2 <sourceFile >.c Input languages: C, C++, Fortran, Java, and others Many target languages: x86, PowerPC, ARM, MC680x0, We’ll be doing this a lot this semester! others int sum( int x, int y) { Extensive documentation is available on-line. int t = x + y; return t; } verbose mode: gcc works in phases: To generate the assembler in file sum.s : gcc -v -O2 -o <objectFile > <sourceFile >.c gcc -S -O2 sum.c CS429 Slideset C: 19 Intro to C CS429 Slideset C: 20 Intro to C

  6. sum.s Assembler Output for hello.c > gcc -S -O2 hello.c > cat hello.s .file "sum.c" .file "hello.c" .text .section .rodata.str1 .1,"aMS",@progbits ,1 .p2align 4,,15 .LC0: .globl sum .string "Hello , world" .type sum , @function .section .text.startup ,"ax",@progbits sum: .p2align 4,,15 .LFB0: .globl main .cfi_startproc .type main , @function leal (%rdi ,% rsi), %eax main: ret .LFB24: .cfi_endproc . cfi_startproc .LFE0: movl $.LC0 , %edi .size sum , .-sum jmp puts .ident "GCC: (Ubuntu 4.8.4 -2 ubuntu1 ˜14 .04) 4.8.4" . cfi_endproc .section .note.GNU -stack ,"",@progbits .LFE24: .size main , .-main .ident "GCC: (Ubuntu 4.8.4 -2 ubuntu1 ˜14.04) 4.8.4" .section .note.GNU -stack ,"",@progbits CS429 Slideset C: 21 Intro to C CS429 Slideset C: 22 Intro to C Assembler Output from Binary What Assembly? objdump can be used to disassemble binary output. Assembly code doesn’t run on any computer! It’s the binary code > gcc -O -c sum.c that runs. > objdump -d sum.o sum.o: file format elf64 -x86 -64 Assembly (like high level languages) is just a convenient, humanly Disassembly of section .text: readible notation for writing programs. There are two common forms for x86 assembly: 0000000000000000 <sum >: 0: 8d 04 37 lea (%rdi ,%rsi ,1) ,%eax Gnu Assembly (GAS) format (what we’ll be using) 3: c3 retq Intel/Microsoft Assembly format -c means to compile to machine code, but don’t link. This is You may notice variations in assembly code depending on what needed here since there’s no main function. produced it, even within one format. Note that the assembly language syntax may differ slightly depending on how you generate it. CS429 Slideset C: 23 Intro to C CS429 Slideset C: 24 Intro to C

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend