113
1 Introduction to Computer Systems

1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

Embed Size (px)

Citation preview

Page 1: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

1

Introduction to Computer Systems

Page 2: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

2

Outline

• Teaching staffs

• Textbook and Grading

• Course Schedule

• Motivation

• Suggested reading

– Preface xvii~xx

Page 3: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

3

Instructor

• 路红

• Email: [email protected]

• Office phone: 65643922• Make Appointment or Open Door

Policy

Page 4: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

4

Teaching Assistants

• 林剑峰,张宏宇• [email protected]

[email protected]

Page 5: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

5

Text Book

• Randy Bryant and David O’Hallaron,– Computer Systems: A Programmer’s

Perspective– Prentice Hall, August 12, 2002.– 电子工业出版社, 2006.

• Brian Kernighan and Dennis Ritchie,– The C Programming Language, Second Edition– Prentice Hall, 1988

Page 6: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

6

Teaching Notes

• 10.11.4.101• User: ics• Pwd: ics• \ 课件• Right: can read/write and cannot delete

Page 7: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

7

Grading

• Exams(50%)– Three in class exams (10% each)– Final (20%)– All exams are open books/open notes.

• Labs (40%)– 5~6 labs, (2-10% each)

• Home work(10%)

Page 8: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

8

Grading

• Lab deadline & Late Submission– Due at 11:59pm of the specific due date– Give 5 late days– Lose 1/5 of points for each other late day

Page 9: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

9

Course Schedule -1

• Mainly introduce the content of Chap 1 ~

Chap 6. Some content in Chap 10, if time

permitted.

Page 10: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

10

Course Schedule -2

• 2006 ICS 课程 PPT 安排表 _Chap 1~Chap 6,

Chap 10.doc

Page 11: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

11

Why are we here?

• From abstractions to details (realities)

• From application level to system level

• From Java to C

Page 12: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

12

Features of this course

• Enduring Concepts

• From programmer’s perspective

• Actively study

• Becoming the rare “power programmer”

Enduring: 持久的

Page 13: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

13

Enduring concepts

• Computer systems consists hardware and systems software that work together to run programs

• Specific implementations of systems change over time

• But the underlying concepts do not• All computer systems have similar

hardware and software components that perform similar functions

Page 14: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

14

From programmer’s perspective

• Written for programmers instead of system builders– Few students would have the opportunity to

build a computer system– Even the computer engineers would be required

to use and program computers on a daily basis

• It covers a topic only if it affected– correctness, performance or utility of user-level

C programs

Page 15: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

15

From programmer’s perspective

• Topics on hardware adder and bus designs were out

• Introduce assembly in a different way– How C constructs are translated by the compiler

• Pointers

• Loops

• Procedure calls and returns

• Switch statements

Page 16: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

16

From programmer’s perspective

• Take a broader and more realistic view of the system – Linking and loading

– Process, signals

– Performance optimization

– I/O and network and concurrent programming ( Not introduced here, can read if you are interested in. )

Page 17: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

17

Actively study

• New concepts are followed by practical problems

• Homework problems and labs are also real• Learning by doing

– Working concrete problems– Writing and running programs on real systems

• Practical, concrete, hands-on and exciting

Page 18: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

18

Becoming the rare “power programmer”

• Enlightened by an understanding of – the underlying computer system – and its impact on your application programs

• You know– How things work and – How to fix them when they break

Page 19: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

19

Examples that we are going to learn

• How to avoid strange numerical errors – caused by the way that computers represent

numbers

• How to optimize the C code by using– Clever tricks that exploit the designs of modern

processors and memory systems

Page 20: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

20

Examples that we are going to learn

• How the compiler implements procedure call

• How to use above knowledge to avoid – The security holes from buffer overflow bugs that

• Plague network and Internet software

• How to recognize and avoid the nasty errors during linking– That confound the average programmer

Plague: 引起麻烦Nasty: 危险的Confound: 混淆

Page 21: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

21

Examples that we are going to learn

• How to write our own (*Optional) – Unix shell

– Dynamic storage allocation

– Web server

Page 22: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

22

Fundamental course for systems

• Compilers

• Operating Systems

• Networking

• Architectures (with digital component design)

Page 23: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

23

Platforms

• Hardware platform– Intel IA-32

• Operating system– Linux

• Programming language– ANSI C

• Compiler– GNU-gcc

Page 24: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

24

The C Programming Language

• C was developed – in 1969 to 1973 – by Dennis Ritchie of Bell Laboratories.

• The American National Standards Institute (ANSI)– ratified the ANSI C standard in 1989.

• The standard defines – the C language – and a set of library functions known as the C standard

library.

Ratify: 批准 , 认可

Page 25: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

25

The C Programming Language

• Kernighan and Ritchie describe ANSI C in their classic book– which is known affectionately as “K&R” .

• In Ritchie’s words [60], C is – quirky, – flawed, – and an enormous success.

• Why the success?

Quirky: 离奇的Flawed: 有缺陷的

Page 26: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

26

The C Programming Language

• C was closely tied with the Unix operating system. – C was developed from the beginning as the system

programming language for Unix. – Most of the Unix kernel, and all of its supporting tools and

libraries, were written in C. – As Unix became popular in universities in the late 1970s

and early 1980s, many people were exposed to C and found that they liked it.

– Since Unix was written almost entirely in C, it could be easily ported to new machines, which created an even wider audience for both C and Unix.

Page 27: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

27

The C Programming Language

• C is a small, simple language. – The design was controlled by a single person,

rather than a committee, and the result was a clean, consistent design with little baggage.

– The K&R book describes the complete language and standard library, with numerous examples and exercises, in only 261 pages.

– The simplicity of C made it relatively easy to learn and to port to different computers.

Page 28: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

28

The C Programming Language

• C was designed for a practical purpose. – C was designed to implement the Unix operating

system. – Later, other people found that they could write

the programs they wanted, without the language getting in the way.

Page 29: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

29

The C Programming Language

• C is the language of choice for system-level programming

• There is a huge installed based of application-level programs as well.

Page 30: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

30

The C Programming Language

• However, it is not perfect for all programmers and all situations– C pointers are a common source of confusion

and programming errors – C also lacks explicit support for useful

abstractions such as classes and objects– Newer languages such as C++ and Java address

these issues for application-level programs

Page 31: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

31

How do we start

• From a very simple program “Hello”

• To run and completion this simple program– Every major part of the system must work in

concert

• This course is to help you understand – what happens and why

– When you run hello on your system

Page 32: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

32

“Hello world” example

1 #include <stdio.h>

2

3 int main()

4 {

5 printf("hello, world\n");

6 }

Page 33: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

33

How do we start

• We begin our study of systems by– Tracing the lifetime of the hello program

– From it is created by a programmer

– To it runs on a system, prints its result, and terminates

Page 34: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

34

Outline

• Bit and Byte

• Context is very important

• Understand machine representations of numbers

• Information storage

• Bit level manipulation

• Suggested reading– Chap 1.1, 2.1

Page 35: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

35

“Hello world” example

1 #include <stdio.h>

2

3 int main()

4 {

5 printf("hello, world\n");

6 }

• Source program– Created by editor and saved as a text file

Page 36: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

36

“Hello world” example

# i n c l u d e <sp> < s t d i o .

35 105 110 99 108 117 100 101 32 60 115 116 100 105 111 46

h > \n \n i n t <sp> m a i n ( ) \n {

104 62 10 10 105 110 116 32 109 97 105 110 40 41 10 123

\n <sp> <sp> <sp> <sp> p r i n t f ( " h e

10 32 32 32 32 112 114 105 110 116 102 40 34 104 101

l l o , <sp> w o r l d \ n " ) ; \n }

108 108 111 44 32 119 111 114 108 100 92 110 34 41 59 10 125

Page 37: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

37

Why Bit?

• The source program is a sequence of bits

• Modern computers store and process

– Information represented as two-valued signals

– These lowly binary digits are bits

• Bits form the basis of the digital revolution

Page 38: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

38

The Decimal Representation

• Base-10• Has been in use for over 1000 years• Developed in India• Improved by Arab mathematicians in the 12th

century• Brought to the West in the 13th century by

– the Italian mathematician Leonardo Pisano, • better known as Fibonacci.

* Fibonacci: 斐波纳契数列 ( 一种整数数列 , 其中每数等于前面两数之和 )

Page 39: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

39

Why Bit?

• Using decimal notation is natural for ten-fingered humans

• But binary values work better when building machines – that store and process information

Page 40: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

40

Why Bit?

• Two-valued signals can readily be – represented, stored, and transmitted,

• Examples– The presence or absence of a hole in a punched

card

– A high or low voltage on a wire

– A magnetic domain oriented clockwise or counterclockwise.

Page 41: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

41

Why Bit?

• The electronic circuitry is very simple and reliable for – storing and performing computations on two-

valued signals

• This enabling manufacturers to integrate– millions of such circuits on a single silicon chip

Page 42: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

42

Group Bits

• In isolation, a single bit is not very useful• However, we are able to represent the

elements of any finite set by using bits• To do this, we

– first group bits together – then apply some interpretation to the different

possible bit patterns• that gives meaning to each patterns

Interpretation: 解释

Page 43: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

43

ASCII standard

• 8-bit chunks are organized as a byte

• Each byte represents some text character in the program

• Most modern systems represent text characters – using the ASCII standard

• ASCII standard represents – each character with a unique byte-sized integer

value

Page 44: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

44

“Hello world” example

# i n c l u d e <sp> < s t d i o .

35 105 110 99 108 117 100 101 32 60 115 116 100 105 111 46

h > \n \n i n t <sp> m a i n ( ) \n {

104 62 10 10 105 110 116 32 109 97 105 110 40 41 10 123

\n <sp> <sp> <sp> <sp> p r i n t f ( " h e

10 32 32 32 32 112 114 105 110 116 102 40 34 104 101

l l o , <sp> w o r l d \ n " ) ; \n }

108 108 111 44 32 119 111 114 108 100 92 110 34 41 59 10 125

Page 45: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

45

Text file

• Source program is stored in a file– As a sequence of bytes– Each byte has an integer value that corresponds

to some character

• Text files– Files that consist exclusively of ASCII characters

• Binary files– Files other than text files

Page 46: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

46

Information is Bits+Context

• All information in a system is represented – as a bunch of bits– Such as

• Disk files• Programs stored in memory• User data stored in memory• Data transferred across a network

Page 47: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

47

Information is Bits+Context

• The only thing that distinguishes different data objects is – the context in which we view them– In different contexts, the same sequence of bytes

might represent• A number (integer or floating point number)• A character string• A machine instruction

• Context is very important in interpreting bits!

Page 48: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

48

Three number encodings

• Unsigned encoding– Representing numbers greater than or equal to 0– Using traditional binary representation

• Two’s-complement encoding– Most common way to represent either positive or

negative numbers

• Floating point encoding– Base-two version of scientific notation for

representing real numbers

Page 49: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

49

Understanding numbers

• Machine representation of numbers are not same as – Integers and real numbers

• They are finite approximations to integers and real numbers– Sometimes, they can behave in unexpected way

Page 50: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

50

‘ int’ is not integer

• Overflow– 200*300*400*500 = -884,901,888– Product of a set of positive numbers yielded a

negative result

• Commutativity & Associativity remain– (500 * 400) * (300 * 200)– ((500 * 400) * 300) * 200– ((200 * 500) * 300) * 400– 400 * (200 * (300 * 500))*Commutativity: 交换律Associativity: 结合律

Page 51: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

51

‘float’ is not real number

• Product of a set of positive numbers is positive

• Overflow and Underflow

• Associativity does not hold

– (3.14+1e20)-1e20 = 0.0

– 3.14+(1e20-1e20) = 3.14

*Overflow: 上溢Underflow: 下溢

Page 52: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

52

Ranges of Values that can be Represented

• Critical to writing programs – that work correctly over the full range of

numeric values – that are portable across different combinations

of • Machine (机器)• operating system (操作系统)• and compiler (编译器)

Page 53: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

53

Properties of Arithmetic Operations

• Several ways are designed – to perform arithmetic operations

• by directly manipulating the bit-level representations of numbers

• It is important for understanding – the machine-level code generated

• when compiling arithmetic expressions.

Page 54: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

54

Hexadecimal

• Base 16 number representation

• Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’

• Write FA1D37B16 in C as

– 0xFA1D37B or

– 0xfa1d37b

Page 55: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

55

Hexadecimal vs. Binary

0x173A4C

Hexadecimal 1 7 3 A 4 C

Binary 0001 0111 0011 1010 0100 1100

1111001010110110110011

Binary 11 1100 1010 1101 1011 0011

Hexadecimal 3 C A D B 3

0x3CADB3

Page 56: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

56

Hexadecimal vs. Decimal

Hexadecimal 0xA7

Decimal 10*16+7 = 167

Decimal 314156 = 19634*16 + 12 (C)

19634 = 1227*16 + 2 (2)

1227 = 76*16 + 11 (B)

76 = 4*16 + 12 (C)

4 = 0*16 + 4 (4)

Hexadecimal 0x4CB2C

Page 57: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

57

Hexadecimal

• Byte = 8 bits– Binary 000000002 to

111111112

– Decimal: 010 to 25510

– Hexadecimal 0016 to FF16

0 0 00001 1 00012 2 00103 3 00114 4 01005 5 01016 6 01107 7 01118 8 10009 9 1001A 10 1010B 11 1011C 12 1100D 13 1101E 14 1110F 15 1111

HexDecim

al

Binary

Page 58: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

58

Virtual Memory (虚拟内存 )

• Most computers cannot access individual bits in a memory directly

• Instead, they use bytes as the smallest addressable unit of memory, that is– Computers can access each byte in a memory

directly

Page 59: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

59

000000010002000300040005000600070008000900100011

Bytes Addr.

001200130014

Virtual Memory

Page 60: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

60

Virtual Memory

• Every byte of memory is identified by a unique number– its address

• The set of all possible addresses is known as – the virtual address space ( 虚拟地址空间 ).

• This virtual address space is just – a conceptual image presented to the machine-

level program

Page 61: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

61

Virtual Memory

• The actual implementation uses a combination of – random-access memory (RAM)– disk storage– special hardware– and operating system software

• These provide the program with what appears to be a monolithic* byte array

*monolithic: 单片集成电路

Page 62: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

62

Subdivide Virtual Memory into More Manageable Units

• One task of – a compiler and – the run-time system

• To store the different program objects– Program data– Instructions– Control information

Page 63: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

63

Word Size

• Indicating the nominal size of – integer and – pointer data

• A virtual address is encoded by – such a word

• The maximum size of the virtual address space– the most important system parameter

determined by the word size

Page 64: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

64

Word Size

• For machine with n-bit word size– Virtual address can range from 0 to 2n-1

• Most current machines are 32 bits (4 bytes)– Limits addresses to 4GB– Becoming too small for memory-intensive

applications

• High-end systems are 64 bits (8 bytes)– Potentially address 1.8 X 1019 bytes

• Machines support multiple data formats– Always integral number of bytes

Page 65: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

65

Sizes of C Objects (in Bytes)

C Declaration Typical 32-bit Compaq Alpha

char 1 1

short int 2 2

int 4 4

long int 4 8

char * 4 8

float 4 4

double 8 8

Page 66: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

66

Pointer Declaration

• For a type T, the declaration – T *p ;– defines a pointer variable p– P is pointing to an object of type T

• For example: char *p ;

Page 67: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

67

Data Size

• Difficulty to make programs portable across different machines and compilers– The program is sensitive to the exact sizes of the

different data types– The C standard sets lower bounds on the

numeric ranges of the different data types– but there are no upper bounds

Page 68: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

68

Data Size

• 32-bit machines have been the standard for the last 20 years

• Many programs have been written – assuming the allocations listed as “typical 32-bit”

in the table

• With the increasing of 64-bit machines – many hidden word size dependencies show up as

• bugs in migrating these programs to new machines

Page 69: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

69

Example

• Many programmers assume that

– a program object declared as type int can be

used to store a pointer

• This works fine for most 32-bit machines

• But leads to problems on an Alpha.

Page 70: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

70

How to Access an Object

• In C, an object may be– an integer– a structure or – some other program unit

• In C, the value of a pointer in C is – the virtual address of the first byte of some block

of storage

Page 71: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

71

How to Access an Object

• The C compiler also associates – type information with each pointer

• It can generate different machine-level code to access the value– stored at the location designated by the pointer– depending on the type of that value.

Designated: 指定的

Page 72: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

72

How to Access an Object

• The actual machine-level program generated by C compiler – has no information about data types – simply treats each program object as a block of

bytes

Page 73: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

73

Byte Ordering

• How should bytes within multi-byte object be ordered in memory

• Little Endian– Least significant byte has lowest address– Alpha, PC

• Big Endian– Least significant byte has highest address– Sun, Mac

• Example: 0x1234567

Page 74: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

74

Big Endian P34

0x100 0x101 0x102 0x103

01 23 45 67

Page 75: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

75

Little Endian P34

0x100 0x101 0x102 0x103

67 45 23 01

Page 76: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

76

Byte Ordering Becomes Visible P36

• Binary data is communicated over a network between different machines

• Reading machine level programs– 80483bd: 01 05 64 94 04 08– add %eax, 0x8049464

• Circumvent the normal type system

Page 77: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

77

Byte Ordering Becomes Visible

• Circumvent the normal type system– Casting– Reference an object according to a different data

type from which it was created– Strongly discouraged for most application

programming– Quite useful and even necessary for system-level

programming

Page 78: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

78

Code to Print Byte Representation P37

typedef unsigned char *pointer;

void show_bytes(pointer start, int len){ int i; for (i = 0; i < len; i++) printf("0x%p\t0x%.2x\n", start+i, start[i]); printf("\n");}

Page 79: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

79

int a = 12345;printf("int a = 12345;\n");show_bytes((pointer) &a, sizeof(int));

Example

Page 80: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

80

Code to Print Byte Representation

• typedef– Giving a name of type– Syntax is exactly like that of declaring a variable

• printf– Format string– %d, %c, %x, %f

* Syntax: 语法

Page 81: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

81

Code to Print Byte Representation

• Pointers and arrays– start is declared as a pointer– It is referenced as an array start[i]

• Pointer creation and dereferencing– Address of &a

• Sizeof– Sizeof(T) returns the number of bytes required to

store an object of type T– One step toward writing code that is portable

across different machine types

Page 82: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

82

int a = 12345;printf("int a = 12345;\n");show_bytes((pointer) &a, sizeof(int));

Example

Page 83: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

83

Result:

int a = 12345;0x11ffffcb80x390x11ffffcb90x300x11ffffcba0x000x11ffffcbb0x00

Example P39

Page 84: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

84

int A = 12345;

long int C = 12345;

Decimal: 12345

Binary: 0011 0000 0011 1001

Hex: 3 0 3 9

Example

Page 85: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

85

39

30

00

00

Linux/Alpha A

30

39

00

00

Sun A

00

00

00

00

39

30

00

00

Alpha C

30

39

00

00

Sun C

39

30

00

00

Linux C

Example P39

Page 86: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

86

Float F = 12345.0;

IEEE Single Precision Floating Point Representation

Hex: 4 6 4 0 E 4 0 0 Binary: 0100 0110 1000 0000 1110 0100 0000 0000

00e44046

Linux/Alpha F

e400

4640

Sun F

Representing Floats

Page 87: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

87

char S[6] = "12345";• Strings in C– Represented by array of characters

– Each character encoded in ASCII format• Standard 7-bit encoding of character set

• Character “0” has code 0x30– Digit i has code 0x30+i

– String should be null-terminated• Final character = 0

Linux/Alpha S Sun S

3334

3132

3500

3334

3132

3500

Representing Strings

Page 88: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

88

char S[6] = "12345";• Compatibility (兼容性 )– Byte ordering not an issue

• Data are single byte quantities

– Text files generally platform independent

• Except for different conventions of line termination character!

Linux/Alpha S Sun S

3334

3132

3500

3334

3132

3500

Representing Strings

Page 89: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

89

Machine-Level Code Representation

• Encode Program as Sequence of Instructions

• Each instruction encoded as bytes– Alpha’s, Sun’s use 4 byte instructions (IA64 use 41bits)

• Reduced Instruction Set Computer (RISC)

– IA32’s use variable length instructions

• Complex Instruction Set Computer (CISC)

• RISC: 精简指令集计算机• CISC: 复杂指令集计算机

Page 90: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

90

Machine-Level Code Representation

• Different instruction types and encodings for different machines– Most code not binary compatible

• Programs are Byte Sequences Too !

Page 91: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

91

Representing Instructions P41

int sum(int x, int y)

{

return x+y;

}

00003042

Alpha sum

0180FA6B

E008

81C3

Sun sum

90020009

E58B

5589

PC sum

450C03450889EC5DC3

Page 92: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

92

Boolean Algebra

• Developed by George Boole in 19th Century– Algebraic representation of logic

• Encode “True” as 1 • Encode “False” as 0

Page 93: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

93

Boolean Algebra P42

AndA&B = 1 when both A=1 and B=1

Not~A = 1 when A=0

OrA|B = 1 when either A=1 or B=1

Exclusive-Or (Xor)A^B = 1 when either A=1 or B=1, but

not both

Page 94: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

94

Integer Ring P43

• Integer Arithmetic Z, +, *, –, 0, 1 forms a “ring”– Addition is “sum” operation– Multiplication is “product” operation– – is additive inverse– 0 is identity for sum– 1 is identity for product

Page 95: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

95

Boolean Algebra

• Boolean Algebra {0,1}, |, &, ~, 0, 1 forms a “Boolean algebra”– Or is “sum” operation– And is “product” operation– ~ is “complement” operation (not additive

inverse)– 0 is identity for sum– 1 is identity for product

Page 96: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

96

Integer Ring & Boolean Algebra P43

Boolean Algebra Integer Ring

Commutativity ( 交换律 ) A | B = B | A A + B = B + A

A & B = B & A A * B = B * A

Associativity ( 结合律 ) (A | B) | C = A | (B | C) (A + B) + C = A + (B + C)

(A & B) & C = A & (B & C) (A * B) * C = A * (B * C)

Distributivity ( 分配律 ) A & (B | C) = (A & B) | (A & C) A * (B + C) = A * B + B * C

Page 97: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

97

Integer Ring & Boolean Algebra

Boolean Algebra Integer RingIdentities ( 同一性 ) A | 0 = A A + 0 = A A & 1 = A A * 1 = A Annihilator ( 零化子 ) A & 0 = 0 A * 0 = 0Cancellation of negation (去否定) ~ (~ A) = A – (– A) = A

Page 98: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

98

Ring Boolean Algebra

Boolean Algebra Integer Ring

Distribuitivity ( 分配律 )

A | (B & C) = (A | B) & (A | C) A + (B * C) (A + B) *

(B + C)

Idempotency ( 幂等性 )

A | A = A A + A A

“A is true” or “A is true” = “A is true”

A & A = A A * A A

Page 99: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

99

Ring Boolean Algebra

Boolean Algebra Integer RingAbsorption ( 吸收律 ) A | (A & B) = A A + (A * B) A “A is true” or “A is true and B is true” = “A is true”

A & (A | B) = A A * (A + B) ALaws of Complements (补集定律) A | ~A = 1 A + –A 1 “A is true” or “A is false”

Every element has additive inverse A | ~A 0 A + –A = 0

Page 100: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

100

Boolean Ring

• Boolean Ring {0,1}, ^, &, , 0, 1 – Identical to integers mod 2– is identity operation: (A) = A

A ^ A = 0

Page 101: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

101

Boolean Ring

• Property Boolean Ring– Commutative sum A ^ B = B ^ A– Commutative product A & B = B & A– Associative sum (A ^ B) ^ C = A ^ (B ^ C)– Associative product (A & B) & C = A & (B & C)– Prod. over sum A & (B ^ C) = (A & B) ^ (B &

C)– 0 is sum identity A ^ 0 = A– 1 is prod. identity A & 1 = A– 0 is product annihilator A & 0 = 0– Additive inverse A ^ A = 0

Page 102: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

102

Relations Between Operations P43

• DeMorgan’s Laws– Express & in terms of |, and vice-versa

A & B = ~(~A | ~B)– A and B are true if and only if neither A nor B is false

A | B = ~(~A & ~B)– A or B are true if and only if A and B are not both

false

Page 103: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

103

Relations Between Operations

• Exclusive-Or using Inclusive OrA ^ B = (~A & B) | (A & ~B)

– Exactly one of A and B is trueA ^ B = (A | B) & ~(A & B)

– Either A is true, or B is true, but not both

*exclusive: 互斥性

Page 104: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

104

General Boolean Algebras P45 Practice Problem 2.8 [Solution P110]

• Operate on Bit Vectors– Operations applied bitwise

01101001& 01010101 01000001

01101001| 01010101 01111101

01101001^ 01010101 00111100

~ 01010101 10101010

Page 105: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

105

General Boolean Algebras P45

• Representation of Sets– Width w bit vector represents subsets of {0, …, w–

1}

– aj = 1 if j A• 01101001 { 0, 3, 5, 6 }• 01010101 { 0, 2, 4, 6 }

– & Intersection 01000001 { 0, 6 }– | Union 01111101 { 0, 2, 3, 4, 5,

6 }– ^ Symmetric difference 00111100 { 2, 3,

4, 5 }– ~ Complement 10101010 { 1, 3,

5, 7 }

Page 106: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

106

Logical Operations P46

• Logical Operators– &&, ||, !

• View 0 as “False”• Anything nonzero as “True”• Always return 0 or 1• Early termination (short cut)

Page 107: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

107

Logical Operations P49

• Examples (char data type)– !0x41 --> 0x00– !0x00 --> 0x01– !!0x41 --> 0x01– 0x69 && 0x55 --> 0x01– 0x69 || 0x55 --> 0x01– p && *p (avoid null pointer access)– a && 5/a (avoid division by zero)

Page 108: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

108

Shift Operations

• Left Shift: x << y– Shift bit-vector x left y positions

• Throw away extra bits on left• Fill with 0’s on right 01100010Argument x

00010000<< 3

10100010Argument x

00010000<< 3

Page 109: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

109

Shift Operations P50

• Right Shift: x >> y– Shift bit-vector x right y

positions• Throw away extra bits on right

– Logical shift• Fill with 0’s on left

– Arithmetic shift• Replicate most significant bit on

right• Useful with two’s complement

integer representation

01100010Argument x

00011000Log. >> 2

00011000Arith. >> 2

10100010Argument x

00101000Log. >> 2

11101000Arith. >> 2

Page 110: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

110

Bit-Level Operations in C

• Operations &, |, ~, ^ Available in C

– Apply to any “integral” data type

• long, int, short, char

– View arguments as bit vectors

– Arguments applied bit-wise

Page 111: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

111

Bit-Level Operations in C P49

• Examples (Char data type)– ~0x41 --> 0xBE– ~010000012 --> 101111102

– ~0x00 --> 0xFF– ~000000002 --> 111111112

– 0x69 & 0x55 --> 0x41– 011010012 & 010101012 --> 010000012

– 0x69 | 0x55 --> 0x7D– 011010012 | 010101012 --> 011111012

Page 112: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

112

Cool Stuff with Xor

• Bitwise Xor is form of addition• With extra property that every value is its

own additive inverse– A ^ A = 0

Page 113: 1 Introduction to Computer Systems. 2 Outline Teaching staffs Textbook and Grading Course Schedule Motivation Suggested reading –Preface xvii~xx

113

Cool Stuff with Xor

void funny(int *x, int *y)

{

*x = *x ^ *y; /* #1 */

*y = *x ^ *y; /* #2 */

*x = *x ^ *y; /* #3 */

}

Step *x *yBegin A B

1 A^B B2 A^B (A^B)^B = A^(B^B) =

A^0 = A3 (A^B)^A = (B^A)^A =

B^(A^A) = B^0 = BA

End B A