Lecture 7. Instructions and High-Level to Machine Code Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.

Slides:



Advertisements
Similar presentations
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Advertisements

Faculty of Computer Science © 2006 CMPUT 229 Why Computer Architecture? An Introduction to CMPUT 229.
Lecture 8: MIPS Instruction Set
Systems Software.
Lecture 2. Instructions and High-level to Machine Code Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced Computer Architecture.
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Lecture 7: Instruction Set Architecture CSE 30: Computer Organization and Systems Programming Winter 2014 Diba Mirza Dept. of Computer Science and Engineering.
COP4020 Programming Languages
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
Lecture 5. AT91 - Memory Map, Timers, and AIC -
1 CS/COE0447 Computer Organization & Assembly Language Pre-Chapter 2.
University of Amsterdam Computer Systems – a guided tour Arnoud Visser 1 Computer Systems A guided Tour.
Introduction to Computer Systems Topics: Theme Four great realities of computer systems Chap 1 in “Computer Systems” book “The Class That Gives.
Intro to Computer Systems Summer 2014 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CSE 2541 – Advanced C Programming Instructor: Yang Zhang
Lecture 3. Virtual Platform and ARM Intro. Prof. Taeweon Suh Computer Science Education Korea University ECM586 Special Topics in Embedded Systems.
COP 3515 IT Program DesignAlessio Gaspar … 1 C Development Environment.
Creating your first C++ program
Programming With C.
Lecture-1 Compilation process
EEC4133 Computer Organization & Architecture Chapter 6: Languages and the Machine by Muhazam Mustapha, May 2014.
Lecture 0. Course Introduction Prof. Taeweon Suh Computer Science Education Korea University ECM585 Special Topics in Computer Design.
Lecture 9. MIPS Processor Design – Instruction Fetch Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education &
CSE 2541 – Advanced C Programming. Course info Prereq – CSE 2221 or CSE 222 Co-req – CSE 2231 Website
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Lecture 4: MIPS Instruction Set Reminders: –Homework #1 posted: due next Wed. –Midterm #1 scheduled Friday September 26 th, 2014 Location: TODD 430 –Midterm.
N from what language did C++ originate? n what’s input, output device? n what’s main memory, memory location, memory address? n what’s a program, data?
Lecture 8. MIPS Instructions #2 – Memory Access (Load/Store) Instructions Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer.
Lecture 4. MIPS Instructions #2 Memory Access (Load/Store) Instructions Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced Computer.
N from what language did C++ originate? n what’s input, output device? n what’s main memory, memory location, memory address? n what’s a program, data?
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Lecture 0. Program Introduction Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education & Research.
Introduction to C CMSC 104, Section 4 Richard Chang 1.
Introduction to C 2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park 1.
CMSC 104, Lecture 111 Introduction to C Topics l Compilation l Using the gcc Compiler l The Anatomy of a C Program l 104 C Programming Standards and Indentation.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Lecture 1. General-Purpose Computer Systems
The Development Process Compilation. Compilation - Dr. Craig A. Struble 2 Programming Process Problem Solving Phase We will spend significant time on.
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
Lecture 0. Course Introduction Prof. Taeweon Suh Computer Science Education Korea University COM515 Advanced Computer Architecture.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Lecture 2. General-Purpose Computer Systems Prof. Taeweon Suh Computer Science Education Korea University ECM586 Special Topics in Embedded Systems.
Lecture 2. Instructions and High-level to Machine Code Prof. Taeweon Suh Computer Science & Engineering Korea University COSE222, COMP212, CYDF210 Computer.
Operating Systems A Biswas, Dept. of Information Technology.
Software Engineering Algorithms, Compilers, & Lifecycle.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Lecture 3 Translation.
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
Computer Systems MTSU CSCI 3240 Spring 2016 Dr. Hyrum D. Carroll
Chapter 1: A Tour of Computer Systems
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Program Execution in Linux
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Computer Organization & Compilation Process
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Introduction to Computer Systems
2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park
Introduction to Computer Systems
Program Execution in Linux
PROGRAM AT RUNTIME Subject code: CSCI-620
Computer Organization & Compilation Process
Introduction to C Topics Compilation Using the gcc Compiler
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
Presentation transcript:

Lecture 7. Instructions and High-Level to Machine Code Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education & Research

Korea Univ Instructions and Instruction Set If you want to talk to foreigners, you should be able to speak their languages  Likewise, to talk to a computer, you must speak its language The words of a computer’s language are called instructions The collection of instructions is called instruction set Different CPUs have different instruction sets  x86 has its own instruction set  MIPS has its own instruction set  But, they have many aspects in common 2

Korea Univ MIPS Instruction examples in 2 forms Human-readable form Machine-readable form 3 addi $2, $0, 5 // $2 = $0 + 5 sub $7, $7, $2 // $7 = $7 + $2 and $5, $3, $4 // $5 = $3 & $ = 0x // addi $2, $0, 5 = 0x00e23822 // sub $7, $7, $2 = 0x // and $5, $3, $4

Korea Univ Instruction Set Examples x86 4 MIPS

Korea Univ MIPS and x86 Instruction Sets 5 For more information on the complete instruction sets of MIPS and x86, refer to the following links  Intel:  MIPS: We are going to study in detail throughout this course

Korea Univ High Level Code to Assembly to Executable What steps did you take to run your program after writing your code “hello.c” on your Linux machine?  %gcc hello.c -o hello” // hello is a machine code (binary or executable)  %./hello  % Hello World!  %objdump –D hello // it shows human-readable code 6 #include int main(void) { printf("Hello World!\n"); return 0; }

Korea Univ Reality check: High Level Code to Assembly to Executable 7 C program compiler assembly code executable library routines linker loader memory Machine code preprocessor Expanded C program assembler object code Human-readable assembly code cpp (C-preprocessor) in Linux GNU C gcc in Linux GNU C as in Linux GNU ld in Linux GNU Linux kernel loads the executable into memory

Korea Univ Reality check: High Level Code to Assembly to Executable (Cont) The command “gcc” hides all the details Try to compile hello.c with “gcc –v hello.c –o hello”  You will see all the details of what gcc does for compilation Compilation goes through several steps to generate machine code  Preprocessor  Compilation  Assembler  Linker 8 #include int main(void) { printf("Hello World!\n"); return 0; }

Korea Univ Reality check: High Level Code to Assembly to Executable (Cont) Preprocessing  Use to expand macros and header files included  %cpp hello.c > hello.i open “hello.i” to see what you got Compilation  Actual compilation of the preprocessed code to assembly language for a specific processor  %gcc -Wall -S hello.i Result will be stored in hello.s Open hello.s to see what you got Assembler  Convert assembly language into machine code and generate an object file  %as hello.s -o hello.o The resulting file ‘hello.o’ contains the machine instructions for the Hello World program, with an undefined reference to printf 9

Korea Univ Reality check: High Level Code to Assembly to Executable (Cont) Linker  Final stage of compilation  Linking object files to create an executable  In practice, an executable requires many external functions from system and C run-time (crt) libraries  Consequently, the actual link commands used internally by GCC are complicated.  Example %ld -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc/i386-redhat-linux/4.3.0/crtbegin.o -L/usr/lib/gcc/i386- redhat-linux/4.3.0 hello.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc/i386-redhat-linux/4.3.0/crtend.o /usr/lib/crtn.o -o hello Note that “i386-redhat-linux/4.3.0/” is dependent on your Linux version Now run your program  %./hello // Linux kernel loads the program into memory  %Hello World! // output 10

Korea Univ Stored Program Concept 11 Memory (DDR) CPU North Bridge South Bridge Main Memory (DDR) FSB (Front-Side Bus) DMI (Direct Media I/F) CPU Hello World Binary (machine code) C compiler (machine code) “Hello World” Source code in C Instructions are represented in binary, just like data Instructions and data are stored in memory CPU fetches instructions and data to execute Programs can operate on programs  e.g., compilers, linkers, … Binary compatibility allows compiled programs to work on different computers  Standardized ISAs Address Bus Data Bus

Korea Univ Cross Compiler Hmmm, sound good so far But, wait! We are talking about MIPS (not x86). Then, How to generate the MIPS machine code without a MIPS machine? You are still able to generate MIPS binaries on an x86 machine…  How? Use a cross-compiler!!! 12 int main() { int a, b, c; a = 3; b = 9; c = a + b; return c; } x86-based laptop MIPS-based laptop (if present!) int main() { int a, b, c; a = 3; b = 9; c = a + b; return c; } compile a = 3; liv0,3 afc20008 swv0,8(s8) b = 9; liv0,9 afc20004 swv0,4(s8) c = a + b; 8fc30008 lwv1,8(s8) 8fc20004 lwv0,4(s8) nop adduv0,v1,v0 afc20000 swv0,0(s8) MIPS machine code Normal compilation a = 3; c7 45 f movl $0x3,-0x10(%ebp) b = 9; c7 45 f movl $0x9,-0xc(%ebp) c = a + b; 8b 55 f4 mov -0xc(%ebp),%edx 8b 45 f0 mov -0x10(%ebp),%eax 01 d0 add %edx,%eax f8 mov %eax,-0x8(%ebp) x86 machine code

Korea Univ Cross Compiler (Cont.) A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is run -- Wiki 13 int main() { int a, b, c; a = 3; b = 9; c = a + b; return c; } x86-based laptop compile cross-compile a = 3; liv0,3 afc20008 swv0,8(s8) b = 9; liv0,9 afc20004 swv0,4(s8) c = a + b; 8fc30008 lwv1,8(s8) 8fc20004 lwv0,4(s8) nop adduv0,v1,v0 afc20000 swv0,0(s8) MIPS machine code a = 3; c7 45 f movl $0x3,-0x10(%ebp) b = 9; c7 45 f movl $0x9,-0xc(%ebp) c = a + b; 8b 55 f4 mov -0xc(%ebp),%edx 8b 45 f0 mov -0x10(%ebp),%eax 01 d0 add %edx,%eax f8 mov %eax,-0x8(%ebp) x86 machine code

Korea Univ MIPS Cross Compiler Check out the class web for instructions on how to build the MIPS cross-compiler on x86-based Linux Test-generate binary from the MIPS assembly program with assembler 14 add $t0, $s1, $s2 # $t0 <= $s1 + $s2 sub $t2, $s3, $s4 # $t2 <= $s3 - $s4 lw $t0, 24($s3) #load (read) word from memory # $t0 <= [$s3 + 24] sw $t2, 8($s3) # store(write) word to memory # [$s3 + 8] <= $t2 0x x x8E xAE6A 0008 MIPS CPU Memory (DDR) Address Bus Data Bus 0x x x8E xAE6A 0008 Don’t worry. We are going to talk deep about this! assembler

Korea Univ This Course … In this course, you need to write some (or many) MIPS assembly code Then, use MIPS assembler to assemble your (assembly) program and a linker to generate executable (binary)  We don’t use preprocessor and compiler to generate assembly code because we don’t do high-level programming here Finally, run your code in the MIPS simulator called SPIM  Then, run your code on the CPU you’ll design later!!! The compiler course (COMP417) hopefully covers details about preprocessor and compiler (and assembler, linker, and loader) Let’s go over MIPS instructions in a great detail from the next class 15