1 / 15

Lecture 7. Instructions and High-Level to Machine Code

2010 R&E Computer System Education & Research. Lecture 7. Instructions and High-Level to Machine Code. Prof. Taeweon Suh Computer Science Education Korea University. Instructions and Instruction Set. If you want to talk to foreigners, you should be able to speak their languages

amal
Download Presentation

Lecture 7. Instructions and High-Level to Machine Code

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. MIPS Instruction examples in 2 forms • Human-readable form • Machine-readable form addi $2, $0, 5 // $2 = $0 + 5 sub $7, $7, $2 // $7 = $7 + $2 and $5, $3, $4 // $5 = $3 & $4 = 0x20020005 // addi $2, $0, 5 = 0x00e23822 // sub $7, $7, $2 = 0x00642824 // and $5, $3, $4 0010 0000 0000 0010 0000 0000 0000 0101 0000 0000 1110 0010 0011 1000 0010 0010 0000 0000 0110 0100 0010 1000 0010 0100

  4. Instruction Set Examples • MIPS • x86

  5. MIPS and x86 Instruction Sets • For more information on the complete instruction sets of MIPS and x86, refer to the following links • Intel: http://www.intel.com/products/processor/manuals/ • MIPS: • We are going to study in detail throughout this course

  6. 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 #include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; }

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

  8. 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 #include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; }

  9. 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

  10. 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

  11. Stored Program Concept • 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 Memory (DDR) Hello World Binary (machine code) CPU Address Bus CPU Main Memory (DDR) 01101000 01100000 00110011 11100101 01101000 01100000 00110011 11100101 11100111 00110000 01010101 11000011 10100000 00011111 11100111 00011110 11110011 11000011 00110011 01010101 11100111 00110000 01010101 11000011 10100000 00011111 11100111 00011110 11110011 11000011 00110011 01010101 FSB (Front-Side Bus) C compiler (machine code) North Bridge Data Bus “Hello World” Source code in C DMI (Direct Media I/F) South Bridge

  12. 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!!! x86 machine code a = 3; c7 45 f0 03 00 00 00movl $0x3,-0x10(%ebp) b = 9; c7 45 f4 09 00 00 00 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 89 45 f8mov %eax,-0x8(%ebp) int main() { int a, b, c; a = 3; b = 9; c = a + b; return c; } compile Normal compilation MIPS machine code MIPS-based laptop (if present!) a = 3; 24020003 li v0,3 afc20008 sw v0,8(s8) b = 9; 24020009 li v0,9 afc20004 sw v0,4(s8) c = a + b; 8fc30008 lw v1,8(s8) 8fc20004 lw v0,4(s8) 00000000 nop 00621021 addu v0,v1,v0 afc20000 sw v0,0(s8) x86-based laptop int main() { int a, b, c; a = 3; b = 9; c = a + b; return c; } compile

  13. Cross Compiler (Cont.) • A cross compileris a compiler capable of creating executable code for a platform other than the one on which the compiler is run -- Wiki x86 machine code a = 3; c7 45 f0 03 00 00 00movl $0x3,-0x10(%ebp) b = 9; c7 45 f4 09 00 00 00 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 89 45 f8mov %eax,-0x8(%ebp) int main() { int a, b, c; a = 3; b = 9; c = a + b; return c; } compile MIPS machine code a = 3; 24020003 li v0,3 afc20008 sw v0,8(s8) b = 9; 24020009 li v0,9 afc20004 sw v0,4(s8) c = a + b; 8fc30008 lw v1,8(s8) 8fc20004 lw v0,4(s8) 00000000 nop 00621021 addu v0,v1,v0 afc20000 sw v0,0(s8) x86-based laptop cross-compile

  14. 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 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 0x0232 4020 0x0274 5022 0x8E68 0018 0xAE6A 0008 assembler Don’t worry. We are going to talk deep about this! Memory (DDR) Address Bus MIPS CPU 0x0232 4020 0x0274 5022 0x8E68 0018 0xAE6A 0008 Data Bus

  15. 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

More Related