1 Portable Programming CS 217. 2 Portability We live in a heterogeneous computing environment  Multiple kinds of HW: IA32, IA64, PowerPC, Sparc, MIPS,

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

INSTRUCTION SET ARCHITECTURES
1 ECE462/562 ISA and Datapath Review Ali Akoglu. 2 Instruction Set Architecture A very important abstraction –interface between hardware and low-level.
1 Portability The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 8 Professor Jennifer Rexford.
Structure of a C program
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
By Tien Phung CS 147 Dr. Sin-Min Lee. High-level Languages Assembly Languages Machine Languages.
Introduction to Computers and Programming. Some definitions Algorithm: –A procedure for solving a problem –A sequence of discrete steps that defines such.
Outline Java program structure Basic program elements
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Data Representation Kieran Mathieson. Outline Digital constraints Data types Integer Real Character Boolean Memory address.
Portability CPSC 315 – Programming Studio Spring 2008 Material from The Practice of Programming, by Pike and Kernighan.
CS150 Introduction to Computer Science 1
Introduction to Computers and Programming. Some definitions Algorithm: Algorithm: A procedure for solving a problem A procedure for solving a problem.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Computer Programming A program is a set of instructions a computer follows in order to perform a task. solve a problem Collectively, these instructions.
Engineering Computing I Chapter 1 – Part A A Tutorial Introduction.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Introduction to Computer Systems and the Java Programming Language.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Arithmetic Expressions
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
1 Writing Portable Programs COS Goals of Today’s Class Writing portable programs in C –Sources of heterogeneity –Data types, evaluation order,
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
CS Computer Science I. BCPL was developed in 1967 as a language for writing operating systems and software compilers In 1970, the creators of the.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Information Storage.
Introduction Why are virtual machines interesting?
The Instruction Set Architecture. Hardware – Software boundary Java Program C Program Ada Program Compiler Instruction Set Architecture Microcode Hardware.
Computer and Programming. Computer Basics: Outline Hardware and Memory Programs Programming Languages and Compilers.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
C is a high level language (HLL)
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
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.
Foundations of Computer Science C & C++ programming lecture 2
7.2 Arithmetic Expressions
CSE 220 – C Programming Bitwise Operators.
The Machine Model Memory
Microprocessor Systems Design I
CPSC 315 – Programming Studio Spring 2012
C Basics.
Information Storage.
Fundamental Data Types
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Introduction to the C Language
Expressions and Assignment Statements
Arithmetic Expressions
Building Java Programs Chapter 2
Portability CPSC 315 – Programming Studio
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Focus of the Course Object-Oriented Software Development
Comp Org & Assembly Lang
Fundamental Data Types
Comp Org & Assembly Lang
Unit 3: Variables in Java
CIS*2450 Advanced Programming Concepts
Presentation transcript:

1 Portable Programming CS 217

2 Portability We live in a heterogeneous computing environment  Multiple kinds of HW: IA32, IA64, PowerPC, Sparc, MIPS, Arms, …  Multiple kinds of systems: Windows, Linux, MAC, SUN, IBM, …  Software will be used in multiple countries It is difficult to design and implement a software system  It takes a lot effort to support multiple hardware and multiple operating systems (multiple versions)  Patches and releases are frequent operations If a program is portable, it requires no change to run on another machine  Correctness portability (primary concern)  Performance portability (secondary concern) Normally, portability is difficult to achieve  But, making the programs more portable is a good practice

3 Language Stick to the standard  Program in high-level language and within the language standard  Standard may be incomplete –char type in C and C++ may be signed or unsigned Program in the mainstream  Mainstream implies the established style and the use –Program enough to know what compilers commonly do –Difficult for large language such as C++ Beware of language trouble spots  Some features are intentionally undefined to give compiler implementers flexibility

4 Size of Data Types What are the sizes of char, short, int, long, float and double in C and C++?  They are not defined, except –char must have at least 8 bits, short and int at least 16 bits –sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) –sizeof(float) ≤ sizeof(double) In Java, sizes are defined  byte : 8 bits  char : 16 bits  short : 16 bits  int : 32 bits  long : 64 bits

5 Order of Evaluation What does the following code do? n = (getchar() >> 4) | getchar(); –The order is not specified strings[i] = names[++i]; –i can be incremented before or after indexing strings ! printf(“%c %c\n”, getchar(), getchar()); –The second character in stdin can be printed first! What are the rules in C and C++?  All side effects and function calls must be completed at “;”  && and || operators execute left to right and only as far as necessary What about Java?  Require expressions including side effects be evaluated left to right  But, Java manual advises not writing code depending on the order Our Advice: do not depend on the order of evaluation in an expression

6 Signed or Unsigned? Is there any problem with the following C code? int i; char s[MAX+1]; for (i = 0; i < MAX; i++) if ((s[i] = getchar()) == ‘\n’ || s[i] == EOF) break; s[i] = ‘\0’;  If char is unsigned, s[i] is 255 but EOF is -1 ! (will hang) Portable C code int c, i; char s[MAX+1]; for (i = 0; i < MAX; i++) { if ((c = getchar()) == ‘\n’ || c == EOF) break; s[i] = c; } s[i] = ‘\0’;

7 Other C Language Issues Arithmetic or logical shift  Signed quantities with >> may be arithmetic or logical in C  Java reserves >> for arithmetic right shift and >>> for logical Byte order  Byte order within short, int and long is not defined Alignment of items within structures, classes and unions  The items are laid out in the order of declaration  The alignment is undefined and there might be holes struct foo { char x; int y; /* can be 2, 4, or 8 bytes from x */ }

8 Use Standard Libraries Pre-ANSI C may have calls not supported in ANSI C  Program will break if you continue use them  Header files can pollute the name space Consider the signals defined  ANSI C defines 6 signals  POSIX defines 19 signals  Most UNIX defines 32 or more Take a look at /usr/include/*.h to see the conditional definitions

9 Use Common Features Motivation  Write a program that runs on Unix and on a cell phone and cell phone environment may have fewer libraries and different type sizes  Use the common ones Avoid conditional compilation  #ifdef are difficult to manage because it can be all over the places … some common code #ifdef MAC … #else #ifdef WINDOWSXP … #endif

10 Isolation Common feature may not always work: Life is hard Localize system dependencies in separate files  Use a separate file to wrap the interface calls for each system  Example: unix.c, windows.c, mac.c, … Hide system dependencies behind interfaces  Abstraction can serve as the boundary between portable and non- portable components  Java goes one big step further: use virtual machine which abstracts the entire machine –Independent of operating systems –Independent of hardware

11 Data Exchange Use ASCII text  Binary is often not portable Still need to be careful  But, even with text, not all systems are the same –Windows systems use use ‘\r’ or ‘\n’ to terminate a line –UNIX uses only ‘\n’  Example: –Use Microsoft Word and Emacs to edit files –CVS assume all lines have been changed and will merge incorrectly  Use standard interfaces which will deal CRLF (carriage-return and line feed) and newline in a consistent manner

12 Byte Order Recall big-endian and little-endian? Consider the following program between two processes  Writing a short to stdout : unsigned short x; x = 0x1000;... fwrite(&x, sizeof(x), 1, stdout)  Later, read it from stdin unsigned short x;... fread(&x, sizeof(x), 1, stdin); What is the value of x after reading?

13 Byte Order Solutions Conditional compilation  Conditional compilation for different byte orders  Swap the byte order if it is necessary  What is the pros and cons of this approach? –Save some instructions –Make the code messy Fix the byte order for data exchange  Sender: unsigned short x; putchar(x >> 8); /* high-order byte */ putchar(x & 0xFF); /* low-order byte */  Receiver: unsigned short x; x = getchar() << 8; /* read high-order byte */ x |= getchar() & 0xFF; /* read low-order byte */

14 More on Byte Order Language solution  Java has a serializable interface that defines how data items are packed  C and C++ require programmers to deal with the byte order Binary files vs. text files  Binary mode for text files –No problem on UNIX –Windows will terminate reading once it sees Ctrl-Z as input

15 Internationalization Don’t assume ASCII  Many countries do not use English  Asian languages use 16 bits per character Standardizations  Latin-1 arguments ASCII by using all 8 bits (superset of ASCII)  Unicode uses 16 bits per character and try to use Latin-1 encoding  Java uses unicode as its native character set for strings Issues with unicode  Byte order issue!  Solution is to use UTF-8 as an intermediate representation or defined the byte order for each character

16 Summary Language  Don’t assume char signed or unsigned  Always use sizeof to compute the size of types  Don’t depend on the order of evaluation of an expression  Beware of right shifting a signed value  Make sure that the data type is big enough Use standard interfaces  Use the common features  Isolation Byte order  Fix byte order for data exchange Internationalization  Don’t assume ASCII and English