Introduction to Machine/Assembler Language Noah Mendelsohn Tufts University Web:

Slides:



Advertisements
Similar presentations
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Advertisements

COMP 2003: Assembly Language and Digital Logic
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 2 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#3) By Dr. Syed Noman.
CS2422 Assembly Language & System Programming September 19, 2006.
Instruction Set Architectures
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
1 Machine-Level Programming I: Basics Computer Systems Organization Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
An Introduction to 8086 Microprocessor.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 2: IA-32 Processor Architecture (c) Pearson Education, All rights reserved. You.
6.828: PC hardware and x86 Frans Kaashoek
CSNB374: Microprocessor Systems Chapter 2: Intel x86 Microprocessor Architecture.
The Pentium Processor.
The Pentium Processor Chapter 3 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
The Pentium Processor Chapter 3 S. Dandamudi.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Computers organization & Assembly Language Chapter 0 INTRODUCTION TO COMPUTING Basic Concepts.
Machine/Assembler Language Control Flow & Compiling Function Calls Noah Mendelsohn Tufts University Web:
Fall 2012 Chapter 2: x86 Processor Architecture. Irvine, Kip R. Assembly Language for x86 Processors 6/e, Chapter Overview General Concepts IA-32.
COMP 2003: Assembly Language and Digital Logic Chapter 0: Some Background Notes by Neil Dickson.
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
CET 3510 Microcomputer Systems Tech. Lecture 2 Professor: Dr. José M. Reyes Álamo.
1 ICS 51 Introductory Computer Organization Fall 2009.
Ted Pedersen – CS 3011 – Chapter 10 1 A brief history of computer architectures CISC – complex instruction set computing –Intel x86, VAX –Evolved from.
RISC and CISC. What is CISC? CISC is an acronym for Complex Instruction Set Computer and are chips that are easy to program and which make efficient use.
Building Your Own Machine The Universal Machine (UM) Introduction Noah Mendelsohn Tufts University Web:
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Machine/Assembler Language Control Flow & Compiling Function Calls Noah Mendelsohn Tufts University Web:
COMP 1321 Digital Infrastructure Richard Henson University of Worcester October 2015.
X86 Assembly Language We will be using the nasm assembler (other assemblers: MASM, as, gas)
COMP 1321 Digital Infrastructure Richard Henson University of Worcester October 2012.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
The Universal Machine (UM) Implementing the UM Noah Mendelsohn Tufts University Web:
1 x86 Programming Model Microprocessor Computer Architectures Lab Components of any Computer System Control – logic that controls fetching/execution of.
Week 6 Dr. Muhammad Ayaz Intro. to Assembly Language.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Computer Science 516 Intel x86 Overview. Intel x86 Family Eight-bit 8080, 8085 – 1970s 16-bit 8086 – was internally 16 bits, externally 8 bits.
Chapter Overview General Concepts IA-32 Processor Architecture
Instruction Set Architecture
Assembly Programming I CSE 351 Spring 2017
Homework Reading Lab with your assigned section starts next week
Assembly language.
Credits and Disclaimers
COMP 1321 Digital Infrastructure
Part of the Assembler Language Programmers Toolbox
x86 Processor Architecture
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Computer Organization & Assembly Language Chapter 3
Computer skills CPU Jakub Yaghob.
Homework Reading Continue work on mp1
Introduction to Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
CS 301 Fall 2002 Computer Organization
COMP 1321 Digital Infrastructure
COMP 1321 Digital Infrastructure
The Microprocessor & Its Architecture
Assembly Language (CSW 353)
Computer Architecture CST 250
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
COMP 1321 Digital Infrastructure
CPU Structure CPU must:
The von Neumann Machine
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Assembly Programming I CSE 351 Spring 2017
Presentation transcript:

Introduction to Machine/Assembler Language Noah Mendelsohn Tufts University Web: COMP 40: Machine Structure and Assembly Language Programming – Fall 2014

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

© 2010 Noah Mendelsohn 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! 3

© 2010 Noah Mendelsohn 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? 4

© 2010 Noah Mendelsohn Introducing Assembler and Machine Language

© 2010 Noah Mendelsohn 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 6

© 2010 Noah Mendelsohn Registers, Memory and Cache

© 2010 Noah Mendelsohn 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! 8

© 2010 Noah Mendelsohn Simple view of a computer 9 Cache Programs and Data Stored as Bit Patterns Memory CPU Registers

© 2010 Noah Mendelsohn IA 32 General Purpose Registers %eax %ecx %edx %ebx %esx %edi %esp %ebp The 32 bit Intel architecture has 8 registers of 32 bits each

© 2010 Noah Mendelsohn IA 32 General Purpose Registers %eax %ecx %edx %ebx %esx %edi %esp %ebp Register names reflect traditional uses…a few still do have special roles

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

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

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

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

© 2010 Noah Mendelsohn 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 16

© 2010 Noah Mendelsohn 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 IA-64 / AMD64 / x86-64 machines! 17

© 2010 Noah Mendelsohn General Purpose Registers – 64 bit 18 %ah $al %ax%eax %rax 0 63

© 2010 Noah Mendelsohn General Purpose Registers – addressing all 64 bits 19 %ah $al %ax%eax %rax 0 63 mov $123,%rax

© 2010 Noah Mendelsohn General Purpose Registers – addressing 16 bits 20 %ah $al %ax%eax %rax 0 63 mov $123,%ax

© 2010 Noah Mendelsohn General Purpose Registers – addressing 32 bits 21 $al %ah %ax%eax %rax 0 63 mov $123,%eax

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

© 2010 Noah Mendelsohn Classes of AMD 64 registers  General purpose registers –16 registers, 64 bits each –Used to compute integer and pointer values –Used for integer call/return values to functions  XMM registers –16 Registers, 128 bits each – latest chips widen these to 256 bits (YMM) –Used to compute float/double values, and for parallel integer computation –Used to pass double/float call/return values  X87 Floating Point registers –8 registers, 80 bits each –Used to compute, pass/return long double 23

© 2010 Noah Mendelsohn Classes of AMD 64 registers  General purpose registers –16 registers, 64 bits each –Used to compute integer and pointer values –Used for integer call/return values to functions  XMM registers –16 Registers, 128 bits each – latest chips widen these to 256 bits (YMM) –Used to compute float/double values, and for parallel integer computation –Used to pass double/float call/return values  X87 Floating Point registers –8 registers, 80 bits each –Used to compute, pass/return long double 24 We will focus mainly on integer, bitmap and pointer computation using the GPRs

© 2010 Noah Mendelsohn History of Intel Architecture Machines

© 2010 Noah Mendelsohn Brief history of Intel Architecture milestones  1978: Intel 8086 – 16 bits 29K transistors  1985: Intel – 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 26

© 2010 Noah Mendelsohn Brief history of Intel Architecture milestones  1978: Intel 8086 – 16 bits 29K transistors  1985: Intel – 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 27 Intel distracted working on incompatible Itanium … AMD grabs de-facto ownership of “Intel” architeture instruction set evolution!

© 2010 Noah Mendelsohn Brief history of Intel Architecture milestones  1978: Intel 8086 – 16 bits 29K transistors  1985: Intel – 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 28

© 2010 Noah Mendelsohn Brief history of Intel Architecture milestones  1978: Intel 8086 – 16 bits 29K transistors  1985: Intel – 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 29

© 2010 Noah Mendelsohn How does the machine compute? High Level Languages vs. Machine Instructions:

© 2010 Noah Mendelsohn Consider a simple function in C 31 int times16(int i) { 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 this way at all! All (almost all) digital computers from the earliest to modern execute very simple instructions encoded as bits.

© 2010 Noah Mendelsohn 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 * 32 * 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.

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

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

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

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

© 2010 Noah Mendelsohn Consider a simple function in C 37 int times16(int i) { return i * 16; } 0:89 f8 mov %edi,%eax 2:c1 e0 04 shl $0x4,%eax 5:c3 retq Huh… What language is this?

© 2010 Noah Mendelsohn Consider a simple function in C 38 int times16(int i) { return i * 16; } 0:89 f8 mov %edi,%eax 2:c1 e0 04 shl $0x4,%eax 5:c3 retq Huh… What language is this? ASSEMBLER LANGUAGE: Source code in ASCII, like C (typically written by people) Assembled into machine code (bits) by the assembler – produces.o file Remember, on our Linux systems the compiler actually produces assembler and then assembles that to make a.o You can see the generated assembler by using the –S option. DO IT!

© 2010 Noah Mendelsohn Assembler vs. machine code 39 int times16(int i) { return i * 16; } 0:89 f8 mov %edi,%eax 2:c1 e0 04 shl $0x4,%eax 5:c3 retq Assembler language ASSEMBLER LANGUAGE: Source code in ASCII, like C (typically written by people) Assembled into machine code (bits) by the assembler – produces.o file Remember, on our Linux systems the compiler actually produces assembler and then assembles that to make a.o You can see the generated assembler by using the –S option. DO IT! Machine code

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

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

© 2010 Noah Mendelsohn How do we build a chip that does this calculation?

© 2010 Noah Mendelsohn Very simplified view of computer 43 Cache Memory

© 2010 Noah Mendelsohn Very simplified view of computer 44 Cache Memory 89 f8 c1 e0 04 c3

© 2010 Noah Mendelsohn Instructions fetched and decoded 45 Cache Memory 89 f8 c1 e0 04 c3

© 2010 Noah Mendelsohn Instructions fetched and decoded 46 Cache Memory 89 f8 c1 e0 04 c3 ALU Arithmetic and Logic Unit executes instructions like add and shift updating registers.

© 2010 Noah Mendelsohn The MIPS CPU Architecture 47

© 2010 Noah Mendelsohn Interpreting vs. Compiling

© 2010 Noah Mendelsohn 49 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

© 2010 Noah Mendelsohn 50 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

© 2010 Noah Mendelsohn 51 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

© 2010 Noah Mendelsohn What a computer is  A computer is a hardware interpreter for machine language programs  Simple machines (RISC): –Hardware directly interprets machine instructions 52

© 2010 Noah Mendelsohn 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 53

© 2010 Noah Mendelsohn 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 54 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

© 2010 Noah Mendelsohn 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: –You will be writing an emulator for a simple CPU later in COMP 40! 55 * The true difference between RISC and CISC is a bit more subtle, but the spirit of this explanation is right

© 2010 Noah Mendelsohn 56 Virtual Machines The C Language Virtual Machine Usually compiled to assembler – occasionally interpreted Or Python, Java, Ruby, etc.

© 2010 Noah Mendelsohn 57 Virtual Machines The C Language Virtual Machine Usually compiled to assembler – occasionally interpreted The Intel IA64 Virtual Machine The machine code a user gets to run

© 2010 Noah Mendelsohn 58 Virtual Machines The C Language Virtual Machine Usually compiled to assembler – occasionally interpreted The Intel IA64 User-mode Virtual Machine The machine code a user gets to run Microcode Low level code underneath a CISC machine

© 2010 Noah Mendelsohn 59 Virtual Machines The C Language Virtual Machine Usually compiled to assembler – occasionally interpreted The Intel IA64 User-mode Virtual Machine The machine code a user gets to run Software emulator or hypervisor Pretend to be a different machine or multiple machines Hypervisors like VMWare and VM/370 create the illusion of multiple hardware machines on a single CPU

© 2010 Noah Mendelsohn 60