CS429: Computer Organization and Architecture

Similar documents
Topics. CS429: Computer Organization and Architecture. File Inclusion. A Simple C Program. Intro to C

CSC 405 Computer Security Shellcode

EL2310 Scientific Programming

EL2310 Scientific Programming

PRINCIPLES OF OPERATING SYSTEMS

Algorithms, Data Structures, and Problem Solving

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CS429: Computer Organization and Architecture

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CSE 333 Lecture 2 Memory

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Compilation, Disassembly, and Profiling (in Linux)

COMP s1 Lecture 1

Intermediate Programming, Spring 2017*

Lab 10: Introduction to x86 Assembly

EL2310 Scientific Programming

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CSE 303: Concepts and Tools for Software Development

CMSC 313 Lecture 12 [draft] How C functions pass parameters

Computer Systems Lecture 9

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Introduction Presentation A

System calls and assembler

C Programming Review CSC 4320/6320

Lecture 3: Instruction Set Architecture

The Compilation Process

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Department of Computer Science and Engineering Yonghong Yan

Arm cross development tools

Procedures, Parameters, Values and Variables. Steven R. Bagley

Dynamic Memory Allocation and Command-line Arguments

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Programming refresher and intro to C programming

Kurt Schmidt. October 30, 2018

Your first C and C++ programs

Introduction to Supercomputing

Dynamic memory allocation

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

How Compiling and Compilers Work

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

CS 270 Systems Programming. Debugging Tools. CS 270: Systems Programming. Instructor: Raphael Finkel

Building an Executable

CSC 1600 Memory Layout for Unix Processes"

CS 241 Data Organization Pointers and Arrays

Array Initialization

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

CPS104 Recitation: Assembly Programming

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

Topic 6: A Quick Intro To C

Lecture 03 Bits, Bytes and Data Types

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Introduction to the C Programming Language

CSE 124 Discussion (10/3) C/C++ Basics

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Problem Set 1: Unix Commands 1

Lecture 2. Xiaoguang Wang. January 16th, 2014 STAT 598W. (STAT 598W) Lecture 2 1 / 41

Basic Data Types. CS429: Computer Organization and Architecture. Array Allocation. Array Access

COMP 2001/2401 Test #1 [out of 80 marks]

Layers of Abstraction CS 3330: C. Compilation Steps. What s in those files? Higher-level language: C. Assembly: X86-64.

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

Program Building. Goals for this Lecture. Help you learn about: Program structure. Detecting and reporting failure

CS C Primer. Tyler Szepesi. January 16, 2013

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Comp 524 Spring 2009 Exercise 1 Solutions Due in class (on paper) at 3:30 PM, January 29, 2009.

Variables and literals

CSC231 C Tutorial Fall 2018 Introduction to C

Link Edits and Relocatable Code

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS 61C: Great Ideas in Computer Architecture Introduction to C

CS 5513 Entry Quiz. Systems Programming (CS2213/CS3423))

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

why we shouldn t use assembly compilers generate pre8y fast, efficient code tedious, easy to screw up not portable

The New C Standard (Excerpted material)

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

CMPE-013/L. Introduction to C Programming

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Compila(on, Disassembly, and Profiling

C - Basics, Bitwise Operator. Zhaoguo Wang

Arrays and Pointers in C. Alan L. Cox

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Introduction to the Message Passing Interface (MPI)

today cs3157-fall2002-sklar-lect05 1

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018

Assembler Programming. Lecture 10

CS-220 Spring 2018 Test 2 Version Practice Apr. 23, Name:

A Fast Review of C Essentials Part I

Lecture 04 Control Flow II. Stephen Checkoway University of Illinois at Chicago CS 487 Fall 2017 Based on Michael Bailey s ECE 422

C and Programming Basics

COMP327 Mobile Computing Session:

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

Transcription:

CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: September 6, 2017 at 18:02 CS429 Slideset C: 1

Topics Simple C programs: basic structure, functions, separate files Compilation: phases, options Assembler: GNU style, byte ordering Tools for inspecting binary: od, objdump CS429 Slideset C: 2

A Simple C Program A first program is to just print a short message. We assume our target is an x86-compatible machine. This program prints Hello, world! to its standard output. The program text is in a file hello.c We use gcc to compile the program. / Hello, world! Program / #include stdio.h int main() { printf ( Hello, world!\n ) ; CS429 Slideset C: 3

File Inclusion A directive of the form: #include "filename" or #include <filename> is replaced (by the preprocessor) by the contents of the file filename. 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 braces, seaching follows an implementation-defined rule to find the file. CS429 Slideset C: 4

Running the Program Several steps are necessary to run the program. Invoke the gcc compiler driver to transform your text file (in this case called hello.c) into an executable image. Then ask the operating system to run the executable. > gcc hello.c > a. out hello, world > The single call to gcc actually invokes: the preprocessor, the compiler, the assembler, and the linker. CS429 Slideset C: 5

A More Complex Program #include <stdio.h> / print Fahrenheit to Celsius table for fahr = 0, 20,..., 300 / main() { int fahr, celsius ; int lower, upper, step ; lower = 0; / low limit of table / upper = 300; / high limit of table / step = 20; / step size / fahr = lower ; while ( fahr <= upper) { celsius = 5 (fahr 32) / 9; printf ( %d\t%d\n, fahr, celsius ) ; fahr = fahr + step ; CS429 Slideset C: 6

Running the Temperature Program > gcc O2 temperature.c > a. out 0 17 20 6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93 220 104 240 115 260 126 280 137 300 148 CS429 Slideset C: 7

Specifying an Output Filename > gcc O2 o tempconvert temperature.c > tempconvert 0 17 20 6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93 220 104 240 115 260 126 280 137 300 148 CS429 Slideset C: 8

TempConvert with For Loop #include <stdio.h> #define LOWER 0 / low limit of table / #define UPPER 300 / high limit of table / #define STEP 20 / step size / / print Fahrenheit to Celsius table for fahr = 0, 20,..., 300 / main() { int fahr ; double celsius ; for ( fahr = LOWER; fahr <= UPPER; fahr += STEP) { celsius = (5.0 / 9.0) ( fahr 32) ; printf ( %3d %6.1f\n, fahr, celsius ) ; CS429 Slideset C: 9

Running TempConvert2 > gcc o tempconvert2 temp2.c > tempconvert2 0 17.8 20 6.7 40 4.4 60 15.6 80 26.7 100 37.8 120 48.9 140 60.0 160 71.1 180 82.2 200 93.3 220 104.4 240 115.6 260 126.7 280 137.8 300 148.9 CS429 Slideset C: 10

Program with Environment Variables Sometimes you d like to get data from the command line, or from the operating environment. This program has environment input variables. Variables argc and argv reflect the command line. #include stdio.h // for the printf command main( int argc, char argv []) { printf ( Status : Program has %d command line args.\n, argc) ; CS429 Slideset C: 11

Command Line Arguments argc is the argument count, including the name of the program. argv is an array of those strings. #include stdio.h main( int argc, char argv [] ) { 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 ] ) ; CS429 Slideset C: 12

Running the Program Here s a compilation and run of the program: > gcc o commargs commargs.c > commargs string with blanks 1 a b 7.45 The 6 command line arguments are : Arg 0: commargs Arg 1: string with blanks Arg 2: 1 Arg 3: a Arg 4: b Arg 5: 7.45 Note: Some command line arguments are treated specially by the OS. E.g., * expands to a list of files in the current directory. CS429 Slideset C: 13

Environment Variables Variable env reflects the environment variables. It holds an array of strings maintained by the OS. #include stdio.h #include stdlib.h 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++; 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: 14

Running the Program > gcc o envargs envargs.c > envargs The environment strings are : Arg 0: PWD=/u/byoung/cs429/c Arg 1: TERM=dumb Arg 2: TERMCAP= Arg 3: COLUMNS=80 Arg 4: EMACS=t Arg 5: INSIDE EMACS=23.3.1,comint Arg 6: SHELL=/lusr/bin/tcsh Arg 7: GROUP=prof Arg 8: GPG AGENT INFO=/tmp/keyring hzhfuv/gpg:0:1 # <lots more, 49 in all > CS429 Slideset C: 15

Accessing Environment Variables Once you know the names of Environment variables, you can access them using the getenv function from stdlib. #include <stdio.h> #include <stdlib.h> int main( int argc, char argv []) { char user = getenv( USER ) ; if ( user!= NULL ) printf ( USER = %s\n, user ) ; return 0; > gcc testgetenv.c > a. out USER = byoung CS429 Slideset C: 16

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, others Extensive documentation is available on-line. gcc works in phases: gcc v O2 o <objectfile> <sourcefile >.c gcc can be used to generate assembly language: gcc S O2 <sourcefile >.c CS429 Slideset C: 17

Assembler Output from gcc You can produce assembler output, without running the assembler. int sum( int x, int y) { int t = x + y; return t ; To generate the assembler in file sum.s: gcc S O2 c sum.c CS429 Slideset C: 18

sum.s sum:.lfb0:. f i l e sum.c.text.p2align 4,,15.globl sum.type sum, @function.cfi startproc leal (%rdi,%rsi ), %eax ret.cfi endproc.lfe0 :.size sum,. sum.ident GCC: (Ubuntu 4.8.4 2ubuntu1 14.04) 4.8.4.section.note.GNU stack,, @progbits CS429 Slideset C: 19

Assembler Output for hello.c > gcc S O2 hello.c > cat hello. s. file hello.c. section. rodata. str1.1, ams,@progbits,1.lc0:. string hello, world. section. text. startup, ax, @progbits. p2align 4,,15. globl main. type main, @function main:.lfb24:. cfi startproc movl $.LC0, %edi jmp puts. cfi endproc.lfe24:. size main,. main. ident GCC: (Ubuntu 4.8.4 2ubuntu1 14.04) 4.8.4. section. note.gnu stack,, @progbits CS429 Slideset C: 20

Assembler Output from Binary objdump can be used to disassemble binary output. > gcc O c sum.c > objdump d sum.o sum.o : file format elf64 x86 64 Disassembly of section.text : 0000000000000000 <sum>: 0: 8d 04 37 lea (%rdi,%rsi,1),% eax 3: c3 retq CS429 Slideset C: 21

Show Bytes Program #include <stdio.h> 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 ) ; void main ( int argc, char argv [], char env [] ) { int i = 15213; float f = 15213.0; double d = 15213.0; int p = &i ; show bytes (( byte pointer ) &i, sizeof ( i )) ; show bytes (( byte pointer ) &f, sizeof ( f )) ; show bytes (( byte pointer ) &d, sizeof (d)) ; show bytes (( byte pointer ) &p, sizeof (p)) ; CS429 Slideset C: 22

Running show bytes Here s how you might compile and run that code: > gcc o showbytes showbytes.c > showbytes 6d 3b 00 00 00 b4 6d 46 00 00 00 00 80 b6 cd 40 48 65 dc 42 ff 7f 00 00 CS429 Slideset C: 23

C Tutorials Available The C Programming Language, 2nd edition, by Kernighan and Richie is a standard reference. There are versions available on-line. Google C tutorial and you ll find lots of options. For example: http://wwww.iu.hio.no/~mark/ctutorial/ctutorial.html CS429 Slideset C: 24

Exercises These won t be collected, but they re good practice: 1 Execute the program on slide 12 (list args) with * in the argument list. What happens? 2 Try the program on slide 14 (list env). How many environment variables are there on your machine? How many can you identify? 3 Can you use getenv to identify your operating system? 4 Generate assembly for the program on slide 18 (sum). Does it differ from what s shown in the slides? 5 Compile the sum program and then use objdump to disassemble it. How does it compare to the assembly shown in the previous step? CS429 Slideset C: 25