Professional Documents
Culture Documents
Number Representation & Iseethec : Computer Science 61C Spring 2019 Weaver
Number Representation & Iseethec : Computer Science 61C Spring 2019 Weaver
1
Signed Integers and
Two’s-Complement Representation
Computer Science 61C Spring 2019 Weaver
2
Two’s-Complement Integers
Computer Science 61C Spring 2019 Weaver
Sign Bit
0000 0000 0000 0000 0000 0000 0000 0000two = 0ten
... ...
... ...
-2 x b + 2 x b + … + 2 x b + 2 x b
Carry
0 010
3
0011
+2 0010
5 0 0101
5
Two’s-Complement Examples
Computer Science 61C Spring 2019 Weaver
3
0011
3
0011
-3
1101
☐ 0 to +31
☐ -16 to +15
☐ -15 to +16
7
Summary: Number Representations
Computer Science 61C Spring 2019 Weaver
8
Agenda
Computer Science 61C Spring 2019 Weaver
• Computer Organization
• C vs Java
9
ENIAC (U.Penn., 1946)
First Electronic General-Purpose Computer
Computer Science 61C Spring 2019 Weaver
• Programs held as
numbers in memory
11
Components of a Computer
Computer Science 61C Spring 2019 Weaver
Memory
Processor Input
Enable?
Read/Write
Control
Program
Datapath
Address
PC Bytes
Write
Registers Data
sw t1, t2, 0
Compiler Anything can be represented
sw t0, t2, 4
Assembly Language as a number,
Program (e.g., RISC-V) i.e., data or instructions
Assembler 0000 1001 1100 0110 1010 1111 0101 1000
Machine Language 1010 1111 0101 1000 0000 1001 1100 0110
1100 0110 1010 1111 0101 1000 0000 1001
Program (RISC-V)
0101 1000 0000 1001 1100 0110 1010 1111
Machine
Interpretation
Hardware Architecture Description
(e.g., block diagrams)
Architecture
Implementation
• C++ or Java OK
14
Language Poll
Computer Science 61C Spring 2019 Weaver
• Please raise your hand for the first one you can say yes to:
15
Intro to C
Computer Science 61C Spring 2019 Weaver
16
Intro to C
Computer Science 61C Spring 2019 Weaver
• Don’t ask me why… If you are starting a new project where performance
matters use either Go or Rust
• Rust, “C-but-safe”: By the time your C is (theoretically) correct with all the necessary
checks it should be no faster than Rust
• You will not learn how to fully code in C in these lectures! You’ll still need
your C reference for this course
• K&R is a must-have
• http://oreilly.com/catalog/javanut/excerpt/index.html
• http://inst.eecs.berkeley.edu/~cs61c/resources/HarveyNotesC1-3.pdf
• Key security concept: All of the above are unsafe: If your program contains an error in these
areas it might not crash immediately but instead leave the program in an inconsistent (and
often exploitable) state
18
Administrivia Break…
Computer Science 61C Spring 2019 Weaver
• Exams:
• DSP accommodations:
• Get those in ASAP. You need to have your exam accommodation letters in by
2/12 to be considered for the 2/19 midterm
• Defaults also keep you from screwing up and making your archives for the class public
• Computer Organization
• C vs Java
21
Compilation: Overview
Computer Science 61C Spring 2019 Weaver
• These differ mainly in exactly when your program is converted to low-level machine
instructions (“levels of interpretation”)
Compiler Compiler/assembler
Compiler
combined here
• but make only rebuilds changed pieces, and can do compiles in parallel
(make -j X)
• C source files first pass through macro processor, CPP, before compiler sees code
• #include <stdio.h> /* Looks for file in standard location, but no actual difference! */
• But they aren't actual functions, instead it just changes the text of the
program
• #define twox(x) (x + x)
• twox(y++);
• (y++ + y++);
27
C vs. Java
Computer Science 61C Spring 2019 Weaver
C Java
Type of Language Function Oriented Object Oriented
Programming Unit Function Class = Abstract Data Type
gcc hello.c creates javac Hello.java creates Java virtual machine
Compilation
machine language code language bytecode
a.out loads and executes
Execution java Hello interprets bytecodes
program
#include<stdio.h>
public class HelloWorld {
int main(void) {
public static void main(String[] args) {
hello, world printf("Hello\n");
System.out.println("Hello");
return 0;
}
} }
New allocates & initializes,
Storage Manual (malloc, free)
Automatic (garbage collection) frees
From http://www.cs.princeton.edu/introcs/faq/c2java.html 28
C vs. Java
Computer Science 61C Spring 2019 Weaver
C Java
Comments /* … */ /* … */ or // … end of line
Constants #define, const final
Preprocessor Yes No
Variable
At beginning of a block Before you use it
declaration
Variable
naming sum_of_squares sumOfSquares
conventions
Accessing a #include <stdio.h> import java.io.File;
library
From http://www.cs.princeton.edu/introcs/faq/c2java.html 29
Typed Variables in C
Computer Science 61C Spring 2019 Weaver
int variable1 = 2; • Must declare the type of data a variable will hold
30
Integers: Python vs. Java vs. C
Computer Science 61C Spring 2019 Weaver
int number_of_people ()
{
return 3;
• You have to declare the type of data you
} plan to return from a function
return 10.33; – Just think of this as saying that no value will be returned
typedef struct {
int length_in_seconds;
int year_recorded;
} Song; Dot notation: x.y = value
Song song1;
song1.length_in_seconds = 213;
song1.year_recorded = 1994;
Song song2;
song2.length_in_seconds = 248;
song2.year_recorded = 1988;
34
A First C Program: Hello World
Computer Science 61C Spring 2019 Weaver
35
C Syntax: main
Computer Science 61C Spring 2019 Weaver
36
A Second C Program:
Compute Table of Sines
Computer Science 61C Spring 2019 Weaver
37
Second C Program
Sample Output
Computer Science 61C Spring 2019 Weaver
Value of PI = 3.141593
angle Sine
0 0.000000
10 0.173648
20 0.342020
30 0.500000
40 0.642788
50 0.766044
60 0.866025
70 0.939693
80 0.984808
90 1.000000
100 0.984808
110 0.939693
120 0.866025
130 0.766044
140 0.642788
....
38
C Syntax: Variable Declarations
Computer Science 61C Spring 2019 Weaver
• Examples of declarations:
40
C Syntax : Control Flow (1/2)
Computer Science 61C Spring 2019 Weaver
• if-else
• if (expression) statement
• if (x == 0) y++;
• if (x == 0) {y++;}
• if (x == 0) {y++; j = j + y;}
• if (expression) statement1 else statement2
• There is an ambiguity in a series of if/else if/else if you don't use {}s, so use {}s to block the code
• while
• for
• switch
• switch (expression){
case const1: statements
case const2: statements
default: statements
}
• break;
• Note: until you do a break statement things keep executing in the switch statement
• But it can result in spectacularly bad code if you use it, so don't!
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
goto fail; /* MISTAKE! THIS LINE SHOULD NOT HAVE BEEN HERE */
42
C Syntax: True or False
Computer Science 61C Spring 2019 Weaver
• 0 (integer)
43
C and Java operators nearly identical
Computer Science 61C Spring 2019 Weaver
• arithmetic: +, -, *, /, %
• subexpression grouping: ()
• assignment: =
• order relations: <, <=, >,
• augmented assignment: +=, >=
• It adds almost all the checks that Java does but C does not
• The result is your program immediately crashes where you make a mistake
45
Agenda
Computer Science 61C Spring 2019 Weaver
• Pointers
• Arrays in C
46
Address vs. Value
Computer Science 61C Spring 2019 Weaver
47
Pointers
Computer Science 61C Spring 2019 Weaver
Location (address)
48
Pointer Syntax
Computer Science 61C Spring 2019 Weaver
• int *p;
• Tells compiler that variable p is address of an int
• p = &y;
• Tells compiler to assign address of y to p
• & called the “address operator” in this context
• z = *p;
• Tells compiler to assign value at address in p to z
49
Creating and Using Pointers
Computer Science 61C Spring 2019 Weaver
“*” (dereference operator): get the value that the pointer points
to
p x 3
*p = 5; p x 5
51
Pointers and Parameter Passing
Computer Science 61C Spring 2019 Weaver
y remains equal to 3
52
Pointers and Parameter Passing
Computer Science 61C Spring 2019 Weaver
add_one(&y);
y is now equal to 4
53
Types of Pointers
Computer Science 61C Spring 2019 Weaver
• Pointers are used to point to any kind of data (int, char, a struct,
etc.)
• Normally a pointer only points to one type (int, char, a struct, etc.).
• Use void * sparingly to help avoid program bugs, and security issues, and other bad things!
54
More C Pointer Dangers
Computer Science 61C Spring 2019 Weaver
typedef struct {
int x; /* dot notation */
int y; int h = p1.x;
} Point; p2.y = p1.y;
56
Pointers in C
Computer Science 61C Spring 2019 Weaver
• If we want to pass a large struct or array, it’s easier / faster / etc. to pass a
pointer than the whole thing
57
Why Pointers in C?
Computer Science 61C Spring 2019 Weaver
• Most other languages use “pass by reference” for objects, which is semantically similar
58