CS-2011 — Machine Organization and Assembly Language

Slides:



Advertisements
Similar presentations
Carnegie Mellon Course Overview Computer Systems Organization (Fall 2014) Section 001 (Honors) and Section 002 (Regular) Professor Andrew Case Teaching.
Advertisements

CSCE 3121 Computer Organization Lecture 1. CSCE 3122 Course Overview Topics: –Theme –Five great realities of computer systems –Computer System Overview.
Computer Systems & Programming (CS 367) Section 002: Prof. Elizabeth White Spring 2010.
1 Carnegie Mellon The course that gives CMU its “Zip”! Course Overview : Introduction to Computer Systems 1 st Lecture, Aug. 24, 2010 Instructors:
University of Washington The Hardware/Software Interface CSE351 Spring st Lecture, March 28 Instructor: Luis Ceze Teaching Assistants: Aaron Miller,
Introduction to Computer Systems Topics: Theme Five great realities of computer systems How this fits within CS curriculum S ‘08 class01a.ppt
Introduction to Computer Systems Topics: Staff, text, and policies Lecture topics and assignments Lab rationale and infrastructure F ’08 class01b.ppt.
Introduction to Computer Systems Topics: Theme Five great realities of computer systems How this fits within CS curriculum CS 213 F ’03 class01a.ppt
Introduction to Computer Systems* Topics: Theme Five great realities of computer systems How this fits within CS curriculum F ’07 class01a.ppt
CS 213 Introduction to Computer Systems Course Organization David O’Hallaron August 28, 2001 Topics: Staff, text, and policies Lecture topics and assignments.
Introduction to Computer Systems Topics: Theme Five great realities of computer systems How this fits within CS curriculum F ’04 class01a.ppt
COMP 321: Introduction to Computer Systems Scott Rixner Alan L. Cox
The course that gives CMU its “Zip”!
Winter 2015 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University Introduction and Overview.
CS 270 CS 270: Computer Organization Course Overview Instructor: Professor Stephen P. Carl.
COURSE OVERVIEW COMPUTER ARCHITECTURE AND ORGANIZATION Instructor: Professor Emmett Witchel.
Carnegie Mellon Course Overview Computer Systems Organization (Fall 2015) Instructor: Jinyang Li.
Introduction and Overview Summer 2014 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CS355 Advanced Computer Architecture Fatima Khan Prince Sultan University, College for Women.
Lec 1Systems Architecture1 Systems Architecture Lecture 1: Random Access Machines Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some material.
1 Carnegie Mellon The course that gives CMU its “Zip”! Course Overview (18-213): Introduction to Computer Systems 1 st Lecture, Aug. 26, 2014 Instructors:
Assembly Language and Computer Organization Topics: Theme Programming in C Great realities of computer systems How this fits within CS curriculum Logistical.
Assembly Language and Computer Organization Topics: Theme Programming in C Great realities of computer systems How this fits within CS curriculum Logistical.
Introduction to Computer Systems Topics: Theme Five great realities of computer systems How this fits within CS curriculum Logistical issues F ‘08.
Introduction to Computer Systems Topics: Theme Five great realities of computer systems (continued) “The class that bytes”
Computer Systems Overview Topics: Staff, text, and policies Lecture topics and assignments Lab rationale CS 105 “Tour of the Black Holes of Computing!”
1 Carnegie Mellon The course that gives CMU its “Zip”! Course Overview (18-213): Introduction to Computer Systems 1 st Lecture, Aug. 28, 2012 Instructors:
Introduction to Computer Systems Topics: Staff, text, and policies Lecture topics and assignments Lab rationale CS 213 F ’02 class01b.ppt “The Class.
Introduction to Computer Systems Topics: Theme Five great realities of computer systems How this fits within CS curriculum F ’06 class01a.ppt
Computer Organization II Topics: Theme Five great realities of computer systems How this fits within CS curriculum CS 2733.
Assembly Language and Computer Organization Topics: Theme Programming in C Great realities of computer systems How this fits within CS curriculum Logistical.
Course Overview CENG331 - Computer Organization
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Overview Course theme Five realities How the course.
CS 213 Introduction to Computer Systems Course Organization David O’Hallaron August 25, 1998 Topics: Staff, text, and policies Lecture topics and assignments.
CS 213 Introduction to Computer Systems Course Organization Guy Blelloch and Bruce Maggs January 16, 2001 Topics: Staff, text, and policies Lecture topics.
Introduction and Overview Winter 2013 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Course Overview Introduction to Computer Systems 1.
CS101 Computer Programming I
Course Overview CSE 238/2038/2138: Systems Programming
The course that gives CMU its “Zip”!
Final exam: Wednesday, March 20, 2:30pm
Computer Organization II
CS 213 Introduction to Computer Systems Course Organization Todd C
Computer Systems & Programming (CS 367)
Course Overview CSCI 2400/ ECE 3217: Computer Architecture
Course Overview CENG331 - Computer Organization
Computer Systems: A Programmer’s Perspective aka: CS:APP
Course Overview Computer Systems Organization (Fall 2016)
Topics: Staff, text, and policies Lecture topics and assignments
Computer Science 102 Data Structures CSCI-UA
Assembly Language and Computer Organization
Assembly Language and Computer Organization
CS-1004 — Introduction to Programming for Non-Majors
Computer Systems Summary
Course Overview CENG331 - Computer Organization
Andy Wang Operating Systems COP 4610 / CGS 5765
Introduction to Computer Systems
Introduction to Computer Systems
Introduction to Computer Systems
Introduction to Computer Systems
CS-2303 System Programming Concepts
Overview Course theme Five realities
Introduction to Computer Systems
Introduction to Computer Systems
Introduction to Computer Systems
Computer Networks CNT5106C
CS-2303 Introduction (continued)
Loose ends from yesterday
CS Problem Solving and Object Oriented Programming Spring 2019
CS201 – Course Expectations
Presentation transcript:

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) CS-20113, D-Term 2013 Introduction

Overview Course theme Five realities How the course fits into the CS curriculum Logistics CS-20113, D-Term 2013 Introduction

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 CS-20113, D-Term 2013 Introduction

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) --> ?? -352,516,352 CS-20113, D-Term 2013 Introduction Source: xkcd.com/571

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 CS-20113, D-Term 2013 Introduction

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); } CS-20113, D-Term 2013 Introduction

Malicious 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); . . . } CS-20113, D-Term 2013 Introduction

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 CS-20113, D-Term 2013 Introduction

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! CS-20113, D-Term 2013 Introduction

Assembly code example Time Stamp Counter Application 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); CS-20113, D-Term 2013 Introduction

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"); } GCC Extended assembly syntax asm( assembler syntax : output operands /*optional */ : input operands /* optional */ : list of clobbered registers /* optional */ ); CS-20113, D-Term 2013 Introduction

Great Reality #3: Memory matters 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 CS-20113, D-Term 2013 Introduction

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 CS-20113, D-Term 2013 Introduction

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 Saved State 4 d7 ... d4 3 d3 ... d0 2 a[1] 1 a[0] Explanation: Location accessed by fun(i) CS-20113, D-Term 2013 Introduction

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) CS-20113, D-Term 2013 Introduction

Memory system performance example 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]; } 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]; } 21 times slower (Pentium 4) Hierarchical memory organization Performance depends on access patterns Including how step through multi-dimensional array CS-20113, D-Term 2013 Introduction

The Memory Mountain Intel Core i7 2.67 GHz 32 KB L1 d-cache 256 KB L2 cache 8 MB L3 cache CS-20113, D-Term 2013 Introduction

Great Reality #4: 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 CS-20113, D-Term 2013 Introduction

Example Matrix Multiplication Matrix-Matrix Multiplication (MMM) on 2 x Core 2 Duo 3 GHz (double precision) Gflop/s 160x Best code (K. Goto) Triple loop Standard desktop computer, vendor compiler, using optimization flags Both implementations have exactly the same operations count (2n3) What is going on? CS-20113, D-Term 2013 Introduction

MMM Plot: Analysis Matrix-Matrix Multiplication (MMM) on 2 x Core 2 Duo 3 GHz Gflop/s Multiple threads: 4x Vector instructions: 4x Memory hierarchy and other optimizations: 20x 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 CS-20113, D-Term 2013 Introduction

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 CS-20113, D-Term 2013 Introduction

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! CS-20113, D-Term 2013 Introduction

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 CS-20113, D-Term 2013 Introduction

Role within CS Curriculum CS-2301 Sys Prog. for non-majors CS-2303 System Prog. Concepts OR CS-2011 Machine Org & Assembly Lang. CS-3516 Computer Networks CS-3013 Operating Systems CS-4516 Advanced Networks CS-4513 Distributed Systems CS-4515 Computer Architecture CS-20113, D-Term 2013 Introduction

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 CS-20113, D-Term 2013 Introduction

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 CS-20113, D-Term 2013 Introduction

Course components Lectures Lab (i.e., programming) projects — 4 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 CS-20113, D-Term 2013 Introduction

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! CS-20113, D-Term 2013 Introduction

Logistics Lectures:– Quizzes:– No make-up quizzes! 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 CS-20113, D-Term 2013 Introduction

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 CS-20113, D-Term 2013 Introduction

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 Hugh C. Lauer Adjunct Professor CS-20113, D-Term 2013 Introduction

TAs and SAs Will Disanto Jiayuan Wang Remy Jette Taymon Beal CS-20113, D-Term 2013 Introduction

Textbooks Brian Kernighan and Dennis Ritchie, 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! CS-20113, D-Term 2013 Introduction

Textbooks (continued) CS-20113, D-Term 2013 Introduction

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 CS-20113, D-Term 2013 Introduction

Grading 40-45% quizzes 40-45% Lab projects 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 CS-20113, D-Term 2013 Introduction

Getting Help Class Web Page: http://www.cs.wpi.edu/~cs2011/d13 myWPI 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 CS-20113, D-Term 2013 Introduction

Getting Help Course mailng list: cs2011-all@cs.wpi.edu Use as discussion list Staff mailing list: cs2011-staff@cs.wpi.edu 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 CS-20113, D-Term 2013 Introduction

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. CS-20113, D-Term 2013 Introduction 39

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). CS-20113, D-Term 2013 Introduction 40

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 CS-20113, D-Term 2013 Introduction

Policies: Assignments (Labs) And Exams No 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 CS-20113, D-Term 2013 Introduction

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 CS-20113, D-Term 2013 Introduction

Timeliness Grace days Lateness penalties Catastrophic events Advice 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 CS-20113, D-Term 2013 Introduction

WPI Honesty Policy What is cheating? What is NOT 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 CS-20113, D-Term 2013 Introduction

Topics for this course Programs and Data The Memory Hierarchy Performance Exceptions Control Flow Virtual Memory Networking and Concurrency CS-20113, D-Term 2013 Introduction

Carnegi Mellon Welcome and Enjoy! CS-20113, D-Term 2013 Introduction