
CS 61C vs. CS 61AB The CS 61 series is an introduction to computer science, with particular emphasis on software and on machines from a programmer's point of view. The first two courses considered programming at a high level of abstraction, introducing a range of programming paradigms and common techniques. This course, the last in the series, concentrates on machines and how they carry out the programs you write. The main topics of CS 61CL involve the low-level system software and the hardware organization of a "logical machine"—not the actual electronic circuits, but the computational operations that those circuits carry out. To make these ideas concrete, you will study the structure of a particular computer, the MIPS R3000 processor, in some detail, down to the level of the design of the processor's on-chip components. Course outline Topics in the first half of CS 61CL will be covered roughly in the sequence specified below. week topics 1 introduction to C 2 C arrays and pointers 3 dynamic storage allocation 4 introduction to assembly language 5 assembly language translation of C operations 6 machine language 7 floating-point representations Topics to be covered in the remainder of the course include the following: input and output linking and loading logic design CPU design C memory management virtual memory caches A notebook for CS 61CL We encourage you to buy a notebook specifically for CS 61CL and bring it to lab every day. T.a.s will want a place to write answers for questions you ask. You will want a place to keep track of issues or techniques that arise in lab. The notebook is also a good place to keep track of errors you make, to help you not make them again. Overview For those of you that are new to the UC-WISE learning environment, this (http://inst.eecs.berkeley.edu/ ~cs61cl/fa07/misc/intro.to.UC-WISE.html) provides an overview. Why C? C arose from work on UNIX at Bell Labs in the late 1960’s. It filled two main needs. First, it is a relatively high-level language that in addition allows flexible low-level access to memory and operating system resources. It's also relatively easy to write a compiler for, so it wasn't too long before implementations of C were available on a variety of computers. (This contributed greatly to the spread of UNIX.) In CS 61CL, we'll be focusing on two aspects of C. First, there are things you need to be an everyday C programmer; you'll write some of the CS 61CL projects in C, so we hope these hints will help you with those programs. Also, C allows easy access to other aspects of the CS 61CL content. C vs. Java To a first approximation, C is just Java without the object-oriented features. Here's a Java program that veterans of CS 61B have probably encountered. public class Account { public Account (int amount) { myAmount = amount; } public void deposit (int amount) { myAmount = myAmount – amount; } public int balance ( ) { return myAmount; } private int myAmount; } Crossing out mentions of class and public/private leaves an almost legal C program: Format of a C program Here's an excerpt of a program we'll see later in this lab session, along with some explanatory notes. #include <stdio.h> int main ( ) { unsigned int exp = 1; int k; /* Compute 2 to the 31st. */ for (k=0; k<31; k++) { exp = exp * 2; } ... return 0; } Notes: #include is loosely similar to the import construct in Java. Mechanically, the C compiler replaces the #include line by the contents of the named file. However, well-designed include files are an important aspect of good C programming style. Important system programming interfaces are described in < > include files. (Surrounding the file name with angle brackets, as in the example, tells the compiler to look for the file in a set of system directories.) User-defined data types and interfaces, such as you will write, are described in " " include files. (Surrounding the file name with double quote marks says to look in the user's directory for the file.) As in Java, main is called by the operating system to run the program. Unlike in Java, main is an int- valued function that returns a success-failure code (0 means success, anything else means failure). Command-line arguments may but are not required to be specified as arguments to main. The C compiler also allows main to be declared as void; K&R contains numerous examples of this. C requires declaration before use; moreover, all declarations must appear after the left brace surrounding a group of statements. C uses "/*" and "*/" as comment delimiters as does Java. More recent versions of C also allow "//" comments as in Java. Another example Here's another example that we'll revisit later in this lab session. #include <stdio.h> int bitCount (unsigned int n); int main ( ) { printf ("%d %d", 0, bitCount (0)); printf ("%d %d", 1, bitCount (1)); printf ("%d %d", 27, bitCount (27)); return 0; } int bitCount (unsigned int n) { int k; return 13; } Notes: Functions should also be declared before they're used. In C, that's done with a function prototype; the line right after the #include is an example. Files whose names end in ".h" (for "header") typically consist of function prototypes and data type definitions. Files whose names end in ".c" contain the code for those functions. The printf function is used to produce output. Its first argument is a format string. Each occurrence of "%" in the format string indicates where one of the subsequent arguments will appear and in what form. This association between form and variable type isn't checked by the C compiler, as we'll note later. Some troublesome details C doesn't have a boolean type. It uses int instead: 0 means "false", anything else means "true". (This is similar to Scheme.) This feature, combined with assignment within expressions and a looser approach to types in general, leads to a common mistake made by beginning C programmers: using "=" instead of "==" in a comparison causes C to interpret the comparison as an assignment statement, whose value is the value assigned. Thus, a typical assignment statement might be: n = 5; and a typical conditional might be: if (n == 5) { ... do something ...} But the following is a funny combination of both: if (n = 5) { .. do something ...} It assigns the value 5 to the variable n and tests whether 5 is not equal to zero. (It's true. 5 is not 0.) The test always succeeds, probably not what the programmer intended. In Java, this causes an error, since the condition in an if statement must be of type boolean. And yes, C programmers do find elegant uses of the assign-and-test idiom, but in moderation. For new C programmers, it is usually a bug. C doesn't check for the failure to initialize variables, or for accessing outside the bounds of an array. These omissions have been the bane of many a C programmer over the years. They are best addressed by good, disciplined programming conventions right from the beginning. Although simple, C is very powerful and allows you to get at anything the machine can do. Without care, that power can be dangerous. Review of some UNIX commands First, use the mkdir command to create a directory named lab1 in your home directory. The mkdir command takes a single argument, the name of the directory to create. Then use the cp command to copy two files from the CS 61CL code directory to the lab1 directory. The cp command takes two arguments: the file to be copied, and the destination file or directory. The files to be copied are ~cs61cl/code/wc1.c and ~cs61cl/code/wc.data.txt. Incidentally, if you're new to UNIX, this web page contains short descriptions of some more useful UNIX commands. The most important is man. If you are not sure how a UNIX command works, run man on it. (Yes, man man works too.) UNIX i/o redirection The default input source for many UNIX programs is the keyboard. However, this source may be redirected to come from a file, using the < operator. For example, a desk calculator program named dc simulates a calculator that takes input in postfix order (operands before operator). When one runs dc with the command dc input comes from the keyboard. For example, to compute 2 + 3, one would type 2 3 + p ("p" prints the result.) To take input instead from a file named dc.cmds, we would type dc < dc.cmds gcc, gdb, and emacs In lab, we'll be using the gcc C compiler and the gdb debugger. The emacs editor provides features that enhance the capability of gdb, with which you'll shortly get some practice. Check right now that the gcc command is set up properly for your account, by typing the alias command to the UNIX shell. Among the entries printed should be one for gcc that includes the options "-Wall", "-g", and "-std=c99". "Wall" means "Warn about all recognized irregularities"; "-g" causes gcc to store enough information in the executable program for gdb to make sense of it; "-std=c99" enables the use of features added to C in the 1999 standard. Warn your instructor if these options do not appear in an alias entry for gcc. Let's try it out. Type gcc. It should come back with gcc: no input files. That's right; you didn't specify any.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages12 Page
-
File Size-