cs-2011 — machine organization and assembly...
TRANSCRIPT
Carnegie Mellon Worcester Polytechnic Institute
CS-2011 — Machine Organization and Assembly Language
Professor Hugh C. Lauer CS-2011, Machine Organization and Assembly Language (Slides include copyright materials from Computer Systems: A Programmer’s Perspective, by Bryant and O’Hallaron, and from The C Programming Language, by Kernighan and Ritchie)
Introduction CS-20113, D-Term 2013 1
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Overview
Course theme Five realities How the course fits into the CS curriculum Logistics
Introduction CS-20113, D-Term 2013 2
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Course Theme: Abstraction is good but don’t forget reality Most CS courses emphasize abstraction Abstract data types Asymptotic analysis
Abstractions have limits Especially in the presence of bugs Need to understand details of underlying implementations
Useful outcomes Become more effective programmers
Able to find and eliminate bugs efficiently Able to understand and tune for program performance
Prepare for later “systems” classes in CS & ECE Compilers, Operating Systems, Networks, Computer Architecture,
Embedded Systems
Introduction CS-20113, D-Term 2013 3
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Great Reality #1: Ints are not integers, floats are not reals Example 1: Is x2 ≥ 0? Float’s: Yes!
Int’s: 40000 * 40000 ➙ 1,600,000,000 50000 * 50000 ➙ ??
Example 2: Is (x + y) + z = x + (y + z)? Unsigned & Signed Int’s: Yes! Float’s: (1e20 + -1e20) + 3.14 --> 3.14 1e20 + (-1e20 + 3.14) --> ??
Source: xkcd.com/571 Introduction
-352,516,352
CS-20113, D-Term 2013 4
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Code security example /* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[KSIZE]; /* Copy at most maxlen bytes from kernel region to user buffer */ int copy_from_kernel(void *user_dest, int maxlen) { /* Byte count len is minimum of buffer size and maxlen */ int len = KSIZE < maxlen ? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; }
Similar to code found in FreeBSD’s implementation of getpeername
There are legions of smart people trying to find vulnerabilities in programs
Introduction CS-20113, D-Term 2013 5
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Typical usage /* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[KSIZE]; /* Copy at most maxlen bytes from kernel region to user buffer */ int copy_from_kernel(void *user_dest, int maxlen) { /* Byte count len is minimum of buffer size and maxlen */ int len = KSIZE < maxlen ? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; }
#define MSIZE 528 void getstuff() { char mybuf[MSIZE]; copy_from_kernel(mybuf, MSIZE); printf(“%s\n”, mybuf); }
Introduction CS-20113, D-Term 2013 6
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Malicious usage
#define MSIZE 528 void getstuff() { char mybuf[MSIZE]; copy_from_kernel(mybuf, -MSIZE); . . . }
Introduction
/* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[KSIZE]; /* Copy at most maxlen bytes from kernel region to user buffer */ int copy_from_kernel(void *user_dest, int maxlen) { /* Byte count len is minimum of buffer size and maxlen */ int len = KSIZE < maxlen ? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; }
CS-20113, D-Term 2013 7
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Computer arithmetic
Does not generate random values Arithmetic operations have important mathematical properties
Cannot assume all “usual” mathematical properties Due to finiteness of representations Integer operations satisfy “ring” properties
Commutative, associative, distributive
Floating point operations satisfy “ordering” properties Monotonicity, values of signs
Observation Need to understand which abstractions apply in which contexts Important issues for compiler writers and serious application
programmers
Introduction CS-20113, D-Term 2013 8
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Great Reality #2: You’ve got to know assembly language Chances are, you’ll never write programs in assembly Compilers are much better & more patient than you are
But: Understanding assembly is key to machine-level execution model Behavior of programs in presence of bugs
High-level language models break down
Tuning program performance Understand optimizations done / not done by the compiler Understanding sources of program inefficiency
Implementing system software Compiler has machine code as target Operating systems must manage process state
Creating / fighting malware x86 assembly is the language of choice!
Introduction CS-20113, D-Term 2013 9
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Assembly code example
Time Stamp Counter Special 64-bit register in Intel-compatible machines Incremented every clock cycle Read with rdtsc instruction
Application Measure time (in clock cycles) required by procedure double t; start_counter(); P(); t = get_counter(); printf("P required %f clock cycles\n", t);
Introduction CS-20113, D-Term 2013 10
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Code to read counter 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"); }
Introduction CS-20113, D-Term 2013 11
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Great Reality #3: Memory matters Random access memory is an unphysical abstraction
Memory is not unbounded It must be allocated and managed Many applications are memory dominated
Memory referencing bugs especially pernicious Effects are distant in both time and space
Memory performance is not uniform Cache and virtual memory effects can greatly affect
program performance Adapting program to characteristics of memory system
can lead to major speed improvements
Introduction CS-20113, D-Term 2013 12
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Memory referencing bug example
double fun(int i) { volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = 1073741824; /* Possibly out of bounds */ return d[0]; }
fun(0) ➙ 3.14 fun(1) ➙ 3.14 fun(2) ➙ 3.1399998664856 fun(3) ➙ 2.00000061035156 fun(4) ➙ 3.14, then segmentation fault
Result is architecture specific
Introduction CS-20113, D-Term 2013 13
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Memory referencing bug example
Location accessed by fun(i)
Explanation: Saved State 4 d7 ... d4 3 d3 ... d0 2 a[1] 1 a[0] 0 Introduction CS-20113, D-Term 2013 14
double fun(int i) { volatile double d[1] = {3.14}; volatile long int a[2]; a[i] = 1073741824; /* Possibly out of bounds */ return d[0]; }
fun(0) ➙ 3.14 fun(1) ➙ 3.14 fun(2) ➙ 3.1399998664856 fun(3) ➙ 2.00000061035156 fun(4) ➙ 3.14, then segmentation fault
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Memory referencing errors C and C++ do not provide any memory protection Out of bounds array references Invalid pointer values Abuses of malloc/free
Can lead to nasty bugs Whether or not bug has any effect depends on system and compiler Action at a distance
Corrupted object logically unrelated to one being accessed Effect of bug may be first observed long after it is generated
How can I deal with this? Program in Java, Ruby or ML Understand what possible interactions may occur Use or develop tools to detect referencing errors (e.g. Valgrind)
Introduction CS-20113, D-Term 2013 15
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Memory system performance example
Hierarchical memory organization Performance depends on access patterns Including how step through multi-dimensional array
void copyji(int src[2048][2048], int dst[2048][2048]) { int i,j; for (j = 0; j < 2048; j++) for (i = 0; i < 2048; i++) dst[i][j] = src[i][j]; }
void copyij(int src[2048][2048], int dst[2048][2048]) { int i,j; for (i = 0; i < 2048; i++) for (j = 0; j < 2048; j++) dst[i][j] = src[i][j]; }
21 times slower (Pentium 4)
Introduction CS-20113, D-Term 2013 16
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
The Memory Mountain
64M
8M
1M 128K 16
K 2K
0
1000
2000
3000
4000
5000
6000
7000
s1 s3 s5 s7 s9
s11
s13
s15
s32
Size (bytes)
Rea
d th
roug
hput
(MB
/s)
Stride (x8 bytes)
L1
L2
Mem
L3
copyij
copyji
Intel Core i7 2.67 GHz 32 KB L1 d-cache 256 KB L2 cache 8 MB L3 cache
Introduction CS-20113, D-Term 2013 17
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Great Reality #4: There’s more to performance than asymptotic complexity Constant factors matter too! And even exact op count does not predict
performance Easily see 10:1 performance range depending on how code
written Must optimize at multiple levels: algorithm, data
representations, procedures, and loops Must understand system to optimize performance How programs compiled and executed How to measure program performance and identify
bottlenecks How to improve performance without destroying code
modularity and generality Introduction CS-20113, D-Term 2013 18
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Example Matrix Multiplication
Standard desktop computer, vendor compiler, using optimization flags Both implementations have exactly the same operations count (2n3) What is going on?
Matrix-Matrix Multiplication (MMM) on 2 x Core 2 Duo 3 GHz (double precision) Gflop/s
160x
Triple loop
Best code (K. Goto)
Introduction CS-20113, D-Term 2013 19
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
MMM Plot: Analysis Matrix-Matrix Multiplication (MMM) on 2 x Core 2 Duo 3 GHz Gflop/s
Memory hierarchy and other optimizations: 20x
Vector instructions: 4x
Multiple threads: 4x
Reason for 20x: Blocking or tiling, loop unrolling, array scalarization, instruction scheduling, search to find best choice
Effect: fewer register spills, L1/L2 cache misses, and TLB misses Introduction CS-20113, D-Term 2013 20
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Great Reality #5:Computers do more than execute programs They need to get data in and out I/O system critical to program reliability and performance
They communicate with each other over networks Many system-level issues arise in presence of network Concurrent operations by autonomous processes Coping with unreliable media Cross platform compatibility Complex performance issues
Introduction CS-20113, D-Term 2013 21
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Previous versions of CS-2011 Taught assembly language of an artificial, simulated
computer A lot of useful lessons about number formats Some useful lessons about how processors work in general
Much too simple and naïve for modern systems Mapping from C to machine language Issues about caches, memory hierarchy Basics of pipelined processors Relevance to today’s problems and systems
None of the previous problems would begin to
become apparent!
Introduction CS-20113, D-Term 2013 22
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
This version of CS-2011
Read and work with Pentium (IA32, aka X86) assembly language
Understand how C compiles to machine code
Understand how modern computers work At 2000-level course
Introduction CS-20113, D-Term 2013 23
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Role within CS Curriculum
Introduction
CS-2301 Sys Prog. for non-majors
CS-2011 Machine Org & Assembly Lang.
CS-3013 Operating Systems
CS-3516 Computer Networks
CS-4513 Distributed
Systems
CS-4516 Advanced Networks
CS-4515 Computer
Architecture
OR CS-2303
System Prog. Concepts
CS-20113, D-Term 2013 24
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Course perspective
Most Systems Courses are Builder-Centric Computer Architecture
Design pipelined processor in Verilog
Operating Systems Implement large portions of operating system
Compilers Write compiler for simple language
Networking Implement and simulate network protocols
Introduction CS-20113, D-Term 2013 25
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Course perspective (continued)
This course is programmer-centric Purpose is to show how by knowing more about the
underlying system, one can be more effective as a programmer Enable you to
Write programs that are more reliable and efficient Incorporate features that require hooks into OS
– E.g., concurrency, signal handlers
Not just a course for dedicated hackers We bring out the hidden hacker in everyone
Cover material in this course that you won’t see elsewhere
Introduction CS-20113, D-Term 2013 26
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Course components Lectures Higher level concepts, applied concepts, clarifications of texts, etc.
Lab (i.e., programming) projects — 4 The heart of the course 1-2 weeks each Provide in-depth understanding of an aspect of systems Programming and measurement
Recitation sessions Get started on Lab projects Work through problems and details interactively
Weekly quizzes (5 small + 1 medium) Test your understanding of concepts & mathematical principles Work out problems from textbook
Introduction CS-20113, D-Term 2013 27
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Lab rationale
Each lab has a well-defined goal such as solving a puzzle or winning a contest
Doing the lab should result in new skills and concepts
We try to use competition in a fun and healthy way Set a reasonable threshold for full credit Post intermediate results (anonymized) on Web page for
glory!
Introduction CS-20113, D-Term 2013 28
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Logistics Lectures:– Monday, Tuesday, Thursday, Friday, 9:00-10:00 AM Upper Fuller Auditorium No classes on Monday, April 16 & Thursday, April 19
Quizzes:– Mondays (except Patriots’s Day); last quiz on Tuesday At start of class time. Approx 20-25 minutes (except last quiz) Stay in seat when you are finished Best four out of six
No make-up quizzes! See Professor if you have to be away Bring a note from Academic Advising if you are sick We will figure something out
Introduction CS-20113, D-Term 2013 29
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Logistics (continued)
Recitation / Lab sections Salisbury 123 8:00 AM, 9:00 AM, 10:00 AM, 11:00 AM
Attendance Counts!
Must attend your own, registered session No space available in later sections if you oversleep!
Waitlist:– Eight people wait-listed for later sections All sections are full except … … plenty of room in 8:00 AM section
Introduction CS-20113, D-Term 2013 30
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Teaching staff
Ph. D. Carnegie-Mellon, 1972-73 Dissertation “Correctness in
Operating Systems”
Faculty at University of Newcastle upon Tyne, UK
Approximately 30 years in industry in USA
WPI since 2006 21 US patents issued 2 seminal contributions to
Computer Science
Introduction
Hugh C. Lauer Adjunct Professor
CS-20113, D-Term 2013 31
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
TAs and SAs
Introduction
Will Disanto
CS-20113, D-Term 2013 32
Jiayuan Wang
Remy Jette Taymon Beal
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Textbooks
Randal E. Bryant and David R. O’Hallaron, “Computer Systems: A
Programmer’s Perspective, Second Edition” (CS:APP2e), Prentice Hall, 2011
http://csapp.cs.cmu.edu This book really matters
for the course! How to solve labs Practice problems typical
of exam problems
Brian Kernighan and Dennis Ritchie, “The C Programming
Language, Second Edition,” Prentice Hall, 1988 You should keep a copy
of this on your desk for the rest of your (professional) life!
Introduction CS-20113, D-Term 2013 33
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Textbooks (continued)
Introduction CS-20113, D-Term 2013 34
Carnegie Mellon Worcester Polytechnic Institute
Reading Assignment — Chapter 1 of Bryant and O’Hallaron
We will assume full knowledge of this chapter throughout the course
There may be questions on the contents of this chapter on any quiz
Introduction CS-20113, D-Term 2013 35
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Grading 40-45% quizzes Final quiz is worth 2 times any other quiz
40-45% Lab projects Roughly equal in weight
10-20% Class and Recitation participation Helping each other Contributing to discussion groups Asking questions & asking for help It is in your interest that the Professor and TAs know you by
name Please don’t be embarrassed/annoyed if we ask you often
Introduction CS-20113, D-Term 2013 36
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Getting Help Class Web Page: http://www.cs.wpi.edu/~cs2011/d12 Complete schedule of lectures, exams, and assignments Copies of lectures, assignments, exams, solutions Clarifications to assignments
myWPI Repository for copyright materials (lecture notes) Discussion board
Lecture Capture Lectures will be recorded using WPI’s Echo Lecture Capturing
system. Previous lectures can be viewed at
http://echo360.wpi.edu/ess/portal/section/88bf99c6-bdc1-4b2f-a9ba-91d0fb5c6f66
Introduction CS-20113, D-Term 2013 37
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Getting Help Course mailng list: [email protected] Use as discussion list
Staff mailing list: [email protected] Use this for ALL communication with the teaching staff Send email to individual instructors or TAs only to schedule
appointments
Office hours (Prof. Lauer): Monday, 1:30 – 3:30 PM Also: see course web-site Tuesday, 12:00 – 1:00 PM Thursday, 10:00 – 11:00 AM Friday, 1:30 – 2:30 PM
1:1 Appointments You can schedule 1:1 appointments with Professor or any of the TAs Introduction CS-20113, D-Term 2013 38
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Ground Rule #1
There are no “stupid” questions.
It is a waste of your time and the class’s time to proceed when you don’t understand the basic terms.
If you don’t understand it, someone else probably doesn’t it, either.
Introduction CS-20113, D-Term 2013 39
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Ground Rule #2
Help each other!
Even when a project or assignment is specified as individual, ask your friends or classmates about stuff you don’t understand.
It is a waste of your time try to figure out some obscure detail on your own when there are lots of resources around.
When you have the answer, write it in your own words (or own coding style).
Introduction CS-20113, D-Term 2013 40
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Names and Faces
It is in your own interest that I know who you are. Class picture — will circulate next time
Students who speak up in class usually get more favorable grades than those who don’t
When speaking in class, please identify yourselves
Introduction CS-20113, D-Term 2013 41
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Policies: Assignments (Labs) And Exams
Work groups You must work alone on all assignments
Turnin Assignments due at 11:59pm on Tues or Thurs evening Electronic turn-ins using web-based Turnin (unless otherwise
specified) Conflict exams, other irreducible conflicts Make PRIOR arrangements with Prof. Lauer Notifying us well ahead of time shows maturity and makes us like
you more (and thus to work harder to help you out of your problem) Appealing grades Within 7 days of completion of grading Labs: Email to the staff mailing list Quizzes: Talk to Prof. Lauer
Introduction CS-20113, D-Term 2013 42
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Facilities Lab projects to be completed on CCC machines
Use X-window and SSH to access
gcc for compilation
gdb for debugging DDD (Data Display Debugger) for GUI
You may use any other platform On your own! Projects graded on CCC platforms Some tools only run on CCC platforms
Introduction CS-20113, D-Term 2013 43
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Timeliness Grace days 2 grace days for the course Limit of 1 grace day per lab used automatically Covers scheduling crunch, out-of-town trips, illnesses, minor
setbacks Save them until late in the term!
Lateness penalties Once grace day(s) used up, get penalized 20% per day No projects accepted later than 3 days after due date
Catastrophic events Major illness, death in family, … Formulate a plan (with your academic advisor) to get back on track
Advice Once you start running late, it’s really hard to catch up
Introduction CS-20113, D-Term 2013 44
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
WPI Honesty Policy What is cheating?
Sharing code: by copying, retyping, looking at, or supplying a file Coaching: helping your friend to write a lab, line by line Copying code from previous course or from elsewhere on WWW
Only allowed to use code we supply, or from CS:APP website
What is NOT cheating? Explaining how to use systems or tools Helping others with high-level design issues
It is a violation of the WPI Academic Honesty Policy to submit someone else’s work as your own.
It is not a violation of WPI’s Academic Honesty Policy to ask for help! Classmates, TAs, friends, mentors, … Explanations of things you don’t understand
Introduction CS-20113, D-Term 2013 45
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute
Topics for this course
Programs and Data
The Memory Hierarchy
Performance
Exceptions Control Flow
Virtual Memory
Networking and Concurrency
Introduction CS-20113, D-Term 2013 46
Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute Carnegi Mellon
Welcome and Enjoy!
Introduction CS-20113, D-Term 2013 54