1 / 54

Introduction to Machine/Assembler Language

COMP 40: Machine Structure and Assembly Language Programming – Spring 2014. Introduction to Machine/Assembler Language. Noah Mendelsohn Tufts University Email: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah. COMP 40 term roadmap. Building Useful Applications in your Language.

ordell
Download Presentation

Introduction to Machine/Assembler Language

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. COMP 40: Machine Structure and Assembly Language Programming – Spring 2014 Introduction toMachine/Assembler Language Noah Mendelsohn Tufts UniversityEmail: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah

  2. COMP 40 term roadmap Building Useful Applications in your Language Ramp up your Programming Skills Big programs that teach you abstraction, pointers, locality, machine representations of data Building a Language Processor on your Emulator Intel Assembler ProgrammingThe Bomb! Emulating your own hardware in software

  3. Goals for today – learn: • How do computers compute? • High level languages (C) vs. low-level (machine/ASM) • Assember vs. machine code: what’s the difference? • Registers vs. main memory • Why assembler programming is: • Interesting • Important • Fun!

  4. Goals for today – learn (in this order actually): • Why assembler programming is important & fun! • Registers vs. main memory • High level languages (C) vs. low-level (machine/ASM) • Assember vs. machine code: what’s the difference? • How do computers compute?

  5. Introducing Assembler and Machine Language

  6. Why assembler / machine code matters • Necessary for understanding how computers work • Necessary for understanding • Compilers • Computer viruses • Anti-virus tools • CPU design • Device drivers • Essential for debugging and performance tuning • A wonderful art…lots of fun! • History: how all programs were written for decades

  7. Registers, Memory and Cache

  8. Why Registers? • Fastest storage – built into CPU • Compact addressing directly from instructions • How many bits does it take to choose one of 16 registers? • The typical modern machine does most of its computing in registers! • To compute on data in memory…first load it into a register! • Comparing to L1 cache • Both are fast: registers typically somewhat faster • Cache used transparently when accessing memory • Registers completely integrated with CPU • Compiler directly controls placement of data in registers!

  9. Simple view of a computer ProgramsandDataStored asBit Patterns Memory Registers Cache CPU

  10. IA 32 General Purpose Registers 0 31 %eax The 32 bit Intel architecture has 8 registers of 32 bits each %ecx %edx %ebx %esx %edi %esp %ebp

  11. IA 32 General Purpose Registers 0 31 %eax Register names reflect traditional uses…a few still do have special roles %ecx %edx %ebx %esx %edi %esp %ebp

  12. IA 32 General Purpose Registers 0 31 %eax Register names reflect traditional uses…a few still do have special roles %esp typically used for stack pointer %ecx %edx %ebx %esx %edi %esp %ebp

  13. IA 32 General Purpose Registers 0 31 %eax %ecx Register names reflect traditional uses…a few still do have special roles %eax typically used to accumlate and return function results %edx %ebx %esx %edi %esp %ebp

  14. IA 32 General Purpose Registers 0 31 %ax %ah $al %eax %ecx Before there were IA32 machines, there were 16 bit machines…. …the 16 bit registers are simulated within the 32 bit ones %edx %ebx %esx %edi %esp %ebp

  15. IA 32 General Purpose Registers 0 31 And within that some instructions can update individual bytes %ax %ah $al %eax %ecx %edx %ebx %esx You want to read Bryant and O’Hallaron for the details! %edi %esp %ebp

  16. What we’ve seen so far – IA 32 • 8 registers with names like %eax • For IA32, each holds 32 bits • This is where data will be placed for: • Arithmetic • Shifting / masking • Calculating pointers for addressing • Smaller registers from 16 & 8 bit Intel architectures implemented as part of 32 bit registers

  17. What we’ve seen so far – IA 32 • 8 registers with names like %eax • For IA32, each holds 32 bits • This is where data will be placed for: • Arithmetic • Shifting / masking • Calculating pointers for addressing • Smaller registers from 16 bit Intel architectures implemented as part of 32 but registers • But…we run on IA64 / AMD64 / x86-64 machines!

  18. History of Intel Architecture Machines

  19. Brief history of Intel Architecture milestones • 1978: Intel 8086 – 16 bits 29K transistors • 1985: Intel 80386– 32 bits 275K transistors • 1995: Intel Pentium Pro– new microarch 5.5M transistors • 1999: Intel Pentium III– 32 bits + SSE/SIMD 8.2M transistors • 2003: AMD Athlon– 64 bits?? M transistors • New architecture known as x86-64 or AMD 64 • Backwards compatible with IA32

  20. Brief history of Intel Architecture milestones • Intel distracted working on incompatible Itanium… AMD grabs de-facto ownership of “Intel” architeture instruction set evolution! • 1978: Intel 8086 – 16 bits 29K transistors • 1985: Intel 80386– 32 bits 275K transistors • 1995: Intel Pentium Pro– new microarch 5.5M transistors • 1999: Intel Pentium III– 32 bits + SSE/SIMD 8.2M transistors • 2003: AMD Athlon– 64 bits?? M transistors • New architecture known as x86-64 or AMD 64 • Backwards compatible with IA32

  21. Brief history of Intel Architecture milestones • 1978: Intel 8086 – 16 bits 29K transistors • 1985: Intel 80386– 32 bits 275K transistors • 1995: Intel Pentium Pro– new microarch 5.5M transistors • 1999: Intel Pentium III– 32 bits + SSE/SIMD 8.2M transistors • 2003: AMD Athlon– 64 bits?? M transistors • New architecture known as x86-64 or AMD 64 • Backwards compatible with IA32 • 2004: Intel Pentium 4E– 64 bits 125M transistors • Intel caves and supports AMD 64 instruction set

  22. Brief history of Intel Architecture milestones • 1978: Intel 8086 – 16 bits 29K transistors • 1985: Intel 80386– 32 bits 275K transistors • 1995: Intel Pentium Pro– new microarch 5.5M transistors • 1999: Intel Pentium III– 32 bits + SSE/SIMD 8.2M transistors • 2003: AMD Athlon– 64 bits?? M transistors • New architecture known as x86-64 or AMD 64 • Backwards compatible with IA32 • 2004: Intel Pentium 4E– 64 bits 125M transistors • Intel caves and supports AMD 64 instruction set • Today: Intel (Core I7) and AMD (e.g. Opteron) compete with same instruction set

  23. X86-64 /AMD 64 / IA 64 General Purpose Registers 63 31 0 %eax %ah %dh %bh %ch $dl $bl $al $cl %bx %dx %cx %ax %rax %rcx %ecx %rdx %edx %rbx %ebx %si %rsi %esi %di %rdi %edi %sp %rsp %esp %bp %rbp %ebp

  24. X86-64 /AMD 64 / IA 64 General Purpose Registers 63 31 0 Original IA 32 registers extended to 64 bits %eax %ah %ch %bh %dh $bl $cl $dl $al %dx %ax %cx %bx %rax %rcx %ecx %rdx %edx %rbx %ebx %si %rsi %esi %di %rdi %edi %sp %rsp %esp %bp %rbp %ebp

  25. X86-64 /AMD 64 / IA 64 General Purpose Registers 0 63 31 0 63 %eax %ah %ch %dh %bh $bl $al $dl $cl %dx %bx %ax %cx %rax %r8 %rcx %ecx %r9 %rdx %r10 %edx %rbx %r11 %ebx %si %rsi %r12 %esi %di %rdi %r13 %edi %sp %rsp %r14 %esp %bp %rbp %r15 %ebp

  26. X86-64 /AMD 64 / IA 64 General Purpose Registers 8 more registers added for a total of 16 0 63 31 0 63 %eax %ch %dh %bh %ah $bl $al $cl $dl %bx %dx %cx %ax %rax %r8 %rcx %ecx %r9 %rdx %r10 %edx %rbx %r11 %ebx %si %rsi %r12 %esi %di %rdi %r13 %edi %sp %rsp %r14 %esp %bp %rbp %r15 %ebp

  27. High Level Languagesvs.Machine Instructions

  28. Consider a simple function in C int times16(inti) { return i * 16; } • Building a machine to execute this directly in hardware would be: • Difficult • Slow • Expensive • Etc. • For the first few decades of computing, nobody programmed thisway at all! All (almost all) digital computers from the earliest to modern execute very simple instructions encoded as bits.

  29. Machine code (typical) • Simple instructions – each does small unit of work • Stored in memory • Bitpacked into compact binary form • Directly executed by transistor/hardware logic* * We’ll show later that some machines execute user-provided machine code directly, some convert it to an even lower level machine code and execute that.

  30. Here’s the machine code for our function int times16(inti) { return i * 16; } Remember: This is what’s really in memory and what the machine executes! 89 f8 c1 e0 04 c3

  31. Here’s the machine code for our function int times16(inti) { return i * 16; } But what does it mean?? 89 f8 c1 e0 04 c3 Does it really implement the times16 function?

  32. Consider a simple function in C int times16(inti) { return i * 16; } 0: 89 f8 mov %edi,%eax 2: c1 e0 04 shl $0x4,%eax 5: c3 retq

  33. Consider a simple function in C Load i into result register %eax int times16(inti) { return i * 16; } 0: 89 f8 mov %edi,%eax 2: c1 e0 04 shl $0x4,%eax 5: c3 retq

  34. Consider a simple function in C Shifting left by 4 is quick way to multiply by 16. int times16(inti) { return i * 16; } 0: 89 f8 mov %edi,%eax 2: c1 e0 04 shl $0x4,%eax 5: c3 retq

  35. Consider a simple function in C Return to caller, which will look for result in %eax int times16(inti) { return i * 16; } 0: 89 f8 mov %edi,%eax 2: c1 e0 04 shl $0x4,%eax 5: c3 retq REMEMBER: you can see the assembler code for any C program by running gcc with the –S flag. Do it!!

  36. How do we build a chipthat does this calculation?

  37. Very simplified view of computer Memory Cache

  38. Very simplified view of computer Memory 89 f8 c1 e0 04 c3 Cache

  39. Instructions fetched and decoded Memory 89 f8 c1 e0 04 c3 Cache

  40. Instructions fetched and decoded Memory 89 f8 c1 e0 04 c3 Cache ALUArithmetic and Logic Unit executes instructions like add and shift updating registers.

  41. The MIPS CPU Architecture

  42. Interpreting vs. Compiling

  43. INTERPRETER Software or hardware that does what the instructions say COMPILER Software that converts a program to another language ASSEMBLER Like a compiler, but the input assembler language is (mostly)1-to-1 with machine instructions

  44. INTERPRETER Software or hardware that does what the instructions say COMPILER Software that converts a program to another language ASSEMBLER Like a compiler, but the input assembler language is (mostly)1-to-1 with machine instructions

  45. INTERPRETER Software or hardware that does what the instructions say COMPILER Software that converts a program to another language ASSEMBLER Like a compiler, but the input assembler language is (mostly)1-to-1 with machine instructions

  46. What a computer is • A computer is a hardware interpreter for machine language programs • Simple machines (RISC): • Hardware directly interprets machine instructions

  47. What a computer is • A computer is a hardware interpreter for machine language programs • Simple machines (RISC): • Hardware directly interprets machine instructions • Complex machines (CISC):* • The real computer handles only simple instructions • Hardware converts user’s complex machine instructions to simple instructions on the fly – then interprets

  48. What a computer is • A computer is a hardware interpreter for machine language programs • Simple machines (RISC): • Hardware directly interprets machine instructions • Complex machines (CISC):* • The real computer handles only simple instructions • Hardware converts user’s complex machine instructions to simple instructions on the fly – then interprets Almost like compiling on the fly! * The true difference between RISC and CISC is a bit more subtle, but the spirit of this explanation is right

  49. What a computer is • A computer is a hardware interpreter for machine language programs • Simple machines (RISC): • Hardware directly interprets machine instructions • Complex machines (CISC):* • The real computer handles only simple instructions • Hardware converts user’s complex machine instructions to simple instructions on the fly – then interprets • Software interpreters - emulators • You can write software programs that do the same thing as hardware computers • Example: http://bellard.org/jslinux/ • You will be writing an emulator for a simple CPU later in COMP 40! * The true difference between RISC and CISC is a bit more subtle, but the spirit of this explanation is right

  50. Virtual Machines Or Python, Java, Ruby, etc. The C Language Virtual Machine Usually compiled to assembler – occasionally interpreted

More Related