machine basics

Upload: alee-mehralieff

Post on 07-Jul-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/18/2019 Machine Basics

    1/44

    Carnegie Mellon

    1and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Machine-Level ProgrammingI: Basics

    !"#$%&"#$: 'ntroduction to Computer Systems!th (ecture, Sep) !, #*!

    Instructors:

    +andal E) Bryant and avid +) O’Hallaron

  • 8/18/2019 Machine Basics

    2/44

    Carnegie Mellon

    2and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Today: MachineProgramming I: Basics

      History of Intel processors andarchitectures

      C, assemly, machine code  !ssemly Basics: "egisters, operands,

    move  !rithmetic # logical operations

  • 8/18/2019 Machine Basics

    3/44

    Carnegie Mellon

    3and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Intel $%& Processors

      'ominate laptop(des)top(server mar)et

      *volutionary design Bac-.ards compati/le up until &*&0, introduced in

    12& Added more 3eatures as time goes on

      Comple$ instruction set computer +CIC

    4any di5erent instructions .ith many di5erent3ormats But, only small su/set encountered .ith (inu6

    programs

    Hard to match per3ormance o3 +educed 'nstruction

    Set Computers 7+'SC8 But, 'ntel has done 9ust that

  • 8/18/2019 Machine Basics

    4/44

    Carnegie Mellon

    4and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Intel $%& *volution: Milestones

    Name Date Transistors MHz %.%& /01% 203 4-/.

    ;irst 0"/it 'ntel processor) Basis 3or 'B4 PC < OS

    4B address space

     5%& /0%4 2143 /&-55 ;irst $# /it 'ntel processor , re3erred to as 'A$#

    Added =>at addressing?, capa/le o3 running @ni6

     Pentium 6* 2..6 /24M 2%..-

    5%.. ;irst 0"/it 'ntel 6&0 processor, re3erred to as 6&0"0

     Core 2 2..& 20/M /.&.-54..

    ;irst multi"core 'ntel processor Core i1 2..% 15/M /1..-

  • 8/18/2019 Machine Basics

    5/44

    Carnegie Mellon

    5and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Intel $%& Processors, cont7 Machine *volution

    $&0 1&! *)$4

    Pentium 11$ $)4

    Pentium%44 112 )!4

    PentiumPro 11! 0)!4

    Pentium ''' 111 &)#4

    Pentium #** #4 Core # uo #**0 #14

    Core i2 #**& 2$4

     !dded 8eatures

    'nstructions to support multimedia operations 'nstructions to ena/le more ecient conditional

    operations

     Transition 3rom $# /its to 0 /its

    4ore cores

  • 8/18/2019 Machine Basics

    6/44

    Carnegie Mellon

    6and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    2./4 tate of the !rt Core i2 Broad.ell #*!

     'es)top Model cores

    'ntegrated graphics

    $)$"$)& DH

    0!F

     erver Model & cores

    'ntegrated '%O

    #"#)0 DH

    !F

  • 8/18/2019 Machine Basics

    7/44

    Carnegie Mellon

    7and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    $%& Clones: !dvanced Micro'evices +!M'

     HistoricallyA4 has 3ollo.ed 9ust /ehind 'ntel

    A little /it slo.er, a lot cheaper

     Then

    +ecruited top circuit designers 3rom igital EGuipmentCorp) and other do.n.ard trending companies

    Built Opteron: tough competitor to Pentium

    eveloped 6&0"0, their o.n e6tension to 0 /its

      "ecent 9ears

    'ntel got its act together (eads the .orld in semiconductor technology

    A4 has 3allen /ehind +elies on e6ternal semiconductor manu3acturer

  • 8/18/2019 Machine Basics

    8/44

    Carnegie Mellon

    8and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Intels &6-Bit History  2../: Intel !ttempts "adical hift from

    I!52 to I!&6  Totally di5erent architecture 7'tanium8

    E6ecutes 'A$# code only as legacy

    Per3ormance disappointing

      2..5: !M' teps in ;ith *volutionaryolution 6&0"0 7no. called =A40?8

      Intel 8elt

  • 8/18/2019 Machine Basics

    9/44

    Carnegie Mellon

    9and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

  • 8/18/2019 Machine Basics

    10/44

    Carnegie Mellon

    10and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Today: MachineProgramming I: Basics

      History of Intel processors andarchitectures

      C, assemly, machine code  !ssemly Basics: "egisters, operands,

    move  !rithmetic # logical operations

    C i M ll

  • 8/18/2019 Machine Basics

    11/44

    Carnegie Mellon

    11and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    'e=nitions

      !rchitecture: +also I!: instruction setarchitecture The parts of a processordesign that one needs to understand or;rite assemly(machine code7

    E6amples: instruction set specication, registers)  Microarchitecture: Implementation of the

    architecture7 E6amples: cache sies and core 3reGuency)

      Code 8orms: 4achine Code: The /yte"level programs that a

    processor e6ecutes

    Assem/ly Code: A te6t representation o3 machinecode

     

    C i M ll

  • 8/18/2019 Machine Basics

    12/44

    Carnegie Mellon

    12and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    CP>

    !ssemly(Machine Code?ie;

    Programmer-?isile tate PC: Program counter

    Address o3 ne6t instruction Called =+'P? 76&0"08

    "egister =le Heavily used program data

    Condition codes Store status in3ormation a/out

    most recent arithmetic or logicaloperation

    PC

    "egisters

    Memory

    Code'atatac) 

    Addresses

    ata

    'nstructionsConditionCodes

    Memory Byte addressa/le array

    Code and user data

    Stac- to supportprocedures

  • 8/18/2019 Machine Basics

    13/44

    C i M ll

  • 8/18/2019 Machine Basics

    14/44

    Carnegie Mellon

    14and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Compiling Into !ssemlyC Code

    +sum7clong plus(long x, long y);

    void sumstore(long x, long y,

    long *dest)

      long t ! plus(x, y);

      *dest ! t;"

    Aenerated $%&-&6

    !ssemlysumstore#  pus$% &r'x  mov% &rdx, &r'x

      call plus

      mov% &rax, (&r'x)

      pop% &r'x

      ret

  • 8/18/2019 Machine Basics

    15/44

    Carnegie Mellon

    15and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    !ssemly Characteristics: 'ataTypes

     

    FIntegerG data of /, 2, 6, or % ytes ata values Addresses 7untyped pointers8

      8loating point data of 6, %, or /. ytes

      Code: Byte seuences encoding series ofinstructions

      o aggregate types such as arrays orstructures  Lust contiguously allocated /ytes in memory

  • 8/18/2019 Machine Basics

    16/44

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    17/44

    Carnegie Mellon

    17and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Code for

    sumstorex+#x

      x

      x+

      xd

      xe

      x2  x

      x

      x

      x

      x+

      x

      x'

      xc

  • 8/18/2019 Machine Basics

    18/44

    Carnegie Mellon

    18and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Machine Instruction*$ample   C Code

    Store value t .heredesignated /y dest

     !ssemly 4ove &"/yte value to

    memory

    Muad .ords in 6&0"0parlance

    Operands:

    t: +egister &rax

    dest: +egister &r'x

    *dest:  4emory MJ&r'x/

     

  • 8/18/2019 Machine Basics

    19/44

    Carnegie Mellon

    19and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    'isassemled

    'isassemling

  • 8/18/2019 Machine Basics

    20/44

    Carnegie Mellon

    20and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    'isassemled

    4ump o assem'ler code or unction sumstore#

     x+ 5# pus$ &r'x

     x+3 51# mov &rdx,&r'x

     x++ 5# call% x+ plus 

     x+e 5+# mov &rax,(&r'x)

     xa1 512#pop &r'x xa2 51#ret%

    !lternate 'isassemly

      ithin gd 'eugger

    gd' sum disassem'le sumstore

    isassem/le procedure

    x61x' sumstore

    E6amine the /ytes starting atsumstore

  • 8/18/2019 Machine Basics

    21/44

    Carnegie Mellon

    21and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    hat Can e'isassemledK

      !nything that can e interpreted ase$ecutale code

      'isassemler e$amines ytes and

    & o'0dump -d 7897O:4.

  • 8/18/2019 Machine Basics

    22/44

    Carnegie Mellon

    22and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Today: MachineProgramming I: Basics

      History of Intel processors andarchitectures

      C, assemly, machine code  !ssemly Basics: "egisters, operands,

    move  !rithmetic # logical operations

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    23/44

    g

    23and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    rsp

    $%&-&6 Integer "egisters

    Can re3erence lo."order /ytes 7also lo."order

    < # /ytes8

    ea$

    e$

    ec$

    ed$

    esi

    edi

    esp

    ep

    r%d

    r0d

    r/.d

    r//d

    r/2d

    r/5d

    r/6d

    r/4d

    r%

    r0

    r/.

    r//

    r/2

    r/5

    r/6

    r/4

    ra$

    r$

    rc$

    rd$

    rsi

    rdi

    rp

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    24/44

    g

    24and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    ome History: I!52"egisters

    &eax

    &ecx

    &edx

    &e'x

    &esi

    &edi

    &esp

    &e'p

    &ax

    &cx

    &dx

    &'x

    &si

    &di

    &sp

    &'p

    &a$

    &c$

    &d$

    &'$

    &al

    &cl

    &dl

    &'l

    /&-it virtual registers+ac);ards compatiility

      g  e  n  e  r  a

       l  p  u  r  p  o  s  e

    accumulate

    counter 

    data

    base

    source

    index 

    destination

    index 

    stack

     pointer 

    base

     pointer 

  • 8/18/2019 Machine Basics

    25/44

    g

    25and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Moving 'ata  Moving 'ata

     mov%  Source, Dest :

      x, >- (i-e C constant, /ut pre6ed .ith ?>@ Encoded .ith , #, or /ytes

    Register: One o3 0 integer registers E6ample: &rax, &r1

    But &rsp reserved 3or special use Others have special uses 3or particular instructions

    Memory: & consecutive /ytes o3 memory at addressgiven /y register Simplest e6ample: (&rax)

    Narious other =address modes?

    &rax

    &rcx

    &rdx

    &r'x

    &rsi

    &rdi&rsp

    &r'p

    &r9

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    26/44

    g

    26and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

     mov%  

  • 8/18/2019 Machine Basics

    27/44

    27and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    imple Memory!ddressing Modes

     ormal +" MemJ"egJ" +egister + species memory address

    Aha Pointer dere3erencing in C

     mov% (&rcx),&rax

     'isplacement'+" MemJ"egJ"N'

    +egister + species start o3 memory region

    Constant displacement species o5set

     mov% (&r'p),&rdx

    Carnegie Mellon

    * l f i l

  • 8/18/2019 Machine Basics

    28/44

    28and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    *$ample of imple!ddressing Modes

    void sBap

      (long *xp, long *yp)

      long t ! *xp;

      long t1 ! *yp;

      *xp ! t1;  *yp ! t;

    "

    sBap#

      mov% (&rdi), &rax

      mov% (&rsi), &rdx

      mov% &rdx, (&rdi)  mov% &rax, (&rsi)

      ret

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    29/44

    29and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    &rdi

    &rsi

    &rax

    &rdx

    >nderstanding SBap+

    void sBap

      (long *xp, long *yp)

      long t ! *xp;

      long t1 ! *yp;

      *xp ! t1;

      *yp ! t;

    "

    Memory

    "egister ?alue

    &rdi xp

    &rsi yp

    &rax t

    &rdx t1

    sBap#

      mov% (&rdi), &rax C t ! *xp

     mov% (&rsi), &rdx C t1 ! *yp

      mov% &rdx, (&rdi) C *xp ! t1

      mov% &rax, (&rsi) C *yp ! t

      ret

    "egisters

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    30/44

    30and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    >nderstanding SBap+

    12

    64&

    &rdi

    &rsi

    &rax

    &rdx

    x12

    x1

    "egisters

    Memory

    sBap#

      mov% (&rdi), &rax C t ! *xp

     mov% (&rsi), &rdx C t1 ! *yp  mov% &rdx, (&rdi) C *xp ! t1

      mov% &rax, (&rsi) C *yp ! t

      ret

    x12

    x11

    x11

    x1

    x1

    !ddress

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    31/44

    31and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    >nderstanding SBap+

    12

    3

    &rdi

    &rsi

    &rax

    &rdx

    x12

    x1

    12

    "egisters

    Memory

    sBap#

       mov% (&rdi), &rax C t ! *xp

     mov% (&rsi), &rdx C t1 ! *yp  mov% &rdx, (&rdi) C *xp ! t1

      mov% &rax, (&rsi) C *yp ! t

      ret

    x12

    x11

    x11

    x1

    x1

    !ddress

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    32/44

    32and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    >nderstanding SBap+

    12

    3

    &rdi

    &rsi

    &rax

    &rdx

    x12

    x1

    12

    3

    "egisters

    Memory

    sBap#

      mov% (&rdi), &rax C t ! *xp

     mov% (&rsi), &rdx C t1 ! *yp  mov% &rdx, (&rdi) C *xp ! t1

      mov% &rax, (&rsi) C *yp ! t

      ret

    x12

    x11

    x11

    x1

    x1

    !ddress

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    33/44

    33and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    >nderstanding SBap+

    3

    3

    &rdi

    &rsi

    &rax

    &rdx

    x12

    x1

    12

    3

    "egistersMemory

    sBap#

      mov% (&rdi), &rax C t ! *xp

     mov% (&rsi), &rdx C t1 ! *yp   mov% &rdx, (&rdi) C *xp ! t1

      mov% &rax, (&rsi) C *yp ! t

      ret

    x12

    x11

    x11

    x1

    x1

    !ddress

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    34/44

    34and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    >nderstanding SBap+

    3

    /25

    &rdi

    &rsi

    &rax

    &rdx

    x12

    x1

    12

    3

    "egistersMemory

    sBap#

      mov% (&rdi), &rax C t ! *xp

     mov% (&rsi), &rdx C t1 ! *yp  mov% &rdx, (&rdi) C *xp ! t1

       mov% &rax, (&rsi) C *yp ! t

      ret

    x12

    x11

    x11

    x1

    x1

    !ddress

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    35/44

    35and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    imple Memory!ddressing Modes

     

    ormal +" MemJ"egJ" +egister + species memory address

    Aha Pointer dere3erencing in C

     mov% (&rcx),&rax

     'isplacement'+" MemJ"egJ"N'

    +egister + species start o3 memory region

    Constant displacement species o5set

     mov% (&r'p),&rdx

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    36/44

    36and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Complete Memory !ddressingModes

     Most Aeneral 8orm

    '+","i,MemJ"egJ"NO"egJ"iN ' : Constant =displacement? , #, or /ytes

    +/: Base register: Any o3 0 integer registers

    +i: 'nde6 register: Any, e6cept 3or &rsp

    S: Scale: , #, , or & 7why these numbers?8

     pecial Cases

    +","i MemJ"egJ"N"egJ"i

    '+","i MemJ"egJ"N"egJ"iN'

    +","i, MemJ"egJ"NO"egJ"i

    Carnegie MellonCarnegie Mellon

  • 8/18/2019 Machine Basics

    37/44

    37and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    E6pressionAddressComputation

    Address

    *6&7rd68

    7rd6,rc68

    7rd6,rc6,8

    *6&*7,rd6,#8

    !ddress Computation*$amples

    E6pressionAddressComputation

    Address

    *6&7rd68 *63*** *6& *63**&

    7rd6,rc68 *63*** *6** *63**

    7rd6,rc6,8 *63*** Q*6** *63**

    *6&*7,rd6,#8 #Q*63*** *6&* *6e*&*

    rd6 *63***

    rc6 *6***

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    38/44

    38and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    Today: MachineProgramming I: Basics

     

    History of Intel processors andarchitectures

      C, assemly, machine code  !ssemly Basics: "egisters, operands,

    move  !rithmetic # logical operations

    Carnegie MellonCarnegie Mellon

  • 8/18/2019 Machine Basics

    39/44

    39and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    !ddress ComputationInstruction

     

    lea rc, 'st Src is address mode e6pression

    Set st to address denoted /y e6pression

      >ses Computing addresses .ithout a memory re3erence

    E)g), translation o3 p R

  • 8/18/2019 Machine Basics

    40/44

    40and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    ome !rithmetic

  • 8/18/2019 Machine Basics

    41/44

    41and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    ome !rithmetic

  • 8/18/2019 Machine Basics

    42/44

    42and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

    !rithmetic *$pression*$ample

    Interesting Instructions lea% : address computation

    sal% : shi3t

    imul% : multiplication But, only used once

    long arit$

    (long x, long y, long D)

      long t1 ! x5y;

      long t2 ! D5t1;

      long t ! x5;  long t ! y * ;

      long t ! t 5 t;

      long rval ! t2 * t;

      return rval;

    "

    arit$#

      lea% (&rdi,&rsi), &rax  add% &rdx, &rax

      lea% (&rsi,&rsi,2), &rdx

      sal% >, &rdx

      lea% (&rdi,&rdx), &rcx

      imul% &rcx, &rax

      ret

  • 8/18/2019 Machine Basics

    43/44

    Carnegie Mellon

  • 8/18/2019 Machine Basics

    44/44

    Machine Programming I:ummary

     

    History of Intel processors andarchitectures Evolutionary design leads to many Guir-s and arti3acts

      C, assemly, machine code Je. 3orms o3 visi/le state: program counter,

    registers, )))

    Compiler must trans3orm statements, e6pressions,procedures into lo."level instruction seGuences

      !ssemly Basics: "egisters, operands,

    move  The 6&0"0 move instructions cover .ide range o3

    data movement 3orms

      !rithmetic C compiler .ill gure out di5erent instruction