当前位置:
文档之家› 清华大学 计算机组成与系统结构 第一周课件
清华大学 计算机组成与系统结构 第一周课件
Computer Arithmetic
Does not generate random values
Arithmetic operations have important mathematical properties Due to finiteness of representations Integer operations satisfy “ring” properties
– 14 –
Code to Read Counter
/* Record the current value of the cycle counter. */ void start_counter() { access_counter(&cyc_hi, &cyc_lo); } /* Number of cycles since the last call to start_counter. */ double get_counter() { unsigned ncyc_hi, ncyc_lo; unsigned hi, lo, borrow; /* Get cycle counter */ access_counter(&ncyc_hi, &ncyc_lo); /* Do double precision subtraction */ lo = ncyc_lo - cyc_lo; borrow = lo > ncyc_lo; hi = ncyc_hi - cyc_hi - borrow; return (double) hi * (1 << 30) * 4 + lo; }
Yes!
» (1e20 + -1e20) + 3.14 --> 3.14 » 1e20 + (-1e20 + 3.14) --> ??
–4–
Code Security Example
/* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[KSIZE]; /* Copy at most maxlenbytes from kernel region to user buffer */ int copy_from_kernel(void *user_dest, int maxlen) { /* Byte count lenis minimum of buffer size and maxlen*/ int len= KSIZE < maxlen? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; }
You’ve got to know assembly
Chances are, you’ll never write program in assembly
Great Reality #2
Compilers are much better & more patient than you are
Understanding assembly key to machine-level execution model
Compilers, Operating Systems, Networks, Computer
Architecture, Embedded Systems
–3–
Great Reality #1
Int’s are not Integers, Float’s are not Reals
Examples
Malicious Usage
#define MSize 528 void getstuff(){ char mybuf[MSize]; copy_from_kernel(mybuf, -MSize); }
–7–
–8–
size_t is unsigned int
–9–
Code Security Example
In Internet companies, to save the number of servers used, and improve response time In high frequency trading companies, performance is money
Prepare for later “systems” classes in CS & ECE
Implementing system software
Compiler has machine code as target
Operating systems must manage process state
Use advanced hardware features
– 12 –
Not supported well with programming language and API
Observation
– 11 –
Need to understand which abstractions apply in which contexts Important issues for compiler writers and serious application programmers
In units of clock cycles
Application
double t; start_counter(); P(); t = get_counter(); printf("P required %f clock cycles\n", t);
– 13 –
Code to Read Counter
What happened when we cast an integer to an unsigned integer?
#define MSize 528 void getstuff(){ char mybuf[MSize]; copy_from_kernel(mybuf, -MSize); } – 10 –
Computer Organization/Architecture
Wenguang CHEN cwg@
Course theme
A typical course would teach you how to build a computer
From bits to byte to ALU to CPU With the emphasis on software And interaction between software and hardware
Is x2 ≥ 0?
Float’s:
Yes!
Int’s:
» 40000 * 40000 --> 1600000000 » 50000 * 50000 --> ??
Is (x + y) + z = x + (y + z)?
Unsigned & Signed Int’s:
Float’s:
Assembly Code Example
Special 64-bit register in Intel-compatible machines Incremented every clock cycle Read with rdtsc instruction Measure time required by procedure
Behavior of programs in presence of bugs
High-level language model breaks down
Tuning program performance
Understanding sources of program inefficiency
How can this course help me?
Understand the organization of a computer to
Become more effective programmers
Able to find and eliminate bugs efficiently Able to tune program performance
Write small amount of assembly code using GCC’s asm facility Inserts assembly code into machine code generated by compiler
static unsigned cyc_hi = 0; static unsigned cyc_lo = 0; /* Set *hi and *lo to the high and low order bits of the cycle counter. */ void access_counter(unsigned *hi, unsigned *lo) { asm("rdtsc; movl %%edx,%0; movl %%eax,%1" : "=r" (*hi), "=r" (*lo) : : "%edx", "%eax"); }
/* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[KSIZE]; /* Copy at most maxlenbytes from kernel region to user buffer */ int copy_from_kernel(void *user_dest, int maxlen) { /* Byte count lenis minimum of buffer size and maxlen*/ int len= KSIZE < maxlen? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; }