Introduction Presentation A

Similar documents
Digital Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Great Reality #2: You ve Got to Know Assembly Does not generate random values Arithmetic operations have important mathematical properties

Lecture 1: Course Overview

Computer Organization - Overview

Course Overview CSCE 312. Instructor: Daniel A. Jiménez. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Introduction to Computer Systems

Introduction to Computer Systems

Course Overview. Jo, Heeseung

Chris Riesbeck, Fall Introduction to Computer Systems

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

Introduction to Computer Systems

ABSTRACTION ISN T THE ENTIRE STORY

Introduction to Computer Systems

CS 3330 Introduction. Daniel and Charles. CS 3330 Computer Architecture 1

Introduction to Computer Systems

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

LC-3 Assembly Language

Introduction to Computer Systems: Semester 1 Computer Architecture

CS , Fall 2004 Exam 1

Compiler Drivers = GCC

Bilgisayar Sistemlerine Genel Bakış

CS356: Discussion #8 Buffer-Overflow Attacks. Marco Paolieri

CS , Fall 2002 Exam 1

CS 33. Linkers. CS33 Intro to Computer Systems XXV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Instruction Set Architectures

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Machine-Level Programming V: Advanced Topics

Handling of Interrupts & Exceptions

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Instruction Set Architectures

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

15-213/18-243, Fall 2010 Exam 1 - Version A

What s next. Computer Systems A Programmer s Perspective

CS , Fall 2007 Exam 1

CSCI 192 Engineering Programming 2. Assembly Language

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

Systems I. Machine-Level Programming I: Introduction

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

CSE 351 Midterm - Winter 2015 Solutions

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Representation of Information

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced:

CS356: Discussion #7 Buffer Overflows. Marco Paolieri

Systems Programming and Computer Architecture ( )

CSE 351 Midterm - Winter 2015

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

CS241 Computer Organization Spring 2015 IA

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

Systems Programming and Computer Architecture ( )

CS , Fall 2001 Exam 1

Machine-Level Programming I: Introduction Jan. 30, 2001

Full Name: CISC 360, Fall 2008 Example of Exam

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley

Compila(on, Disassembly, and Profiling

Hardware: Logical View

CSCI2467: Systems Programming Concepts

C and Programming Basics

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

CS 3330 Introduction 1

Course Overview. CSCI 224 / ECE 317: Computer Architecture. Instructors: Prof. Jason Fritts. Slides adapted from Bryant & O Hallaron s slides

BUFFER OVERFLOW. Jo, Heeseung

Buffer Overflow. Jo, Heeseung

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

W4118: PC Hardware and x86. Junfeng Yang

Towards the Hardware"

Introduction to Computer Systems , fall th Lecture, Sep. 28 th

The Compilation Process

Credits and Disclaimers

Credits and Disclaimers

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Lecture 3: Instruction Set Architecture

CSC 591 Systems Attacks and Defenses Return-into-libc & ROP

THE ASCII TABLE. USING THE HEX COLUMN, CONVERT THE FOLLOWING HEX DATA TO CHARACTER (i.e text) DATA: What does it say?

Chapter 11 Introduction to Programming in C

231 Spring Final Exam Name:

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CS 107 Lecture 0: Welcome

CS , Spring 2004 Exam 1

Machine Programming 1: Introduction

CPEG421/621 Tutorial

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Intel x86-64 and Y86-64 Instruction Set Architecture

CS241 Computer Organization Spring

Compiler Design IIIT Kalyani, West Bengal 1. Introduction. Goutam Biswas. Lect 1

Buffer overflows. Specific topics:

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Buffer Overflow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

The Hardware/Software Interface CSE351 Spring 2013 (spring has sprung!)

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Buffer Overflow Attack

Transcription:

CSE 2421/5042: Systems I Low-Level Programming and Computer Organization Introduction Presentation A Read carefully: Bryant Chapter 1 Study: Reek Chapter 2 Skim: Reek Chapter 1 08/22/2018 Gojko Babić Some Basic Terms Algorithm a sequence of precise instructions which leads to a solution; Program an algorithm expressed in a language the computer can understand; Source code the original in a high level language; Compiler translates s in high-level language to machine language; Object code the (compiler) translated version in machine language; Linker combines the object code for the s we write, and the object code for the pre-compiled routines, into the executable code the CPU can run; g. babic Presentation A 2 1

Preparing C Program for Running Very often compilation and linking are done in one step, as far as a user can see. g. babic Presentation A 3 Layout of a Simple C Program /* include statements; if any define statements; if any */ // main function is mandatory // if you do not include this statement, // compiler will do it automatically The source code of a C is stored in one or more files; Each function has to be completely contained in one file; Each source file is individually compiled to produce an object file; g. babic Presentation A 4 2

Main Memory Long list of memory locations each contains some pattern of zeroes and ones can change during execution Binary Digit or Bit: a digit that can only be zero or one Byte: each memory location has eight bits Address: a number that identifies a memory location Some data is too large for a single byte e.g. most integers and real numbers are too large next few consecutive bytes can store the additional bits for larger data address of larger data refers to the first byte g. babic Presentation A 5 Encoding Byte Values Byte = 8 bits Binary (base 2 representation): 000000002 to 111111112 Use characters 0 and 1 e.g. 01100101 Decimal (base 10 representation): 010 to 25510 Use characters 0 to 9 e.g. 127 Hexadecimal (base 16 representation): 0016 to FF16 Use characters 0 to 9 and A to F e.g. 0x7D Octal (base 8 representation): 000 8 to 377 8 Use characters 0 to 7 e.g. 075 Write FA1D37B16 in C 0xFA1D37B or 0xfa1d37b? bits Write 1143768 in C 0114376? bits Presentation A 6 3

Unsigned Integer Encoding Here is how in C an 16-bit unsigned integer variable y can be declared and initialized to 15,213: short unsigned int x = 15213; But how is y represented as a binary number? Let binary number X have w-bit pattern: x w-1 x w-2 x w-3 x 3 x 2 x 1 x 0 x i = 0 or 1 Binary to Unsigned decimal number interpretation: B2U(X) w 1 i 0 x i 2 i Decimal Hex Binary x 15213 3B 6D 00111011 01101101 Presentation A 7 Unsigned Integer Encoding Examples x = 15213: 00111011 01101101 Weight 15213 1 1 1 2 0 0 4 1 4 8 1 8 16 0 0 32 1 32 64 1 64 128 0 0 256 1 256 512 1 512 1024 0 0 2048 1 2048 4096 1 4096 8192 1 8192 16384 0 0 32768 0 0 Sum 15213 8 4

The hello # include <stdio.h> int main() { printf( hello, world\n ); return 0; } ASCI text representation of hello.c : # 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 ( 10 32 32 32 32 112 114 105 110 116 102 40 34 ASCII 7-bit code; ASCII is a part of UTF-8 that uses up to 4 bytes. g. babic Presentation A 9 On Linux system, the translation from C in the source file hello.c to executable object in the file abc is performed by the following command: unix> gcc o abc hello.c hello.c Source (text) #includes expanded #defines replaced Preprocessor (cpp) abc.i Modified source (text) Compilation Compiler (cc1) abc.s Assembly (text) Assembler (as) printf.o? abc.o Relocatable object s (binary) Linker (ld) abc Executable object (binary) To run executable file on a Unix system, type its name: > abc hello, world > g. babic Presentation A 10 5

o file put the output in file gcc o abc hello.c -E stop after preprocessing; do not compile gcc E o abc.i hello.c -S stop after compilation; do not assembly gcc S o abc.s hello.c -c stop after assembly, do not link gcc c o abc.o hello.c hello.c Source (text) Preprocessor (cpp) More About gcc Command abc.i Modified source (text) Compiler (cc1) abc.s Assembly (text) Assembler (as) printf.o abc.o Relocatable object s (binary) Linker (ld) g. babic Presentation A 11 abc Executable object (binary) hello.c x86-64 Assembly Code #include <stdio.h> int main() { printf("hello, world\n"); return 0; } main: subq movl call movl addq ret $8, %rsp $.LC0, %edi puts $0, %eax $8, %rsp This Linux gcc command: gcc -S O1 hello.c produces x86-64 assembly code (64 bit code as default in our system) in file hello.s (on right) from C code in the file hello.c (on left). Note: switch O1 sets level 1 optimization g. babic 12 6

#include <stdio.h> hello.c x86 Assembly Code int main() { printf("hello, world\n"); return 0; } This Linux gcc command: gcc -S O1 m32 hello.c produces x86 assembly code (32 bit code) in file hello.s (on right) from C code in the file hello.c (on left). Note: switch m32 produces 32 bit code g. babic main: pushl %ebp movl %esp, %ebp andl $-16, %esp subl $16, %esp movl $.LC0, (%esp) call puts movl $0, %eax leave ret 13 64-bit Executable Code of hello.c Main Function 00000000004004c4 <main>: 4004c4: 48 83 ec 08 sub $0x8,%rsp 4004c8: bf d8 05 40 00 mov $0x4005d8,%edi 4004cd: e8 e6 fe ff ff callq 4003b8 <puts@plt> 4004d2: b8 00 00 00 00 mov $0x0,%eax 4004d7: 48 83 c4 08 add $0x8,%rsp 4004db: c3 retq 4004dc: 90 nop 4004dd: 90 nop 4004de: 90 nop 4004df: 90 nop 14 7

32-bit Executable Code of hello.c Main Function 080483b4 <main>: 80483b4: 55 push %ebp 80483b5: 89 e5 mov %esp,%ebp 80483b7: 83 e4 f0 and $0xfffffff0,%esp 80483ba: 83 ec 10 sub $0x10,%esp 80483bd: c7 04 24 94 84 04 08 movl $0x8048494,(%esp) 80483c4: e8 27 ff ff ff call 80482f0 <puts@plt> 80483c9: b8 00 00 00 00 mov $0x0,%eax 80483ce: c9 leave 80483cf: c3 ret 15 Typical Computer Organization CPU Register file PC ALU System bus Memory bus Bus interface I/O bridge Main memory USB controller Graphics adapter I/O bus Disk controller Expansion slots for other devices such as network adapters bryant Mouse Keyboard Display File abc (hello.c executable) Disk stored on disk Presentation A 16 8

Cache memory CPU chip Register file Cache memories ALU System bus Memory bus Bus interface I/O bridge Main memory Main memory size: in Giga bytes; 1GB =?? Cache size: in Mega bytes; 1MB =?? Disk size: in Tera bytes; 1TB =?? Presentation A 17 Network Is Just Another I/O Device CPU chip Register file PC ALU System bus Memory bus Bus interface I/O bridge Main memory Expansion slots I/O bus USB controller Graphics adapter Disk controller Network adapter MouseKeyboard Monitor Disk Network Presentation A 18 9

Operating System Protect the computer from misuse, Provide an abstraction for using the hardware so that s can be written for a variety of different hardware, Operating system provide to user s service of accessing I/O devices: keyboard, display, disc and network, Manage the resources to allow for reasonable use by all users and s on a computer. Programs are often written as if they are the only things running on a system and OS allows them to work this way by providing an abstraction known as a process, Process is a running (one or more threads of control), along with all the data associated with it, OS uses context switching to give the appearance of multiple processes executing at once on a single processor. g. babic Presentation A 19 Moving-Head Disk Mechanism A sector size = 512B, A sector (only) is a unit of transfer between memory and disc. g. babic Presentation A 20 10

Reality No. 1: Computer Arithmetic Is x 2 0? Floating point numbers: Yes! But, what are floating point numbers? Integers: 40000 2 1600000000 50000 2?? Is (x + y) + z = x + (y + z)? Integers: Yes! Floating point numbers: (1e20 1e20) + 3.14 --> 3.14 1e20 + ( 1e20 + 3.14) -->?? bryant Presentation A 21 Reality No. 2: Assembly Programming Chances are, you ll never write s in assembly Compilers are much better But: Understanding assembly is key to machine-level execution model Behavior of s in presence of bugs high-level language models break down Tuning performance understand optimizations done (or not done) by the compiler understanding sources of inefficiency Implementing system software bryant Presentation A 22 11

Reality No. 3: Memory Matters 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, main memory and disk effects can greatly affect performance adapting to characteristics of memory system can lead to major speed improvements bryant Presentation A 23 Memory Reference Errors C (as well as and C++) doesn t provide memory protection: out of bounds array references invalid pointer values abuses of C functions malloc/free Can lead to nasty bugs and whether or not bug has any effect depends on system and compiler 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? understand what possible interactions may occur bryant Presentation A 24 12

Reality No. 4: About Performance Carnegie Mellon 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]; } 4.3ms 81.8ms Programs above run on 2.0 GHz Intel Core i7 Haswell The AdHello #include<stdio.h> int main() { char c1; printf("enter 'a', 'b' or 'c':"); scanf("%c", &c1); if (c1=='a') printf("hello A, world\n"); else if (c1=='b') printf("hello B, world\n"); else if (c1=='c') printf("hello C, world\n"); else printf("wrong input\n"); return 0; } g. babic Presentation A 26 13

Monday In Class Assignment Caldwell Lab room CL112 Logon to Windows and then Linux. Create directory Cse2421 and subdirectory A1. In A1 directory, using one of Linux editors create hello.c; include your name at the beginning of the file. Compile hello.c and run it; you should get expected results. In the same directory, using one of Linux editors create AdHello.c; include your name at the beginning of the file. Compile AdHello.c and run it; you should get expected results. At the end of class, turn in hard copies of both source files. Also, electronically submit hello.c and AdHello.c using Carmen at Testing1. To activate Carmen on Linux server first issue command: %firefox & to run browser. 27 14