05 machine basics

Upload: sharmavasu786

Post on 01-Jun-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 05 Machine Basics

    1/51

    Carnegie Mellon

    1

    Machine-Level ProgrammingI: Basics

    15-213/18-213: Introduction to Computer Systems5thLecture, Jan 28, 201

    Instructors:

    Seth Copen !o"dstein, #nthony $o%e, !re& 'esden

  • 8/9/2019 05 Machine Basics

    2/51

    Carnegie Mellon

    2

    Today: MachineProgramming I: Basics

    History of Intel processors andarchitectures

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

    move Intro to #$%-%&

  • 8/9/2019 05 Machine Basics

    3/51

    Carnegie Mellon

    3

    Intel #$% Processors

    Totally dominate laptop'des(top'servermar(et

    )volutionary design

    (ac)%ards compati*"e up unti" 808+, introduced in18

    #dded more .eatures as time &oes on

    Comple# instruction set computer *CI+C any dierent instructions %ith many dierent.ormats

    (ut, on"y sma"" su*set encountered %ith Linupro&rams

    ard to match per.ormance o. $educed InstructionSet Computers $ISC4

  • 8/9/2019 05 Machine Basics

    4/51

    Carnegie Mellon

    4

    Intel #$% )volution: Milestones

    Name Date Transistors MHz$$% ./0$ 1/2 3-.

    irst 1+-*it Inte" processor7 (asis .or I( 9C ;

  • 8/9/2019 05 Machine Basics

    5/51

    Carnegie Mellon

    5

    Moore6s La7

    Goodness

    Time

  • 8/9/2019 05 Machine Basics

    6/51

    Carnegie Mellon

    6

    Moore6s La7

    Goodness

    Time

  • 8/9/2019 05 Machine Basics

    7/51

    Carnegie Mellon

    7

    Moore6s La7

    Goodness

    Time

  • 8/9/2019 05 Machine Basics

    8/51

    Carnegie Mellon

    8

    Moore6s La7

    Goodness

    Time

    Happy

    Bday

    Happ yB Day

    Happy

    BDay

    ?

  • 8/9/2019 05 Machine Basics

    9/51

    Carnegie Mellon

    9

    More on Moore6s La7You can uy !"is #o$ %6 !oday&

    Compare to./$4

  • 8/9/2019 05 Machine Basics

    10/51

    Carnegie Mellon

    1'

    More on Moore6s La7You can uy !"is #o$ %6 !oday&

    (o$e !"an 39)8'')'''*

    imp$o+emen! in %,cc3

    -n 1983 do..a$s) !"e e/ui+a.en! cos! 0%125)'''&'' i! in 01)25' o*es

    C i M ll

  • 8/9/2019 05 Machine Basics

    11/51

    Carnegie Mellon

    11

    Intel #$% Processors, cont8Machine )volution

    38+ 185 073 9entium 13 371

    9entium/A 1 75

    9entium9ro 15 +75

    9entium III 1 872

    9entium 2001 2 Core 2 ;uo 200+ 21

    Core i 2008 31

    !dded 5eatures

    Instructions to support mu"timedia operations Instructions to ena*"e more eBcient conditiona"

    operations

    ransition .rom 32 *its to + *its

    ore cores

    C i M ll

  • 8/9/2019 05 Machine Basics

    12/51

    Carnegie Mellon

    12

    #$% Clones: !dvanced Micro9evices *!M9

    Historically#; has .o""o%ed ust *ehind Inte"

    # "itt"e *it s"o%er, a "ot cheaper

    Then

    $ecruited top circuit desi&ners .rom ;i&ita" DEuipmentCorp7 and other do%n%ard trendin& companies

    (ui"t

  • 8/9/2019 05 Machine Basics

    13/51

    Carnegie Mellon

    13

    Intel6s %&-Bit Intel !ttempted "adical +hift from I!41 to

    I!%& ota""y dierent architecture Itanium4

    Decutes I#32 code on"y as "e&acy

    9er.ormance disappointin&

    !M9 +tepped in 7ith )volutionary+olution 8+-+ no% ca""ed =#;+?4

    Intel 5elt ligated to 5ocus on I!%&

    ard to admit mista)e or that #; is *etter 1&: Intel !nnounces )M%&T e#tension to

    I!41 Dtended emory +-*it echno"o&y

    #"most identica" to 8+-+6

    !ll ut lo7-end #$% rocessors su ort

    C i M ll

  • 8/9/2019 05 Machine Basics

    14/51

    Carnegie Mellon

    14

    ur Coverage

    I!41 he traditiona" 8+

    shark> gcc m32 hello.c

    #$%-%&

    he emer&in& standard shark> gcc hello.c

    shark> gcc m64 hello.c

    Presentation (oo) presents I#32 in Sections 371G3712

    CoFers 8+-+ in 3713

    He %i"" coFer *oth simu"taneous"y

    Some "a*s %i"" *e *ased on 8+-+, others on I#32

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    15/51

    Carnegie Mellon

    15

    Today: MachineProgramming I: Basics

    History of Intel processors andarchitectures

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

    move Intro to #$%-%&

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    16/51

    Carnegie Mellon

    16

    9e;nitions

    !rchitecture:*also I+!: instruction setarchitecture The parts of a processordesign that one needs to understand to7rite assemly code8 Damp"es: instruction set specication, re&isters7

    Microarchitecture:Implementation of thearchitecture8 Damp"es: cache sies and core .reEuency7

    )#ample I+!s *Intel: #$%, I!

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    17/51

    Carnegie Mellon

    17

    CPect program *p1.op2.o

    )#ecutale program *p

    +taticliraries *.a

    Turning C into >ectCode Code in "esp1.c p2.c

    Compi"e %ith command: gcc O1 p1.c p2.c -o p @se *asic optimiations -O14 9ut resu"tin& *inary in "ep

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    19/51

    Carnegie Mellon

    19

    Compiling Into !ssemly

    C Code

    int sum(int x, int y){ int t = x+y; rturn t;!

    ?enerated I!41

    !ssemlysum" pus#l $%p mo&l $sp,$%p mo&l 12($%p),$ax addl '($%p),$ax

    popl $%p rt

    tain 7ith commandusrlocal%ingcc O1 -S cod.c

    Produces ;le cod.s

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    20/51

    Carnegie Mellon

    2'

    !ssemly Characteristics: 9ataTypes

    @IntegerA data of ., 1, or & ytes ;ata Fa"ues #ddresses untyped pointers4

    5loating point data of &, $, or . ytes

    o aggregate types such as arrays orstructures Just conti&uous"y a""ocated *ytes in memory

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    21/51

    Carnegie Mellon

    21

    !ssemly Characteristics:perations

    Perform arithmetic function on register ormemory data

    Transfer data et7een memory and register

    Load data .rom memory into re&ister Store re&ister data into memory

    Transfer control

    @nconditiona" umps to/.rom procedures Conditiona" *ranches

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    22/51

    Carnegie Mellon

    22

    Code for sum

    x*1* sum"

    x x' x x'%

    x* xc x/ x* x' xd xc/

    >ect Code

    !ssemler

    rans"ates .sinto .o

    (inary encodin& o. each instruction

    Kear"y-comp"ete ima&e o. eecuta*"ecode

    issin& "in)a&es *et%een code indierent "es

    Lin(er $eso"Fes re.erences *et%een "es

    Com*ines %ith static run-time "i*raries

    D7&7, code .ormalloc,print0

    Some "i*raries are dynamically linked

    Lin)in& occurs %hen pro&ram *e&inseecution

    Total of ..ytes

    )achinstruction., 1, or 4

    ytes +tarts at

    Carnegie Mellon

    M hi I i

  • 8/9/2019 05 Machine Basics

    23/51

    g

    23

    Machine Instruction)#ample C Code

    #dd t%o si&ned inte&ers

    !ssemly #dd t%o -*yte inte&ers

    =Lon&? %ords in !CCpar"ance

    Same instruction %hethersi&ned or unsi&ned

  • 8/9/2019 05 Machine Basics

    24/51

    g

    24

    9isassemled

    9isassemling >ectCode

    9isassemler

    o%4dump -d p @se.u" too" .or eaminin& o*ect code

    #na"yes *it pattern o. series o. instructions

    9roduces approimate rendition o. assem*"y code

    Can *e run on either a.outcomp"ete eecuta*"e4 or .o

    "e

    '*'/c* sum"'*'/c*" pus# $%p'*'/c" ' mo& $sp,$%p'*'/c5" '% * c mo& xc($%p),$ax'*'/ca" / * ' add x'($%p),$ax

    '*'/cd" d pop $%p'*'/c" c/ rt

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    25/51

    g

    25

    9isassemled

    6ump o0 assm%lr cod 0or 0unction sum"x'*'/c* sum+" pus# $%px'*'/c sum+1" mo& $sp,$%px'*'/c5 sum+/" mo& xc($%p),$axx'*'/ca sum+7" add x'($%p),$ax

    x'*'/cd sum+" pop $%px'*'/c sum+1" rt

    !lternate 9isassemly

    Dithin gd 9eugger

    gd% pdisassm%l sum

    ;isassem*"e procedure

    x11x% sum

    Damine the 11 *ytes startin& at sum

    >ect

    x*1*"x

    x' x x'% x* xc x/ x* x' xd

    xc/

  • 8/9/2019 05 Machine Basics

    26/51

  • 8/9/2019 05 Machine Basics

    27/51

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    28/51

    28

    Integer "egisters *I!41

    $ax

    $cx

    $dx

    $%x

    $si

    $di

    $sp

    $%p

    $ax

    $cx

    $dx

    $%x

    $si

    $di

    $sp

    $%p

    $a#

    $c#

    $d#

    $%#

    $al

    $cl

    $dl

    $%l

    .%-it virtual registers*ac(7ards compatiility

    genera

    lpurpose

    accumulate

    counter

    data

    base

    source

    index

    destination

    index

    stackpointer

    base

    pointer

    rigin*mostly osolet

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    29/51

    29

    Moving 9ata: I!41 Moving 9ata

    mo&lSource, Dest:

    perand Types Immediate:Constant inte&er data

    Damp"e: ?x*, ?-// Li)e C constant, *ut preed %ith @?A Dncoded %ith 1, 2, or *ytes

    Register:

  • 8/9/2019 05 Machine Basics

    30/51

    3'

    Moving 9ata: I!41 Moving 9ata

    mo&lSource, Dest:

    perand Types Immediate:Constant inte&er data

    Damp"e: ?x*, ?-// Li)e C constant, *ut preed %ith @?A Dncoded %ith 1, 2, or *ytes

    Register:

  • 8/9/2019 05 Machine Basics

    31/51

    31

    mo&lperandCominations

    Cannot do memory-memory transfer wit a

    sing!e instruction

    mo&l

    Imm

    Reg

    Mem

    Reg

    Mem

    Reg

    Mem

    Reg

    +ource 9est C !nalog

    mo&l ?x*,$ax tmp = x*;

    mo&l ?-1*5,($ax) p = -1*5;

    mo&l $ax,$dx tmp2 = tmp1;

    mo&l $ax,($dx) p = tmp;

    mo&l ($ax),$dx tmp = p;

    +rc,9est

  • 8/9/2019 05 Machine Basics

    32/51

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    33/51

    33

  • 8/9/2019 05 Machine Basics

    34/51

    34

  • 8/9/2019 05 Machine Basics

    35/51

    35

  • 8/9/2019 05 Machine Basics

    36/51

    36

  • 8/9/2019 05 Machine Basics

    37/51

    37

  • 8/9/2019 05 Machine Basics

    38/51

    38

  • 8/9/2019 05 Machine Basics

    39/51

    39

    *7

  • 8/9/2019 05 Machine Basics

    40/51

    4'

  • 8/9/2019 05 Machine Basics

    41/51

    41

    *7

    *7

  • 8/9/2019 05 Machine Basics

    42/51

    42

  • 8/9/2019 05 Machine Basics

    43/51

    43

    Complete Memory !ddressingModes

    Most ?eneral 5orm

    9*","i,+Mem"eg"FG+K"eg"iFG 9F ;: Constant =disp"acement? 1, 2, or *ytes

    $*: (ase re&ister: #ny o. 8 inte&er re&isters

    $i: Inde re&ister: #ny, ecept .or $sp

    @n"i)e"y youMd use $%p, either S: Sca"e: 1, 2, , or 8 why these numbers?4

    +pecial Cases

    *","i Mem"eg"FG"eg"iFF

    9*","i Mem"eg"FG"eg"iFG9F

    *","i,+ Mem"eg"FG+K"eg"iFF

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    44/51

    44

    Today: MachineProgramming I: Basics

    History of Intel processors andarchitectures

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

    move Intro to #$%-%&

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    45/51

    45

    9ata "epresentations: I!41G #$%-%&

    +ies of C >ects *in Bytes C ;ata ype !eneric 32-*itInte" I#32 8+-+

    unsi&ned

    int

    "on& int

    8

    char 1 11

    short 2 22

    >oat

    dou*"e 8 88

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    46/51

    46

    rsp

    #$%-%& Integer "egisters

    Dtend eistin& re&isters7 #dd 8 ne% ones7

    a)e Oe*p/Or*p &enera" purpose

    ea#

    e#

    ec#

    ed#

    esi

    edi

    esp

    ep

    r$d

    r/d

    r.d

    r..d

    r.1d

    r.4d

    r.&d

    r.3d

    r$

    r/

    r.

    r..

    r.1

    r.4

    r.&

    r.3

    ra#

    r#

    rc#

    rd#

    rsi

    rdi

    rp

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    47/51

    47

    Instructions

    Long 7ord l *& Bytes Nuad 7ord O *$Bytes

    e7 instructions:

    moF" P moFE add" P addE

    sa"" P sa"E

    etc7

    41-it instructions that generate 41-itresults Set hi&her order *its o. destination re&ister to 0

    Damp"e: add"

    Carnegie Mellon

  • 8/9/2019 05 Machine Basics

    48/51

    48

    41-it code for s7ap

    &oid sBap(int xp, int yp){ int t = xp; int t1 = yp; xp = t1; yp = t;!

    Body

    +et

  • 8/9/2019 05 Machine Basics

    49/51

    49

    %&-it code for s7ap

    perands passed in registers *7hyusefulE

    irst p4 in Ordi, second yp4 in Orsi

    +-*it pointers

    o stac( operations reOuired 41-it data

    ;ata he"d in re&isters Oea and Oed

    &oid sBap(int xp, int yp){ int t = xp; int t1 = yp; xp = t1; yp = t;!

    Body

    +et

  • 8/9/2019 05 Machine Basics

    50/51

    5'

    %&-it code for long ints7ap

    %&-it data ;ata he"d in re&isters Ora and Ord

    moFE operation

    =E? stands .or Euad-%ord

    &oid sBap(long xp, long yp){ long t = xp; long t1 = yp; xp = t1; yp = t;!

    Body

    +et

  • 8/9/2019 05 Machine Basics

    51/51

    Machine Programming I:+ummary

    History of Intel processors andarchitectures DFo"utionary desi&n "eads to many Euir)s and arti.acts

    C, assemly, machine code Compi"er must trans.orm statements, epressions,

    procedures into "o%-"eFe" instruction seEuences

    !ssemly Basics: "egisters, operands,move he 8+ moFe instructions coFer %ide ran&e o. data

    moFement .orms Intro to #$%-%&

    # maor departure .rom the sty"e o. code seen in I#32