4615-4713_chapter 2.ppt

44
 C   o   p   y   r  i  g   h  t   ©    D  o   r  l  i  n  g   K  i  n  d   e   r  s   l  e   y   I  n  d   i  a   P  v  t    L  t   d  The x86 Microprocessor - Lyla B Das 1 PROGRAMMING CONCEPTS 1 CHAPTER 2

Upload: a-samuel-clement

Post on 02-Jun-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 4615-4713_Chapter 2.ppt

    1/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 1

    PROGRAMMING

    CONCEPTS 1

    CHAPTER 2

  • 8/10/2019 4615-4713_Chapter 2.ppt

    2/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 2

    ASSEMBLY LANGUAGE

    PROGRAMMINGTo code efciently in assembly langage !o" a

    #a"ticla" #"ocesso"$ t%e #"e"e&isites a"e

    a good 'no(ledge o! t%e inte"nal a"c%itect"eo! t%e #"ocesso" and add"essing modes

    2

  • 8/10/2019 4615-4713_Chapter 2.ppt

    3/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 3

    T%e Assembly P"ocessAn assemble" is a t"anslato" t%at t"anslates

    so"ce inst"ctions )in symbolic langage*

    into ta"get inst"ctions )in mac%ine langage*on a one+to+one basis

    3

  • 8/10/2019 4615-4713_Chapter 2.ppt

    4/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 4

    ,eat"es o! assemble"s-abels a"e sed !o" memo"y add"esses

    -abels a"e sed !o" constants

    Mac"os a"e allo(ed

    4

  • 8/10/2019 4615-4713_Chapter 2.ppt

    5/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 5

    Inst"ctions and .i"ecti/esInst"ctions a"e e0ectable statements

    .i"ecti/es a"e non+e0ectable

    .i"ecti/es a"e also called #sedo inst"ctions.i"ecti/es aid t%e assembly #"ocess

    5

  • 8/10/2019 4615-4713_Chapter 2.ppt

    6/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 6

    %at t%e assemble" doesIt ta'es t%e so"ce code )in assembly

    langage* and

    con/e"ts it to t%e ob3ect code in mac%inelangage

    6

  • 8/10/2019 4615-4713_Chapter 2.ppt

    7/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 7

    ASSEM4-ERS ,OR 056NASM$ ,ASM$ MASM$ TASM and H-A

    ,ASM and NASM can "n nde" .OS$ -in0

    and indo(sTASM and MASM a"e /e"y #o#la"

    It is found now that Windows 7, a new64-bit operating system, does not

    directly support 16-bit programs so 16bit assemblers may not work withWindows 7 directly

    7

  • 8/10/2019 4615-4713_Chapter 2.ppt

    8/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 8

    DOS.OS stands !o" .is' O#e"ating Systems It

    cold be an ac"onym !o" any OS$ bt it is mosto!ten sed as a s%o"t%and !o" MS+.OS)Mic"oso!t .is' O#e"ating Systems*

  • 8/10/2019 4615-4713_Chapter 2.ppt

    9/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 9

    BIOS and DOS function calls

    A set o! !nctions !o" sing I7O de/ices

    Can be sed !o" dis#laying data

    Can be sed !o" in#tting data !"om t%e'eyboa"d

  • 8/10/2019 4615-4713_Chapter 2.ppt

    10/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 10

    T%e E8IT command is a s%o"t%and notation!o"

    MO9 AH $:CHINT 21H

  • 8/10/2019 4615-4713_Chapter 2.ppt

    11/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 11

    .OS ,nction Calli* Read t%e 'eyboa"d (it% ec%o

    MO9 AH$ ;1 INT 21H

    T%is call e0its (it% t%e ASCII /ale o! t%e 'ey#"essed$ being a/ailable in A-

    T%e 'ey #"essed is also echoed on t%e sc"een

  • 8/10/2019 4615-4713_Chapter 2.ppt

    12/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 12

    .OS ,nction Call < Contdii* Read =eyboa"d (it%ot ec%o

    MO9 AH$ ;5

    INT 21HT%is call e0its (it% t%e ASCII /ale o! t%e 'ey

    #"essed being a/ailable in A-

    T%e 'ey #"essed is not echoed on t%e

    sc"een

  • 8/10/2019 4615-4713_Chapter 2.ppt

    13/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 13

    .is#laying a c%a"acte"iii* "ite a c%a"acte" to t%e standa"d dis#lay

    nit

    ,o" t%is $t%e ASCII /ale o! t%e c%a"acte" to bedis#layed s%old be in .-

    MO9 .-$ >S?

    MO9 AH$ ;2

    INT 21H

  • 8/10/2019 4615-4713_Chapter 2.ppt

    14/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 14

    STRING .ISP-A@i/* .is#lay a c%a"acte" st"ing on t%e standa"d

    dis#lay nit

    T%e logical add"ess .S.8 s%old #oint to t%ebeginning o! t%e st"ing

    T%is is to be !ollo(ed by t%e !ollo(inginst"ctions

    MO9 AH$ ;B

    INT 21H

  • 8/10/2019 4615-4713_Chapter 2.ppt

    15/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 15

    %y MASM Mic"oso!t %as ("itten conside"able

    docmentation

    T%i"d #a"ties %a/e ("itten assembly langage"e!e"ence manals !o" MASM

    T%e /e"sions o! MASM 6; and abo/e %a/e alot mo"e !eat"es )aimed at sim#liDcation o!

    ("iting code* t%an #"e/ios /e"sions

    15

  • 8/10/2019 4615-4713_Chapter 2.ppt

    16/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 16

    He"e (e se MASM 61:$ (%ic% is .OS basedIn t%is$ .OS and 4IOS inte""#ts can be sed

    MASMF2$ (%ic% is (indo(s basedIn t%is$ .OS and 4IOS inte""#ts cannot be

    sed

    16

  • 8/10/2019 4615-4713_Chapter 2.ppt

    17/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 17

    ASSEM4-@ -ANGAGE

    PROGRAMMINGSTEPS

    "ite t%e code (it% t%e %el# o! an edito"

    O#en t%e .OS (indo(

    Go to t%e MASM and t%en to t%e 4IN di"ecto"yAssemble t%e code

    -in' t%e code

    Rn t%e e0ectable Dle

    17

  • 8/10/2019 4615-4713_Chapter 2.ppt

    18/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 18

    Memo"y Models,o" deDning segments

    .ot models %a/e s%o"t cts

    T%e tiny model is sed (%en only onesegment is needed

    To s#eci!y a segment$ ("ite

    . MO.E- MO.E- NAME

    18

  • 8/10/2019 4615-4713_Chapter 2.ppt

    19/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 19

    E0am#le 21 < T%e Tiny Model MO.E- TIN@

    CO.E

    STARTPMO9 A-$6H

    MO9 4-$:H

    A.. A-$4-

    MO9 .-$A-E8IT

    EN.

    19

  • 8/10/2019 4615-4713_Chapter 2.ppt

    20/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 20

    Sa/e it as tinyasm

    O#en MASM and go to t%e 4IN di"ecto"yse t%e !ollo(ing commands

    l tin!.as "fo# asse$lin% and lin&in% l'(l tin!.as J!o" t%e list Dle

    20

  • 8/10/2019 4615-4713_Chapter 2.ppt

    21/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 21

    T%e "eslt o! assembling and

    lin'ing)*+as,.-+BIN/l tin!.asMic"oso!t )R* Mac"o Assemble" 9e"sion 61:5:::Co#y"ig%t )C* Mic"oso!t Co"# 1B51+1BB All "ig%ts "ese"/edAssembling tinymasmMic"oso!t )R* Segmented E0ectable -in'e" 9e"sion 6;FFB

    .ec 1BB:Co#y"ig%t )C* Mic"oso!t Co"# 1B5:+1BBF All "ig%ts "ese"/edOb3ect Modles Kob3L tinymob3 7tRn ,ile KtinymcomL tinymcom

    -ist ,ile Knlma#L N--ib"a"ies KlibL.eDnitions ,ile Knlde!L

    21

  • 8/10/2019 4615-4713_Chapter 2.ppt

    22/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 22

    E0am#le 2FPa"t o! t%e -ist Dle

    MO.E- TIN@

    ;;;; CO.E

    ;1;; 4; 6 MO9 A-$6H;1;2 4F : MO9 4-$:H

    ;1;: ;2 CF A.. A-$4-

    ;1;6 5A .; MO9 .-$A-

    E8IT

    EN.

    22

  • 8/10/2019 4615-4713_Chapter 2.ppt

    23/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 23

    S%o"t cts in t%e modelE8IT

    STARTP

    T%e (o"'ing o! all t%ese can be obtained bysing t%e listall di"ecti/e in t%e #"og"am

    E8IT gets t"anslated to

    MO9 AH$:CH

    INT 21H

    23

  • 8/10/2019 4615-4713_Chapter 2.ppt

    24/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 2424

  • 8/10/2019 4615-4713_Chapter 2.ppt

    25/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    Ind

    iaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 25

    sing t%e debgge"To ente" t%e debgge"$ ty#e de$u%

    tin!.co

    e get an nde"sco"e as t%e #"om#tOn ty#ing >"?$ (e can see t%e contents o! t%e

    "egiste"s$ be!o"e e0ection o! t%e #"og"am

    No( ty#e >?$ (%ic% is t%e command !o"

    nassembling

    25

  • 8/10/2019 4615-4713_Chapter 2.ppt

    26/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 26

    E0am#le 2C:\masm6.14\BIN>debug tinym.com

    -r

    AX=0000 BX=0000 CX=010C DX=0000 SP=0000 BP=0000SI=0000 DI=0000

    DS=13AD ES=13AD SS=13BD CS=13BD IP=0100 NV UP EI PLNZ NA PO NC

    13BD:0100 B067 MOV AL,67

    -u

    13BD:0100 B067 MOV AL,67

    13BD:0102 B345 MOV BL,45

    13BD:0104 02C3 ADD AL,BL

    13BD:0106 8AD0 MOV DL,AL

    13BD:0108 B44C MOV AH,4C

    13BD:010A CD21 INT 21

    26

  • 8/10/2019 4615-4713_Chapter 2.ppt

    27/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 27

    )OM and E0E 1lesA tiny model gene"ates only a com Dle$

    (%ile any ot%e" memo"y model gene"ates an

    e0e Dle

    27

  • 8/10/2019 4615-4713_Chapter 2.ppt

    28/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 28

    ,eat"es o! a com Dle Sie is limited to 6:=

    Only one segment$ (%ic% is t%e code

    segment .ata is deDned in t%is code segment

    Code sta"ts at oset ;1;; H$

    Smalle" Dle com#a"ed to e0e Dles

    28

  • 8/10/2019 4615-4713_Chapter 2.ppt

    29/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 29

    E0am#le 2

    29

  • 8/10/2019 4615-4713_Chapter 2.ppt

    30/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 30

    .ata deDnitions in MASM Table

    21

    30

  • 8/10/2019 4615-4713_Chapter 2.ppt

    31/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 31

    T%e small modelHe"e t(o segments a"e sed ?*

    4-AN= .4 1;.P)*

    R.S . : .P),,;,H*

    35

  • 8/10/2019 4615-4713_Chapter 2.ppt

    36/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 36

    2he E6U Di#ecti3esed to e&ate names to constants

    T%e assemble" 3st "e#laces t%e names by t%e/ales mentioned

    E0am#les

    TEMP EQ F:

    PRICE EQ 1BB

    36

  • 8/10/2019 4615-4713_Chapter 2.ppt

    37/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 37

    E0am#le 21;

    37

  • 8/10/2019 4615-4713_Chapter 2.ppt

    38/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 38

    2he ORG Di#ecti3eORG is a di"ecti/e (%ic% means >o"igin?

    In t%e conte0t o! assembly langage

    #"og"amming$ it can c%ange t%e location o!sto"age o! data o" code in memo"y

    38

  • 8/10/2019 4615-4713_Chapter 2.ppt

    39/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 39

    E0am#le 211a

    39

  • 8/10/2019 4615-4713_Chapter 2.ppt

    40/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 40

    E0am#le211b

    40

  • 8/10/2019 4615-4713_Chapter 2.ppt

    41/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 41

    2a$le 7.7 Othe# Models

    41

  • 8/10/2019 4615-4713_Chapter 2.ppt

    42/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 42

    E0am#le 212 ,ll segment model

    42

  • 8/10/2019 4615-4713_Chapter 2.ppt

    43/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    The x86 Microprocessor - Lyla B Das 43

    Gene"al "les !o" ("iting assembly

    langage1 All labels s%old begin (it% a lette" o" t%e

    s#ecial c%a"acte"s $ $ and

    2 A label can %a/e 1 to F1 c%a"acte"s (%ic%may be digits $lette"s in ##e" o" lo(e" caseo" t%e s#ecial c%a"acte"s at )*$dolla")* $nde"sco"e )* $dot)* o" &estion ma"')*

    F No "ese"/ed (o"ds o! MASM may be sed: Eac% label mst be ni&e

    43

  • 8/10/2019 4615-4713_Chapter 2.ppt

    44/44

    Cop

    yrigh

    t

    Dorlin

    gKin

    de

    rsley

    In

    diaPvt

    Ltd

    E0am#le 21F