Assembly 64

Embed Size (px)

Citation preview

  • 8/19/2019 Assembly 64

    1/366

    x86-64

    Assembly LanguageProgramming

    with

    Ubuntu

    Ed Jorgensen

    Version 1!"#

    January "!16

  • 8/19/2019 Assembly 64

    2/366

    Cover image:AMD Opteron, the first CPU to introduce the x86-6 extensions in !""#$%ource: http:&&en$'i(ipedia$org&'i(i&)i*e:AMD+Opteron+6+enus,+!"".$/pg

    Cover 0ac(ground:12 1en/amint 3O'n 'or(4%ource: http:&&commons$'i(imedia$org&'i(i&)i*e5#A%'ir*2+0e*t$/pg

    Cop2right !"., !"6 02 7d orgensen

    9ou are free:to %hare to cop2, distri0ute and transmit the 'or(to ;emix to adapt the 'or(

    Under the fo**o'ing conditions:Attri0ution 9ou must attri0ute the 'or( in the manner specified 02 the authoror *icensor 30ut not in an2 'a2 that suggests that the2 endorse 2ou or 2our use ofthe 'or(4$

  • 8/19/2019 Assembly 64

    3/366

    $able o% &ontents

    Table of Contents

     1! 'ntrodu(tion1 $ Prere>uisites$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $! ?hat is Assem0*2 @anguage$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! $# ?h2 @earn Assem0*2 @anguage$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!

     $#$ ain a 1etter Understanding of Architecture =ssues$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$# $#$ Understanding the Boo* Chain$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$# $#$ =mprove A*gorithm Deve*opment %(i**s$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$# $#$ =mproves Understanding of )unctions&Procedures$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$# $#$ ain an Understanding of =&O 1uffering$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

     $#$ Understand Compi*er %cope$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $#$ =ntroduction to Mu*ti-processing Concepts$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $#$ =ntroduction to =nterrupt Processing Concepts$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

     $ Additiona* ;eferences$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$ U0untu ;eferences$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$. $$! 1A% Command @ine ;eferences$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$. $$# Architecture ;eferences$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$. $$ Boo* Chain ;eferences$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.

     $$$ 9A%M ;eferences$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6 $$$! DDD De0ugger ;eferences$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6

     "! Ar(hite(ture )*er*iew# !$ Architecture Overvie'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ !$! Data %torage %iEes$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$8 !$# Centra* Processing Unit$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$F

     !$#$ CPU ;egisters$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" !$#$$ enera* Purpose ;egisters 3P;s4$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" !$#$$! %tac( Pointer ;egister 3;%P4$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! !$#$$# 1ase Pointer ;egister 3;1P4$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! !$#$$ =nstruction Pointer ;egister 3;=P4$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!

     !$#$$. )*ag ;egister 3r)*ags4$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! !$#$$6 GMM ;egisters$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#

     !$#$! Cache Memor2$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ !$ Main Memor2$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6 !$. Memor2 @a2out$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    Page iii

  • 8/19/2019 Assembly 64

    4/366

    $able o% &ontents

     !$6 Memor2 ierarch2$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ !$ 7xercises$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$F

     !$$ HuiE Huestions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$F

     +! ,ata e.resentation"1

     #$ =nteger ;epresentation$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! #$$ B'oIs Comp*iment$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!# #$$! 12te 7xamp*e$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!# #$$# ?ord 7xamp*e$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!

     #$! Unsigned and %igned Addition$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! #$# )*oating-point ;epresentation$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!

     #$#$ =777 #!-0it ;epresentation$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!. #$#$$ =777 #!-0it ;epresentation 7xamp*es$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!6

     #$#$$$ 7xamp*e J $."$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!6 #$#$$$! 7xamp*e J "$!."$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!6

     #$#$$$# 7xamp*e J """"6$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! #$#$! =777 6-0it ;epresentation$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! #$#$#

  • 8/19/2019 Assembly 64

    5/366

    $able o% &ontents

     .$!$ Assem0*e Commands$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$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$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.8

     6$!$!$ ;un & Continue$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6" 6$!$!$!

  • 8/19/2019 Assembly 64

    6/366

    $able o% &ontents

     $# Addresses vs a*ues$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$. $ Conversion =nstructions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6

     $$ uares$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$8 $F 7xercises$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$F

     $F$ HuiE Huestions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$F $F$! %uggested Pro/ects$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#

     8! Addressing 2odes1"# 8$ Addresses vs a*ues$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!

     8$$ ;egister Mode Addressing$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!8

     8$$! =mmediate Mode Addressing$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!8 8$$# Memor2 Mode Addressing$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!F 8$! 7xamp*e Program, @ist %ummation$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#! 8$# 7xamp*e Program, P2ramid Areas and o*umes$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$## 8$ 7xercises$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#8

    Page vi

  • 8/19/2019 Assembly 64

    7/366

    $able o% &ontents

     8$$ HuiE Huestions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#F 8$$! %uggested Pro/ects$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!

     3! Pro(ess ta(140 F$ %tac( 7xamp*e$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.

     F$! %tac( =nstructions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6 F$# %tac( =mp*ementation$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

     F$#$ %tac( @a2out$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$8 F$#$! %tac( Operations$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$F

     F$ %tac( 7xamp*e$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$. F$. 7xercises$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.!

     F$.$ HuiE Huestions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.# F$.$! %uggested Pro/ects$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.

     1!! Program ,e*elo.ment100

     "$ Understand the Pro0*em$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.. "$! Create the A*gorithm$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.6 "$# =mp*ement the Program$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.8 "$ Best&De0ug the Program$$$$$$$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$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$66

     $!$ Macro Definition$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$66 $!$! Using a Macros$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6

     $# Macro 7xamp*e$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6 $ De0ugging Macros$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6F $. 7xercises$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"

     $.$ HuiE Huestions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" $.$! %uggested Pro/ects$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"

     1"! /un(tions1#1 !$ %tac( D2namic @oca* aria0*es$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    Page vii

  • 8/19/2019 Assembly 64

    8/366

    $able o% &ontents

     !$! )unction Dec*aration$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! !$# %tandard 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$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$F #$!

  • 8/19/2019 Assembly 64

    9/366

    $able o% &ontents

     14! 2ulti.le our(e /iles""# $ 7xtern %tatement$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!! $! 7xamp*e, %um and Average$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!!8

     $!$ Assem0*2 Main$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!!8 $!$! )unction %ource$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#" $!$# Assem0*e and @in($$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#!

     $# =nterfacing 'ith a igh-@eve* @anguage$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#! $#$ 7xamp*e, CLL Main & Assem0*2 )unction$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#! $#$! Compi*e, Assem0*e, and @in($$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#

     $ 7xercises$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#. $$ HuiE Huestions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#. $$! %uggested Pro/ects$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#.

     10! ta( 5u%%er )*er%low"+# .$ Understanding a %tac( 1uffer Overf*o'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#8

     .$! Code to =n/ect$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#F .$# Code =n/ection$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!! .$ Code =n/ection Protections$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!#

     .$$ Data %tac( %mashing Protector 3or Canaries4$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!# .$$! Data 7xecution Prevention$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! .$$# Data Address %pace @a2out ;andomiEation$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!

     .$. 7xercises$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! .$.$ HuiE Huestions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! .$.$! %uggested Pro/ects$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!.

     16! &ommand Line Arguments"4# 6$ Parsing Command @ine Arguments$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$! 6$! igh-@eve* @anguage 7xamp*e$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!8 6$# Argument Count and Argument ector Ba0*e$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!F 6$ Assem0*2 @anguage 7xamp*e$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!." 6$. 7xercises$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!.

     6$.$ HuiE Huestions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!. 6$.$! %uggested Pro/ects$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!..

     1#! 'n.ut)ut.ut 5u%%ering"0#

     $ ?h2 1uffer$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!. $! 1uffering A*gorithm$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!.F $# 7xercises$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!6!

     $#$ HuiE Huestions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!6! $#$! %uggested Pro/ects$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!6#

    Page ix

  • 8/19/2019 Assembly 64

    10/366

  • 8/19/2019 Assembly 64

    11/366

    $able o% &ontents

     !"$! =nterrupt B2pes and @eve*s$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"" !"$!$ =nterrupt B2pes$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"" !"$!$! Privi*ege @eve*s$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#""

     !"$# =nterrupt Processing$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"! !"$#$ =nterrupt %ervice ;outine 3=%;4$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"! !"$#$! Processing %teps$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"!

     !"$#$!$ %uspension$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"! !"$#$!$! O0taining =%; Address$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"! !"$#$!$# ump to =%;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"# !"$#$!$ %uspension 7xecute =%;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"# !"$#$!$. ;esumption$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"

     !"$ %uspension =nterrupt Processing %ummar2$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#" !"$. 7xercises$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#".

     !"$.$ HuiE Huestions$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#".

     !"$.$! %uggested Pro/ects$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"6 "1! A..endix A 7 A&'' $able+!#

     ""! A..endix 5 7 'nstru(tion et ummary+!3 !!$

  • 8/19/2019 Assembly 64

    12/366

    $able o% &ontents

     !$! HuiE Huestion Ans'ers, Chapter !$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$## !$# HuiE Huestion Ans'ers, Chapter #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$##! !$ HuiE Huestion Ans'ers, Chapter !$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$## !$. HuiE Huestion Ans'ers, Chapter .$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$##. !$6 HuiE Huestion Ans'ers, Chapter 6$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$##6 !$ HuiE Huestion Ans'ers, Chapter $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$## !$8 HuiE Huestion Ans'ers, Chapter 8$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#" !$F HuiE Huestion Ans'ers, Chapter F$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$# !$" HuiE Huestion Ans'ers, Chapter "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$# !$ HuiE Huestion Ans'ers, Chapter $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#! !$! HuiE Huestion Ans'ers, Chapter !$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#! !$# HuiE Huestion Ans'ers, Chapter #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$## !$ HuiE Huestion Ans'ers, Chapter $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$## !$. HuiE Huestion Ans'ers, Chapter .$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#

     !$6 HuiE Huestion Ans'ers, Chapter 6$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$# !$ HuiE Huestion Ans'ers, Chapter $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#. !$8 HuiE Huestion Ans'ers, Chapter 8$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#. !$F HuiE Huestion Ans'ers, Chapter F$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#6 !$!" HuiE Huestion Ans'ers, Chapter !"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#6

     "0! Al.habeti(al 'ndex+43

    Page xii

  • 8/19/2019 Assembly 64

    13/366

    $able o% &ontents

    Illustration Index

    =**ustration : Computer Architecture$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=**ustration !: CPU 1*oc( Diagram$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.=**ustration #: Memor2 ierarch2$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$8=**ustration : Overvie': Assem0*e, @in(, @oad$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!=**ustration .: @in(ing Mu*tip*e )i*es$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$F=**ustration 6: =nitia* De0ugger %creen$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.6=**ustration : De0ugger %creen 'ith 1rea(point %et$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.8=**ustration 8: De0ugger %creen 'ith reen Arro'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.F=**ustration F: DDD Command 1ar$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6"=**ustration ": ;egister ?indo'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$6

    =**ustration : MO =nstruction Overvie'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#=**ustration !: Privi*ege @eve*s$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"=**ustration #: =nterrupt Processing Overvie'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"

    Page xiii

  • 8/19/2019 Assembly 64

    14/366

    $able o% &ontents

    Page xiv

  • 8/19/2019 Assembly 64

    15/366

     1! 'ntrodu(tion

    Bhe purpose of this text is to provide a reference for Universit2 *eve* assem0*2 *anguageand s2stems programming courses$ %pecifica**2, this text addresses the x86-6

    instruction set for the popu*ar x86-6 c*ass of processors using the U0untu 6-0itOperating %2stem 3O%4$ ?hi*e the provided code and various examp*es shou*d 'or(under an2 @inux-0ased 6-0it O%, the2 have on*2 0een tested under U0untu $" @B%36-0it4$

    Bhe x86-6 is a Comp*ex =nstruction %et Computing 3C=%C!4 is a CPU design$ Bhisrefers to the interna* processor design phi*osoph2$ C=%C processors t2pica**2 inc*ude a'ide variet2 of instructions 3sometime over*apping4, var2ing instructions siEes, and a'ide range of addressing modes$ Bhe term 'as retroactive*2 coined in contrast to;educed =nstruction %et Computer 3;=%C#4$

     11 Prere:uisites

    =t must 0e noted that the text is not geared to'ard *earning ho' to program$ =t isassumed that the reader has a*read2 0ecome proficient in a high-*eve* programming*anguage$ %pecifica**2, the text is genera**2 geared to'ard a compi*ed, C-0ased high*eve* *anguage such as C, CLL, or ava$ Man2 of the exp*anations and examp*es assumethe reader is a*read2 fami*iar 'ith programming concepts such as dec*arations,arithmetic operations, contro* structures, iteration, function ca**s, functions, indirection3i$e$, pointers4, and varia0*e scoping issues$

    Additiona**2, the reader shou*d 0e comforta0*e using a @inux-0ased operating s2steminc*uding using the command *ine$ =f the reader is ne' to @inux, the Additiona*

    ;eferences section has *in(s to some usefu* documentation$

    )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&G86-6! )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Comp*ex+instruction+set+computing# )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&;educed+instruction+set+computing

    Page

    Chapter

    1

     If you give someone a program, you willfrustrate them for a day; if you teach themto program, you will frustrate them for alifetime.

  • 8/19/2019 Assembly 64

    16/366

    Chapter $" =ntroduction

     1" ;hat is Assembly Language

    Bhe t2pica* >uestion is I'h2 *earn assem0*2NI$ 1efore addressing that >uestion, *etsc*arif2 'hat exact*2 assem0*2 *anguage is$

    Assem0*2 *anguage is machine specific$ )or examp*e, code 'ritten for an x86-6processor 'i** not run on a different processor such as a ;=%C processor 3popu*ar inta0*ets and smart-phones4$

    Assem0*2 *anguage is a *o' *eve* *anguage and provides the 0asic instructiona*interface to the computer processor$ Assem0*2 *anguage is as c*ose to the processor as2ou can get as a programmer$ Programs 'ritten in a high-*eve* *anguage are trans*atedinto assem0*2 *anguage in order for the processor to execute the program$ Bhe high-*eve* *anguage is an a0straction 0et'een the *anguage and the actua* processorinstructions$ As such, the idea that assem0*2 is dead is nonsense$

    Assem0*2 *anguage gives 2ou direct contro* of the s2stemIs resources$ Bhe invo*vessetting processor registers, accessing memor2 *ocations, and interfacing 'ith otherhard'are e*ements$ Bhis re>uires a significant*2 deeper understanding of exact*2 ho'the processor and memor2 'or($

     1+ ;hy Learn Assembly Language

    Bhe goa* of this text is to provide a comprehensive introduction to programming inassem0*2 *anguage$ Bhe reasons for *earning assem0*2 *anguage are more a0outunderstanding ho' a computer 'or(s instead of deve*oping *arge programs$ %inceassem0*2 *anguage is machine specific, the *ac( of porta0i*it2 is ver2 *imiting forprogramming pro/ects$

    Bhe process of actua**2 *earning assem0*2 *anguage invo*ves 'riting non-trivia*programs to perform specific *o'-*eve* actions inc*uding arithmetic operations, functionca**s, using stac(-d2namic *oca* varia0*es, and operating s2stem interaction for activitiessuch as input&output$ ust *oo(ing at a sma** assem0*2 *anguage programs 'i** not 0eenough$

    =n the *ong run *earning the under*2ing principa*s, inc*uding assem0*2 *anguage, is 'hatma(es the difference 0et'een a coding technician una0*e to cope 'ith changing*anguages and a computer scientist 'ho is a0*e to adapt to the ever changingtechno*ogies$

    Bhe fo**o'ing sections provide some detai* on the various, more specific reasons for*earning assem0*2 *anguage$

    Page !

  • 8/19/2019 Assembly 64

    17/366

    Chapter $" Q =ntroduction

     1+1 uires morethought and more attention to detai*$ =n the high*2 un*i(e*2 event that a program doesnot 'or( the first time, de0ugging assem0*2 *anguage a*so provides practice de0uggingand re>uires a more nuanced approach since /ust adding a 0unch of output statements ismore difficu*t at the assem0*2 *anguage *eve*$ Bhis t2pica**2 invo*ves a more

    comprehensive use of a de0ugger 'hich is a usefu* s(i** for an2 programmer$

     1+1 'm.ro*es Understanding o% /un(tionsPro(edures

    ?or(ing at assem0*2 *anguage provides an improved understanding of ho'function&procedure ca**s 'or($ Bhis inc*udes the contents and structure of the functionca** frame, a*so referred to as the activation record$ Depending on the specific instance,the activation record might inc*ude stac(-0ased arguments, preserved registers, and&orstac( d2namic *oca* varia0*es$ Bhere are some significant imp*ementation and securit2imp*ications regarding stac( d2namic *oca* varia0*es that are 0est understood 'or(ing at

    a *o' *eve*$ Due to the securit2 imp*ications, it 'ou*d 0e appropriate to remind readersto a*'a2s use their po'ers for good$ Additiona**2, use of the stac( and the associatedca** frame is the 0asis for recursion and understanding the fair*2 straightfor'ardimp*ementation of recursive functions$

    Page #

  • 8/19/2019 Assembly 64

    18/366

    Chapter $" =ntroduction

     1+1

  • 8/19/2019 Assembly 64

    19/366

    Chapter $" Q =ntroduction

     141 Ubuntu e%eren(es

    Bhere is significant documentation avai*a0*e for the U0untu O%$ Bhe primar2 user guideis as fo**o's:

    ◦ U0untu Communit2 ?i(i

    ◦ etting %tarted 'ith U0untu $"

    =n addition, there are man2 other sites dedicated to providing he*p using U0untu 3orother @inux-0ased O%Is4$

     14" 5A= &ommand Line e%eren(es

    1A% is the defau*t she** for U0untu$ Bhe reader shou*d 0e fami*iar 'ith 0asiccommand *ine operations$ %ome additiona* references are as fo**o's:

    @inux Command @ine 3on-*ine Butoria* and text4◦ An =ntroduction to the @inux Command %he** )or 1eginners 3pdf4

    =n addition, there are man2 other sites dedicated to providing information regarding the1A% command she**$

     14+ Ar(hite(ture e%eren(es

    %ome (e2 references pu0*ished 02 =nte* provide a detai*ed technica* description thearchitecture and programming environment of =nte* processors supporting =A-#! and=nte* 6 Architectures$

    ◦ =nte*R 6 and =A-#! Architectures %oft'are Deve*operIs Manua*: 1asic

    Architecture$

    ◦ =nte* 6 and =A-#! Architectures %oft'are Deve*operIs Manua*: =nstruction

    %et ;eference$

    ◦ =nte* 6 and =A-#! Architectures %oft'are Deve*operIs Manua*: %2stem

    Programming uide$

    =f the em0edded *in(s do not 'or(, an =nternet search can he*p find the ne' *ocation$

     144 $ool &hain e%eren(es

    Bhe too* chain inc*udes the assem0*er, *in(er, *oader, and de0ugger$ Chapter ., Boo*Chain provides an overvie' of the too* chain 0eing used in this text$ Bhe fo**o'ingreferences provide a more detai*ed information and documentation$

    Page .

    https://help.ubuntu.com/community/CommunityHelpWikihttp://files.ubuntu-manual.org/manuals/getting-started-with-ubuntu/14.04/en_US/screen/Getting%20Started%20with%20Ubuntu%2014.04.pdfhttp://linuxcommand.org/index.phphttp://vic.gedris.org/Manual-ShellIntro/1.2/ShellIntro.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-system-programming-manual-325384.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-system-programming-manual-325384.pdfhttps://help.ubuntu.com/community/CommunityHelpWikihttp://files.ubuntu-manual.org/manuals/getting-started-with-ubuntu/14.04/en_US/screen/Getting%20Started%20with%20Ubuntu%2014.04.pdfhttp://linuxcommand.org/index.phphttp://vic.gedris.org/Manual-ShellIntro/1.2/ShellIntro.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-1-manual.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-system-programming-manual-325384.pdfhttp://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-system-programming-manual-325384.pdf

  • 8/19/2019 Assembly 64

    20/366

    Chapter $" =ntroduction

     1441 >A2 e%eren(es

    Bhe 9A%M assem0*er is an open source assem0*er common*2 avai*a0*e on @inux-0aseds2stems$ Bhe 9A%M references are as fo**o's:

    ◦ 9asm ?e0 %ite 

    ◦ 9asm Documentation

    Additiona* information regarding 9A%M ma2 0e at a num0er of assem0*2 *anguage sitesand can 0e found through and =nternet search$

     144" ,,, ,ebugger e%eren(es

    Bhe DDD de0ugger is an open source de0ugger capa0*e to supporting assem0*2*anguage$

    ◦ DDD ?e0 %ite

    ◦ DDD Documentation

    Additiona* information regarding DDD ma2 0e at a num0er of assem0*2 *anguage sitesand can 0e found through an =nternet search$

    Page 6

    http://yasm.tortall.net/http://yasm.tortall.net/Guide.htmlhttp://www.gnu.org/software/ddd/http://www.gnu.org/software/ddd/manual/http://yasm.tortall.net/http://yasm.tortall.net/Guide.htmlhttp://www.gnu.org/software/ddd/http://www.gnu.org/software/ddd/manual/

  • 8/19/2019 Assembly 64

    21/366

     "! Ar(hite(ture )*er*iew

    Bhis chapter presents a 0asic, genera* overvie' of the x86-6 architecture$ )or a moredetai*ed exp*anation, refer to the additiona* references noted in Chapter , =ntroduction$

     "1 Ar(hite(ture )*er*iew

    Bhe 0asic components of a computer inc*ude a Centra* Processing Unit 3CPU4, Primar2%torage or ;andom Access Memor2 3;AM4, %econdar2 %torage, and =nput&Outputdevices 3e$g$, screen, (e20oard, mouse4, and an interconnection referred to as the 1us$

    A ver2 0asic diagram of the computer architecture is as fo**o's:

    Page

     Illustration 1: Computer Architecture

    Chapter

    2

    Warning, keyoard not found. !ress enterto continue.

    Screen / Keyboard /

    MouseSecondary Storage

    (i.e., SSD / Disk Drive /

    Other Storage Media)

    Primary Storage

    Random ccess

    Memory (RM)

    !P"

    #"S

    ($nterconnection)

  • 8/19/2019 Assembly 64

    22/366

    Chapter !$" Architecture Overvie'

    Bhe architecture is t2pica**2 referred to as the on

  • 8/19/2019 Assembly 64

    23/366

    Chapter !$" Q Architecture Overvie'

    )or examp*e, C&CLL dec*arations are mapped as fo**o's:

    && ,e(laration torage i9e ?bits@ i9e ?bytes@

    char 12te 8-0its 02te

    short ?ord 6-0its ! 02tesint Dou0*e-'ord #!-0its 02tes

    unsigned int Dou0*e-'ord #!-0its 02tes

    *ong. Dou0*e-'ord #!-0its 02tes

    *ong *ong Huad'ord 6-0its 8 02tes

    char S Huad'ord 6-0its 8 02tes

    int S Huad'ord 6-0its 8 02tes

    f*oat Dou0*e-'ord #!-0its 02tes

    dou0*e Huad'ord 6-0its 8 02tes

    Bhe asteris( indicates an address varia0*e$ )or examp*e, int B means the address of aninteger$ Other high *eve* *anguages t2pica**2 have simi*ar mappings$

     "+ &entral Pro(essing Unit

    Bhe Centra* Processing Unit6  3CPU4 is t2pica**2 referred to as the 0rains of thecomputer since that is 'here the actua* ca*cu*ations are performed$ Bhe CPU is housed

    in a sing*e chip, sometimes ca**ed a processor, chip, or die$ Bhe cover image sho's onesuch CPU$

    Bhe CPU chip inc*udes a num0er of functiona* units, inc*uding the Arithmetic @ogicUnit8 3A@U4 'hich is the part of the chip that actua**2 performs the arithmetic and*ogica* ca*cu*ations$ =n order to support the A@U, processor registersF  and Cache"

    memor2 are a*so inc*uded on the die 3term for inside the chip4$ Bhe CPU registers andcache memor2 are descri0ed in su0se>uent sections$

    .  "ote, the I*ongI t2pe dec*aration is compi*er dependent$ B2pe sho'n is for g(( and g compi*ers$6 )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Centra*+processing+unit )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Die+3integrated+circuit48 )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Arithmetic+*ogic+unitF )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Processor+register" )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Cache+3computing4

    Page F

  • 8/19/2019 Assembly 64

    24/366

    Chapter !$" Architecture Overvie'

    =t shou*d 0e noted that the interna* design and configuration of a modern processor is>uite comp*ex$ Bhis section provides a ver2 simp*ified, high-*eve* vie' of some (e2functiona* units 'ithin a CPU$ ;efer to the footnotes or additiona* references for moreinformation$

     "+1 &PU egisters

    A CPU register, or /ust register, is a temporar2 storage or 'or(ing *ocation 0ui*t into theCPU itse*f 3separate from memor24$ Computations are t2pica**2 performed 02 the CPUusing registers$

     "+11

  • 8/19/2019 Assembly 64

    25/366

    Chapter !$" Q Architecture Overvie'

    r14 r14d r14w r14b

    r15 r15d r15w r15b

    Additiona**2, some of the P; registers are used for dedicated purposes as descri0ed inthe *ater sections$

    ?hen using data e*ement siEes *ess than 6-0its 3i$e$, #!-0it, 6-0it, or 8-0it4, the *o'erportion of the register can 0e accessed 02 using a different register name as sho' ta0*e$

    )or examp*e, 'hen accessing the *o'er portions of the 6-0it rax register, the *a2out isas fo**o's:

      C eax D

     C ax D

      rax T ah al

    As sho'n in the diagram, the first four registers, rax, rbx, r(x, and rdx a*so a**o' the0its 8-. to 0e accessed 'ith ah, bh, (h, and dh register names$ Bhis is provided for*egac2 support and 'i** not 0e used in this text$

    Bhe a0i*it2 to access portions of the register means that, if the >uad'ord rax register isset to .",""",""","""" 3fift2 0i**ion4, the rax register 'ou*d contain the fo**o'ing va*uein hex$

    rax = 0000 000B A43B 7400

    =f a su0se>uent operation sets the dou0*e-'ord eax register to ,""","""" 3one mi**ion,'hich is """)!"64, the rax register 'ou*d contain the fo**o'ing va*ue in hex$

    rax = 0000 000B 000F 4240

    uent operation sets the 'ord siEed ax register to .,"""" 3fifteen thousand,

    'hich is #AF864, the rax register 'ou*d contain the fo**o'ing va*ue in hex$rax = 0000 000B 000F 3A98

    Page

  • 8/19/2019 Assembly 64

    26/366

    Chapter !$" Architecture Overvie'

    =n this case, 'hen the *o'er 6-0it ax portion of the 6-0it rax register is set, the upper8-0its are unaffected$

    =f a su0se>uent operation sets the 02te siEed al register to ."" 3fift2, 'hich is #!64, therax register 'ou*d contain the fo**o'ing va*ue in hex$

    rax = 0000 000B 000F 3A32

    ?hen the *o'er 8-0it al portion of the 6-0it rax register is set, the upper .6-0its areunaffected$

     "+1" ta( Pointer egister ?P@

    One of the PU registers, rs., is used to point to the current top of the stac($ Bhe rs. register shou*d not 0e used for data or other uses$ Additiona* information regarding thestac( and stac( operations is provided in the Chapter F, Process %tac($

     "+1+ 5ase Pointer egister ?5P@

    One of the PU registers, rb., is used to as a 0ase pointer during function ca**s$ Bherb. register shou*d not 0e used for data or other uses$ Additiona* information regardingthe functions and function ca**s is provided in the Chapter !, )unctions$

     "+14 'nstru(tion Pointer egister ?'P@

    =n addition to the P;s, there is specia* register, ri., that is used 02 the CPU to point tothe next instruction to be executed $ %pecifica**2, since the ri.  points to the next

    instruction, that means the instruction 0eing pointed to 02 ri., and sho'n in thede0ugger, has not 2et 0een executed$ Bhis is an important distinction 'hich canconfusing 'hen revie'ing code in a de0ugger$

     "+10 /lag egister ?r/lags@

    Bhe f*ag register, r/lags, is used for status and CPU contro* information$ Bhe r/lagregister is updated 02 the CPU after each instruction and not direct*2 accessi0*e 02programs$ Bhis register stores status information a0out the instruction that 'as /ustexecuted$ Of the 6-0its in the r/lag register, man2 are reserved for future use$

    Bhe fo**o'ing ta0*e sho's some of the status 0its in the f*ag register$ame ymbol 5it Use

    Carr2 C) " Used to indicate if the previous operationresu*ted in a carr2$

    Page !

  • 8/19/2019 Assembly 64

    27/366

    Chapter !$" Q Architecture Overvie'

    Parit2 P) ! Used to indicate if the *ast 02te has an evennum0er of Is 3i$e$, even parit24$

    Ad/ust A) Used to support 1inar2 Coded Decima*operations$

    Kero K) 6 Used to indicated if the previous operationresu*ted in a Eero resu*t$

    %ign %) Used to indicated if the resu*t of theprevious operation resu*ted in a in themost significant 0it 3indicating negative incontext of signed data4$

    Direction D) " Used to specif2 the direction 3increment ofdecrement4 for some string operations$

    Overf*o' O) Used to indicate if the previous operation

    resu*ted in an overf*o'$

    Bhere are a num0er of additiona* 0its not specified in this text$ More information can 0eo0tained from the additiona* references noted in Chapter , =ntroduction$

     "+16 F22 egisters

    Bhere are a set of dedicated registers used to support 6-0it and #!-0it f*oating pointoperations and %ing*e =nstruction Mu*tip*e Data 3%=MD4 instructions$ Bhe %=MDinstructions a**o' a sing*e instruction to 0e app*ied simu*taneous*2 to mu*tip*e data

    items$ Used effective*2, this can resu*t in a significant performance increase$ B2pica*app*ications inc*ude some graphics processing and digita* signa* processing$

    Bhe GMM registers as fo**o's:

    1"8-bit egisters

    xmm0

    xmm1

    xmm2

    xmm3xmm4

    xmm5

    Page #

  • 8/19/2019 Assembly 64

    28/366

    Chapter !$" Architecture Overvie'

    xmm6

    xmm7

    xmm8

    xmm9xmm10

    xmm11

    xmm12

    xmm13

    xmm14

    xmm15

    uests that can 0e served from

    cache, the faster the s2stem 'i** t2pica**2 perform$ %uccessive generations of CPUchips have increased cache memor2 and improved cache mapping strategies in order toimprove overa** performance$

    Page

  • 8/19/2019 Assembly 64

    29/366

    Chapter !$" Q Architecture Overvie'

    A 0*oc( diagram of a t2pica* CPU chip configuration is as fo**o's

    Current chip designs t2pica**2 inc*ude an @ cache per core and a shared @! cache$Man2 of the ne'er CPU chips 'i** have an additiona* @# cache$

    As can 0e noted form the diagram, a** memor2 accesses trave* through each *eve* ofcache$ As such, there is a potentia* for mu*tip*e, dup*icate copies of the va*ue 3CPUregister, @ cache, @! cache, and main memor24$ Bhis comp*ication is managed 02 theCPU and is not something the programmer can change$ Understanding the cache andassociated performance gain is usefu* in understanding ho' a computer 'or(s$

    Page .

     Illustration #: C!$ %lock &iagram

    Core 0

    L2 Cache

    Core 1

    L1 Cache L1 Cache

    BUS

    CPU Chip

  • 8/19/2019 Assembly 64

    30/366

    Chapter !$" Architecture Overvie'

     "4 2ain 2emory

    Memor2 can 0e vie'ed as a series of 02tes, one after another$ Bhat is, memor2 is yteaddressale$ Bhis means each memor2 address ho*ds one 02te of information$ Bo storea dou0*e-'ord, four 02tes are re>uired 'hich use four memor2 addresses$

    Additiona**2, architecture is little-endian$ Bhis means that the @east %ignificant 12te3@%14 is stored in the *o'est memor2 address$ Bhe Most %ignificant 12te 3M%14 isstored in the highest memor2 *ocation$

    )or a dou0*e-'ord 3#!-0its4, the M%1 and @%1 are a**ocated as sho'n 0e*o'$

    # #" !F !8 ! !6 !. ! !# !! ! !" F 8 6 . # ! " F 8 6 . # ! "

    25 L5

    )or examp*e, assuming the va*ue of, .,""",""""  3""C1"64, is to 0e p*aced in a

    dou0*e-'ord varia0*e named var1$

    )or a *itt*e-endian architecture, the memor2 picture 'ou*d 0e as fo**o's:

    variab%ename

    va%ue ddress(in he&)

    ?   0100100C

    00   0100100B

    4C   0100100A

    4B   01001009

    var'    40   01001008

    ?   01001007

    1ased on the *itt*e-endian architecture, the @%1 is stored in the *o'est memor2 addressand the M%1 is stored in the highest memor2 *ocation$

    Page 6

  • 8/19/2019 Assembly 64

    31/366

    Chapter !$" Q Architecture Overvie'

     "0 2emory Layout

    Bhe genera* memor2 *a2out for a program is as sho'n:

    high memory stack

    .

    .

    .

    heap

    BSS – uninitialized data 

    data

    text (code)%o memory reserved

    Bhe reserved section is not avai*a0*e to user programs$ Bhe text 3or code4 section is'here the machine *anguage 3i$e$, the Is and "Is that represent the code4 is stored$ Bhedata section is 'here the initia*iEed data is stored$ Bhis inc*udes dec*ared varia0*es thathave 0een provided an initia* va*ue at assem0*e time$ Bhe uninitia*iEed data section,t2pica**2 ca** 1%% section, is 'here dec*ared varia0*es that have not 0een provided aninitia* va*ue are stored$ =f accessed 0efore 0eing set, the va*ue 'i** not 0e meaningfu*$

    Bhe heap is 'here d2namica**2 a**ocated data 'i** 0e stored 3if re>uested4$ Bhe stac(starts in high memor2 and gro's do'n'ard$

    @ater sections 'i** provide additiona* detai* for the text and data sections$

     "6 2emory =ierar(hy

    =n order to fu**2 understand the various different memor2 *eve*s and associated usage, itis usefu* to revie' the memor2 hierarch2!$ =n genera* terms, faster memor2 is moreexpensive and s*o'er memor2 0*oc(s are *ess expensive$ Bhe CPU registers are sma**,fast, and expensive$ Primar2 storage devices 3dis( drives, %%DIs, etc$4 are *arge, s*o'er,

    and *ess expensive$ Bhe overa** goa* is to 0a*ance performance 'ith cost$

    )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Machine+code! )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Memor2+hierarch2

    Page

  • 8/19/2019 Assembly 64

    32/366

    Chapter !$" Architecture Overvie'

    An overvie' of the memor2 hierarch2 is as fo**o's

    ?here the top of the triang*e represents the fastest, sma**est, and most expensivememor2 memor2$ As 'e move do'n *eve*s, the memor2 0ecome s*o'er, *arger, and*ess expensive$ Bhe goa* is to use an effective 0a*ance 0et'een the sma**, fast,expensive memor2 and the *arge, s*o'er, and cheaper memor2$

    Page 8

     Illustration ': (emory )ierarchy

    CPURegisters

    Cache

    Main Memory (RAM)

    Primary Storage(disk drives, SSD's, etc.)

    Secondary Storage(remote storage, backups, etc.)

    Smaller, faster, and moreexpensive

    Larger, slower, and less

    expensive

  • 8/19/2019 Assembly 64

    33/366

    Chapter !$" Q Architecture Overvie'

    %ome t2pica* performance and siEe characteristics are as fo**o's

    2emory Unit Exam.le i9e $y.i(al .eed

    ;egisters 6, 6-0it registers V nanoseconds#

    Cache Memor2 - 8L Mega02tes

    3@ and @!4 V.-6" nanoseconds

    Main Memor2 ! W #!L iga02tes. V""-." nanoseconds

    Primar2 %torage ."" iga02tes W    L Bera02tes6

    V#-. mi**iseconds

    1ased on this ta0*e, a main memor2 access at "" nanoseconds 3"" × "-F4 is #","""

    times faster than a primar2 storage access, at # mi**iseconds 3# × "-#4$

    Bhe t2pica* speeds improve over time 3and these are a*read2 out of date4$ Bhe (e2 pointis the re*ative difference 0et'een each memor2 unit is significant$ Bhis difference0et'een the memor2 units app*ies even as ne'er, faster %%Ds are 0eing imp*ement$

     "# Exer(ises

    1e*o' are some >uestions 0ased on this chapter$

     "#1 ui9 uestions

    1e*o' are some >uiE >uestions$

    4 Dra' a picture of the on

  • 8/19/2019 Assembly 64

    34/366

    Chapter !$" Architecture Overvie'

    84 iven the #!-0it hex ""C1"6 'hat is the:

    $ @east %ignificant 12te 3@%14

    !$ Most %ignificant 12te 3M%14

    F4 iven the #!-0it hex ""C1"6, sho' the *itt*e-endian memor2 *a2out sho'ingeach 02te in memor2$

    "4 Dra' a picture of the *a2out for the rax register$

    4 o' man2 0its does each of the fo**o'ing represent:

    $ al

    !$ r(x

    #$ bx

    $ edx.$ r11

    6$ r8b

    $ sil

    8$ r14w 

    !4 ?hich register points to the next instruction to 0e executedN

    #4 ?hich register points to current top of the stac(N

    4 =f al is set to ".6 and ax is set to """6, eax is set to """"""!"6, and rax is setto """"""""""""""""6, and sho' the fina* comp*ete contents of the comp*eterax register$

    .4 =f the rax register is set to 8,F8.,.!F,!6,86,8F."  3!#.68FA1CD7)64,'hat are the contents of the fo**o'ing registers in hexN

    $ al 

    !$ ax

    #$  eax 

    $ rax 

    Page !"

  • 8/19/2019 Assembly 64

    35/366

     +! ,ata e.resentation

    Data representation refers to ho' information is stored 'ithin the computer$ Bhere is aspecific method for storing integers 'hich is different than storing f*oating point va*ues'hich is different than storing characters$ Bhis chapter presents a 0rief summar2 of theinteger, f*oating-point, and A%C== representation schemes$

    =t is assumed the reader is a*read2 genera**2 fami*iar 'ith 0inar2, decima*, and hexnum0ering s2stems$

    =t shou*d 0e noted that if not specified, a num0er is in 0ase-"$ Additiona**2, a num0erpreceded 02 "x is a hex va*ue$ )or examp*e, F T F" T #6 T "x#$

     +1 'nteger e.resentation

    ;epresenting integer num0ers refers to ho' the computer stores or represents a num0erin memor2$ Bhe computer represents num0ers in 0inar2 3Is and "Is4$ o'ever, the

    computer has a *imited amount of space that can 0e used for each num0er or varia0*e$Bhis direct*2 impacts the siEe, or range, of the num0er that can 0e represented$ )orexamp*e, a 02te 38 0its4 can 0e used to represent !8 or !.6 different num0ers$ Bhose !.6different num0ers can 0e unsigned 3a** positive4 in 'hich case 'e can represent an2num0er 0et'een " and !.. 3inc*usive4$ =f 'e choose signed  3positive and negativeva*ues4, then 'e can represent an2 num0er 0et'een -!8 and L! 3inc*usive4$

    =f that range is not *arge enough to hand*e the intended va*ues, a *arger siEe must 0eused$ )or examp*e, a 'ord 36 0its4 can 0e used to represent !6 or 6.,.#6 differentva*ues, and a dou0*e-'ord 3#!-0its4 can 0e used to represent !#!  or ,!F,F6,!F6

    different num0ers$ %o, if 2ou 'anted to store a va*ue of "",""" then a dou0*e-'ord'ou*d 0e re>uired$ As 2ou ma2 reca** from C, CLL, or ava, an integer dec*aration 3e$g$,int G*ariableH4 is a sing*e dou0*e-'ord 'hich can 0e used to represent va*ues 0et'een-!# 3X!,,8#,684 and L!# 3L!,,8#,64$

    Page !

    Chapter

    3

    *here are 1+ types of people in the world;those that understand inary and those thatdont.

  • 8/19/2019 Assembly 64

    36/366

    Chapter #$" Data ;epresentation

    Bhe fo**o'ing ta0*e sho's the ranges associated 'ith t2pica* siEes:

    i9e i9e Unsigned ange igned ange

    12tes 38 0its4 !8

    " to !.. -!8 to L!?ords 36 0its4 !6 " to 6.,.#. X#!,68 to L#!,6

    Dou0*e-'ords 3#! 0its4 !#! " to ,!F,F6,!F. X!,,8#,68 toL!,,8#,6

    Huad'ord !6 " to !6- -!6#- to !6#-

    Dou0*e >uad'ord !!8 " to !!8- -!!- to !!-

    =n order to determine if a va*ue can 0e represented, 2ou 'i** need to (no' the siEe of

    storage e*ement 302te, 'ord, dou0*e-'ord, >uad 'ord, etc$4 0eing used and if the va*uesare signed or unsigned$

    • )or representing unsigned  va*ues 'ithin the range of a given storage siEe,

    standard 0inar2 is used$• )or representing signed  va*ues 'ithin the range, twoIs (om.liment  is used$

    %pecifica**2, the t'oIs comp*iment encoding process app*ies to the va*ues in thenegative range$ )or va*ues 'ithin the positive range, standard 0inar2 is used$

    )or examp*e, the unsigned 02te range can 0e represented using a num0er *ine as fo**o's:

    )or examp*e, the signed 02te range can a*so 0e represented using a num0er *ine asfo**o's:

    Bhe same concept app*ies to ha*f'ords and 'ords 'hich have *arger ranges$

    %ince unsigned va*ues have a different, positive on*2, range than signed va*ues, there isover*ap 0et'een the va*ues$ Bhis can 0e ver2 confusing 'hen examining varia0*es inmemor2 3'ith the de0ugger4$

    Page !!

    "00!

    -1"8 ! 1"#

  • 8/19/2019 Assembly 64

    37/366

    Chapter #$" Q Data ;epresentation

    )or examp*e 'hen the unsigned and signed va*ues are 'ithin the over*apping positiverange 3" to L!4:

    • A signed 02te representation of !"  is "x"C6 

    • An unsigned 02te representation of !"  is a*so "x"C6 

    ?hen the unsigned and signed va*ues are outside the over*apping range:

    • A signed 02te representation of -."  is "x)6 

    • An unsigned 02te representation of !"  is a*so "x)6 

    Bhis over*ap can cause confusion un*ess the data t2pes are c*ear*2 and correct*2 defined$

     +11 $woIs &om.liment

    Bhe fo**o'ing descri0es ho' to find the t'oIs comp*iment representation for negativeva*ues 3not positive va*ues4$

    Bo ta(e the t'oIs comp*iment of a num0er:

    $ ta(e the oneIs comp*iment 3negate4

    !$ add 3in 0inar24

    Bhe same process is used to encode a decima* va*ue into t'oIs comp*iment and fromt'oIs comp*iment 0ac( to decima*$ Bhe fo**o'ing sections provide some examp*es$

     +1" 5yte Exam.le

    )or examp*e, to find the 02te siEe 38-0its4, t'oIs comp*iment representation of -F and-!$

    F 38L4 T """""" ! 38L4 T """"""

    %tep "" %tep : ""

    %tep ! " """

    -F 3in hex4 T ) -! 3in hex4 T )

     "ote, a** 0its for the given siEe, 02te in this examp*e, must 0e specified$

    Page !#

  • 8/19/2019 Assembly 64

    38/366

    Chapter #$" Data ;epresentation

     +1+ ;ord Exam.le

    Bo find the 'ord siEe 36-0its4, t'oIs comp*iment representation of -8 and -"$

    8 36L!4 T 0000000000010010 " 3#!L84 T 0000000000101000%tep 1111111111101100 %tep 1111111111010111

    %tep ! 1111111111101110 %tep ! 1111111111011000

    -8 3hex4 T 0xFFEE -" 3hex4 T 0xFFD8

     "ote, a** 0its for the given siEe, 'ords in these examp*es, must 0e specified$

     +" Unsigned and igned Addition

    As previous*2 noted, the unsigned and signed representations ma2 provide differentinterpretations for the fina* va*ue 0eing represented$ o'ever, the addition andsu0traction operations are the same$ )or examp*e:

    ! """ -. """

    L """"" L """""

    !8 """ -8 """

    !8 T )8 -8 T )8

    Bhe fina* resu*t of "x)8 ma2 0e interpreted as !8 for unsigned representation and -8 fora signed representation$ Additiona**2, "x)86 is the Y 3degree s2m0o*4 in the A%C== ta0*e$

    As such, it is ver2 important to have a c*ear definition of the siEes 302te, ha*f'ord, 'ord,etc$4 and t2pes 3signed, unsigned4 of data for the operations 0eing performed$

     ++ /loating-.oint e.resentationBhe representation issues for f*oating points num0ers are more comp*ex$ Bhere are aseries of f*oating point representations for various ranges of the va*ue$ )or simp*icit2,'e 'i** *oo( primari*2 at the =777 . #!-0it f*oating-point standard$

    Page !

  • 8/19/2019 Assembly 64

    39/366

    Chapter #$" Q Data ;epresentation

     ++1 'EEE +"-bit e.resentation

    Bhe =777 . #!-0it f*oating-point standard is defined as fo**o's:

    # #" !F !8 ! !6 !. ! !# !! ! !" F 8 6 . # ! " F 8 6 . # ! "

    s biased ex.onent %ra(tion

    ?here  s  is the sign 3" TZ positive and TZ negative4$ More forma**2, this can 0e'ritten as

     N   −

    1)

    1 F ×

    " E−1"#

    ?hen representing f*oating point va*ues, the first step is to convert f*oating point va*ueinto 0inar2$ Bhe fo**o'ing ta0*e provides a 0rief reminder of ho' 0inar2 hand*esfractiona* components:

    !#

    !!

    !

    !"

    !-

    !-!

    !-#

    $$$ 8 ! $ &! & &8 $$$

    ! ! ! ! ! ! !

    )or examp*e, ""$"! 'ou*d 0e $6!."$ )or repeating decima*s, ca*cu*ating the 0inar2va*ue can 0e time consuming$ o'ever, there is a *imit since computers have finitestorage siEes 3#!-0its in this examp*e4$

    Bhe next step is to sho' the va*ue in norma*iEed scientific notation in 0inar2$ Bhis

    means that the num0er shou*d have a sing*e, non-Eero *eading digit to the *eft of thedecima* point$ )or examp*e, 8$!."  is """$""!  3or """$""!  x !"4 and in 0inar2norma*iEed scientific notation that 'ou*d 0e 'ritten as $""""" x !# 3since the decima*point 'as moved three p*aces to the *eft4$ Of course, if the num0er 'as "$!."  the0inar2 'ou*d 0e "$""! 3or "$""! x !"4 and the norma*iEed scientific notation 'ou*d 0e$" x !-# 3since the decima* point 'as moved three p*aces to the right4$ Bhe num0ersafter the *eading , not  inc*uding the *eading , are stored *eft-/ustified in the fractionportion of the dou0*e-'ord$

    Bhe next step is to ca*cu*ate the iased e-ponent , 'hich is the exponent from thenorma*iEed scientific notation 'ith p*us the 0ias$ Bhe 0ias for the =777 . #!-0itf*oating-point standard is !"$ Bhe resu*t shou*d 0e converted to a 02te 38 0its4 andstored in the 0iased exponent portion of the 'ord$

    Page !.

  • 8/19/2019 Assembly 64

    40/366

    Chapter #$" Data ;epresentation

     "ote, converting from the =777 . #!-0it f*oating-point representation to the decima*va*ue is done in reverse, ho'ever *eading must 0e added 0ac( 3as it is not stored in the'ord4$ Additiona**2, the 0ias is su0tracted 3instead of added4$

     ++11 'EEE +"-bit e.resentation Exam.les

    Bhis section presents severa* examp*es of encoding and decoding f*oating-pointrepresentation for reference$

     ++111 Exam.le D ##01!

    )or examp*e, to find the =777 . #!-0it f*oating-point representation for -$.":

    Exam.le 1 -##0• determine sign -$. TZ 3since negative4

    • convert to 0inar2 -$. T -"$!

    • norma*iEed scientific notation T $ x !!

    • compute 0iased exponent !" L !"  T !F" 

    ◦ and convert to 0inar2 T """"""!• 'rite components in 0inar2:

    sign exponent mantissa  """""" """""""""""""""""""

    • convert to hex 3sp*it into groups of 4

      """""""""""""""""""""""""  "" """" """ """" """" """" """"

      C " ) 8 " " " "• fina* resu*t: &!/8 !!!!16

     ++11" Exam.le D !1"01!

    )or examp*e, to find the =777 . #!-0it f*oating-point representation for -"$!.":

    Exam.le " -!1"0• determine sign -"$!. TZ 3since negative4

    • convert to 0inar2 -"$!. T -"$""!•

    norma*iEed scientific notation T $" x !-#

    • compute 0iased exponent -#"  L !"  T !" 

    ◦ and convert to 0inar2 T """!• 'rite components in 0inar2:

    Page !6

  • 8/19/2019 Assembly 64

    41/366

    Chapter #$" Q Data ;epresentation

    sign exponent mantissa  """ """""""""""""""""""""""

    • convert to hex 3sp*it into groups of 4

      """"""""""""""""""""""""""  " " """" """" """" """" """" """"

      1 7 " " " " " "• fina* resu*t: 5E!! !!!!

    16

     ++11+ Exam.le D 4144!!!!16

    )or examp*e, given the =777 . #!-0it f*oating-point representation """"6  findthe decima* va*ue:

    Exam.le + 4144!!!!16• convert to 0inar2

      """ """ """ """ """" """" """" """"!• sp*it into components

      " """""" """""""""""""""""""""!• determine exponent """"""!  T #"" 

    ◦ and remove 0ias #"" - !"  T #"• determine sign " TZ positive

    • 'rite resu*t L$""" x !# T L""$" T 1""0

     ++" 'EEE 64-bit e.resentation

    Bhe =777 . 6-0it f*oating-point standard is defined as fo**o's:

    6# 6! .! . "

    s biased ex.onent %ra(tion

    Bhe representation process is the same, ho'ever the format a**o's for an -0it 0iasedexponent 3'hich support *arge and sma**er va*ues4$ Bhe -0it 0iased exponent uses a0ias of ["!#$

     +++ ot a umber ?a@

    ?hen a va*ue is interpreted as a f*oating point va*ue and it does not conform to thedefined standard 3either for #!-0it or 6-0it4, the it can not 0e used as a f*oating pointva*ue$ Bhis might occur if an integer representation is treated as a f*oating point

    Page !

  • 8/19/2019 Assembly 64

    42/366

    Chapter #$" Data ;epresentation

    representation or a f*oating-point arithmetic operation 3add, su0tract, mu*tip*e, ordivide4 resu*ts in a va*ue that is too *arge or too sma** to 0e represented$ Bhe incorrectformat or unrepresenta0*e num0er is referred to as a a 'hich is an a00reviation fornot a numer$

    +4 &hara(ters and trings

    =n addition to numeric data, s2m0o*ic data is often re>uired$ %2m0o*ic or non-numericdata might inc*ude an important message such as e**o ?or*d 8 a common greetingfor first programs$ %uch s2m0o*s are 'e** understand 02 7ng*ish *anguage spea(ers$Computer memor2 is designed to store and retrieve num0ers$ Conse>uent*2, thes2m0o*s are represented 02 assigning numeric va*ues to each s2m0o* or character$

     +41 &hara(ter e.resentation

    =n a computer, a character

    F

     is a unit of information that corresponds to a s2m0o* such asa *etter in the a*pha0et$ 7xamp*es of characters inc*ude *etters, numerica* digits,common punctuation mar(s 3such as \$\ or \]\4, and 'hitespace$ Bhe genera* concepta*so inc*udes contro* characters, 'hich do not correspond to s2m0o*s in a particu*ar*anguage, 0ut to other information used to process text$ 7xamp*es of contro* charactersinc*ude carriage return or ta0$

     +411 Ameri(an tandard &ode %or 'n%ormation 'nter(hange

    Characters are represented using the American %tandard Code for =nformation=nterchange 3A%C==!"4$ 1ased on the A%C== ta0*e, each character and contro* character

    is assigned a numeric va*ue$ ?hen using A%C==, the character disp*a2ed is 0ased on theassigned numeric va*ue$ Bhis on*2 'or(s if ever2one agrees on common va*ues, 'hichis the purpose of the A%C== ta0*e$ )or examp*e, the *etter A is defined as 6." 3"x4$Bhe "x is stored in computer memor2, and 'hen disp*a2ed to the conso*e, the *etterA is sho'n$ ;efer to Appendix A for the comp*ete A%C== ta0*e$

    Additiona**2, numeric s2m0o*s can 0e represented in A%C==$ )or examp*e, F isrepresented as ." 3"x#F4 in computer memor2$ Bhe F can 0e disp*a2ed as output tothe conso*e$ =f sent to the conso*e, the integer va*ue F" 3"x"F4 'ou*d 0e interpreted asan A%C== va*ue 'hich in the case 'ou*d 0e a ta0$

    8 )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&e**o,+?or*d]+programF )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Character+3computing4!" )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&A%C==

    Page !8

  • 8/19/2019 Assembly 64

    43/366

    Chapter #$" Q Data ;epresentation

    =t is ver2 important to understand the difference 0et'een characters 3such as !4 andintegers 3such a !"4$ Characters can 0e disp*a2ed to the conso*e, 0ut can not 0e used forca*cu*ations$ =ntegers can 0e used for ca*cu*ations 0ut can not 0e disp*a2ed to theconso*e 3'ithout changing the represent ion4$

    A character is t2pica**2 stored in a 02te 38-0its4 of space$ Bhis 'or(s 'e** since memor2is 02te addressa0*e$

     +41" Uni(ode

    =t shou*d 0e noted that Unicode! uses ! 02tes for each character$ Bhe additiona* spacesupports a much 'ider range of characters 'hich a**o's for man2 non-7ng*ish*anguages$ Detai*s regarding Unicode representation are not addressed in this text$

     +4" tring e.resentation

    A string!!

     is a series of A%C== characters, t2pica**2 terminated 'ith a

  • 8/19/2019 Assembly 64

    44/366

    Chapter #$" Data ;epresentation

     +0 Exer(ises

    1e*o' are some >uestions 0ased on this chapter$

     +01 ui9 uestions

    1e*o' are some >uiE >uestions$

    4 Provide the range for each of the fo**o'ing:

    $ signed 02te

    !$ unsigned 02te

    #$ signed 'ord

    $ unsigned 'ord

    .$ signed dou0*e-'ord

    6$ unsigned dou0*e-'ord

    !4 Provide the decima* va*ues of the fo**o'ing 0inar2 num0ers:

    $ """""! 

    !$ """""! 

    #$ """"! 

    $ """"! 

    #4 Provide the hex, byte siEe, t'oIs comp*iment va*ues of the fo**o'ing decima*va*ues$  "ote, t'o hex digits expected$

    $ -#" 

    !$ L" 

    #$ -F" 

    $ -!" 

    4 Provide the hex, word  siEe, t'oIs comp*iment va*ues of the fo**o'ing decima*va*ues$  "ote, four hex digits expected$

    $ -" 

    !$ L" 

    Page #"

  • 8/19/2019 Assembly 64

    45/366

    Chapter #$" Q Data ;epresentation

    #$ -#" 

    $ -#8" 

    .4 Provide the hex, double-word   siEe, t'oIs comp*iment va*ues of the fo**o'ingdecima* va*ues$  "ote, eight hex digits expected$

    $ -" 

    !$ -!" 

    #$ L" 

    $ -!6" 

    64 Provide the decima* va*ues of the fo**o'ing hex, dou0*e-'ord siEed, t'oIscomp*iment va*ues$

    $ )))))))16

     

    !$ ))))))7A6 

    #$ )))))))#6

     

    $ )))))))86 

    4 ?hich of the fo**o'ing decima* va*ues has an exact  representation in 0inar2N

    $ "$

    !$ "$!

    #$ "$#

    $ "$

    .$ "$ .

    84 Provide the decima* representation of the fo**o'ing =777 #!-0it f*oating pointva*ues$

    $ "xC""""

    !$ "x""""

    #$ "xC"D"""""

    $ "x")"""""

    Page #

  • 8/19/2019 Assembly 64

    46/366

    Chapter #$" Data ;epresentation

    F4 Provide hex, =777 #!-0it f*oating point representation of the fo**o'ing f*oatingpoint va*ues$

    $ L$!." 

    !$ -$!." 

    #$ L!$8." 

    $ -"$." 

    "4 ?hat is the A%C== code, in hex, for each of the fo**o' characters:

    $ A

    !$ a

    #$ "

    $ 8.$ ta0

    4 ?hat are the A%C== va*ues, in hex, for each of the fo**o'ing strings:

    $ ?or*d

    !$ !#

    #$ 9es]N

    Page #!

  • 8/19/2019 Assembly 64

    47/366

     4! Program /ormat

    Bhis chapter summariEes the formatting re>uirements for assem0*2-*anguage programs$Bhe formatting re>uirements are specific to the yasm assem0*er$ Other assem0*ers ma20e s*ight*2 different$ A comp*ete assem0*2 *anguage program is presented todemonstrate the appropriate program formatting$

    A proper*2 formatted assem0*2 source fi*e consists of severa* main parts

    • Data section 'here initia*iEed data is dec*ared and defined$

    • 1%% section 'here uninitia*iEed data is dec*ared$

    • Bext section 'here code is p*aced$

    Bhe fo**o'ing sections summariEe the 0asic formatting re>uirements$ On*2 the 0asicformatting and assem0*er s2ntax is presented$ )or additiona* information, refer to theyasm reference manua* 3as noted in Chapter , =ntroduction4$

     41 &omments

    Bhe semico*on 3K4 is used to note program comments$ Comments 3using the K4 ma2 0ep*aced an2'here, inc*uding after an instruction$ An2 characters after the K are ignore 02the assem0*er$ Bhis can 0e used to exp*ain steps ta(en in the code or to comment outsections of code$

     4" umeri( Values

  • 8/19/2019 Assembly 64

    48/366

    Chapter $" Program )ormat

    ?hen specif2ing octa*, or-0ase-8 va*ues, the2 are fo**o'ed 02 a q$ )or examp*e,to specif2 . as octa*, it 'ou*d 0e 777q$

    Bhe defau*t radix 30ase4 is decima*, so no specia* notation is re>uired for decima* 30ase-"4 num0ers$

     4+ ,e%ining &onstants

    Constants are defined 'ith equ$ Bhe genera* format is:

    equ

    Bhe va*ue of a constant can not 0e changed during program execution$

    Bhe constants are su0stituted for their defined va*ues during the assem0*2 process$ Assuch, a constant is not assigned a memor2 *ocation$ Bhis ma(es the constant more

    f*exi0*e since it is not assigned a specific t2pe&siEe 302te, 'ord, dou0*e-'ord, etc$4$ Bheva*ues are su0/ect to the range *imitations of the intended use$ )or examp*e, thefo**o'ing constant,

    SIZE equ 10000

    cou*d 0e used as a 'ord or a dou0*e-'ord, 0ut not a 02te$

     44 ,ata e(tion

    Bhe initia*iEed data must 0e dec*ared in the \section $data\ section$ A** initia*iEed

    varia0*es and constants are p*aced in this section$ aria0*e names start 'ith a *etterfo**o'ed 02 *etters or num0ers inc*uding some specia* characters 3such as theunderscore, \+\4$ aria0*e definitions must inc*ude the name, the data t2pe, and theinitia* va*ue for the varia0*e$

    Bhe genera* format is:

    ;efer to the fo**o'ing sections for a series of examp*es using various data t2pes$

    Bhe supported data t2pes are as fo**o's:,e(laration

    db  8-0it varia0*e3s4

    Page #

  • 8/19/2019 Assembly 64

    49/366

    Chapter $" Q Program )ormat

    dw   6-0it varia0*e3s4

    dd  #!-0it varia0*e3s4

    dq  6-0it varia0*e3s4

    ddq  !8-0it varia0*e3s4 J integer

    dt  !8-0it varia0*e3s4 J f*oat

    Bhese are the primar2 assem0*er directives for initia*iEed data dec*arations$ Otherdirectives are referenced in different sections$

    =nitia*iEed arra2s are defined 'ith comma separated va*ues$

    %ome simp*e examp*es inc*ude:

    bVar db 10 ; byte variablecVar db "H" ; single characterstr db "Hello World" ; string wVar dw 5000 ; word variabledVar dd 50000 ; 32-bit variablearr dd 100, 200, 300 ; 3 element arrayflt1 dd 3.14159 ; 32-bit floatqVar dq 1000000000 ; 64-bit variable

    Bhe va*ue specified must 0e a0*e to fit in the specified data t2pe$ )or examp*e, if theva*ue of a 02te siEed varia0*es is defined as ."", it 'ou*d generate an assem0*er error$

     40 5 e(tion

    Unintia*iEed data is dec*ared in the \section $0ss\ section$ A** unintia*iEed varia0*es aredec*ared in this section$ aria0*e names start 'ith a *etter fo**o'ed 02 *etters ornum0ers inc*uding some specia* characters 3such as the underscore, \+\4$ aria0*edefinitions must inc*ude the name, the data t2pe, and the count$

    Bhe genera* format is:

    ;efer to the fo**o'ing sections for a series of examp*es using various data t2pes$

    Page #.

  • 8/19/2019 Assembly 64

    50/366

    Chapter $" Program )ormat

    Bhe supported data t2pes are as fo**o's:

    ,e(laration

    resb  8-0it varia0*e3s4

    resw   6-0it varia0*e3s4

    resd  #!-0it varia0*e3s4

    resq  6-0it varia0*e3s4

    resdq  !8-0it varia0*e3s4

    Bhese are the primar2 assem0*er directives for uninitia*iEed data dec*arations$ Other

    directives are referenced in different sections$

    %ome simp*e examp*es inc*ude:

    bArr resb 10 ; 10 element byte array wArr resw 50 ; 50 element word arraydArr resd 100 ; 100 element double arrayqArr resq 200 ; 200 element quad array

    Bhe a**ocated arra2 is not initia*iEed to an2 specific va*ue$

     46 $ext e(tionBhe code is p*aced in the \section $text\ section$ Bhe instructions are specified one per*ine and it must 0e a va*id instruction 'ith the appropriate re>uired operands$

    Bhe text section 'i** inc*ude some (ind of *a0e*s the define the initia* program entr2point$ )or examp*e, assuming a 0asic program using the standard s2stem *in(er, thefo**o'ing dec*arations must 0e inc*uded$

    global _start _start:

    uired to terminate the program$ o'ever, a s2stemservice shou*d 0e used to inform the operating s2stem that the program shou*d 0eterminated$

    ;efer to the examp*e program in the fo**o'ing section$

    Page #6

  • 8/19/2019 Assembly 64

    51/366

    Chapter $" Q Program )ormat

     4# Exam.le Program

    A ver2 simp*e assem0*2 *anguage program is presented to demonstrate the appropriateprogram formatting$

    ; Simple example demonstrating basic program ; format and layout.

    ; ************************************************; Some basic data declarations

    section.data

    ; -----; Define constants

    EXIT_SUCCESS equ 0 ; successful operationSYS_exit equ 60 ; call code for terminate

    ; -----; Byte (8-bit) variable declarations

    bVar1 db 17bVar2 db 9bResult db 0

    ; -----; Word (16-bit) variable declarations

     wVar1 dw 17000 wVar2 dw 9000 wResult dw 0

    ; -----; Double-word (32-bit) variable declarations

    dVar1 dd 17000000dVar2 dd 9000000dResult dd 0

    Page #

  • 8/19/2019 Assembly 64

    52/366

    Chapter $" Program )ormat

    ; -----; Quad-word (64-bit) variable declarations

    qVar1 dq 170000000qVar2 dq 90000000

    qResult dq 0

    ; *************************************************************; Code Section

    section.textglobal _start _start:

    ; Performs a series of very basic addition operations

    ; to demonstrate basic program format.

    ; ----------; Byte example; bResult = bVar1 + bVar2

     mov al, byte [bVar1]add al, byte [bVar2] mov byte [bResult], al

    ; ----------; Word example; wResult = wVar1 + wVar2

     mov ax, word [wVar1]add ax, word [wVar2] mov word [wResult], ax

    ; ----------; Double-word example

    ; dResult = dVar1 + dVar2

     mov eax, dword [dVar1]add eax, dword [dVar2] mov dword [dResult], eax

    Page #8

  • 8/19/2019 Assembly 64

    53/366

    Chapter $" Q Program )ormat

    ; ----------; Quadword example; qResult = qVar1 + qVar2

     mov rax, qword [qVar1]

    add rax, qword [qVar2] mov qword [qResult], rax

    ; ************************************************************; Done, terminate program.

    last: mov rax, SYS_exit ; Call code for exit mov rdi, EXIT_SUCCESS ; Exit program with successsyscall

    Bhis examp*e program 'i** 0e referenced and further exp*ained in the fo**o'ingchapters$

     48 Exer(ises

    1e*o' are some >uestions 0ased on this chapter$

     481 ui9 uestions

    1e*o' are some >uiE >uestions$

    4 ?hat is the name of the assem0*er 0eing used in this chapterN

    !4 o' are comments mar(ed in an assem0*2 *anguage programN

    #4 ?hat is the name of the section 'here the initia*iEed data dec*aredN

    4 ?hat is the name of the section 'here the uninitia*iEed data dec*aredN

    .4 ?hat is the name of the section 'here the code is p*acedN

    64 ?hat is the data dec*aration for each of the fo**o'ing varia0*es 'ith the givenva*ues:

    $ 02te siEed varia0*e bNum set to "" 

    !$ 'ord siEed varia0*e wNum set to ",!F" 

    Page #F

  • 8/19/2019 Assembly 64

    54/366

    Chapter $" Program )ormat

    #$ dou0*e-'ord siEed varia0*e dwNum set to !,!6,""" 

    $ >uad 'ord siEed varia0*e qwNum set to ",""",""","""" 

    4 ?hat is the uninitia*iEed data dec*aration for each of the fo**o'ing:

    $ 02te siEed arra2 named bArr  'ith "" e*ements!$ 'ord siEed arra2 named wArr  'ith #""" e*ements

    #$ dou0*e-'ord siEed arra2 named dwArr  'ith !"" e*ements

    $ >uad 'ord siEed arra2 named qArr  'ith .""" e*ements

    84 ?hat are the re>uired dec*arations to signif2 the start of a program 3in the textsection4$

    Page "

  • 8/19/2019 Assembly 64

    55/366

     0! $ool &hain

    =n genera*, the set of programing too*s used to create a program is referred to as the toolchain!#$ )or the purposes of this text, the too* chain consists of the fo**o'ing

    • Assem0*er

    • @in(er

    • @oader

    • De0ugger

    ?hi*e there are man2 options for the too* chain, this text uses a fair*2 standard set ofopen-source too*s that 'or( 'e** together and fu**2 support the x86 6-0it environment$

    7ach of these programming too*s is exp*ained in the fo**o'ing sections$

     01 AssembleLinLoad )*er*iew

    =n 0road terms, the assem0*e, *in(, and *oad process is ho' programmer 'ritten sourcefi*es are converted into an executa0*e program$

    Bhe human reada0*e source fi*e is converted into an o0/ect fi*e 02 the assem0*er$ =n themost 0asic form, the o0/ect fi*e is converted into a executa0*e fi*e 02 the *in(er$ Bhe*oader 'i** *oad the executa0*e fi*e into memor2$

    !# )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Boo*chain

    Page

    Chapter

    5

    *here are two ways to write errorfree programs; only the third works.

  • 8/19/2019 Assembly 64

    56/366

    Chapter .$" Boo* Chain

    An overvie' of the process is provided in the fo**o'ing diagram$

    Bhe assem0*e, *in(, and *oad steps are descri0ed in more detai* in the fo**o'ing sections$

    Page !

     Illustration /: 0verview: Assemle, ink, oad

    Assembly

    LanguageSourceFile

    ASSEMBLE

    ListFile

    ObjectFile

    LINK

    LOADExecutableFile

     RAM

    OtherObjectFiles(if any)

    LibraryRoutines(if any)

    SharedObjectFiles(if any)

  • 8/19/2019 Assembly 64

    57/366

    Chapter .$" Q Boo* Chain

     0" Assembler

    Bhe assem0*er! is a program that 'i** read an assem0*2 *anguage input fi*e and convertthe code into a machine *anguage 0inar2 fi*e$ Bhe input fi*e is an assem0*2 *anguagesource fi*e containing assem0*2 *anguage instructions in human reada0*e form$ Bhe

    machine *anguage output is referred to as an o0/ect fi*e$ As part of this process, thecomments are removed, and the varia0*e names and *a0e*s are converted intoappropriate addresses 3as re>uired 02 the CPU during execution4$

    Bhe assem0*er used in this text is the yasm assem0*er$ @in(s to the yasm 'e0 site anddocumentation can 0e found in Chapter , =ntroduction

     0"1 Assemble &ommands

    Bhe appropriate yasm assem0*er command for reading the assem0*2 *anguage sourcefi*e, such as the examp*e from the previous chapter, is as fo**o's:

    yasm -g dwarf2 -f elf64 example.asm -l example.lst

     "ote, the -l is a dash *o'er-case *etter @ 3'hich is easi*2 confused 'ith the num0er 4$

    Bhe -g dwar%" option is used to inform the assem0*er to inc*ude de0ugging informationin the fina* o0/ect fi*e$ Bhis increases the siEe of the o0/ect fi*e, 0ut is necessar2 to a**o'effective de0ugging$ Bhe -% el%64 informs the assem0*er to create the o0/ect fi*e in theEL/64"0  format 'hich is appropriate for 6-0it, @inux 0ased s2stems$ Bheexam.leasm  is the name of the assem0*2 *anguage source fi*e for input$ Bhe -lexam.lelst 3dash *o'er-case *etter @4 informs the assem0*er to create a *ist fi*e named

    e-ample.lst $

    =f an error occurs during the assem0*2 process, it must 0e reso*ved 0efore continuing tothe *in( step$

     0"" List /ile

    =n addition, the assem0*er is optiona**2 capa0*e of creating a *ist fi*e$ Bhe *ist fi*e sho'sthe *ine num0er, the re*ative address, the machine *anguage version of the instruction3inc*uding varia0*e references4, and the origina* source *ine$ Bhe *ist fi*e can 0e usefu*'hen de0ugging$

    ! )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Assem0*er+3computing4^Assem0*er!. )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&7xecuta0*e+and+@in(a0*e+)ormat

    Page #

  • 8/19/2019 Assembly 64

    58/366

    Chapter .$" Boo* Chain

    )or examp*e, a fragment from the *ist fi*e data section, from the examp*e program in theprevious chapter is as fo**o's:

      36 00000009 40660301 dVar1 dd 1700000037 0000000D 40548900 dVar2 dd 9000000

    38 00000011 00000000 dResult dd 0

    On the first *ine, the +6  is the *ine num0er$ Bhe next num0er, !x!!!!!!!3, is there*ative address in the data area of 'here that varia0*e 'i** 0e stored$ %ince d2ar1 is adou0*e-'ord, 'hich re>uires four 02tes, the address for the next varia0*e is!x!!!!!!!,$ Bhe d2ar1 varia0*e uses 02tes as addresses !x!!!!!!!3, !x!!!!!!!A,!x!!!!!!!5, and !x!!!!!!!&$ Bhe rest of the *ine is the data dec*aration as t2ped inthe origina* assem0*2 *anguage source fi*e$

    Bhe !x4!66!+!1  is the va*ue, in hex, as p*aced in memor2$ Bhe ,""",""""  is!x!1!+664!$ ;eca**ing that the architecture is *itt*e-endian, the *east significant 02te3!x4!4 is p*aced in the *o'est memor2 address$ As such, the "x" is p*aced in re*ativeaddress !x!!!!!!!3, the next 02te, !x66, is p*aced in address !x!!!!!!!3 and so forth$Bhis can 0e confusing as at first g*ance the num0er ma2 appear 0ac('ards or gar0*ed3depending on ho' it is vie'ed4$

    Bo he*p visua*iEe, the memor2 picture 'ou*d 0e as fo**o's:

    variab%ename

    va%ue address

    00   0x00000010

    89   0x0000000F

    54   0x0000000E

    d*ar+    40   0x0000000D

    01   0x0000000C

    03   0x0000000B

    66   0x0000000A

    d*ar'    40   0x00000009

    Page

  • 8/19/2019 Assembly 64

    59/366

    Chapter .$" Q Boo* Chain

    )or examp*e, a fragment of the *ist fi*e text section, excerpted from the examp*eprogram in the previous chapter is as fo**o's:

      95 last:96 0000005A 48C7C03C000000 mov rax, SYS_exit

    97 00000061 48C7C300000000 mov rbx, EXIT_SUCCESS98 00000068 0F05 syscall

    Again, the num0ers to the *eft are the *ine num0ers$ Bhe next num0er, !x!!!!!!0A, isthe re*ative address of 'here the *ine of code 'i** 0e p*aced$

    Bhe next num0er, !x48&!+&!!!!!!, is the machine *anguage version of theinstruction, in hex, that the CPU reads and understands$ Bhe rest of the *ine is theorigina* assem0*2 *anguage source instruction$

    Bhe *a0e*, last, does not have a machine *anguage instruction since the *a0e* is used toreference a specific address and is not an executa0*e instruction$

     0"+ $wo-Pass Assembler

    Bhe assem0*er!6  'i** read the source fi*e and convert each assem0*2 *anguageinstruction, t2ped 02 the programmer, into a set of Is and "Is that the CPU (no's to 0ethat instruction$ Bhe Is and "Is are referred to as machine *anguage$ Bhere is a one-to-one correspondence 0et'een the assem0*2 *anguage instructions and the 0inar2 machine*anguage$ Bhis re*ationship means that machine *anguage, in the form of an executa0*efi*e can 0e converted 0ac( into human reada0*e assem0*2 *anguage$ Of course, the

    comments, varia0*e names, and *a0e* names are missing, so the resu*ting code can 0ever2 difficu*t to read$

    As the assem0*er reads each *ine of assem0*2 *anguage it generates machine code forthat instruction$ Bhis 'i** 'or( 'e** for instructions to do not perform /umps$ o'ever,for instructions that might change the contro* f*o' 3e$g$, =) statements, unconditiona* /umps4, the assem0*er is not a0*e to convert the instruction$ )or examp*e, given thefo**o'ing code fragment:

     mov rax, 0jmp skipRest

    ...

    ...skipRest:

    !6 )or more information, refer to: http:&&en$'i(ipedia$org&'i(i&Assem0*2+*anguage^Assem0*er

    Page .

  • 8/19/2019 Assembly 64

    60/366

    Chapter .$" Boo* Chain

    Bhis is referred to as a for'ard reference$ =f the assem0*er reads the assem0*2 fi*e one*ine at a time, it has not read the *ine 'here skip3est  is defined$ =n fact, it does not even(no' for sure if skip3est  is defined at a**$

    Bhis situation can 0e reso*ved 02 reading the assem0*2 source fi*e t'ice$ Bhe entire

    process is referred to as a t'o-pass assem0*er$ Bhe steps re>uired for each pass aredetai*ed in the fo**o'ing sections$

     0"+1 /irst Pass

    Bhe steps ta(en on the first pass var2 0ased on the design of the specific assem0*er$o'ever, some of the 0asic operations performed on the first pass inc*ude thefo**o'ing:

    • Create s2m0o* ta0*e

    • 7xpand macros

    • 7va*uate constant expressions

    A macro is a program e*ement that is expanded into a set of programmer predefinedinstructions$ )or more information, refer to Chapter , Macros$

    A constant expression is an expression composed on entire*2 constants$ %ince theexpression is constants on*2, it can 0e fu**2 eva*uated at assem0*e time$ )or examp*e,assuming the constant 1U)) is defined, the fo**o'ing instruction contains a constantexpression

     mov rax, BUFF+5

    Bhis t2pe of constant expression is used common*2 in *arge