[
  {
    "path": ".gitignore",
    "content": "*.png\n*.lyx~\n"
  },
  {
    "path": "CHANGELOG.md",
    "content": "## 0.0.1 (2017-02-15)\n\n  * [#6] Fix the figure `far_jmp_ex.svg` in chapter 4, where the segment and the offset in memory are reversed.\n  * [#7] Fix example 5.3.1: change NULL section to .interp section.\n  * [#8] Fix command output to reflect the source code.\n  * [#9] abort(), not .abort(). A function call, not a section.\n  * [#10] Use `__FUNCTION__` for consistency.\n  * [#11] Fix incorrect filename.\n  * [#12] Fix confusing sentence.\n  * [#13] Fix a typo.\n"
  },
  {
    "path": "README.md",
    "content": "\n[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=tuhdo1710%40gmail%2ecom&lc=VN&item_number=tuhdo&currency_code=USD&bn=PP%2dDonationsBF%3aDonate%2dPayPal%2dgreen%2esvg%3aNonHosted)\n\n[Operating Systems: From 0 to 1](https://tuhdo.github.io/os01/)\n=============================\n\nThis book helps you gain the foundational knowledge required to write an\noperating system from scratch. Hence the title, 0 to 1.\n\nAfter completing this book, at the very least you will learn:\n\n- How to write an operating system from scratch by reading hardware datasheets.\n  In the real world, it works like that. You won't be able to consult Google for\n  a quick answer.\n\n- A big picture of how each layer of a computer is related to the other, from hardware to software.\n\n- Write code independently. It's pointless to copy and paste code. Real learning\n  happens when you solve problems on your own. Some examples are given to kick\n  start, but most problems are yours to conquer. However, the solutions are\n  available online for you to examine after giving it a good try.\n\n- Linux as a development environment and how to use common tools for low-level\n  programming.\n\n- x86 assembly in-depth.\n\n- How a program is structured so that an operating system can run.\n\n- How to debug a program running directly on hardware with gdb and QEMU.\n\n- Linking and loading on bare metal x86_64, with pure C. No standard library. No\n  runtime overhead.\n\n[Download the book](https://github.com/tuhdo/os01/blob/master/Operating_Systems_From_0_to_1.pdf)\n\n# The pedagogy of the book\n\n> You give a poor man a fish and you feed him for a day. You teach him to fish\n> and you give him an occupation that will feed him for a lifetime.\n\nThis has been the guiding principle of the book when I was writing it. The book does\nnot try to teach you everything, but enough to enable you to learn by yourself.\nThe book itself, at this point, is quite \"complete\": once you master part 1 and\npart 2 (which consist of 8 chapters), you can drop the book and learn by\nyourself. At this point, smart readers should be able to continue on their own.\nFor example, they can continue their journeys\non [OSDev wiki](http://wiki.osdev.org/Main_Page); in fact, after you study\neverything in part 1 and part 2, you only meet\nthe [minimum requirement](http://wiki.osdev.org/Required_Knowledge) by OSDev\nWiki (well, not quite, the book actually goes deeper for the suggested topics).\nOr, if you consider developing an OS for fun is impractical, you can continue\nwith a Linux-specific book, such as this free\nbook [Linux Insides](https://0xax.gitbooks.io/linux-insides/content/), or other\npopular Linux kernel books. The book tries hard to provide you a strong\nfoundation, and that's why part 1 and part 2 were released first.\n\nThe book teaches you core concepts, such as x86 Assembly, ELF, linking and\ndebugging on bare metal, etc., but more importantly, where such information\ncome from. For example, instead of just teaching x86 Assembly, it also teaches\nhow to use reference manuals from Intel. Learning to read the official\nmanuals is important because only the hardware manufacturers themselves\nunderstand how their hardware work. If you only learn from the secondary\nresources because it is easier, you will never gain a complete understanding of\nthe hardware you are programming for. Have you ever read a book on Assembly, and\nwondered where all the information came from? How does the author know\neverything he says is correct? And how one seems to magically know so much about\nhardware programming? This book gives pointers to such questions.\n\nAs an example, you should skim through chapter 4, \"x86 Assembly and C\", to see\nhow it makes use of the Intel manual, Volume 2. And in\nthe process, it guides you how to use the official manuals.\n\nPart 3 is planned as a series of specifications that a reader will implement to\ncomplete each operating system component. It does not contain code aside from a\nfew examples. Part 3 is just there to shorten the reader's time when reading the\nofficial manuals by giving hints where to read, explaining difficult concepts\nand how to use the manuals to debug. In short, the implementation is up to the\nreader to work on his or her own; the chapters are just like university assignments.\n\n# Prerequisites\n\nKnow some circuit concepts:\n+ Basic Concepts of Electricity: atoms, electrons, protons, neutrons, current flow.\n+ Ohm's law\n\nHowever, if you know absolutely nothing about electricity, you can quickly learn it here:\n<http://www.allaboutcircuits.com/textbook/>, by reading chapter 1 and chapter 2.\n\nC programming. In particular:\n\n+ Variable and function declarations/definitions\n\n+ While and for loops\n\n+ Pointers and function pointers\n\n+ Fundamental algorithms and data structures in C\n\nLinux basics:\n\n+ Know how to navigate directory with the command line\n\n+ Know how to invoke a command with options\n\n+ Know how to pipe output to another program\n\nTouch typing. Since we are going to use Linux, touch typing helps. I know typing\nspeed does not relate to problem-solving, but at least your typing speed should\nbe fast enough not to let it get it the way and degrade the learning experience.\n\nIn general, I assume that the reader has basic C programming knowledge, and can\nuse an IDE to build and run a program.\n\n# Status:\n* Part 1\n    - Chapter 1: Complete\n    - Chapter 2: Complete\n    - Chapter 3: Almost. Currently, the book relies on the Intel Manual for fully explaining x86 execution environment.\n    - Chapter 4: Complete\n    - Chapter 5: Complete\n    - Chapter 6: Complete\n* Part 2\n    - Chapter 7: Complete\n    - Chapter 8: Complete\n* Part 3\n    - Chapter 9: Incomplete\n    - Chapter 10: Incomplete\n    - Chapter 11: Incomplete\n    - Chapter 12: Incomplete\n    - Chapter 13: Incomplete\n\n    ... and future chapters not included yet ...\n\nIn the future, I hope to expand part 3 to cover more than the first 2 parts. But\nfor the time being, I will try to finish the above chapters first.\n\n# Sample OS\n[This repository](https://github.com/tuhdo/sample-os) is the sample OS of the\nbook that is intended as a reference material for part 3. It covers 10 chapters\nof the \"System Programming Guide\" (Intel Manual Volume 3), along with a simple\nkeyboard and video driver for input and output. However, at the moment, only the\nfollowing features are implemented:\n\n- Protected mode.\n- Creating and managing processes with TSS (Task State Structure).\n- Interrupts\n- LAPIC.\n\nPaging and I/O are not yet implemented. I will try to implement it as the book progresses.\n\n# Contributing\n\nIf you find any grammatical issues, please report it using Github Issues. Or, if\nsome sentence or paragraph is difficult to understand, feel free to open an\nissue with the following title format: `[page number][type] Descriptive Title`.\n\nFor example: `[pg.9][grammar] Incorrect verb usage`.\n\n`type` can be one of the following:\n\n- `Typo`: indicates typing mistake.\n- `Grammar`: indicates incorrect grammar usage.\n- `Style`: indicates a style improvement.\n- `Content`: indicates problems with the content.\n\nEven better, you can make a pull request with the provided book source. The main\ncontent of the book is in the file \"Operating Systems: From 0 to 1.lyx\". You can\nedit the .txt file, then I will integrate the changes manually. It is a\nworkaround for now since Lyx can cause a huge diff which makes it impossible to\nreview changes.\n\nThe book is in development, so please bear with me if the English irritates you.\nI really appreciate it.\n\nFinally, if you like the project and if it is possible, please donate to help\nthis project and keep it going.\n\n# Got questions?\nIf you have any question related to the material or the development of the book,\nfeel free to [open a Github issue](https://github.com/tuhdo/os01/issues/new).\n"
  },
  {
    "path": "_config.yml",
    "content": "theme: jekyll-theme-architect"
  },
  {
    "path": "book_src/Operating Systems From 0 to 1.lyx",
    "content": "#LyX 2.3 created this file. For more info see http://www.lyx.org/\r\n\\lyxformat 544\r\n\\begin_document\r\n\\begin_header\r\n\\save_transient_properties true\r\n\\origin unavailable\r\n\\textclass tufte-book\r\n\\begin_preamble\r\n% DO NOT ALTER THIS PREAMBLE!!!\r\n%\r\n% This preamble is designed to ensure that the manual prints\r\n% out as advertised. If you mess with this preamble,\r\n% parts of the manual may not print out as expected.  If you\r\n% have problems LaTeXing this file, please contact \r\n% the documentation team\r\n% email: lyx-docs@lists.lyx.org\r\n\r\n\\usepackage[makeindex]{imakeidx}\r\n\\makeindex[intoc]\r\n\r\n\\usepackage{hyperref}\r\n\r\n% if pdflatex is used\r\n\\usepackage{ifpdf}\r\n\\ifpdf\r\n\r\n % set fonts for nicer pdf view\r\n \\IfFileExists{lmodern.sty}\r\n  {\\usepackage{lmodern}}{}\r\n\r\n\\fi % end if pdflatex is used\r\n\r\n% the pages of the TOC are numbered roman\r\n% and a PDF-bookmark for the TOC is added\r\n\\pagenumbering{roman}\r\n\\let\\myTOC\\tableofcontents\r\n\\renewcommand{\\tableofcontents}{%\r\n \\pdfbookmark[1]{\\contentsname}{}\r\n \\myTOC\r\n \\cleardoublepage\r\n \\pagenumbering{arabic}}\r\n\r\n\\usepackage{xcolor}\r\n\r\n% extra space for tables\r\n\\newcommand{\\extratablespace}[1]{\\noalign{\\vskip#1}}\r\n\\usepackage{titlesec}\r\n\\usepackage{graphicx}\r\n\r\n\\definecolor{mygray}{gray}{0.3.}\r\n\\definecolor{lightgray}{gray}{0.6}\r\n\\definecolor{green}{rgb}{0.31, 0.78, 0.47}\r\n\\definecolor{yellow}{rgb}{1.0, 0.87, 0.0}\r\n\\definecolor{cyan}{rgb}{0.0, 0.72, 0.92}\r\n\r\n\\hyphenation{MovCursor}\r\n\\usepackage{multirow}\r\n\r\n% add numbers to chapters, sections, subsections\r\n \\setcounter{secnumdepth}{4}\r\n\r\n\r\n% section format\r\n\\titleformat{\\section}%\r\n  {\\normalfont\\LARGE\\bfseries\\color{mygray}}% format applied to label+text\r\n  {\\llap{\\colorbox{mygray}{\\parbox{3.5cm}{\\hfill\\color{white}\\thesection}}}}% label\r\n  {1em}% horizontal separation between label and title body\r\n  {}% before the title body\r\n  []% after the title body\r\n\r\n\\titleformat{\\subsection}%\r\n  {\\color{gray}\\normalfont\\large\\itshape}\r\n  {}\r\n  {0em}\r\n  {\\large\\thesubsection\\hspace{0.6em}}\r\n[{\\titlerule[0.8pt]}]\r\n\r\n\r\n\\usepackage[activate={true, nocompatibility}, final, tracking=true, kerning=true, spacing=true, factor=1100, stretch=20, shrink=20]{microtype}\r\n\\hyphenpenalty=10\r\n\\exhyphenpenalty=10\r\n\\doublehyphendemerits=10\r\n\\finalhyphendemerits=5000\r\n\\uchyph=0\r\n\r\n\\titleformat{\\chapter}[display]\r\n  {\\normalfont\\bfseries\\color{mygray}}\r\n  {\\filleft\\hspace*{-60pt}%\r\n    \\rotatebox[origin=c]{90}{%\r\n      \\normalfont\\color{black}\\Large%\r\n        \\textls[180]{\\textsc{ }}%\r\n    }\\hspace{10pt}%\r\n    {\\setlength\\fboxsep{0pt}%\r\n    \\colorbox{mygray}{\\parbox[c][3cm][c]{3.5cm}{%\r\n      \\centering\\color{white}\\fontsize{80}{90}\\fontfamily{lmtt}\\selectfont\\thechapter}%\r\n    }}%\r\n  }\r\n  {10pt}\r\n  {\\raggedleft\\Huge\\itshape\\bfseries\\fontfamily{pzc}\\selectfont}\r\n\r\n\r\n\r\n\\usepackage{listings}\r\n\r\n\\lstset{\r\nbasicstyle=\\ttfamily,\r\ncolumns=fullflexible,\r\nbreaklines=true,\r\nescapeinside={@|}{|@}\r\n}\r\n\r\n\\usepackage{bookmark}\r\n\r\n% table of contents styling\r\n\\usepackage{titletoc}\r\n\\usepackage{etoolbox}\r\n\r\n\\newcommand\\frontformat{%\r\n\\titlecontents{chapter}[0em]\r\n  {\\itshape}{\\contentslabel{0em}}\r\n  {}{\\normalfont\\titlerule*[1pc]{.}\\contentspage}}\r\n\\newcommand\\mainformat{%\r\n\\titlecontents{chapter}[1.4em]\r\n  {\\addvspace{10pt}\\bfseries}{\\contentslabel{1.5em}}\r\n  {}{\\normalfont\\titlerule*[1pc]{.}\\bfseries\\contentspage}\r\n}\r\n\\newcommand\\backformat{%\r\n\\titlecontents{chapter}[1.5em]\r\n  {\\addvspace{10pt}\\itshape}{\\contentslabel{1.5em}}\r\n  {\\hspace*{-1.5em}}{\\normalfont\\titlerule*[1pc]{.}\\contentspage}}\r\n\r\n\\titlecontents{section}[3.8em]\r\n  {\\itshape}{\\contentslabel{2.3em}}\r\n  {\\hspace*{-2.3em}}{\\titlerule*[1pc]{.}\\contentspage}\r\n\r\n\\apptocmd{\\frontmatter}{\\frontformat}{}{}\r\n\\apptocmd{\\mainmatter}{\\mainformat}{}{}\r\n\\apptocmd{\\appendix}{\\backformat}{}{}\r\n\r\n% caption customization\r\n\\usepackage[font=small,labelfont=bf]{caption}\r\n\\usepackage[many]{tcolorbox}\r\n\r\n\\definecolor{greentitle}{RGB}{61,170,61}\r\n\\definecolor{greentitleback}{RGB}{216,233,213}\r\n\r\n\\newtcolorbox{shelloutput16.6}[1][]{%\r\nbreakable,\r\nenhanced,\r\ntitle=Output,\r\narc=0mm,\r\nauto outer arc,\r\ncolback=white,\r\nboxrule=1pt,\r\nleftrule=5pt,\r\nbefore skip = 0mm,\r\nfonttitle=\\bfseries\\texttt\\smaller,\r\nenlarge top initially by=5mm,\r\nwidth=16.6cm,\r\nattach boxed title to top left={xshift=-15.8mm,yshift=-5.72mm},\r\nboxed title style={skin=enhancedfirst jigsaw,size=small,arc=0mm,bottom=0mm,\r\n                             interior style={fill=none,\r\n                             top color=mygray,\r\n                             bottom color=mygray}},\r\n#1\r\n}\r\n\\definecolor{whitesmoke}{rgb}{0.96, 0.96, 0.96}\r\n\\newtcolorbox{shelloutput}[1][]{%\r\nbreakable,\r\nenhanced,\r\ncolback=white,\r\ntitle=Output,\r\narc=0mm,\r\nauto outer arc,\r\nboxrule=1pt,\r\nleftrule=5pt,\r\nfonttitle=\\bfseries\\texttt\\smaller,\r\nenlarge top initially by=5mm,\r\nattach boxed title to top left={xshift=-15.8mm,yshift=-5.72mm},\r\nboxed title style={skin=enhancedfirst jigsaw,size=small,arc=0mm,bottom=0mm,\r\n                             interior style={fill=none,\r\n                             top color=mygray,\r\n                             bottom color=mygray}},\r\n#1\r\n}\r\n\r\n\\newtcolorbox{shellcommand}[1][]{%\r\nenlarge top initially by=5mm,\r\n}\r\n\\RequirePackage{ragged2e}\r\n\\setlength{\\RaggedRightRightskip}{\\z@ plus 0.01\\hsize}\r\n\\end_preamble\r\n\\options bibliography=totoc,index=totoc,BCOR7.5mm,titlepage,captions=tableheading\r\n\\use_default_options true\r\n\\begin_modules\r\neqs-within-sections\r\nfigs-within-sections\r\nlogicalmkup\r\nmulticol\r\nshapepar\r\nalgorithm2e\r\ntcolorbox\r\ntheorems-ams-bytype\r\nenumitem\r\ntabs-within-sections\r\ntheorems-ams-extended-bytype\r\ntheorems-sec-bytype\r\nfix-cm\r\nfixltx2e\r\n\\end_modules\r\n\\maintain_unincluded_children false\r\n\\begin_local_layout\r\nFormat 7\r\nInsetLayout CharStyle:MenuItem\r\nLyxType               charstyle\r\nLabelString           menu\r\nLatexType             command\r\nLatexName             menuitem\r\nFont\r\nFamily              Sans\r\nEndFont\r\nPreamble\r\n\\newcommand*{\\menuitem}[1]{{\\sffamily #1}}\r\nEndPreamble\r\nEnd\r\n\\end_local_layout\r\n\\language english\r\n\\language_package none\r\n\\inputencoding auto\r\n\\fontencoding global\r\n\\font_roman \"lmodern\" \"default\"\r\n\\font_sans \"lmss\" \"default\"\r\n\\font_typewriter \"lmtt\" \"default\"\r\n\\font_math \"auto\" \"auto\"\r\n\\font_default_family default\r\n\\use_non_tex_fonts false\r\n\\font_sc false\r\n\\font_osf false\r\n\\font_sf_scale 110 100\r\n\\font_tt_scale 100 100\r\n\\use_microtype false\r\n\\use_dash_ligatures false\r\n\\graphics default\r\n\\default_output_format default\r\n\\output_sync 1\r\n\\output_sync_macro \"\\synctex=1\"\r\n\\bibtex_command default\r\n\\index_command default\r\n\\paperfontsize 12\r\n\\spacing onehalf\r\n\\use_hyperref true\r\n\\pdf_title \"LyX's Additional Features manual\"\r\n\\pdf_author \"LyX Team\"\r\n\\pdf_subject \"LyX's additional features documentation\"\r\n\\pdf_keywords \"LyX, Documentation, Additional\"\r\n\\pdf_bookmarks true\r\n\\pdf_bookmarksnumbered true\r\n\\pdf_bookmarksopen false\r\n\\pdf_bookmarksopenlevel 1\r\n\\pdf_breaklinks false\r\n\\pdf_pdfborder false\r\n\\pdf_colorlinks true\r\n\\pdf_backref false\r\n\\pdf_pdfusetitle false\r\n\\pdf_quoted_options \"linkcolor=black, citecolor=black, urlcolor=blue, filecolor=blue, pdfpagelayout=OneColumn, pdfnewwindow=true, pdfstartview=XYZ, plainpages=false\"\r\n\\papersize a4paper\r\n\\use_geometry true\r\n\\use_package amsmath 1\r\n\\use_package amssymb 1\r\n\\use_package cancel 0\r\n\\use_package esint 0\r\n\\use_package mathdots 1\r\n\\use_package mathtools 0\r\n\\use_package mhchem 1\r\n\\use_package stackrel 0\r\n\\use_package stmaryrd 0\r\n\\use_package undertilde 0\r\n\\cite_engine natbib\r\n\\cite_engine_type authoryear\r\n\\biblio_style plain\r\n\\use_bibtopic true\r\n\\use_indices false\r\n\\paperorientation portrait\r\n\\suppress_date false\r\n\\justification true\r\n\\use_refstyle 0\r\n\\use_minted 0\r\n\\notefontcolor #aa007f\r\n\\index Index\r\n\\shortcut idx\r\n\\color #008000\r\n\\end_index\r\n\\leftmargin 2cm\r\n\\rightmargin 2cm\r\n\\secnumdepth 2\r\n\\tocdepth 1\r\n\\paragraph_separation skip\r\n\\defskip smallskip\r\n\\is_math_indent 1\r\n\\math_indentation default\r\n\\math_numbering_side default\r\n\\quotes_style english\r\n\\dynamic_quotes 0\r\n\\papercolumns 1\r\n\\papersides 2\r\n\\paperpagestyle fancy\r\n\\listings_params \"language=C,commentstyle={\\color{lightgray}\\itshape},emphstyle={\\itshape},breaklines=true,basicstyle={\\ttfamily},stringstyle={\\color{gray}},frame=shadowbox,rulesepcolor={\\color{black}}\"\r\n\\bullet 0 0 17 -1\r\n\\tracking_changes false\r\n\\output_changes false\r\n\\html_math_output 0\r\n\\html_css_as_file 0\r\n\\html_be_strict true\r\n\\end_header\r\n\r\n\\begin_body\r\n\r\n\\begin_layout Title\r\n\\noindent\r\nOperating Systems:\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\nFrom 0 to 1\r\n\\end_layout\r\n\r\n\\begin_layout Author\r\nTu, Do Hoang\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Newpage cleardoublepage\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsetcounter{page}{1}% Start page number with 1\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nrenewcommand{\r\n\\backslash\r\nthepage}{\r\n\\backslash\r\nRoman{page}}% Roman numerals for page counter\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset CommandInset toc\r\nLatexCommand tableofcontents\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsetcounter{page}{1}% Start page number with 1\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nrenewcommand{\r\n\\backslash\r\nthepage}{\r\n\\backslash\r\nroman{page}}% Roman numerals for page counter\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Chapter*\r\n\r\n\\emph on\r\nPreface\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\naddcontentsline{toc}{chapter}{Preface}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nGreetings!\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\nYou've probably asked yourself at least once how an operating system is\r\n written from the ground up.\r\n You might even have years of programming experience under your belt, yet\r\n your understanding of operating systems may still be a collection of abstract\r\n concepts not grounded in actual implementation.\r\n To those who've never built one, an operating system may seem like magic:\r\n a mysterious thing that can control hardware while handling a programmer's\r\n requests via the API of their favorite programming language.\r\n Learning how to build an operating system seems intimidating and difficult;\r\n no matter how much you learn, it never feels like you know enough.\r\n You're probably reading this book right now to gain a better understanding\r\n of operating systems to be a better software engineer.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf that is the case, this book is for you.\r\n By going through this book, you will be able to find the missing pieces\r\n that are essential and enable you to implement your own operating system\r\n from scratch! Yes, from scratch, without going through any existing operating\r\n system layer to prove to yourself that you are an operating system developer.\r\n You may ask,\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nIsn't it more practical to learn the internals of Linux?\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nYes...\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nand no.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLearning Linux can help your workflow at your day job.\r\n However, if you follow that route, you still won't achieve the ultimate\r\n goal of writing an actual operating system.\r\n By writing your own operating system, you will gain knowledge that you\r\n will not be able to glean just from learning Linux.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHere's a list of some benefits of writing your own OS:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nYou will learn how a computer works at the hardware level, and you will\r\n learn to write software to manage that hardware directly.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nYou will learn the fundamentals of operating systems, allowing you to adapt\r\n to any operating system, not just Linux\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nTo hack on Linux internals suitably, you'll need to write at least one operating\r\n system on your own.\r\n This is just like applications programming: to write a large application,\r\n you'll need to start with simple ones.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nYou will open pathways to various low-level programming domains such as\r\n reverse engineering, exploits, building virtual machines, game console\r\n emulation and more.\r\n Assembly language will become one of your most indispensable tools for\r\n low-level analysis.\r\n (But that does not mean you have to write your operating system in Assembly!)\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nWriting an operating system is fun!\r\n\\end_layout\r\n\r\n\\begin_layout Section*\r\n\r\n\\emph on\r\nWhy another book on Operating Systems?\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThere are many books and courses on this topic made by famous professors\r\n and experts out there already.\r\n Who am I to write a book on such an advanced topic? While it's true that\r\n many quality resources exist, I find them lacking.\r\n Do any of them show you how to compile your C code and the C runtime library\r\n independent of an existing operating system? Most books on operating system\r\n design and implementation only discuss the software side; how the operating\r\n system communicates with the hardware is skipped.\r\n Important hardware details are skipped, and it's difficult for a self-learner\r\n to find relevant resources on the Internet.\r\n The aim of this book is to bridge that gap: not only will you learn how\r\n to program hardware directly, but also how to read official documents from\r\n hardware vendors to program it.\r\n You no longer have to seek out resources to help yourself interpret hardware\r\n manuals and documentation: you can do it yourself.\r\n Lastly, I wrote this book from an autodidact's perspective.\r\n I made this book as self-contained as possible so you can spend more time\r\n learning and less time guessing or seeking out information on the Internet.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOne of the core focuses of this book is to guide you through the process\r\n of reading official documentation from vendors to implement your software.\r\n Official documents from hardware vendors like Intel are critical for implementi\r\nng an operating system or any other software that directly controls the\r\n hardware.\r\n At a minimum, an operating system developer needs to be able to comprehend\r\n these documents and implement software based on a set of hardware requirements.\r\n Thus, the first chapter is dedicated to discussing relevant documents and\r\n their importance.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAnother distinct feature of this book is that it is \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nHello World\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n centric.\r\n Most examples revolve around variants of a \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nHello World\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n program, which will acquaint you with core concepts.\r\n These concepts must be learned before attempting to write an operating\r\n system.\r\n Anything beyond a simple \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nHello World\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n example gets in the way of teaching the concepts, thus lengthening the\r\n time spent on getting started writing an operating system.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLet's dive in.\r\n With this book, I hope to provide enough foundational knowledge that will\r\n open doors for you to make sense of other resources.\r\n This book will be beneficial to students who've just finished their first\r\n C/C++ course greatly.\r\n Imagine how cool it would be to show prospective employers that you've\r\n already built an operating system.\r\n\\end_layout\r\n\r\n\\begin_layout Section*\r\n\r\n\\emph on\r\nPrerequisites\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nBasic knowledge of circuits\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Itemize\r\nBasic Concepts of Electricity: atoms, electrons, proton, neutron, current\r\n flow.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nOhm's law\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf you are unfamiliar with these concepts, you can quickly learn them here:\r\n \r\n\\begin_inset Flex URL\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttp://www.allaboutcircuits.com/textbook/\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, by reading chapter 1 and chapter 2.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nC programming.\r\n In particular:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Itemize\r\nVariable and function declarations/definitions\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nWhile and for loops\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nPointers and function pointers\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nFundamental algorithms and data structures in C\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nLinux basics: \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Itemize\r\nKnow how to navigate directory with the command line\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nKnow how to invoke a command with options\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nKnow how to pipe output to another program\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nTouch typing.\r\n Since we are going to use Linux, touch typing helps.\r\n I know typing speed does not relate to problem-solving, but at least your\r\n typing speed should be fast enough not to let it get in the way and degrade\r\n the learning experience.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn general, I assume that the reader has basic C programming knowledge,\r\n and can use an IDE to build and run a program.\r\n\\end_layout\r\n\r\n\\begin_layout Section*\r\n\r\n\\emph on\r\nWhat you will learn in this book \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nHow to write an operating system from scratch by reading hardware datasheets.\r\n In the real world, you will not be able to consult Google for a quick answer.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nWrite code independently.\r\n It's pointless to copy and paste code.\r\n Real learning happens when you solve problems on your own.\r\n Some examples are provided to help kick start your work, but most problems\r\n are yours to conquer.\r\n However, the solutions are available online for you after giving a good\r\n try.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nA big picture of how each layer of a computer related to each other, from\r\n hardware to software.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nHow to use Linux as a development environment and common tools for low-level\r\n programming.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nHow a program is structured so that an operating system can run.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nHow to debug a program running directly on hardware with \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n and QEMU.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nLinking and loading on bare metal x86_64, with pure C.\r\n No standard library.\r\n No runtime overhead.\r\n\\end_layout\r\n\r\n\\begin_layout Section*\r\n\r\n\\emph on\r\nWhat this book is not about\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex NewThought\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nElectrical Engineering\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n: The book discusses some concepts from electronics and electrical engineering\r\n only to the extent of how software operates on bare metal.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nHow to use Linux or any OS types of books\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n: Though Linux is used as a development environment and as a medium to demonstra\r\nte high-level operating system concepts, it is not the focus of this book.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nLinux Kernel development\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n: There are already many high-quality books out there on this subject.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nOperating system books focused on algorithms\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n: This book focuses more on actual hardware platform - Intel x86_64 - and\r\n how to write an OS that utilizes of OS support from the hardware platform.\r\n\\end_layout\r\n\r\n\\begin_layout Section*\r\nThe organization of the book\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nPart\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n1 provides a foundation for learning operating system.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Itemize\r\nChapter 1 briefly explains the importance of domain documents.\r\n Documents are crucial for the learning experience, so they deserve a chapter.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChapter 2 explains the layers of abstractions from hardware to software.\r\n The idea is to provide insight into how code runs physically.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChapter 3 provides the general architecture of a computer, then introduces\r\n a sample computer model that you will use to write an operating system.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChapter 4 introduces the x86 assembly language through the use of the Intel\r\n manuals, along with commonly used instructions.\r\n This chapter gives detailed examples of how high-level syntax corresponds\r\n to low-level assembly, enabling you to read generated assembly code comfortably.\r\n It is necessary to read assembly code when debugging an operating system.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChapter 5 dissects ELF in detail.\r\n Only by understanding how the structure of a program at the binary level,\r\n you can build one that runs on bare metal.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChapter 6 introduces \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n debugger with extensive examples for commonly used commands.\r\n After acquainting the reader with \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n, it then provides insight on how a debugger works.\r\n This knowledge is essential for building a debuggable program on the bare\r\n metal.\r\n \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nPart\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n2 presents how to write a bootloader to bootstrap a kernel.\r\n Hence the name \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nGroundwork\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n.\r\n After mastering this part, the reader can continue with the next part,\r\n which is a guide for writing an operating system.\r\n However, if the reader does not like the presentation, he or she can look\r\n elsewhere, such as OSDev Wiki: \r\n\\begin_inset Flex URL\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttp://wiki.osdev.org/\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Itemize\r\nChapter 7 introduces what the bootloader is, how to write one in assembly,\r\n and how to load it on QEMU, a hardware emulator.\r\n This process involves typing repetitive and long commands, so GNU Make\r\n is applied to improve productivity by automating the repetitive parts and\r\n simplifying the interaction with the project.\r\n This chapter also demonstrates the use of GNU Make in context.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChapter 8 introduces linking by explaining the relocation process when combining\r\n object files.\r\n In addition to a bootloader and an operating system written in C, this\r\n is the last piece of the puzzle required for building debuggable programs\r\n on bare metal, including the bootloader written in Assembly and an operating\r\n system written in C.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nPart\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n3 provides guidance on how to write an operating system, as you should implement\r\n an operating system on your own and be proud of your creation.\r\n The guidance consists of simpler and coherent explanations of necessary\r\n concepts, from hardware to software, to implement the features of an operating\r\n system.\r\n Without such guidance, you will waste time gathering information spread\r\n through various documents and the Internet.\r\n It then provides a plan on how to map the concepts to code.\r\n\\end_layout\r\n\r\n\\begin_layout Section*\r\n\r\n\\emph on\r\nAcknowledgments\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThank you, my beloved family.\r\n Thank you, the contributors.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nmainmatter\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nrenewcommand{\r\n\\backslash\r\nthepage}{\r\n\\backslash\r\narabic{page}}% Arabic numerals for page counter\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsetcounter{page}{1}% Start page number with 2\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Part\r\nPreliminary\r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nDomain documents\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nProblem domains\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the real world, software engineering is not only focused on software,\r\n but also the problem domain it is trying to solve.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Quote\r\nA \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nproblem domain\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nproblem domain\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nproblem domain\r\n\\emph default\r\n is \r\n\\emph on\r\nthe part of the world\r\n\\emph default\r\n where the computer is to produce effects, together with the means available\r\n to produce them, directly or indirectly.\r\n \r\n\\begin_inset CommandInset citation\r\nLatexCommand citep\r\nkey \"Kovitz_psr\"\r\nliteral \"true\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\nproblem domain\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nproblem domain\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is anything outside of programming that a software engineer needs to understand\r\n to produce correct code that can achieve the desired effects.\r\n \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nDirectly\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n means include anything that the software can control to produce the desired\r\n effects, e.g.\r\n keyboards, printers, monitors, other software, etc.\r\n \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nIndirectly\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n means anything not part of the software but relevant to the problem domain\r\n e.g.\r\n appropriate people to be informed by the software when some event happens,\r\n students that move to correct classrooms according to the schedule generated\r\n by the software.\r\n To write a finance application, a software engineer needs to learn sufficient\r\n finance concepts to understand the \r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nrequirements\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nrequirements\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nrequirements \r\n\\emph default\r\nof a customer and implement such requirements, correctly.\r\n\\end_layout\r\n\r\n\\begin_layout Quote\r\nRequirements are the effects that the machine is to exert in the problem\r\n domain by virtue of its programming.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nProgramming alone is not too complicated; programming to solve a problem\r\n domain, is \r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nWe refer to the concept of \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nprogramming\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n here as someone able to write code in a language, but not necessary know\r\n any or all software engineering knowledge.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n Not only a software engineer needs to understand how to implement the software,\r\n but also the problem domain that it tries to solve, which might require\r\n in-depth expert knowledge.\r\n The software engineer must also select the right programming techniques\r\n that apply to the problem domain he is trying to solve because many techniques\r\n that are effective in one domain might not be in another.\r\n For example, many types of applications do not require performant written\r\n code, but a short time to market.\r\n In this case, interpreted languages are widely popular because it can satisfy\r\n such need.\r\n However, for writing huge 3D games or operating system, compiled languages\r\n are dominant because it can generate the most efficient code required for\r\n such applications.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOften, it is too much for a software engineer to learn non-trivial domains\r\n (that might require a bachelor degree or above to understand the domains).\r\n Also, it is easier for a \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\ndomain expert\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\ndomain expert\r\n\\emph default\r\n to learn enough programming to break down the problem domain into parts\r\n small enough for the software engineers to implement.\r\n Sometimes, domain experts implement the software themselves.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nProblem domains: Software and Non-software.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/01/domains_general.pdf\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOne example of such scenario is the domain that is presented in this book:\r\n \r\n\\emph on\r\noperating system\r\n\\emph default\r\n.\r\n A certain amount of electrical engineering (EE) knowledge is required to\r\n implement an operating system.\r\n If a computer science (CS) curriculum does not include minimum EE courses,\r\n students in the curriculum have little chance to implement a working operating\r\n system.\r\n Even if they can implement one, either they need to invest a significant\r\n amount of time to study on their own, or they fill code in a predefined\r\n framework just to understand high-level algorithms.\r\n For that reason, EE students have an easier time to implement an OS, as\r\n they only need to study a few core CS courses.\r\n In fact, only \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nC programming\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n and \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nAlgorithms and Data Structures\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n classes are usually enough to get them started writing code for device\r\n drivers, and later generalize it into an \r\n\\emph on\r\noperating system.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nOperating System domain.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/01/domains_os_example.pdf\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOne thing to note is that software is its own problem domain.\r\n A problem domain does not necessarily divide between software and itself.\r\n Compilers, 3D graphics, games, cryptography, artificial intelligence, etc.,\r\n are parts of software engineering domains (actually it is more of a computer\r\n science domain than a software engineering domain).\r\n In general, a software-exclusive domain creates software to be used by\r\n other software.\r\n Operating System is also a domain, but is overlapped with other domains\r\n such as electrical engineering.\r\n To effectively implement an operating system, it is required to learn enough\r\n of the external domain.\r\n How much learning is enough for a software engineer? At the minimum, a\r\n software engineer should be knowledgeable enough to understand the documents\r\n prepared by hardware engineers for using (i.e.\r\n programming) their devices.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLearning a programming language, even C or Assembly, does not mean a software\r\n engineer can automatically be good at hardware programming or any related\r\n low-level programming domains.\r\n One can spend 10 years, 20 years or his entire life writing C/C++ code,\r\n and he still cannot write an operating system, simply because of the ignorance\r\n of relevant domain knowledge.\r\n Just like learning English does not mean a person automatically becomes\r\n good at reading Math books written in English.\r\n Much more than that is needed.\r\n Knowing one or two programming languages is not enough.\r\n If a programmer writes software for a living, he had better be specialized\r\n in one or two problem domains outside of software if he does not want his\r\n job taken by domain experts who learn programming in their spare time.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nDocuments for implementing a problem domain\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nDocuments are essential for learning a problem domain (and actually, anything)\r\n since information can be passed down in a reliable way.\r\n It is evident that this written text has been used for thousands of years\r\n to pass knowledge from generation to generation.\r\n Documents are integral parts of non-trivial projects.\r\n Without the documents:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nNew people will find it much harder to join a project.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nIt is harder to maintain a project because people may forget important unresolve\r\nd bugs or quirks in their system.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nIt is challenging for customers to understand the product they are going\r\n to use.\r\n However, documents do not need to be written in book format.\r\n It can be anything from HTML format to database format to be displayed\r\n by a graphical user interface.\r\n Important information must be stored somewhere safe, readily accessible.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThere are many types of documents.\r\n However, to facilitate the understanding of a problem domain, these two\r\n documents need to be written: \r\n\\emph on\r\nsoftware requirement document\r\n\\emph default\r\n and \r\n\\emph on\r\nsoftware specification\r\n\\emph default\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nSoftware Requirement Document\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSoftware requirement document\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nSoftware requirement\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nSoftware requirement document\r\n\\emph default\r\n includes both a list of requirements and a description of the problem domain\r\n \r\n\\begin_inset CommandInset citation\r\nLatexCommand citep\r\nkey \"Kovitz_psr\"\r\nliteral \"true\"\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA software solves a business problem.\r\n But, which problems to solve, are requested by a customer.\r\n Many of these requests make a list of requirements that our software needs\r\n to fulfill.\r\n However, an enumerated list of features is seldom useful in delivering\r\n software.\r\n As stated in the previous section, the tricky part is not programming alone\r\n but programming according to a problem domain.\r\n The bulk of software design and implementation depends upon the knowledge\r\n of the problem domain.\r\n The better understood the domain, the higher quality software can be.\r\n For example, building a house is practiced over thousands of years and\r\n is well understood, and it is easy to build a high-quality house; software\r\n is no different.\r\n Code that is difficult to understand is usually due to the author's ignorance\r\n of a problem domain.\r\n In the context of this book, we seek to understand the low-level working\r\n of various hardware devices.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBecause software quality depends upon the understanding of the problem domain,\r\n the amount of software requirement document should consist of problem domain\r\n description.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBe aware that software requirements are not:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nWhat\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nvs\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nHow\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nwhat\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n and \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nhow\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n are vague terms.\r\n What is the \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nwhat\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n? Is it nouns only? If so, what if a customer requires his software to perform\r\n specific steps of operations, such as purchasing procedure for a customer\r\n on a website.\r\n Does it include \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nverbs\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n now? However, isn't the \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nhow\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n supposed to be step by step operations? Anything can be the \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nwhat\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n and anything can be the \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nhow\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n.\r\n \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nSketches\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nSoftware requirement document is all about the problem domain.\r\n It should not be a high-level description of an implementation.\r\n Some problems might seem straightforward to map directly from its domain\r\n description to the structure of an implementation.\r\n For example:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nUsers are given a list of books in a \r\n\\series bold\r\n\\emph on\r\ndrop-down menu\r\n\\series default\r\n\\emph default\r\n to choose.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nBooks are stored in a \r\n\\series bold\r\n\\emph on\r\nlinked list\r\n\\series default\r\n\\emph default\r\n\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\netc\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the future, instead of a drop-down menu, all books are listed directly\r\n on a page in thumbnails.\r\n Books might be reimplemented as a graph, and each node is a book for finding\r\n related books, as a recommender is going to be added in the next version.\r\n The requirement document needs updating again to remove all the outdated\r\n implementation details, thus required additional efforts to maintain the\r\n requirement document, and when the effort for syncing with the implementation\r\n is too much, the developers give up documentation, and everyone starts\r\n ranting how useless documentation is.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nMore often than not there is no straightforward one-to-one mapping.\r\n For example, a regular computer user expects an OS to be something that\r\n runs some program with GUI, or their favorite computer games.\r\n But for such requirements, an operating system is implemented as multiple\r\n layers, each hiding the details from the upper layers.\r\n To implement an operating system, a large body of knowledge from multiple\r\n fields is required, especially if the operating system runs on non-PC devices.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIt's best to include information related to the problem domain in the requiremen\r\nt document.\r\n A good way to test the quality of a requirement document is to provide\r\n it to a domain expert for proofreading, to ensure he can understand the\r\n material thoroughly.\r\n A requirement document is also useful as a help document later, or for\r\n writing one much easier.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nSoftware Specification\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSoftware specification\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nSoftware specification\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\nSoftware specification\r\n\\emph default\r\n document states rules relating desired behavior of the output devices to\r\n all possible behavior of the input devices, as well as any rules that other\r\n parts of the problem domain must obey.\r\n\\begin_inset CommandInset citation\r\nLatexCommand cite\r\nkey \"Kovitz_psr\"\r\nliteral \"true\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimply put, software specification is interface design, with constraints\r\n for the problem domain to follow e.g.\r\n the software can accept certain types of input such as the software is\r\n designed to accept English but no other language.\r\n For a hardware device, a specification is always needed, as software depends\r\n on its hardwired behaviors.\r\n And in fact, it is mostly the case that hardware specifications are well-define\r\nd, with the tiniest details in it.\r\n It needs to be that way because once hardware is physically manufactured,\r\n there's no going back, and if defects exist, it's a devastating damage\r\n to the company on both finance and reputation.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNote that, similar to a requirement document, a specification only concerns\r\n interface design.\r\n If implementation details leak in, it is a burden to sync between the actual\r\n implementation and the specification, and soon to be abandoned.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAnother important remark is that, though a specification document is important,\r\n it does not have to be produced \r\n\\emph on\r\nbefore\r\n\\emph default\r\n the implementation.\r\n It can be prepared in any order: before or after a complete implementation;\r\n or at the same time with the implementation, when some part is done, and\r\n the interface is ready to be recorded in the specification.\r\n Regardless of methods, what matter is a complete specification at the end.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nDocuments for writing an x86 Operating System\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen problem domain is different from software domain, requirement document\r\n and specification are usually separated.\r\n However, if the problem domain is inside software, specification most often\r\n includes both, and content of both can be mixed with each other.\r\n As demonstrated by previous sections the importance of documents, to implement\r\n an OS, we will need to collect relevant documents to gain sufficient domain\r\n knowledge.\r\n These documents are as follow:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nIntel® 64 and IA-32 Architectures Software Developer’s Manual (Volume 1,\r\n 2, 3)\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nIntel® 3 Series Express Chipset Family Datasheet\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nSystem V Application Binary Interface\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAside from the Intel's official website, the website of this book also hosts\r\n the documents for convenience\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nIntel may change the links to the documents as they update their website,\r\n so this book doesn't contain any link to the documents to avoid confusion\r\n for readers.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIntel documents divide the requirement and specification sections clearly,\r\n but call the sections with different names.\r\n The corresponding to the requirement document is a section called \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nFunctional Description\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n, which consists mostly of domain description; for specification, \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nRegister Description\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n section describes all programming interfaces.\r\n Both documents carry no unnecessary implementation details\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nAs it should be, those details are trade secret.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n Intel documents are also great examples of how to write well requirements/speci\r\nfications, as explained in this chapter.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOther than the Intel documents, other documents will be introduced in the\r\n relevant chapters.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nchapter[From hardware to software: Layers of abstraction]{From hardware\r\n to software: \r\n\\backslash\r\n\r\n\\backslash\r\n Layers of abstraction}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis chapter gives an intuition on how hardware and software connected together,\r\n and how software is represented physically.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nThe physical implementation of a bit\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAll electronic devices, from simple to complex, manipulate this flow to\r\n achieve desired effects in the real world.\r\n Computers are no exception.\r\n When we write software, we indirectly manipulate electrical current at\r\n the physical level, in such a way that the underlying machine produces\r\n desired effects.\r\n To understand the process, we consider a simple light bulb.\r\n A light bulb can change two states between on and off with a switch, periodical\r\nly: an off means number 0, and an on means 1.\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nA lightbulb\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/02/bulb.svg\r\n\tscale 15\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHowever, one problem is that such a switch requires manual intervention\r\n from a human.\r\n What is required is an automatic switch based on the voltage level, as\r\n described above.\r\n To enable automatic switching of electrical signals, a device called \r\n\\emph on\r\ntransistor\r\n\\emph default\r\n, invented by William Shockley, John Bardeen and Walter Brattain.\r\n This invention started the whole computer industry.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAt the core, a \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\ntransistor\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\ntransistor\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\ntransistor\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is just a resistor whose values can vary based on an input voltage value.\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nModern transistor\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/02/transistor.svg\r\n\tscale 25\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n With this property, a transistor can be used as a current amplifier (more\r\n voltage, less resistance) or switch electrical signals off and on (block\r\n and unblock an electron flow) based on a voltage level.\r\n At 0\r\n\\begin_inset space \\thinspace{}\r\n\\end_inset\r\n\r\nv, no current can pass through a transistor, thus it acts like a circuit\r\n with an open switch (light bulb off) because the resistor value is enough\r\n to block the electrical flow.\r\n Similarly, at +3.5\r\n\\begin_inset space \\thinspace{}\r\n\\end_inset\r\n\r\nv, current can flow through a transistor because the resistor value is lessened,\r\n effectively enables electron flow, thus acts like a circuit with a closed\r\n switch.\r\n\\begin_inset Marginal\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nIf you want a deeper explanation of transistors e.g.\r\n how electrons move, you should look at the video \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nHow semiconductors work\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n on Youtube, by Ben Eater.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA bit has two states: 0 and 1, which is the building block of all digital\r\n systems and software.\r\n Similar to a light bulb that can be turned on and off, bits are made out\r\n of this electrical stream from the power source: Bit 0 are represented\r\n with 0\r\n\\begin_inset space \\thinspace{}\r\n\\end_inset\r\n\r\nv (no electron flow), and bit 1 is +3.5\r\n\\begin_inset space \\thinspace{}\r\n\\end_inset\r\n\r\nv to +5\r\n\\begin_inset space \\thinspace{}\r\n\\end_inset\r\n\r\nv (electron flow).\r\n Transistor implements a bit correctly, as it can regulate the electron\r\n flow based on voltage level.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nMOSFET transistors\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe classic transistors invented open a whole new world of micro digital\r\n devices.\r\n Prior to the invention, vacuum tubes - which are just fancier light bulbs\r\n - were used to present 0 and 1, and required human to turn it on and off.\r\n \r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nMOSFET\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMOSFET\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nMOSFET\r\n\\emph default\r\n, or \r\n\\series bold\r\n\\emph on\r\nM\r\n\\series default\r\netal–\r\n\\series bold\r\nO\r\n\\series default\r\nxide–\r\n\\series bold\r\nS\r\n\\series default\r\nemiconductor \r\n\\series bold\r\nF\r\n\\series default\r\nield-\r\n\\series bold\r\nE\r\n\\series default\r\nffect \r\n\\series bold\r\nT\r\n\\series default\r\nransistor\r\n\\emph default\r\n, invented in 1959 by Dawon Kahng and Martin M.\r\n (John) Atalla at Bell Labs, is an improved version of classic transistors\r\n that is more suitable for digital devices, as it requires shorter switching\r\n time between two states 0 and 1, more stable, consumes less power and easier\r\n to produce.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThere are also two types of MOSFETs analogous to two types of transistors:\r\n n-MOSFET and p-MOSFET.\r\n n-MOSFET and p-MOSFET are also called NMOS and PMOS transistors for short.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nBeyond transistors: digital logic gates\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAll digital devices are designed with logic gates.\r\n A \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nlogic gate\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nlogic gate\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\nlogic gate\r\n\\emph default\r\n is a device that implements a boolean function.\r\n Each logic gate includes a number of inputs and an output.\r\n All computer operations are built from the combinations of logic gates,\r\n which are just combinations of boolean functions.\r\n \r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nExample: NAND gate\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/02/Nand-gate.svg\r\n\tscale 30\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nThe theory behind logic gates\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLogic gates accept only binary inputs\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nInput that is either a 0 or 1.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n and produce binary outputs.\r\n In other words, logic gates are functions that transform binary values.\r\n Fortunately, a branch of math that deals exclusively with binary values\r\n already existed, called \r\n\\emph on\r\nBoolean Algebra\r\n\\emph default\r\n, developed in the 19\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nth\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\ncentury by George Boole.\r\n With a sound mathematical theory as a foundation logic gates were created\r\n\\emph on\r\n.\r\n \r\n\\emph default\r\nAs logic gates implement Boolean functions, a set of Boolean functions is\r\n \r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nfunctionally complete\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nfunctionally complete\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\nfunctionally complete\r\n\\emph default\r\n, if this set can construct all other Boolean functions can be constructed\r\n from.\r\n Later, Charles Sanders Peirce (during 1880 – 1881) proved that either Boolean\r\n function of NOR or NAND alone is enough to create all other Boolean logic\r\n functions.\r\n Thus NOR and NAND gates are functionally complete \r\n\\begin_inset CommandInset citation\r\nLatexCommand cite\r\nkey \"Peirce\"\r\nliteral \"true\"\r\n\r\n\\end_inset\r\n\r\n.\r\n Gates are simply the implementations of Boolean logic functions, therefore\r\n NAND or NOR gate is enough to implement \r\n\\series bold\r\n\\emph on\r\nall\r\n\\series default\r\n\\emph default\r\n other logic gates.\r\n The simplest gates CMOS circuit can implement are inverters (NOT gates)\r\n and from the inverters, comes NAND gates.\r\n With NAND gates, we are confident to implement everything else.\r\n This is why the inventions of transistors, then CMOS circuit revolutionized\r\n computer industry.\r\n\\begin_inset Marginal\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nIf you want to understand why and how from NAND gate we can create all Boolean\r\n functions and a computer, I suggest the course \r\n\\emph on\r\nBuild a Modern Computer from First Principles: From Nand to Tetris \r\n\\emph default\r\navailable on Coursera: \r\n\\begin_inset Flex URL\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttps://www.coursera.org/learn/build-a-computer\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n Go even further, after the course, you should take the series \r\n\\emph on\r\nComputational Structures\r\n\\emph default\r\n on Edx.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe should realize and appreciate how powerful boolean functions are available\r\n in all programming languages.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nLogic Gate implementation: CMOS circuit\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nUnderlying every logic gate is a circuit called \r\n\\series bold\r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nCMOS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nCMOS\r\n\\series default\r\n\\emph default\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\emph on\r\nCMOS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n - \r\n\\series bold\r\n\\emph on\r\nC\r\n\\series default\r\nomplementary \r\n\\series bold\r\nMOS\r\n\\series default\r\nFET\r\n\\emph default\r\n.\r\n CMOS consists of two complementary transistors, \r\n\\emph on\r\nNMOS\r\n\\emph default\r\n and \r\n\\emph on\r\nPMOS.\r\n \r\n\\emph default\r\nThe simplest CMOS circuit is an inverter or a \r\n\\emph on\r\nNOT\r\n\\emph default\r\n gate:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset VSpace vfill\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Newpage pagebreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure} \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Electron flows of an inverter.\r\n Input is on the left side and output on the right side.\r\n The upper component is a PMOS and the lower component is a NMOS, both connect\r\n to the input and output.\r\n (Source: Created with \r\n\\backslash\r\nurl{http://www.falstad.com/circuit/})}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[When input is low]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.6]{images/02/inverter-0}}\r\n\\backslash\r\nhfill{}\r\n\\backslash\r\nsubfloat[When input is high]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.6]{images/02/inverter-1}}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure} \r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFrom NOT gate, a NAND gate can be created:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure} \r\n\\backslash\r\ncaption{Electron flows of a NAND gate.}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[Input = 00, Ouput = 1]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.52]{images/02/nand-00}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[Input = 01, Ouput = 1]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.52]{images/02/nand-01}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[Input = 10, Output = 1]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.52]{images/02/nand-10}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[Input = 11, Output = 0]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.52]{images/02/nand-11}}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure} \r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFrom NAND gate, we have all other gates.\r\n As demonstrated, such a simple circuitry performs the logical operators\r\n in day-to-day program languages e.g.\r\n NOT operator \r\n\\family typewriter\r\n~\r\n\\family default\r\n is executed directly by an inverter circuit, and operator \r\n\\family typewriter\r\n&\r\n\\family default\r\n is executed by an AND circuit and so on.\r\n Code does not run on a magic black box.\r\n In contrast, code execution is precise and transparent, often as simple\r\n as running some hardwired circuit.\r\n When we write software, we simply manipulate electrical current at the\r\n physical level to run appropriate circuits to produce desired outcomes.\r\n However, this whole process somehow does not relate to any thought involving\r\n electrical current.\r\n That is the real magic and will be explained soon.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOne interesting property of CMOS is that \r\n\\series bold\r\n\\emph on\r\na k-input gate uses k PMOS and k NMOS transistors\r\n\\series default\r\n\\emph default\r\n \r\n\\begin_inset CommandInset citation\r\nLatexCommand citep\r\nkey \"John_digital\"\r\nliteral \"true\"\r\n\r\n\\end_inset\r\n\r\n.\r\n All logic gates are built by pairs of NMOS and PMOS transistors, and gates\r\n are the building blocks of all digital devices from simple to complex,\r\n including any computer.\r\n Thanks to this pattern, it is possible to separate between the actual physical\r\n circuit implementation and logical implementation.\r\n Digital designs are done by designing with logic gates then later be \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\ncompiled\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n into physical circuits.\r\n In fact, later we will see that logic gates become a language that describes\r\n how circuits operate.\r\n Understanding how CMOS works is important to understand how a computer\r\n is designed, and as a consequence, how a computer works\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nAgain, if you want to understand how logic gates make a computer, consider\r\n the suggested courses on Coursera and Edx earlier.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, an implemented circuit with its wires and transistors is stored\r\n physically in a package called a \r\n\\emph on\r\nchip\r\n\\emph default\r\n.\r\n A \r\n\\emph on\r\nchip\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nchip\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a substrate that an integrated circuit is etched onto.\r\n However, a chip also refers to a completely packaged integrated circuit\r\n in consumer market.\r\n Depends on the context, it is understood differently.\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\n74HC00 chip physical view\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/02/74hc00_nxp_physical.jpg\r\n\tscale 60\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"-4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n74HC00 is a chip with four 2-input NAND gates.\r\n The chip comes with 8 input pins and 4 output pins, 1 pin for connecting\r\n to a voltage source and 1 pin for connecting to the ground.\r\n This device is the physical implementation of NAND gates that we can physically\r\n touch and use.\r\n But instead of just a single gate, the chip comes with 4 gates that can\r\n be combined.\r\n Each combination enables a different logic function, effective creating\r\n other logic gates.\r\n This feature is what make the chip popular.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\n74HC00 logic diagrams (Source: 74HC00 datasheet, \r\n\\begin_inset Flex URL\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttp://www.scrpdf.com/pdf/Semiconductors_new/Logic/74HCT/74HC_HCT00.pdf\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nLogic diagram of 74HC00\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/02/7400_block_diagram.png\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nLogic diagram of one NAND gate\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/02/7400_logic_diagram.png\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEach of the gates above is just a simple NAND circuit with the electron\r\n flows, as demonstrated earlier.\r\n Yet, many these NAND-gates chips combined can build a simple computer.\r\n Software, at the physical level, is just electron flows.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure} \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Gates built from NAND gates, each accepts 2 input signals and generate\r\n 1 output signal.}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[NOT gate]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.5]{images/02/not-gate}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[AND gate]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.5]{images/02/and-gate}}\r\n\\backslash\r\nhfill{}\r\n\\backslash\r\n\r\n\\backslash\r\n[0.5cm]\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[OR gate]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.5]{images/02/or-gate}}\r\n\\backslash\r\nqquad\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[NOR gate]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.5]{images/02/nor-gate}} \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHow can the above gates be created with 74HC00? It is simple: as every gate\r\n has 2 input pins and 1 output pin, we can write the output of 1 NAND gate\r\n to an input of another NAND gate, thus chaining NAND gates together to\r\n produce the diagrams as above.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nBeyond Logic Gates: Machine Language\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nMachine language\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBeing built upon gates, as gates only accept a series of 0 and 1, a hardware\r\n device only understands 0 and 1.\r\n However, a device only takes 0 and 1 in a systematic way.\r\n \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nMachine language\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nMachine language\r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nMachine language\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a collection of unique bit patterns that a device can identify and perform\r\n a corresponding action.\r\n A \r\n\\emph on\r\nmachine instruction\r\n\\emph default\r\n is a unique bit pattern that a device can identify.\r\n In a computer system, a device with its language is called \r\n\\series bold\r\n\\emph on\r\nCPU\r\n\\series default\r\n - \r\n\\series bold\r\nC\r\n\\series default\r\nentral \r\n\\series bold\r\nP\r\n\\series default\r\nrocessing \r\n\\series bold\r\nU\r\n\\series default\r\nnit\r\n\\emph default\r\n, which controls all activities going inside a computer.\r\n For example, in the x86 architecture, the pattern \r\n\\family typewriter\r\n10100000\r\n\\family default\r\n means telling a CPU to add two numbers, or \r\n\\family typewriter\r\n000000101\r\n\\family default\r\n to halt a computer.\r\n In the early days of computers, people had to write completely in binary.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhy does such a bit pattern cause a device to do something? The reason is\r\n that underlying each instruction is a small circuit that implements the\r\n instruction.\r\n Similar to how a function/subroutine in a computer program is called by\r\n its name, a bit pattern is a name of a little function inside a CPU that\r\n got executed when the CPU finds one.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNote that CPU is not the only device with its language.\r\n CPU is just a name to indicate a hardware device that controls a computer\r\n system.\r\n A hardware device may not be a CPU but still has its language.\r\n A device with its own machine language is a \r\n\\emph on\r\nprogrammable device\r\n\\emph default\r\n, since a user can use the language to command the device to perform different\r\n actions.\r\n For example, a printer has its set of commands for instructing it how to\r\n print a page.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"-4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"exa:74HC00-chip-can\"\r\n\r\n\\end_inset\r\n\r\nA user can use 74HC00 chip without knowing its internal, but only the interface\r\n for using the device.\r\n First, we need to know its layout:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\n74HC00 Pin Layout (Source: 74HC00 datasheet, \r\n\\begin_inset Flex URL\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttp://www.nxp.com/documents/data_sheet/74HC_HCT00.pdf\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/02/7400_pin_configuration.pdf\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, the functionality of each pin:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float table\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nPin Description (Source: 74HC00 datasheet, \r\n\\begin_inset Flex URL\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttp://www.nxp.com/documents/data_sheet/74HC_HCT00.pdf\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"6\" columns=\"3\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3cm\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nSymbol\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nPin\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1A to 4A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1, 4, 9, 12 \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\ndata input\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1B to 4B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n2, 5, 10, 13\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\ndata input\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1Y to 4Y\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n3, 6, 8, 11\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\ndata output\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nGND\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nground (0\r\n\\begin_inset space \\thinspace{}\r\n\\end_inset\r\n\r\nV)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nV\r\n\\begin_inset script subscript\r\n\r\n\\begin_layout Plain Layout\r\ncc\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset script subscript\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n14\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nsupply voltage\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, how to use the pins:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float table\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nFunctional Description\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"6\" columns=\"3\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3cm\">\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nInput\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nOutput\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nnA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nnB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nnY\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Marginal\r\nstatus collapsed\r\n\r\n\\begin_layout Itemize\r\nn is a number, either 1, 2, 3, or 4\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nH = HIGH voltage level; L = LOW voltage level; X = don’t care.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nThe functional description provides a truth table with all possible pin\r\n inputs and outputs, which also describes the usage of all pins in the device.\r\n A user needs not to know the implementation, but on such a table to use\r\n the device.\r\n We can say that the truth table above is the machine language of the device.\r\n Since the device is digital, its language is a collection of binary strings:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe device has 8 input pins, and this means it accepts binary strings of\r\n 8 bits.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe device has 4 output pins, and this means it produces binary strings\r\n of 4 bits from the 8-bit inputs.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe number of input strings is what the device understand, and the number\r\n of output strings is what the device can speak.\r\n Together, they make the language of the device.\r\n Even though this device is simple, yet the language it can accept contains\r\n quite many binary strings: \r\n\\begin_inset Formula $2^{8}+2^{4}=272$\r\n\\end_inset\r\n\r\n.\r\n However, the number is a tiny fraction of a complex device like a CPU,\r\n with hundreds of pins.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen leaving as is, 74HC00 is simply a NAND device with two 4-bit inputs\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nOr simply 4-bit NAND gate, as it can only accept 4 bits of input at the\r\n maximum.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"3\" columns=\"13\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nInput\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nOutput\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nPin\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n1A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n1B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n2A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n2B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n3A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n3B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n4A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n4B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n1Y\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n2Y\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n3Y\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n4Y\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nValue\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset VSpace medskip\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe inputs and outputs as visually presented:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nPins when receiving digital signals that correspond to a binary string.\r\n Green signals are inputs; blue signals are outputs.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/02/7400_bin_string1.pdf\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOn the other hand, if OR gate is implemented, we can only build a 2-input\r\n OR gate from 74HC00, as it requires 3 NAND gates: 2 input NAND gates and\r\n 1 output NAND gate.\r\n Each input NAND gate represents only a 1-bit input of the OR gate.\r\n In the following figure, the pins of each input NAND gates are always set\r\n to the same values (either both inputs are A or both inputs are B) to represent\r\n a single bit input for the final OR gate:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{2-bit OR gate implementation}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[2-bit OR gate logic diagram, built from 3 NAND gates with 4 pins\r\n just\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nfor 2 bits of input.\r\n\\backslash\r\nnewline]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.6]{images/02/or-gate-ex}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[Pin 3A and 3B take the values from 1Y and 2Y.]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/02/or-gate-layout-ex}}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset VSpace bigskip\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Float margintable\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nTruth table of OR logic diagram.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"5\" columns=\"5\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nY\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo implement a 4-bit OR gate, we need a total of four of 74HC00 chips configured\r\n as OR gates, packaged as a single chip as in figure \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"or-chip-74hc00\"\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\n4-bit OR chip made from four 74HC00 devices\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"or-chip-74hc00\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/02/4-bit-or-gate-layout.pdf\r\n\tscale 41\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nAssembly Language\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAssembly language is the symbolic representation of binary machine code,\r\n by giving bit patterns mnemonic names.\r\n It was a vast improvement when programmers had to write 0 and 1.\r\n For example, instead of writing \r\n\\family typewriter\r\n000000101\r\n\\family default\r\n, a programmer simply write \r\n\\family typewriter\r\nhlt\r\n\\family default\r\n to stop a computer.\r\n Such an abstraction makes instructions executed by a CPU easier to remember,\r\n and thus more instructions could be memorized, less time spent looking\r\n up CPU manual to find instructions in bit forms and as a result, code was\r\n written faster.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nUnderstand assembly language is crucial for low-level programming domains,\r\n even to this day.\r\n The more instructions a programmer want to understand, the deeper understanding\r\n of machine architecture is required.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWe can build a device with 2 assembly instructions:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\nor   <op1>, <op2>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nnand <op1>, <op2>\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nor\r\n\\family default\r\n accepts two 4-bit operands.\r\n This corresponds to a 4-input OR gate device built from 4 74HC00 chips.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nnand\r\n\\family default\r\n accepts two 4-bit operands.\r\n This corresponds to a single 74HC00 chips, leave as is.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEssentially, the gates in the example \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"exa:74HC00-chip-can\"\r\n\r\n\\end_inset\r\n\r\n implements the instructions.\r\n Up to this point, we only specify input and output and manually feed it\r\n to a device.\r\n That is, to perform an operation:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nPick a device by hands.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nManually put electrical signals into pins.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFirst, we want to automate the process of device selection.\r\n That is, we want to simply write assembly instruction and the device that\r\n implements the instruction is selected correctly.\r\n Solving this problem is easy:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nGive each instruction an index in binary code, called \r\n\\emph on\r\noperation code\r\n\\emph default\r\n or \r\n\\emph on\r\nopcode\r\n\\emph default\r\n for short, and embed it as part of input.\r\n The value for each instruction is specified as in table \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"ex-ins-ops\"\r\n\r\n\\end_inset\r\n\r\n.\r\n\\begin_inset Float margintable\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nInstruction-Opcode mapping.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"ex-ins-ops\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\noindent\r\n\\align center\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"3\" columns=\"2\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nInstruction\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nBinary Code\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nnand\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nor\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nEach input now contains additional data at the beginning: an opcode.\r\n For example, the instruction:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\nnand\r\n\\color inherit\r\n 1100, 1100\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\ncorresponds to the binary string: \r\n\\family typewriter\r\n\\color red\r\n00\r\n\\color inherit\r\n11001100\r\n\\family default\r\n.\r\n The first two bits \r\n\\family typewriter\r\n\\color red\r\n00\r\n\\family default\r\n\\color inherit\r\n encodes a \r\n\\family typewriter\r\nnand\r\n\\family default\r\n instruction, as listed in the table above.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nAdd another device to select a device, based on a binary code peculiar to\r\n an instruction.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSuch a device is called a \r\n\\emph on\r\ndecoder\r\n\\emph default\r\n, an important component in a CPU that decides which circuit to use.\r\n In the above example, when feeding \r\n\\family typewriter\r\n\\color red\r\n00\r\n\\color inherit\r\n11001100\r\n\\family default\r\n to the decoder, because the opcode is \r\n\\family typewriter\r\n\\color red\r\n00\r\n\\family default\r\n\\color inherit\r\n, data are sent to NAND device for computing.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, writing assembly code is just an easier way to write binary strings\r\n that a device can understand.\r\n When we write assembly code and save in a text file, a program called an\r\n \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nassembler\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nassembler\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nassembler\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n translates the text file into binary strings that a device can understand.\r\n So, how can an assembler exist in the first place? Assume this is the first\r\n assembler in the world, then it is written in binary code.\r\n In the next version, life is easier: the programmers write the assembler\r\n in the assembly code, then use the first version to compile itself.\r\n These binary strings are then stored in another device that later can be\r\n retrieved and sent to a decoder.\r\n A\r\n\\emph on\r\n \r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nstorage device\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nstorage device\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nstorage device\r\n\\emph default\r\n is the device that stores machine instructions, which is an array of circuits\r\n for saving 0 and 1 states.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA decoder is built out of logic gates similar to other digital devices.\r\n However, a storage device can be anything that can store 0 and 1 and is\r\n retrievable.\r\n A storage device can be a magnetized device that uses magnetism to store\r\n information, or it can be made out of electrical circuits that can change\r\n and rermember states when a voltage is applied.\r\n Regardless of the technology used, as long as the device can store data\r\n and is accessible to retrieve data, it suffices.\r\n Indeed, the modern devices are so complex that it is impossible and unnecessary\r\n to understand every implementation detail.\r\n Instead, we only need to learn the interfaces, e.g.\r\n the pins, that the devices expose.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset VSpace vfill\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Newpage pagebreak\r\n\\end_inset\r\n\r\n\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{A decoder retrieves the current instruction pointed by the arrow\r\n and selects the NAND device to execute the \r\n\\backslash\r\ntexttt{nand} instruction.}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/02/decoder-ex}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA computer essentially implements this process: \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\emph on\r\nFetch\r\n\\emph default\r\n an instruction from a storage device.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\emph on\r\nDecode\r\n\\emph default\r\n the instruction.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\emph on\r\nExecute\r\n\\emph default\r\n the instruction.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOr in short, a \r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nf@fetch – decode – execute\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nfetch – decode – execute cycle.\r\n The above device is extremely rudimentary, but it already represents a\r\n computer with a \r\n\\emph on\r\nfetch\r\n\\emph default\r\n – \r\n\\emph on\r\ndecode\r\n\\emph default\r\n – \r\n\\emph on\r\nexecute\r\n\\emph default\r\n cycle.\r\n More instructions can be implemented by adding more devices and allocating\r\n more opcodes for the instructions, then update the decoder accordingly.\r\n The Apollo Guidance Computer, a digital computer produced for the Apollo\r\n space program from 1961 – 1972, was built entirely with NOR gates - the\r\n other choice to NAND gate for creating other logic gates.\r\n Similarly, if we keep improving our hypothetical device, it eventually\r\n becomes a full-fledge computer.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nProgramming Languages\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAssembly language is a step up from writing 0 and 1.\r\n As time goes by, people realized that many pieces of assembly code had\r\n repeating patterns of usages.\r\n It would be nice if instead of writing all the repeating blocks of code\r\n all over again in all places, we simply refer to such blocks of code with\r\n easier to use text forms.\r\n For example, a block of assembly code checks whether one variable is greater\r\n than another and if so, execute a block of code, else execute another block\r\n of code; in C, such block of assembly code is represented by an \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nif\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n statement that is close to human language.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nRepeated assembly patterns are generalized into a new language.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/02/asm_to_proglang.pdf\r\n\tscale 60\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nPeople created text forms to represent common blocks of assembly code, such\r\n as the \r\n\\family typewriter\r\nif\r\n\\family default\r\n syntax above, then write a program to translate the text forms into assembly\r\n code.\r\n The program that translates such text forms to machine code is called a\r\n \r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\ncompiler\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\ncompiler\r\n\\emph default\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\emph on\r\ncompiler\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{From high-level language back to low-level language.}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/02/proglang_to_asm}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAny software logic a programming language can implement, hardware can also\r\n implement.\r\n The reverse is also true: any hardware logic that is implemented in a circuit\r\n can be reimplemented in a programming language.\r\n The simple reason is that programming languages, or assembly languages,\r\n or machine languages, or logic gates are just languages to express computations.\r\n It is impossible for software to implement something hardware is incapable\r\n of because programming language is just a simpler way to use the underlying\r\n hardware.\r\n At the end of the day, programming languages are translated to machine\r\n instructions that are valid to a CPU.\r\n Otherwise, code is not runnable, thus a useless software.\r\n In reverse, software can do everything hardware (that run the software)\r\n can, as programming languages are just an easier way to use the hardware.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn reality, even though all languages are equivalent in power, not all of\r\n them are capable of express programs of each other.\r\n Programming languages vary between two ends of a spectrum: high level and\r\n low level.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe higher level a programming language is, the more distant it becomes\r\n from the hardware.\r\n In some high-level programming languages, such as Python, a programmer\r\n cannot manipulate underlying hardware, despite being able to deliver the\r\n same computations as low-level programming languages.\r\n The reason is that high-level languages want to hide hardware details to\r\n free programmers from dealing with irrelevant details not related to current\r\n problem domains.\r\n Such convenience, however, is not free: it requires software to carry an\r\n extra code for managing hardware details (e.g.\r\n memory) thus making the code run slower, and it makes hardware programming\r\n difficult or impossible.\r\n The more abstractions a programming language imposes, the more difficult\r\n it is for writing low-level software, such as hardware drivers or an operating\r\n system.\r\n This is the reason why C is usually a language of choice for writing an\r\n operating system, since C is just a thin wrapper of the underlying hardware,\r\n making it easy to understand how exactly a hardware device runs when executing\r\n a certain piece of C code.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEach programming language represents a way of thinking about programs.\r\n Higher-level programming languages help to focus on problem domains that\r\n are not related to hardware at all, and where programmer performance is\r\n more important than computer performance.\r\n Lower-level programming languages help to focus on the inner-working of\r\n a machine, thus are best suited for problem domains that are related to\r\n control hardware.\r\n That is why so many languages exist.\r\n Use the right tools for the right job to achieve the best results.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Note Note\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nExplain the two ways to create new abstractions in programming languages\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nAbstraction\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\nAbstraction\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nAbstraction\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\emph default\r\nis a technique for hiding complexity that is irrelevant to the problem in\r\n context.\r\n For example, writing programs without any other layer except the lowest\r\n layer: with circuits.\r\n Not only a person needs an in-depth understanding of how circuits work,\r\n making it much more obscure to design a circuit because the designer must\r\n look at the raw circuits but think in higher-level such as logic gates.\r\n It is a distracting process, as a designer must constantly translate the\r\n idea into circuits.\r\n It is possible for a designer simply thinks his high-level ideas straight,\r\n and later translate the ideas into circuits.\r\n Not only it is more efficient, but it is also more accurate as a designer\r\n can focus all his efforts into verifying the design with high-level thinking.\r\n When a new designer arrives, he can easily understand the high-level designs,\r\n thus can continue to develop or maintain existing systems.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nWhy abstraction works\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn all the layers, abstractions manifest itself:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nLogic gates abstract away the details of CMOS.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nMachine language abstracts away the details of logic gates.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nAssembly language abstracts away the details of machine languages.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nProgramming language abstracts away the details of assembly languages.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe see repeating patterns of how lower-layers build upper-layers:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nA lower layer has a recurring pattern.\r\n Then, this recurring pattern is taken out and built a language on top of\r\n it.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nA higher layer strips away layer-specific (non-recurring) details to focus\r\n on the recurring details.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe recurring details are given a new and simpler language than the languages\r\n of the lower layers.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhat to realize is that every layer is just\r\n\\emph on\r\n a more convenient language to \r\n\\series bold\r\ndescribe\r\n\\series default\r\n the lower layer\r\n\\emph default\r\n.\r\n Only after a description is fully created with the language of the higher\r\n layer, it is then be \r\n\\emph on\r\nimplemented\r\n\\emph default\r\n with the language of the lower layer.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nCMOS layer has a recurring pattern that makes sure logic gates are reliably\r\n translated to CMOS circuits: \r\n\\series bold\r\n\\emph on\r\na k-input gate uses k PMOS and k NMOS transistors\r\n\\series default\r\n\\emph default\r\n \r\n\\begin_inset CommandInset citation\r\nLatexCommand citep\r\nkey \"John_digital\"\r\nliteral \"true\"\r\n\r\n\\end_inset\r\n\r\n.\r\n Since digital devices use CMOS exclusively, a language arose to describe\r\n higher level ideas while hiding CMOS circuits: Logic Gates.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nLogic Gates hides the language of circuits and focuses on how to implement\r\n primitive Boolean functions and combine them to create new functions.\r\n All logic gates receive input and generate output as binary numbers.\r\n Thanks to this recurring patterns, logic gates are hidden away for the\r\n new language: Assembly, which is a set of predefined binary patterns that\r\n cause the underlying gates to perform an action.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nSoon, people realized that many recurring patterns arisen from within Assembly\r\n language.\r\n Repeated blocks of Assembly code appear in Assembly source files that express\r\n the same or similar idea.\r\n There were many such ideas that can be reliably translated into Assembly\r\n code.\r\n Thus, the ideas were extracted for building into the high level programming\r\n languages that everyone programmer learns today.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nRecurring patterns are the key to abstraction.\r\n Recurring patterns are why abstraction works.\r\n Without them, no language can be built, and thus no abstraction.\r\n Fortunately, human already developed a systematic discipline for studying\r\n patterns: Mathematics.\r\n As quoted from the British mathematician G.\r\n H.\r\n Hardy \r\n\\begin_inset CommandInset citation\r\nLatexCommand citeyearpar\r\nkey \"Hardy\"\r\nliteral \"true\"\r\n\r\n\\end_inset\r\n\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Quote\r\nA mathematician, like a painter or a poet, is a maker of patterns.\r\n If his patterns are more permanent than theirs, it is because they are\r\n made with ideas.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIsn't that a mathematical formula a representation of a pattern? A variable\r\n represents values with the same properties given by constraints? Mathematics\r\n provides a formal system to identify and describe existing patterns in\r\n nature.\r\n For that reason, this system can certainly be applied in the digital world,\r\n which is just a subset of the real world.\r\n Mathematics can be used as a common language to help translation between\r\n layers easier, and help with the understanding of layers.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Mathematics as a universal language for all layers.\r\n Since all layers\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\ncan express mathematics with their technologies, each layer can be\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\ntranslated into another layer.}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.5]{images/02/layer_translation}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nWhy abstraction reduces complexity\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAbstraction by building language certainly leverages productivity by stripping\r\n irrelevant details to a problem.\r\n Imagine writing programs without any other layout except the lowest layer:\r\n with circuits.\r\n This is how complexity emerges: when high-level ideas are expressed with\r\n lower-level language, as the example above demonstrated.\r\n Unfortunately, this is the case with software as programming languages\r\n at the moment are more emphasized on software rather than the problem domains.\r\n That is, without prior knowledge, code written in a language is unable\r\n to express itself the knowledge of its target domain.\r\n In other words, \r\n\\emph on\r\na language is expressive if its syntax is designed to express the problem\r\n domain it is trying to solve\r\n\\emph default\r\n.\r\n Consider this example: That is, the \r\n\\emph on\r\nwhat\r\n\\emph default\r\n it will do rather the \r\n\\emph on\r\nhow\r\n\\emph default\r\n it will do.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"-4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nGraphviz (\r\n\\begin_inset Flex URL\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttp://www.graphviz.org/\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n) is a visualization software that provides a language, called \r\n\\family typewriter\r\ndot\r\n\\family default\r\n, for describing graph:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{From graph description to graph.}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/02/digraph}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAs can be seen, the code perfectly expresses itself how the graph is connected.\r\n Even a non-programmer can understand and use such language easily.\r\n An implementation in C would be more troublesome, and that's assuming that\r\n the functions for drawing graphs are already available.\r\n To draw a line, in C we might write something like:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\ndraw_line(a, b);\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHowever, it is still verbose compared with:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\na -> b;\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAlso, \r\n\\family typewriter\r\na\r\n\\family default\r\n and \r\n\\family typewriter\r\nb\r\n\\family default\r\n must be defined in C, compared to the implicit nodes in the \r\n\\family typewriter\r\ndot\r\n\\family default\r\n language.\r\n However, if we do not factor in the verbosity, then C still has a limitation:\r\n it cannot change its syntax to suit the problem domain.\r\n A domain-specific language might even be more verbose, but it makes a domain\r\n more understandable.\r\n If a problem domain must be expressed in C, then it is constraint by the\r\n syntax of C.\r\n Since C is not a specialized language for a problem domain that, but is\r\n a \r\n\\emph on\r\ngeneral-purpose\r\n\\emph default\r\n programming language, the domain knowledge is buried within the implementation\r\n details.\r\n As a result, a C programmer is needed to decipher and extract the domain\r\n knowledge out.\r\n If the domain knowledge cannot be extracted, then the software cannot be\r\n further developed.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nLinux is full of applications controlled by many domain-specific languages\r\n and are placed in \r\n\\family typewriter\r\n/etc\r\n\\family default\r\n directory, such as a web server.\r\n Instead of reprogramming the software, a domain-agnostic language is made\r\n for it.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn general, code that can express a problem domain must be understandable\r\n by a domain expert.\r\n Even within the software domain, building a language out of repeated programmin\r\ng patterns is useful.\r\n It helps people aware the existence of such patterns in code and thus making\r\n software easier to maintain, as software structure is visible as a language.\r\n Only a programming language that is capable of morphing itself to suit\r\n a problem domain can achieve that goal.\r\n Such language is called a \r\n\\emph on\r\nprogrammable programming language\r\n\\emph default\r\n.\r\n Unfortunately, this approach of turning software structure visible is not\r\n favored among programmers, as a new language must be made out of it along\r\n with new toolchain to support it.\r\n Thus, software structure and domain knowledge are buried within code written\r\n in the syntax of a general-purpose language, and if a programmer is not\r\n familiar or even aware of the existence of a code pattern, then it is hopeless\r\n to understand the code.\r\n A prime example is reading C code that controls hardware, e.g.\r\n an operating system: if a programmer knows absolutely nothing about hardware,\r\n then it is impossible to read and write operating system code in C, even\r\n if he could have 20 years of writing application C code.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWith abstraction, a software engineer can also understand the inner-working\r\n of a device without specialized knowledge of physical circuit design, enables\r\n the software engineer to write code that controls a device.\r\n The separation between logical and physical implementation also entails\r\n that gate designs can be reused even when the underlying technologies changed.\r\n For example, in some distant future biological computer could be a reality,\r\n and gates might not be implemented as CMOS but some kind of biological\r\n cells e.g.\r\n as living cells; in either technology: electrical or biological, as long\r\n as logic gates are physically realized, the same computer design could\r\n be implemented.\r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nComputer Architecture\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo write lower level code, a programmer must understand the architecture\r\n of a computer.\r\n It is similar to when one writes programs in a software framework, he must\r\n know what kinds of problems the framework solves, and how to use the framework\r\n by its provided software interfaces.\r\n But before getting to the definition of what computer architecture is,\r\n we must understand what exactly is a computer, as many people still think\r\n that a computer is a regular computer we put on a desk, or at best, a server.\r\n Computers come in various shapes and sizes and are devices that people\r\n never imagine they are computers, and that code can run on such devices.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nWhat is a computer?\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\ncomputer\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\ncomputer\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\ncomputer\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a hardware device that consists of at least a processor (CPU), a memory\r\n device and input/output interfaces.\r\n All the computers can be grouped into two types:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSingle-purpose\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\ncomputer is a computer built at the \r\n\\emph on\r\nhardware level\r\n\\emph default\r\n for specific tasks.\r\n For example, dedicated application encoders/decoders , timer, image/video/sound\r\n processors.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nGeneral-purpose\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\ncomputer is a computer that can be programmed (without modifying its hardware)\r\n to emulate various features of single-purpose computers.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nServer\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nserver\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nserver\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nserver\r\n\\emph default\r\n is a general-purpose high-performance computer with huge resources to provide\r\n large-scale services for a broad audience.\r\n The audience are people with their personal computer connected to a server.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nBlade servers.\r\n Each blade server is a computer with a modular design optimize for the\r\n use of physical space and energy.\r\n The enclosure of blade servers is called a \r\n\\emph on\r\nchassis\r\n\\emph default\r\n.(Source: \r\n\\begin_inset CommandInset href\r\nLatexCommand href\r\nname \"Wikimedia\"\r\ntarget \"https://commons.wikimedia.org/wiki/File:Wikimedia_Foundation_Servers-8055_35.jpg\"\r\nliteral \"false\"\r\n\r\n\\end_inset\r\n\r\n, author: Victorgrigas)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/03/Wikimedia_Foundation_Servers-8055_35.jpg\r\n\tscale 80\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nDesktop Computer\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\ndesktop computer\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\ndesktop computer\r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\ndesktop computer\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a general-purpose computer with an input and output system designed\r\n for a human user, with moderate resources enough for regular use.\r\n The input system usually includes a mouse and a keyboard, while the output\r\n system usually consists of a monitor that can display a large mount of\r\n pixels.\r\n The computer is enclosed in a chassis large enough for putting various\r\n computer components such as a processor, a motherboard, a power supply,\r\n a hard drive, etc.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nA typical desktop computer.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/03/computer-158675.svg\r\n\tscale 50\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nMobile Computer\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nmobile computer\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nmobile computer\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nmobile computer\r\n\\emph default\r\n is similar to a desktop computer with fewer resources but can be carried\r\n around.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Mobile computers}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[A laptop]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/03/macbook}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[A tablet]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/03/tablet}}\r\n\\backslash\r\nhfill{}  \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[A mobile phone]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/03/mobile_phone}}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nGame Consoles\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nGame consoles are similar to desktop computers but are optimized for gaming.\r\n Instead of a keyboard and a mouse, the input system of a game console are\r\n game controllers, which is a device with a few buttons for controlling\r\n on-screen objects; the output system is a television.\r\n The chassis is similar to a desktop computer but is smaller.\r\n Game consoles use custom processors and graphic processors but are similar\r\n to ones in desktop computers.\r\n For example, the first Xbox uses a custom Intel Pentium III processor.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Current-gen Game Consoles}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[A Play Station 4]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/03/PS4-Console-wDS4}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[A Xbox One]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/03/Xbox_One_Console_Set}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[A Wii U]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nhfill{}\r\n\\backslash\r\nincludegraphics[scale=0.7]{images/03/Wii_U_Console_and_Gamepad}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nhfill \r\n\\backslash\r\nbreak\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHandheld game consoles are similar to game consoles, but incorporate both\r\n the input and output systems along with the computer in a single package.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Some Handheld Consoles}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[A Nintendo DS]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/03/256px-Nintendo-DS-Lite-w-stylus}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[A PS Vita]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics{images/03/PlayStation-Vita}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nEmbedded Computer\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAn \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nembedded computer\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nembedded computer\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nembedded computer\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a single-board or single-chip computer with limited resources designed\r\n for integrating into larger hardware devices.\r\n \r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nAn Intel 82815 Graphics and Memory Controller Hub embedded on a PC motherboard.\r\n (Source: \r\n\\begin_inset CommandInset href\r\nLatexCommand href\r\nname \"Wikimedia\"\r\ntarget \"https://commons.wikimedia.org/wiki/File:Intel_82815_GMCH.jpg\"\r\nliteral \"false\"\r\n\r\n\\end_inset\r\n\r\n, author: Qurren)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/03/Intel_82815_GMCH.jpg\r\n\tscale 50\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nA PIC microcontroller.\r\n (Soure: \r\n\\begin_inset CommandInset href\r\nLatexCommand href\r\nname \"Microchip\"\r\ntarget \"http://www.microchip.com/wwwproducts/en/PIC18F4620\"\r\nliteral \"false\"\r\n\r\n\\end_inset\r\n\r\n)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/03/medium-PIC18F4620-PDIP-40.png\r\n\tscale 50\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA\r\n\\emph on\r\n \r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nmicrocontroller\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nmicrocontroller\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMicrocontroller\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is an embedded computer designed for controlling other hardware devices.\r\n A microcontroller is mounted on a chip.\r\n Microcontrollers are general-purpose computers, but with limited resources\r\n so that it is only able to perform one or a few specialized tasks.\r\n These computers are used for a single purpose, but they are still general-purpo\r\nse since it is possible to program them to perform different tasks, depends\r\n on the requirements, without changing the underlying hardware.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAnother type of embedded computer is \r\n\\emph on\r\nsystem-on-chip\r\n\\emph default\r\n.\r\n A\r\n\\emph on\r\n system-on-chip\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nsystem-on-chip\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a full computer on a single chip.\r\n Though a microcontroller is housed on a chip, its purpose is different:\r\n to control some hardware.\r\n A microcontroller is usually simpler and more limited in hardware resources\r\n as it specializes only in one purpose when running, whereas a system-on-chip\r\n is a general-purpose computer that can serve multiple purposes.\r\n A system-on-chip can run like a regular desktop computer that is capable\r\n of loading an operating system and run various applications.\r\n A system-on-chip typically presents in a smartphone, such as Apple A5 SoC\r\n used in Ipad2 and iPhone 4S, or Qualcomm Snapdragon used in many Android\r\n phones.\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nApple A5 SoC\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/03/128px-Apple_A5_Chip.jpg\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBe it a microcontroller or a system-on-chip, there must be an environment\r\n where these devices can connect to other devices.\r\n This environment is a circuit board called a \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nPCB\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nPCB\r\n\\emph default\r\n – \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nPrinted Circuit Board\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\series bold\r\nP\r\n\\series default\r\nrinted \r\n\\series bold\r\nC\r\n\\series default\r\nircuit \r\n\\series bold\r\nB\r\n\\series default\r\noard.\r\n\r\n\\emph default\r\n A \r\n\\emph on\r\nprinted circuit board\r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nPrinted Circuit Board\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a physical board that contains lines and pads to enable electron flows\r\n between electrical and electronics components.\r\n Without a PCB, devices cannot be combined to create a larger device.\r\n As long as these devices are hidden inside a larger device and contribute\r\n to a larger device that operates at a higher level layer for a higher level\r\n purpose, they are embedded devices.\r\n Writing a program for an embedded device is therefore called \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nembedded programming\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nembedded programming\r\n\\emph default\r\n.\r\n Embedded computers are used in automatically controlled devices including\r\n power tools, toys, implantable medical devices, office machines, engine\r\n control systems, appliances, remote controls and other types of embedded\r\n systems.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Raspberry Pi B+ Rev 1.2, a single-board computer that includes both\r\n a system-on-chip and a microcontroller.}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[Functional View.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        \r\n\\backslash\r\nnewline The SoC is a Broadcom BCM2835.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n      \r\n\\backslash\r\nnewline The microcontroller is the Ethernet Controller LAN9514.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nnewline (Source: \r\n\\backslash\r\nprotect\r\n\\backslash\r\nhref{https://commons.wikimedia.org/wiki/File:Raspberry_Pi_B\r\n\\backslash\r\n%2B_rev_1.2.svg}{Wikimedia}, author: Efa2)]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nhfill{}\r\n\\backslash\r\nincludegraphics[scale=1.1]{images/03/Raspberry_Pi_B}\r\n\\backslash\r\nhfill{}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[Physical View]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.075]{images/03/Raspberry_Pi_2_Model_B}}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe line between a microcontroller and a system-on-chip is blurry.\r\n If hardware keeps evolving more powerful, then a microcontroller can get\r\n enough resources to run a minimal operating system on it for multiple specializ\r\ned purposes.\r\n In contrast, a system-on-chip is powerful enough to handle the job of a\r\n microcontroller.\r\n However, using a system-on-chip as a microcontroller would not be a wise\r\n choice as price will rise significantly, but we also waste hardware resources\r\n since the software written for a microcontroller requires little computing\r\n resources.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nField Gate Programmable Array\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nField Programmable Gate Array\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nField Programmable Gate Array\r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nField Gate Programmable Array\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n (\r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nFPGA\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nFPGA\r\n\\emph default\r\n) is a hardware an array of reconfigurable gates that makes circuit structure\r\n programmable after it is shipped away from the factory\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThis is why it is called \r\n\\series bold\r\n\\emph on\r\nField\r\n\\series default\r\n\\emph default\r\n Gate Programmable Array.\r\n It is changeable \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nin the field\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n where it is applied.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n Recall that in the previous chapter, each 74HC00 chip can be configured\r\n as a gate, and a more sophisticated device can be built by combining multiple\r\n 74HC00 chips.\r\n In a similar manner, each FPGA device contains thousands of chips called\r\n \r\n\\emph on\r\nlogic blocks\r\n\\emph default\r\n, which is a more complicated chip than a 74HC00 chip that can be configured\r\n to implement a Boolean logic function.\r\n These logic blocks can be chained together to create a high-level hardware\r\n feature.\r\n This high-level feature is usually a dedicated algorithm that needs high-speed\r\n processing.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nFPGA Architecture (Source: \r\n\\begin_inset CommandInset href\r\nLatexCommand href\r\nname \"National Instruments\"\r\ntarget \"http://www.ni.com/tutorial/6097/en/\"\r\nliteral \"false\"\r\n\r\n\\end_inset\r\n\r\n)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/03/fpga_400x212.jpg\r\n\tscale 80\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nDigital devices can be designed by combining logic gates, without regarding\r\n actual circuit components, since the physical circuits are just multiples\r\n of CMOS circuits.\r\n Digital hardware, including various components in a computer, is designed\r\n by writing code, like a regular programmer, by using a language to describe\r\n how gates are wired together.\r\n This language is called a \r\n\\emph on\r\nHardware Description Language\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nHardware Description Language\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n.\r\n Later the hardware description is compiled to a description of connected\r\n electronic components called a \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nnetlist\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nnetlist\r\n\\emph default\r\n, which is a more detailed description of how gates are connected.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe difference between FPGA and other embedded computers is that programs\r\n in FPGA are implemented at the digital logic level, while programs in embedded\r\n computers like microcontrollers or system-on-chip devices are implemented\r\n at assembly code level.\r\n An algorithm written for a FPGA device is a description of the algorithm\r\n in logic gates, which the FPGA device then follows the description to configure\r\n itself to run the algorithm.\r\n An algorithm written for a microcontroller is in assembly instructions\r\n that a processor can understand and act accordingly.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFPGA is applied in the cases where the specialized operations are unsuitable\r\n and costly to run on a regular computer such as real-time medical image\r\n processing, cruise control system, circuit prototyping, video encoding/decoding\r\n, etc.\r\n These applications require high-speed processing that is not achievable\r\n with a regular processor because a processor wastes a significant amount\r\n of time in executing many non-specialized instructions - which might add\r\n up to thousands of instructions or more - to implement a specialized operation,\r\n thus more circuits at physical level to carry the same operation.\r\n A FPGA device carries no such overhead; instead, it runs a single specialized\r\n operation implemented in hardware directly.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nApplication-Specific Integrated Circuit\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAn \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nApplication-Specific Integrated Circuit\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\series bold\r\nA\r\n\\series default\r\npplication-\r\n\\series bold\r\nS\r\n\\series default\r\npecific \r\n\\series bold\r\nI\r\n\\series default\r\nntegrated \r\n\\series bold\r\nC\r\n\\series default\r\nircuit\r\n\\emph default\r\n (or \r\n\\emph on\r\nASIC\r\n\\series bold\r\n\r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nASIC\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\series default\r\n\\emph default\r\n) is a chip designed for a particular purpose rather than for general-purpose\r\n use.\r\n ASIC does not contain a generic array of logic blocks that can be reconfigured\r\n to adapt to any operation like an FPGA; instead, every logic block in an\r\n ASIC is made and optimized for the circuit itself.\r\n FPGA can be considered as the prototyping stage of an ASIC, and ASIC as\r\n the final stage of circuit production.\r\n ASIC is even more specialized than FPGA, so it can achieve even higher\r\n performance.\r\n However, ASICs are very costly to manufacture and once the circuits are\r\n made, if design errors happen, everything is thrown away, unlike the FPGA\r\n devices which can simply be reprogrammed because of the generic gate array.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nComputer Architecture\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe previous section examined various classes of computers.\r\n Regardless of shapes and sizes, every computer is designed for an architect\r\n from high level to low level.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Formula \r\n\\[\r\nComputer\\,Architecture=Instruction\\,Set\\,Architecture+Computer\\,Organization+Hardware\r\n\\]\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAt the highest-level is the Instruction Set Architecture.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAt the middle-level is the Computer Organization.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAt the lowest-level is the Hardware.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nInstruction Set Architecture\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAn \r\n\\emph on\r\ninstruction set\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\ninstruction set\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\emph default\r\nis the basic set of commands and instructions that a microprocessor understands\r\n and can carry out.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAn \r\n\\series bold\r\n\\emph on\r\nI\r\n\\series default\r\nnstruction \r\n\\series bold\r\nS\r\n\\series default\r\net \r\n\\series bold\r\nA\r\n\\series default\r\nrchitecture\r\n\\emph default\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nInstruction Set Architecture\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, or \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nISA\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\series bold\r\nISA\r\n\\series default\r\n\\emph default\r\n, is the design of an environment that implements an instruction set.\r\n Essentially, a runtime environment similar to those interpreters of high-level\r\n languages.\r\n The design includes all the instructions, registers, interrupts, memory\r\n models (how memory are arranged to be used by programs), addressing modes,\r\n I/O, etc., of a CPU.\r\n The more features (e.g.\r\n more instructions) a CPU has, the more circuits are required to implement\r\n it.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nComputer organization\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nComputer organization\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nComputer organization\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nComputer organization\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is the functional view of the design of a computer.\r\n In this view, hardware components of a computer are presented as boxes\r\n with input and output that connects to each other and form the design of\r\n a computer.\r\n Two computers may have the same ISA, but different organizations.\r\n For example, both AMD and Intel processors implement x86 ISA, but the hardware\r\n components of each processor that make up the environments for the ISA\r\n are not the same.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nComputer organizations may vary depend on a manufacturer's design, but they\r\n are all originated from the Von Neumann architecture\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\emph on\r\nJohn von Neumann\r\n\\emph default\r\n was a mathematician and physicist who invented a computer architecture.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nVon-Neumann Architecture\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\space{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/03/von_neumann_architecture.pdf\r\n\tscale 50\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\space{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nCPU\r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nCPU\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n fetches instructions continuously from main memory and execute.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nMemory\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMemory\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n stores program code and data.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nBus\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nBus\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n are electrical wires for sending raw bits between the above components.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nI/O\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nDevices\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nI/O Devices\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n are devices that give input to a computer i.e.\r\n keyboard, mouse, sensor, etc, and takes the output from a computer i.e.\r\n monitor takes information sent from CPU to display it, LED turns on/off\r\n according to a pattern computed by CPU, etc.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe Von-Neumann computer operates by storing its instructions in main memory,\r\n and CPU repeatedly fetches those instructions into its internal storage\r\n for executing, one after another.\r\n Data are transferred through a data bus between CPU, memory and I/O devices,\r\n and where to store in the devices is transferred through the address bus\r\n by the CPU.\r\n This architecture completely implements the \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nf@fetch – decode – execute\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nfetch – decode – execute\r\n\\emph default\r\n cycle.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe earlier computers were just the exact implementations of the Von Neumann\r\n architecture, with CPU and memory and I/O devices communicate through the\r\n same bus.\r\n Today, a computer has more buses, each is specialized in a type of traffic.\r\n However, at the core, they are still Von Neumann architecture.\r\n To write an OS for a Von Neumann computer, a programmer needs to be able\r\n to understand and write code that controls the cores components: CPU, memory,\r\n I/O devices, and bus.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nCPU\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\series bold\r\nCPU\r\n\\series default\r\n\\emph default\r\n, or \r\n\\series bold\r\n\\emph on\r\nC\r\n\\series default\r\nentral \r\n\\series bold\r\nP\r\n\\series default\r\nrocessing \r\n\\series bold\r\nU\r\n\\series default\r\nnit\r\n\\emph default\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nCentral Processing Unit\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, is the heart and brain of any computer system.\r\n Understand a CPU is essential to writing an OS from scratch:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nTo use these devices, a programmer needs to controls the CPU to use the\r\n programming interfaces of other devices.\r\n CPU is the only way, as CPU is the only direct device a programmer can\r\n use and the only device that understand code written by a programmer.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nIn a CPU, many OS concepts are already implemented directly in hardware,\r\n e.g.\r\n task switching, paging.\r\n A kernel programmer needs to know how to use the hardware features, to\r\n avoid duplicating such concept in software, thus wasting computer resources.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nCPU built-in OS features boost both OS performance and developer productivity\r\n because those features are actual hardware, the lowest possible level,\r\n and developers are free to implement such features.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nTo effectively use the CPU, a programmer needs to understand the documentation\r\n provided by the CPU manufacturer.\r\n For example, \r\n\\begin_inset CommandInset href\r\nLatexCommand href\r\nname \"Intel® 64 and IA-32 Architectures Software Developer Manuals\"\r\ntarget \"[http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html\"\r\nliteral \"false\"\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nAfter understanding one CPU architecture well, it is easier to learn other\r\n CPU architectures.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA CPU is an implementation of an ISA, effectively the implementation of\r\n an assembly language (and depending on the CPU architecture, the language\r\n may vary).\r\n Assembly language is one of the interfaces that are provided for software\r\n engineers to control a CPU, thus control a computer.\r\n But how can every computer device be controlled with only access to\r\n the CPU? The simple answer is that a CPU can communicate with other devices\r\n through these two interfaces, thus commanding them:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\emph on\r\nRegisters\r\n\\emph default\r\n \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nRegisters\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nRegisters\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\nare a hardware component for high-speed data access and communication with\r\n other hardware devices.\r\n Registers allow software to control hardware directly by writing to registers\r\n of a device, or receive information from a hardware device when reading from\r\n registers of a device.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nNot all registers are used for communication with other devices.\r\n In a CPU, most registers are used as high-speed storage for temporary data.\r\n Other devices that a CPU can communicate with always have a set of registers\r\n for interfacing with the CPU.\r\n \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\emph on\r\nPort\r\n\\emph default\r\n \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPort\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nPort\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\nis a specialized register in a hardware device used for communication with\r\n other devices.\r\n When data is written to a port, it causes a hardware device to perform\r\n some operation according to values written to the port.\r\n The difference between a port and a register is that a port does not store\r\n data, but delegates data to some other circuit.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThese two interfaces are extremely important, as they are the only interfaces\r\n for controlling hardware with software.\r\n Writing device drivers is essentially learning the functionality of each\r\n register and how to use them properly to control the device.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nMemory\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nMemory\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMemory\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a storage device that stores information.\r\n Memory consists of many cells.\r\n Each cell is a byte with its address number, so a CPU can use such address\r\n number to access an exact location in memory.\r\n Memory is where software instructions (in the form of machine language)\r\n is stored and retrieved to be executed by CPU; memory also stores data\r\n needed by some software.\r\n Memory in a Von Neumann machine does not distinguish between which bytes\r\n are data and which bytes are software instructions.\r\n It's up to the software to decide, and if somehow data bytes are fetched\r\n and executed as instructions, CPU still does it if such bytes represents\r\n valid instructions, but will produce undesirable results.\r\n To a CPU, there's no code and data; both are merely different types of\r\n data for it to act on: one tells it how to do something in a specific manner,\r\n and one is necessary materials for it to carry such action.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe RAM is controlled by a device called a \r\n\\emph on\r\nmemory controller\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nmemory controller\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n.\r\n Currently, most processors have this device embedded, so the CPU has a\r\n dedicated memory bus connecting the processor to the RAM.\r\n On older CPU\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPrior to the CPU's produced in 2009\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, however, this device was located in a chip also known as \r\n\\series bold\r\nMCH\r\n\\series default\r\n or \r\n\\series bold\r\n\\emph on\r\nM\r\n\\series default\r\nemory \r\n\\series bold\r\nC\r\n\\series default\r\nontroller \r\n\\series bold\r\nH\r\n\\series default\r\nub\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMemory Controller Hub\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n.\r\n In this case, the CPU does not communicate directly to the RAM, but to\r\n the MCH chip, and this chip then accesses the memory to read or write data.\r\n The first option provides better performance since there is no middleman\r\n in the communications between the CPU and the memory.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{CPU - Memory Communication}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[Old CPU]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.4]{images/03/cpu_chipset_memory}}\r\n\\backslash\r\nhfill{}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[Modern CPU]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.4]{images/03/cpu_memory_chipset}}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nhfill \r\n\\backslash\r\nbreak\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAt the physical level, RAM is implemented as a grid of cells that each contain\r\n a transistor and an electrical device called a \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\ncapacitor\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\ncapacitor\r\n\\emph default\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\ncapacitor\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, which stores charge for short periods of time.\r\n The transistor controls access to the capacitor; when switched on, it allows\r\n a small charge to be read from or written to the capacitor.\r\n The charge on the capacitor slowly dissipates, requiring the inclusion\r\n of a refresh circuit to periodically read values from the cells and write\r\n them back after amplification from an external power source.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nBus\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nBus\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nBus\r\n\\emph default\r\n is a subsystem that transfers data between computer components or between\r\n computers.\r\n Physically, buses are just electrical wires that connect all components\r\n together and each wire transfer a single big chunk of data.\r\n The total number of wires is called \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nbus width\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nbus width\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nbus width\r\n\\emph default\r\n, and is dependent on how many wires a CPU can support.\r\n If a CPU can only accept 16 bits at a time, then the bus has 16 wires connectin\r\ng from a component to the CPU, which means the CPU can only retrieve 16\r\n bits of data a time.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nHardware\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHardware is a specific implementation of a computer.\r\n A line of processors implement the same instruction set architecture and\r\n use nearly identical organizations but differ in hardware implementation.\r\n For example, the Core i7 family provides a model for desktop computers\r\n that is more powerful but consumes more energy, while another model for\r\n laptops is less performant but more energy efficient.\r\n To write software for a hardware device, seldom we need to understand a\r\n hardware implementation if documents are available.\r\n Computer organization and especially the instruction set architecture are\r\n more relevant to an operating system programmer.\r\n For that reason, the next chapter is devoted to study the x86 instruction\r\n set architecture in depth.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nx86 architecture\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nchipset\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nchipset\r\n\\emph default\r\n is a chip with multiple functions.\r\n Historically, a chipset is actually a set of individual chips, and each\r\n is responsible for a function, e.g.\r\n memory controller, graphic controllers, network controller, power controller,\r\n etc.\r\n As hardware progressed, the set of chips were incorporated into a single\r\n chip, thus more space, energy, and cost efficient.\r\n In a desktop computer, various hardware devices are connected to each other\r\n through a PCB called a \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nmotherboard\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nmotherboard\r\n\\emph default\r\n.\r\n Each CPU needs a compatible motherboard that can host it.\r\n Each motherboard is defined by its chipset model that determine the environment\r\n that a CPU can control.\r\n This environment typically consists of \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\na slot or more for CPU\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\na chipset of two chips which are the Northbridge and Southbridge chips\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Itemize\r\nNorthbridge chip is responsible for the high-performance communication between\r\n CPU, main memory and the graphic card.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nSouthbridge chip is responsible for the communication with I/O devices and\r\n other devices that are not performance sensitive.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nslots for memory sticks\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\na slot or more for graphic cards.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\ngeneric slots for other devices, e.g.\r\n network card, sound card.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nports for I/O devices, e.g.\r\n keyboard, mouse, USB.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nMotherboard organization.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"mobo-organization\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/03/Motherboard_diagram.svg\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo write a complete operating system, a programmer needs to understand how\r\n to program these devices.\r\n After all, an operating system manages hardware automatically to free applicati\r\non programs doing so.\r\n However, of all the components, learning to program the CPU is the most\r\n important, as it is the component present in any computer, regardless of\r\n what type a computer is.\r\n For this reason, the primary focus of this book will be on how to program\r\n an x86 CPU.\r\n Even solely focused on this device, a reasonably good minimal operating\r\n system can be written.\r\n The reason is that not all computers include all the devices as in a normal\r\n desktop computer.\r\n For example, an embedded computer might only have a CPU and limited internal\r\n memory, with pins for getting input and producing an output; yet, operating\r\n systems were written for such devices.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHowever, learning how to program an x86 CPU is a daunting task, with 3 primary\r\n manuals written for it: almost 500 pages for volume 1, over 2000 pages\r\n for volume 2 and over 1000 pages for volume 3.\r\n It is an impressive feat for a programmer to master every aspect of x86\r\n CPU programming.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nIntel Q35 Chipset\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nQ35 is an Intel chipset released September 2007.\r\n Q35 is used as an example of a high-level computer organization because\r\n later we will use QEMU to emulate a Q35 system, which is latest Intel system\r\n that QEMU can emulate.\r\n Though released in 2007, Q35 is relatively modern to the current hardware,\r\n and the knowledge can still be reused for current chipset model.\r\n With a Q35 chipset, the emulated CPU is also relatively up-to-date with\r\n features presented in current day CPUs so we can use the latest software\r\n manuals from Intel.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFigure \r\n\\begin_inset CommandInset ref\r\nLatexCommand vref\r\nreference \"mobo-organization\"\r\n\r\n\\end_inset\r\n\r\n is a typical current-day motherboard organization, in which Q35 shares\r\n similar organization.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nx86 Execution Environment\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAn \r\n\\emph on\r\nexecution environment\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nexecution environment\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is an environment that provides the facility to make code executable.\r\n The execution environment needs to address the following question:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSupported\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\noperations?\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n data transfer, arithmetic, control, floating-point, etc.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nWhere are operands stored?\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n registers, memory, stack, accumulator\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nHow many explicit operands are there for each instruction?\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n 0, 1, 2, or 3\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nHow is the operand location specified?\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n register, immediate, indirect, etc.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nWhat type and size of operands are supported?\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n byte, int, float, double, string, vector, etc.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\netc.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFor the remain of this chapter, please carry on the reading to chapter 3\r\n in Intel Manual Volume 1, \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nBasic Execution Environment\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n .\r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nx86 Assembly and C\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn this chapter, we will explore assembly language, and how it connects\r\n to C.\r\n But why should we do so? Isn't it better to trust the compiler, plus no\r\n one writes assembly anymore? \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNot quite.\r\n Surely, the compiler at its current state of the art is trustworthy, and\r\n we do not need to write code in assembly, \r\n\\emph on\r\nmost of the time\r\n\\emph default\r\n.\r\n A compiler can generate code, but as mentioned previously, a high-level\r\n language is a collection of patterns of a lower-level language.\r\n It does not cover everything that a hardware platform provides.\r\n As a consequence, not every assembly instruction can be generated by a\r\n compiler, so we still need to write assembly code for these circumstances\r\n to access hardware-specific features.\r\n Since hardware-specific features require writing assembly code, debugging\r\n requires reading it.\r\n We might spend even more time reading than writing.\r\n Working with low-level code that interacts directly with hardware, assembly\r\n code is unavoidable.\r\n Also, understand how a compiler generates assembly code could improve a\r\n programmer's productivity.\r\n For example, if a job or school assignment requires us to write assembly\r\n code, we can simply write it in C, then let \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n does the hard working of writing the assembly code for us.\r\n We merely collect the generated assembly code, modify as needed and be\r\n done with the assignment.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe will learn \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n extensively, along with how to use Intel documents to aid in understanding\r\n x86 assembly code.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nobjdump\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nobjdump\r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nobjdump\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is a program that displays information about object files.\r\n It will be handy later to debug incorrect layout from manual linking.\r\n Now, we use \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nobjdump\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n to examine how high level source code maps to assembly code.\r\n For now, we ignore the output and learn how to use the command first.\r\n Supposed that we have a executable binary named \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nhello\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n compiled from a \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nhello.c\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n thath prints \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nHello World', it is simple to use \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nobjdump\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n :\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump -d hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n-d\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n option only displays assembled contents of executable sections.\r\n A \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nsection\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nsection\r\n\\emph default\r\n is a block of memory that contains either program code or data.\r\n A code section is executable by the CPU, while a data section is not executable.\r\n Non-executable sections, such as \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n.data\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n and \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n.bss\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n (for storing program data), debug sections, etc, are not displayed.\r\n We will learn more about section when studying ELF binary file format in\r\n chapter \r\n\\begin_inset CommandInset ref\r\nLatexCommand vref\r\nreference \"chap:The-Anatomy-of-a-program\"\r\n\r\n\\end_inset\r\n\r\n .\r\n On the other hand:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump -D hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nwhere \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n-D\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n option displays assembly contents of all sections.\r\n If \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n-D\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n-d\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is implicitly assumed.\r\n \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nobjdump\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is mostly used for inspecting assembly code, so \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n-d\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is the most useful and thus is set by default.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe output overruns the terminal screen.\r\n To make it easy for reading, send all the output to \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nless\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump -d hello | less\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo intermix source code and assembly, the binary must be compiled with \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n-g\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n option to include source code in it, then add \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n-S\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n option:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump -S hello | less\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe default syntax used by \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nobjdump\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is AT&T syntax.\r\n To change it to the familiar Intel syntax:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump -M intel -D hello | less\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nWhen using \r\n\\family typewriter\r\n-M\r\n\\family default\r\n option, option \r\n\\family typewriter\r\n-D\r\n\\family default\r\n or \r\n\\family typewriter\r\n-d\r\n\\family default\r\n must be explicitly supplied.\r\n Next, we will use \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nobjdump\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n to examine how compiled C data and code are represented in machine code.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, we will write a 32-bit kernel, therefore we will need to compile\r\n a 32-bit binary and examine it in 32-bit mode:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump -M i386,intel -D hello | less\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n-M i386\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n tells objdump to display assembly content using 32-bit layout.\r\n Knowing the difference between 32-bit and 64-bit is crucial for writing\r\n kernel code.\r\n We will examine this matter later on when writing our kernel.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nReading the output\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAt the start of the output displays the file format of the object file:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\\noindent\r\n\\align left\r\nhello: file format elf64-x86-64\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter the line is a series of disassembled sections:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\\noindent\r\n\\align left\r\nDisassembly of section .interp:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDisassembly of section .note.ABI-tag:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDisassembly of section .note.gnu.build-id:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\netc\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, each disassembled section displays its actual content - which is\r\n a sequence of assembly instructions - with the following format:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\\noindent\r\n\\align left\r\n\r\n\\color red\r\n4004d6\r\n\\color inherit\r\n:       \r\n\\color blue\r\n55\r\n\\color inherit\r\n                      \r\n\\color green\r\npush   rbp\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe \r\n\\color red\r\nfirst column\r\n\\color inherit\r\n is the address of an assembly instruction.\r\n In the above example, the address is \r\n\\family typewriter\r\n0x4004d6\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe \r\n\\color blue\r\nsecond column\r\n\\color inherit\r\n is assembly instruction in raw hex values.\r\n In the above example, the value is \r\n\\family typewriter\r\n0x55\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe \r\n\\color green\r\nthird column\r\n\\color inherit\r\n is the assembly instruction.\r\n Depends on the section, the assembly instruction might be meaningful or\r\n meaningless.\r\n For example, if the assembly instructions are in a \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section, then the assembly instructions are actual program code.\r\n On the other hand, if the assembly instructions are displayed in a \r\n\\family typewriter\r\n.data \r\n\\family default\r\nsection, then we can safely ignore the displayed instructions.\r\n The reason is that \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nobjdump\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n doesn't know which hex values are code and which are data, so it blindly\r\n translates every hex values into assembly instructions.\r\n In the above example, the assembly instruction is \r\n\\family typewriter\r\npush %rbp\r\n\\family default\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe optional fourth column is a comment - appears when there is a reference\r\n to an address - to inform where the address originates.\r\n For example, the comment in \r\n\\color blue\r\nblue\r\n\\color inherit\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Full Width\r\n\r\n\\family typewriter\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nlea    r12,\r\n\\color red\r\n[rip+0x2008ee]\r\n\\color inherit\r\n        \r\n\\color blue\r\n# 600e10 <__frame_dummy_init_array_entry>\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nis to inform that the referenced address from \r\n\\family typewriter\r\n\\color red\r\n[rip+0x2008ee]\r\n\\family default\r\n\\color inherit\r\n is \r\n\\family typewriter\r\n\\color blue\r\n0x600e10\r\n\\family default\r\n\\color inherit\r\n, where the variable \r\n\\family typewriter\r\n__frame_dummy_init_array_entry\r\n\\family default\r\n resides.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nIn a disassembled section, it may also contain \r\n\\emph on\r\nlabels\r\n\\emph default\r\n.\r\n A label is a name given to an assembly instruction.\r\n The label denotes the purpose of an assembly block to a human reader, to\r\n make it easier to understand.\r\n For example, \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section carries many of such labels to denote where code in a program start;\r\n\\family typewriter\r\n .text\r\n\\family default\r\n section below carries two functions: \r\n\\family typewriter\r\n\\color red\r\n_start\r\n\\family default\r\n\\color inherit\r\n and \r\n\\family typewriter\r\n\\color red\r\nderegister_tm_clones\r\n\\family default\r\n\\color inherit\r\n.\r\n The \r\n\\family typewriter\r\n\\color red\r\n_start\r\n\\family default\r\n\\color inherit\r\n function starts at address \r\n\\family typewriter\r\n\\color blue\r\n4003e0\r\n\\family default\r\n\\color inherit\r\n, is annotated to the left of the function name.\r\n Right below \r\n\\family typewriter\r\n\\color red\r\n_start\r\n\\color inherit\r\n \r\n\\family default\r\nlabel is also the instruction at address \r\n\\family typewriter\r\n\\color blue\r\n4003e0\r\n\\family default\r\n\\color inherit\r\n.\r\n This whole thing means that a label is simply a name of a memory address.\r\n The function \r\n\\family typewriter\r\nderegister_tm_clones \r\n\\family default\r\nalso shares the same format as every function in the section.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000000000\r\n\\color blue\r\n4003e0\r\n\\color inherit\r\n \r\n\\color red\r\n<_start>\r\n\\color inherit\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color blue\r\n4003e0\r\n\\color inherit\r\n:       31 ed                   xor    ebp,ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  4003e2:       49 89 d1                mov    r9,rdx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  4003e5:       5e                      pop    rsi\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n...more assembly code....\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000000000\r\n\\color blue\r\n400410\r\n\\color inherit\r\n \r\n\\color red\r\n<deregister_tm_clones>\r\n\\color inherit\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color blue\r\n400410\r\n\\color inherit\r\n:       b8 3f 10 60 00          mov    eax,0x60103f\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  400415:       55                      push   rbp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  400416:       48 2d 38 10 60 00       sub    rax,0x601038\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n...more assembly code....\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nIntel manuals\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe best way to understand and use assembly language properly is to understand\r\n precisely the underlying computer architecture and what each machine instructio\r\nn does.\r\n To do so, the most reliable source is to refer to documents provided by\r\n vendors.\r\n After all, hardware vendors are the one who made their machines.\r\n To understand Intel's instruction set, we need the document \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\nIntel 64 and IA-32 architectures software developer's manual combined volumes\r\n 2A, 2B, 2C, and 2D: Instruction set reference, A-Z\r\n\\emph default\r\n\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n.\r\n The document can be retrieved here: \r\n\\begin_inset Flex URL\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttps://software.intel.com/en-us/articles/intel-sdm\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChapter 1 provides brief information about the manual, and the comment notations\r\n used in the book.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChapter 2 provides an in-depth explanation of the anatomy of an assembly\r\n instruction, which we will investigate in the next section.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChapter 3 - 5 provide the details of every instruction of the x86_64 architectur\r\ne.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChapter 6 provides information about safer mode extensions.\r\n We won't need to use this chapter.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe first volume \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\nIntel® 64 and IA-32 Architectures Software Developer’s Manual Volume 1:\r\n Basic Architecture\r\n\\emph default\r\n\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n describes the basic architecture and programming environment of Intel processor\r\ns.\r\n In the book, Chapter 5 gives the summary of all Intel instructions, by\r\n listing instructions into different categories.\r\n We only need to learn general-purpose instructions listed \r\n\\emph on\r\nchapter 5.1\r\n\\emph default\r\n for our OS.\r\n \r\n\\emph on\r\nChapter 7\r\n\\emph default\r\n describes the purpose of each category.\r\n Gradually, we will learn all of these instructions.\r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nRead section 1.3 in volume 2, exclude sections 1.3.5 and 1.3.7.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nExperiment with assembly code \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe subsequent sections examine the anatomy of an assembly instruction.\r\n To fully understand, it is necessary to write code and see the code in\r\n its actual form displayed as hex numbers.\r\n For this purpose, we use \r\n\\family typewriter\r\nnasm\r\n\\family default\r\n assembler to write a few line of assembly code and see the generated code.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Full Width\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"-4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nSuppose we want to see the machine code generated for this instruction:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\njmp eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, we use an editor e.g.\r\n Emacs, then create a new file, write the code and save it in a file, e.g.\r\n \r\n\\family typewriter\r\ntest.asm\r\n\\family default\r\n.\r\n Then, in the terminal, run the command: \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ nasm -f bin test.asm -o test\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\r\n\\family typewriter\r\n-f\r\n\\family default\r\n option specifies the file format, e.g.\r\n ELF, of the final output file.\r\n But in this case, the format is \r\n\\family typewriter\r\nbin\r\n\\family default\r\n, which means this file is just a flat binary output without any extra informati\r\non.\r\n That is, the written assembly code is translated to machine code as is,\r\n without the overhead of the metadata from file format like ELF.\r\n Indeed, after compiling, we can examine the output using this command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ hd test\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nhd\r\n\\family default\r\n (short for hexdump) is a program that displays the content of a file in\r\n hex format\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nThough its name is short for hexdump, \r\n\\family typewriter\r\nhd \r\n\\family default\r\ncan display in different base, e.g.\r\n binary, other than hex.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n\r\n\\family typewriter\r\n \r\n\\family default\r\nAnd get the following output:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000000  66 ff e0                          |f..|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000003\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe file only consists of 3 bytes: \r\n\\family typewriter\r\n\\color red\r\n66 ff e0\r\n\\family default\r\n\\color inherit\r\n, which is equivalent to the instruction \r\n\\family typewriter\r\njmp eax\r\n\\family default\r\n.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nIf we were to use \r\n\\family typewriter\r\nelf\r\n\\family default\r\n as file format:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ nasm -f elf test.asm -o test\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nIt would be more challenging to learn and understand assembly instructions\r\n with all the added noise\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe output from \r\n\\family typewriter\r\nhd.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000010  01 00 03 00 01 00 00 00  00 00 00 00 00 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000020  40 00 00 00 00 00 00 00  34 00 00 00 00 00 28 00  |@.......4.....(.|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000030  05 00 02 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n*\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000060  00 00 00 00 00 00 00 00  01 00 00 00 01 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000070  06 00 00 00 00 00 00 00  10 01 00 00 02 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000080  00 00 00 00 00 00 00 00  10 00 00 00 00 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000090  07 00 00 00 03 00 00 00  00 00 00 00 00 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n000000a0  20 01 00 00 21 00 00 00  00 00 00 00 00 00 00 00  | ...!...........|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n000000b0  01 00 00 00 00 00 00 00  11 00 00 00 02 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n000000c0  00 00 00 00 00 00 00 00  50 01 00 00 30 00 00 00  |........P...0...|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n000000d0  04 00 00 00 03 00 00 00  04 00 00 00 10 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n000000e0  19 00 00 00 03 00 00 00  00 00 00 00 00 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n000000f0  80 01 00 00 0d 00 00 00  00 00 00 00 00 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000100  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000110  ff e0 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000120  00 2e 74 65 78 74 00 2e  73 68 73 74 72 74 61 62  |..text..shstrtab|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000130  00 2e 73 79 6d 74 61 62  00 2e 73 74 72 74 61 62  |..symtab..strtab|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000140  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n*\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000160  01 00 00 00 00 00 00 00  00 00 00 00 04 00 f1 ff  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000170  00 00 00 00 00 00 00 00  00 00 00 00 03 00 01 00  |................|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000180  00 74 65 73 74 2e 61 73  6d 00 00 00 00 00 00 00  |.disp8-5.asm....|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000190\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThus, it is better just to use flat binary format in this case, to experiment\r\n instruction by instruction.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nWith such a simple workflow, we are ready to investigate the structure of\r\n every assembly instruction.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\series bold\r\nNote: \r\n\\series default\r\nUsing the bin format puts \r\n\\family typewriter\r\nnasm\r\n\\family default\r\n by default into 16-bit mode.\r\n To enable 32-bit code to be generated, we must add this line at the beginning\r\n of an \r\n\\family typewriter\r\nnasm\r\n\\family default\r\n source file:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nbits 32\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nAnatomy of an Assembly Instruction\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nChapter 2 of the instruction reference manual provides an in-depth of view\r\n of instruction format.\r\n But, the information is too much that it can overwhelm beginners.\r\n This section provides an easier instruction before reading the actual chapter\r\n in the manual.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Intel 64 and IA-32 Architectures Instruction Format}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Graphics\r\n\tfilename images/04/x86_instruction_format.pdf\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nRecall that an assembly instruction is simply a fixed-size series of bits.\r\n The length of an instruction varies and depends on how complicated an instructi\r\non is.\r\n What every instruction shares is a common format described in the figure\r\n above that divides the bits of an instruction into smaller parts that encode\r\n different types of information.\r\n These parts are:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nInstruction\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nPrefixes\r\n\\family default\r\n appears at the beginning of an instruction.\r\n Prefixes are optional.\r\n A programmer can choose to use a prefix or not because in practice, a so-called\r\n prefix is just another assembly instruction to be inserted before another\r\n assembly instruction that such prefix is applicable.\r\n Instructions with 2 or 3-bytes opcodes include the prefixes by default.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nOpcode\r\n\\family default\r\n is a unique number that identifies an instruction.\r\n Each opcode is given an mnemonic name that is human readable, e.g.\r\n one of the opcodes for instruction \r\n\\family typewriter\r\nadd \r\n\\family default\r\nis \r\n\\family typewriter\r\n04\r\n\\family default\r\n.\r\n When a CPU sees the number \r\n\\family typewriter\r\n04\r\n\\family default\r\n in its instruction cache, it sees instruction \r\n\\family typewriter\r\nadd \r\n\\family default\r\nand execute accordingly.\r\n Opcode can be 1,2 or 3 bytes long and includes an additional 3-bit field\r\n in the \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n byte when needed.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nThis instruction:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\njmp [0x1234]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\ngenerates the machine code:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\nff\r\n\\color inherit\r\n 26 34 12\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nThe very first byte, \r\n\\family typewriter\r\n\\color red\r\n0xff\r\n\\family default\r\n\\color inherit\r\n is the opcode, which is unique to \r\n\\family typewriter\r\njmp\r\n\\family default\r\n instruction.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nModR/M \r\n\\family default\r\nspecifies operands of an instruction.\r\n Operand can either be a register, a memory location or an immediate value.\r\n This component of an instruction consists of 3 smaller parts:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n\\emph on\r\nmod\r\n\\family default\r\n \r\n\\emph default\r\nfield, or \r\n\\emph on\r\nmodifier\r\n\\emph default\r\n field, is combined with \r\n\\family typewriter\r\nr/m\r\n\\family default\r\n field for a total of 5 bits of information to encode 32 possible values:\r\n 8 registers and 24 addressing modes.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n\\emph on\r\nreg/opcode\r\n\\family default\r\n\\emph default\r\n field encodes either a register operand, or extends the \r\n\\family typewriter\r\nOpcode\r\n\\family default\r\n field with 3 more bits.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n\\emph on\r\nr/m\r\n\\family default\r\n\\emph default\r\n field encodes either a register operand or can be combined with \r\n\\family typewriter\r\nmod\r\n\\family default\r\n field to encode an addressing mode.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe tables \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"mod-rm-16\"\r\n\r\n\\end_inset\r\n\r\n and \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"mod-rm-32\"\r\n\r\n\\end_inset\r\n\r\n list all possible 256 values of \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n byte and how each value maps to an addressing mode and a register, in 16-bit\r\n and 32-bit modes.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegingroup\r\n\\backslash\r\ntabcolsep=2pt\r\n\\backslash\r\ndef\r\n\\backslash\r\narraystretch{0.7}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{table*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsetlength{\r\n\\backslash\r\narrayrulewidth}{.2mm}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{16-Bit Addressing Forms with the ModR/M Byte}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"41\" columns=\"11\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"4cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nr8(/r)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nAL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nCL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nDL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nBL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nAH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nCH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nDH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nBH \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nr16(/r)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nAX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nCX \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nDX \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nBX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nSP \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nBP\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nSI\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nDI \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nr32(/r)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEAX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nECX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEDX \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEBX \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nESP\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEBP\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nESI\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEDI\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nmm(/r)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nxmm(/r) \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n(In decimal) /digit (Opcode)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n1 \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n7 \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n(In binary) REG =\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nEffective Address\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nMod\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nR/M\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nValues of ModR/M Byte (In Hexadecimal)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BX + SI]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n08\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n10\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n18\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n20\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n28\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n30\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n38\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BX + DI]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n09\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n11\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n19\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n21\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n29\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n31\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n39\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BP + SI] \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n02\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n12\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n22\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BP + DI] \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n03\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n13\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n23\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n33\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[SI] \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n04\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n14\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n24\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n34\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[DI] \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n05\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n15\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n25\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n35\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\ndisp16\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\n2\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n06\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n26\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n36\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BX]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n07\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n17\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n27\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n37\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BX + SI] + disp8\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\n3\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n40\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n48\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n50\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n58\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n60\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n68\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n70\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n78\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BX + DI] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n41\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n49\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n51\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n59\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n61\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n69\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n71\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n79\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BP + SI] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n42\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n52\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n62\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n72\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BP + DI] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n43\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n53\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n63\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n73\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[SI] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n44\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n54\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n64\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n74\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[DI] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n45\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n55\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n65\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n75\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BP] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n46\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n56\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n66\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n76\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BX] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n47\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n57\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n67\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n77\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BX + SI] + disp16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n10\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n80\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n88\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n90\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n98\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BX + DI] + disp16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n81\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n89\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n91\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n99\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BP + SI] + disp16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n82\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n92\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BP + DI] + disp16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n83\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n93\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[SI] + disp16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n84\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n94\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[DI] + disp16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n85\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n95\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BP] + disp16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n86\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n96\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[BX] + disp16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n87\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n97\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEAX/AX/AL/MM0/XMM0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n11\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nECX/CX/CL/MM1/XMM1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEDX/DX/DL/MM2/XMM2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEBX/BX/BL/MM3/XMM3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nESP/SP/AHMM4/XMM4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEBP/BP/CH/MM5/XMM5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nED\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nESI/SI/DH/MM6/XMM6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEDI/DI/BH/MM7/XMM7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" usebox=\"none\" width=\"14.5cm\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Enumerate\r\n\r\n\\family sans\r\n\\size footnotesize\r\nThe default segment register is SS for the effective addresses containing\r\n a BP index, DS for other effective addresses.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\n\r\n\\family sans\r\n\\size footnotesize\r\nThe disp16 nomenclature denotes a 16-bit displacement that follows the ModR/M\r\n byte and that is added to the index.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\n\r\n\\family sans\r\n\\size footnotesize\r\nThe disp8 nomenclature denotes an 8-bit displacement that follows the ModR/M\r\n byte and that is sign-extended and added to the index.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"mod-rm-16\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{table*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nendgroup\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegingroup\r\n\\backslash\r\ntabcolsep=2pt\r\n\\backslash\r\ndef\r\n\\backslash\r\narraystretch{0.7}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{table*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsetlength{\r\n\\backslash\r\narrayrulewidth}{.2mm}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{32-Bit Addressing Forms with the ModR/M Byte}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"41\" columns=\"11\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"4cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nr8(/r)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nAL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nCL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nDL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nBL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nAH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nCH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nDH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nBH \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nr16(/r)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nAX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nCX \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nDX \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nBX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nSP \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nBP\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nSI\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nDI \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nr32(/r)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEAX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nECX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEDX \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEBX \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nESP\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEBP\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nESI\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEDI\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nmm(/r)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nMM7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nxmm(/r) \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nXMM7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n(In decimal) /digit (Opcode)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n1 \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n7 \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n(In binary) REG =\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nEffective Address\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nMod\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nR/M\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nValues of ModR/M Byte (In Hexadecimal)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EAX]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n08\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n10\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n18\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n20\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n28\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n30\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n38\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ECX]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n09\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n11\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n19\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n21\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n29\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n31\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n39\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDX]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n02\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n12\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n22\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBX]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n03\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n13\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n23\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n33\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[-\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\n--\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n][-\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\n--\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n]\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n04\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n14\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n24\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n34\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\ndisp32\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\n2\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n05\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n15\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n25\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n35\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ESI]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n06\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n26\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n36\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDI]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n07\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n0F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n17\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n1F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n27\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n2F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n37\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n3F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EAX] + disp8\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n3\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n40\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n48\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n50\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n58\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n60\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n68\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n70\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n78\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ECX] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n41\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n49\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n51\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n59\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n61\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n69\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n71\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n79\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDX] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n42\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n52\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n62\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n72\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBX] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n43\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n53\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n63\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n73\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[-\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\n--\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n][-\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\n--\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n44\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n54\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n64\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n74\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBP] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n45\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n55\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n65\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n75\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ESI] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n46\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n56\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n66\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n76\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDI] + disp8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n47\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n4F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n57\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n5F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n67\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n6F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n77\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n7F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EAX] + disp32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n10\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n80\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n88\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n90\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n98\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ECX] + disp32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n81\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n89\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n91\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n99\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDX] + disp32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n82\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n92\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBX] + disp32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n83\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n93\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[-\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\n--\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n][-\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\n--\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n] + disp32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n84\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n94\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBP] + disp32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n85\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n95\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ESI] + disp32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n86\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n96\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDI] + disp32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n87\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n8F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n97\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n9F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nA7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nAF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nB7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nBF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEAX/AX/AL/MM0/XMM0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n11\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nECX/CX/CL/MM/XMM1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEDX/DX/DL/MM2/XMM2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEBX/BX/BL/MM3/XMM3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nESP/SP/AH/MM4/XMM4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEBP/BP/CH/MM5/XMM5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nED\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nESI/SI/DH/MM6/XMM6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEDI/DI/BH/MM7/XMM7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nC7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nCF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nD7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nDF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nE7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nEF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nF7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nFF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" usebox=\"none\" width=\"14.5cm\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Enumerate\r\n\r\n\\family sans\r\n\\size footnotesize\r\nThe [-\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\n--\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n][-\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\n--\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n] nomenclature means a SIB follows the ModR/M byte.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\n\r\n\\family sans\r\n\\size footnotesize\r\nThe disp32 nomenclature denotes a 32-bit displacement that follows the ModR/M\r\n byte (or the SIB byte if one is present) and that is added to the index.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\n\r\n\\family sans\r\n\\size footnotesize\r\nThe disp8 nomenclature denotes an 8-bit displacement that follows the ModR/M\r\n byte (or the SIB byte if one is present) and that is sign-extended and\r\n added to the index.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"mod-rm-32\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{table*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nendgroup\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset VSpace vfill\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Newpage pagebreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nHow to read the table:\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn an instruction, next to the opcode is a \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n byte.\r\n Then, look up the byte value in this table to get the corresponding operands\r\n in the row and column.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"-4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nAn instruction uses this addressing mode:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\njmp [0x1234]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nThen, the machine code is:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nff \r\n\\color red\r\n26\r\n\\color inherit\r\n 34 12\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\r\n\\family typewriter\r\n0xff\r\n\\family default\r\n is the opcode.\r\n Next to it, \r\n\\family typewriter\r\n\\color red\r\n0x26\r\n\\family default\r\n\\color inherit\r\n is the \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n byte.\r\n Look up in the 16-bit table \r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nRemember, using \r\n\\family typewriter\r\nbin \r\n\\family default\r\nformat generates 16-bit code by default\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, the first operand is in the row, equivalent to a \r\n\\family typewriter\r\ndisp16\r\n\\family default\r\n, which means a 16-bit offset.\r\n Since the instruction does not have a second operand, the column can be\r\n ignored.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nAn instruction uses this addressing mode:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nadd eax, ecx\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nThen the machine code is:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n66 01 \r\n\\color red\r\nc8\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nThe interesting feature of this instruction is that \r\n\\family typewriter\r\n0x66\r\n\\family default\r\n is the not the opcode.\r\n \r\n\\family typewriter\r\n0x01\r\n\\family default\r\n is the opcode.\r\n So then, what is \r\n\\family typewriter\r\n0x66\r\n\\family default\r\n? Recall that for every assembly instruction, there will be an optional\r\n instruction prefix, and that is what \r\n\\family typewriter\r\n0x66\r\n\\family default\r\n is.\r\n According to the Intel manual, vol 1:\r\n\\end_layout\r\n\r\n\\begin_layout Quote\r\nThe operand-size override prefix allows a program to switch between 16-\r\n and 32-bit operand sizes.\r\n Either size can be the default; use of the prefix selects the non-default\r\n size.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf the CPU is switched to 32-bit mode, when it runs an instruction with\r\n \r\n\\family typewriter\r\n0x66\r\n\\family default\r\n prefix, the instruction operands are limited to only 16-bit width.\r\n On the other hand, if the CPU is in 16-bit environment, as a result, 32-bit\r\n is considered non-standard and as such, instruction operands are temporary\r\n upgraded to 32-bit width while the instructions without the prefix use\r\n 16-bit operands.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nNext to it, \r\n\\family typewriter\r\n\\color red\r\nc8\r\n\\color inherit\r\n \r\n\\family default\r\nis the\r\n\\family typewriter\r\n ModR/M \r\n\\family default\r\nbyte.\r\n Look up in the 16-bit table at \r\n\\family typewriter\r\n\\color red\r\nc8\r\n\\family default\r\n\\color inherit\r\n value, the row tells the first operand is \r\n\\family typewriter\r\nax\r\n\\family default\r\n \r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nRemember, using bin format generates 16-bit code by default\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, the column tells the second operand is \r\n\\family typewriter\r\ncx\r\n\\family default\r\n; the column can't be ignored as the second operand is in the instruction.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhy is the first operand in the row and the second in a column? Let's break\r\n down the \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n byte, with an example value \r\n\\family typewriter\r\nc8\r\n\\family default\r\n, into bits:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align center\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"2\" columns=\"8\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nmod\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nreg/opcode\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nr/m\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe \r\n\\family typewriter\r\nmod\r\n\\family default\r\n field divides addressing modes into 4 different categories.\r\n Further combines with the \r\n\\family typewriter\r\nr/m\r\n\\family default\r\n field, exactly one addressing mode can be selected from one of the 24 rows.\r\n If an instruction only requires one operand, then the column can be ignored.\r\n Then the \r\n\\family typewriter\r\nreg/opcode\r\n\\family default\r\n field finally provides an extra register or different variants, if an instructi\r\non requires one.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nSIB\r\n\\family default\r\n\\series bold\r\n \r\n\\series default\r\nis \r\n\\series bold\r\n\\emph on\r\nS\r\n\\series default\r\ncale-\r\n\\series bold\r\nI\r\n\\series default\r\nndex-\r\n\\series bold\r\nB\r\n\\series default\r\nase\r\n\\emph default\r\n byte.\r\n This byte encodes ways to calculate the memory position into an element\r\n of an array.\r\n SIB is the name that is based on this formula for calculating an effective\r\n address:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align center\r\n\\begin_inset Formula $\\mathtt{Effective\\,address=scale*index+base}$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nIndex\r\n\\family default\r\n is an offset into an array.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nScale\r\n\\family default\r\n is a factor of \r\n\\family typewriter\r\nIndex\r\n\\family default\r\n.\r\n \r\n\\family typewriter\r\nScale\r\n\\family default\r\n is one of the values 1, 2, 4 or 8; any other value is invalid.\r\n To scale with values other than 2, 4 or 8, the scale factor must be set\r\n to 1, and the offset must be calculated manually.\r\n For example, if we want to get the address of the \r\n\\family typewriter\r\nn\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nth\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n element in an array and each element is \r\n\\family typewriter\r\n12\r\n\\family default\r\n-bytes long.\r\n Because each element is 12-bytes long instead of 1, 2, 4 or 8, \r\n\\family typewriter\r\nScale\r\n\\family default\r\n is set to 1 and a compiler needs to calculate the offset:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align center\r\n\\begin_inset Formula $\\mathtt{Effective\\,address=1*(12*n)+base}$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhy do we bother with SIB when we can manually calculate the offset? The\r\n answer is that in the above scenario, an additional \r\n\\family typewriter\r\nmul\r\n\\family default\r\n instruction must be executed to get the offset, and the \r\n\\family typewriter\r\nmul \r\n\\family default\r\ninstruction consumes more than 1 byte, while the SIB only consumes 1 byte.\r\n More importantly, if the element is repeatedly accessed many times in a\r\n loop, e.g.\r\n millions of times, then an extra \r\n\\family typewriter\r\nmul\r\n\\family default\r\n instruction can detriment the performance as the CPU must spend time executing\r\n millions of these additional \r\n\\family typewriter\r\nmul\r\n\\family default\r\n instructions.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe values 2, 4 and 8 are not random chosen.\r\n They map to 16-bit (or 2 bytes), 32-bit (or 4 bytes) and 64-bit (or 8 bytes)\r\n numbers that are often used for intensive numeric calculations.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nBase\r\n\\family default\r\n is the starting address.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBelow is the table listing all 256 values of \r\n\\family typewriter\r\nSIB\r\n\\family default\r\n byte, with the lookup rule similar to \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n tables:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegingroup\r\n\\backslash\r\ntabcolsep=2pt\r\n\\backslash\r\ndef\r\n\\backslash\r\narraystretch{0.7}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{table*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsetlength{\r\n\\backslash\r\narrayrulewidth}{.2mm}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{32-Bit Addressing Forms with the SIB Byte}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"37\" columns=\"11\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"4cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nr32(/r)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEAX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nECX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEDX \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEBX \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nESP\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEBP\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nESI\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEDI\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n(In decimal) /digit (Opcode)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n1 \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n7 \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n(In binary) REG =\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nEffective Address\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nSS\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nR/M\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nValues of SIB Byte (In Hexadecimal)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EAX]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n02\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n03\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n04\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n05\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n06\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n07\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ECX]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n08\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n09\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDX]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n10\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n11\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n12\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n13\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n14\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n15\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n17\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBX]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n18\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n19\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nnone\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n20\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n21\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n22\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n23\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n24\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n25\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n26\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n27\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBP]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n28\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n29\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n2A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n2B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n2C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n2D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n2E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n2F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ESI]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n30\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n31\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n33\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n34\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n35\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n36\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n37\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDI]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n38\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n39\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n3A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n3B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n3C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n3D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n3E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n3F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EAX*2]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n40\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n41\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n42\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n43\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n44\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n45\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n46\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n47\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ECX*2]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n48\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n49\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n4A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n4B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n4C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n4D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n4E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n4F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDX*2]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n50\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n51\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n52\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n53\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n54\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n55\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n56\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n57\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBX*2]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n58\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n59\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n5A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n5B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n5C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n5D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n5E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n5F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nnone\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n60\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n61\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n62\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n63\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n64\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n65\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n66\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n67\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBP*2]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n68\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n69\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n6A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n6B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n6C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n6D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n6E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n6F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ESI*2]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n70\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n71\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n72\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n73\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n74\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n75\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n76\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n77\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDI*2]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n78\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n79\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n7A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n7B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n7C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n7D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n7E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n7F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EAX*4]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n10\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n80\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n81\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n82\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n83\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n84\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n85\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n86\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n87\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ECX*4]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n88\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n89\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n8A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n8B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n8C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n8D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n8E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n8F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDX*4]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n90\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n91\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n92\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n93\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n94\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n95\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n96\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n97\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBX*4]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n98\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n99\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n9A\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n9B\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n9C\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n9D\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n9E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n9F\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nnone\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBP*4]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nAA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nAB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nAC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nAD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nAE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nAF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ESI*4]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nB0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nB1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nB2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nB3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nB4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nB5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nB6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nB7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDI*4]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nB8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nB9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nBA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nBB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nBC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nBD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nBE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nBF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EAX*8]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n11\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nC0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nC1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nC2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nC3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nC4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nC5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nC6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nC7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ECX*8]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nC8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nC9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nCA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nCB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nCC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nCD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nCE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nCF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDX*8]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nD0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nD1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nD2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nD3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nD4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nD5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nD6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nD7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBX*8]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n011\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nD8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nD9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nDA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nDB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nDC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nDD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nDE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nDF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\nnone\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EBP*8]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n101\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nEA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nEB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nEC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nED\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nEE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nEF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[ESI*8]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n110\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nF0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nF1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nF2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nF3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nF4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nF5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nF6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nF7\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n[EDI*8]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\size footnotesize\r\n111\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nF8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nF9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nFA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nFB\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nFC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nFD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nFE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nFF\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" usebox=\"none\" width=\"14.5cm\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Enumerate\r\n\r\n\\family sans\r\n\\size footnotesize\r\nThe [*] nomenclature means a disp32 with no base if the MOD is 00B.\r\n Otherwise, [*] means disp8 or disp32 + [EBP].\r\n This provides the following address modes:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"4\" columns=\"2\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nMOD bits\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nEffective Address\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n[scaled index] + disp32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n[scaled index] + disp8 + [EBP]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n10\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\n[scaled index] + disp32 + [EBP]\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"sib\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{table*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nendgroup\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nThis instruction:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\njmp [eax*2 + ebx]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\ngenerates the following code:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\family typewriter\r\n00000000  67 ff 24 \r\n\\color red\r\n43\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFirst of all, the first byte, \r\n\\family typewriter\r\n0x67\r\n\\family default\r\n is \r\n\\emph on\r\nnot\r\n\\emph default\r\n an opcode but a \r\n\\emph on\r\nprefix\r\n\\emph default\r\n.\r\n The number is a predefined prefix for address-size override prefix.\r\n After the prefix, comes the opcode \r\n\\family typewriter\r\n0xff \r\n\\family default\r\nand the \r\n\\family typewriter\r\nModR/M \r\n\\family default\r\nbyte \r\n\\family typewriter\r\n0x24\r\n\\family default\r\n.\r\n The value from \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n suggests that there exists a \r\n\\family typewriter\r\nSIB\r\n\\family default\r\n byte that follows.\r\n The \r\n\\family typewriter\r\nSIB\r\n\\family default\r\n byte is \r\n\\family typewriter\r\n\\color red\r\n0x43\r\n\\family default\r\n\\color inherit\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLook up in the \r\n\\family typewriter\r\nSIB\r\n\\family default\r\n table, the row tells that \r\n\\family typewriter\r\neax\r\n\\family default\r\n is scaled by 2, and the column tells that the base to be added is in \r\n\\family typewriter\r\nebx\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nDisplacement \r\n\\family default\r\nis the offset from the start of the base index.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nThis instruction:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\njmp [\r\n\\color red\r\n0x1234\r\n\\color inherit\r\n]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\ngenerates machine code is:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nff 26 \r\n\\color red\r\n34 12\r\n\\color inherit\r\n      \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color red\r\n0x1234\r\n\\family default\r\n\\color inherit\r\n, which is generated as \r\n\\family typewriter\r\n34 12\r\n\\family default\r\n in raw machine code, is the displacement and stands right next to \r\n\\family typewriter\r\n0x26\r\n\\family default\r\n, which is the \r\n\\family typewriter\r\nModR/M \r\n\\family default\r\nbyte.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nThis instruction:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\njmp [eax * 4 + \r\n\\color red\r\n0x1234\r\n\\color inherit\r\n]\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\ngenerates the machine code:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n67 ff 24 85 \r\n\\color red\r\n34 12 00 00\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n0x67\r\n\\family default\r\n is an address-size override prefix.\r\n Its meaning is that if an instruction runs a default address size e.g.\r\n 16-bit, the use of prefix enables the instruction to use non-default address\r\n size, e.g.\r\n 32-bit or 64-bit.\r\n Since the binary is supposed to be 16-bit, \r\n\\family typewriter\r\n0x67\r\n\\family default\r\n changes the instruction to 32-bit mode.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n0xff\r\n\\family default\r\n is the opcode.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n0x24\r\n\\family default\r\n is the \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n byte.\r\n According to table \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"mod-rm-32\"\r\n\r\n\\end_inset\r\n\r\n, the value suggests that a SIB byte follows, .\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n0x85\r\n\\family default\r\n is the \r\n\\family typewriter\r\nSIB\r\n\\family default\r\n byte.\r\n According to table \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"sib\"\r\nplural \"false\"\r\ncaps \"false\"\r\nnoprefix \"false\"\r\n\r\n\\end_inset\r\n\r\n, the byte \r\n\\family typewriter\r\n0x85\r\n\\family default\r\n can be destructured into bits as follow:\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align center\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"2\" columns=\"8\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.4cm\">\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nSS\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nR/M\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nREG\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe above values are obtained through the columns \r\n\\series bold\r\nSS\r\n\\series default\r\n,\r\n\\series bold\r\n R/M\r\n\\series default\r\n and finally the 8 column of \r\n\\series bold\r\nREG\r\n\\series default\r\n respectively.\r\n The total bits combined into the value \r\n\\family typewriter\r\n10000101\r\n\\family default\r\n, which is \r\n\\family typewriter\r\n0x85 \r\n\\family default\r\nin hex value.\r\n By default, if a register after the displacement is not specified, it is\r\n set to \r\n\\family typewriter\r\nEBP\r\n\\family default\r\n register, and thus the 6\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nth\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n column (bit pattern \r\n\\family typewriter\r\n101\r\n\\family default\r\n) is always chosen.\r\n If the example uses another register:\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nFor example:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\njmp [eax * 4 + eax + \r\n\\color red\r\nesi\r\n\\color inherit\r\n]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nthe \r\n\\family typewriter\r\nSIB\r\n\\family default\r\n byte becomes \r\n\\family typewriter\r\n0x86\r\n\\family default\r\n instead of , which is in the 7\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nth\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n column.\r\n Try to verify with the table \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"sib\"\r\nplural \"false\"\r\ncaps \"false\"\r\nnoprefix \"false\"\r\n\r\n\\end_inset\r\n\r\n again.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n\\color red\r\n34 12 00 00\r\n\\color inherit\r\n \r\n\\family default\r\nis the displacement.\r\n As can be seen, the displacement is 4 bytes in size, which is equivalent\r\n to 32-bit, due to address-size override prefix.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nImmediate \r\n\\family default\r\nWhen an instruction accepts a fixed value, e.g.\r\n \r\n\\family typewriter\r\n0x1234\r\n\\family default\r\n, as an operand, this optional field holds the value.\r\n Note that this field is different from displacement: the value is not necessary\r\n used an offset, but an arbitrary value of anything.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nThis instruction:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\nmov eax, \r\n\\color red\r\n0x1234\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\ngenerates the code:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n66 b8 34 12 00 00\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n0x66\r\n\\family default\r\n is operand-sized override prefix.\r\n Similar to address-size override prefix, this prefix enables operand-size\r\n to be non-default.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n0xb8\r\n\\family default\r\n is one of the opcodes for \r\n\\family typewriter\r\nmov\r\n\\family default\r\n instruction.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n0x1234 \r\n\\family default\r\nis the value to be stored in register \r\n\\family typewriter\r\neax\r\n\\family default\r\n.\r\n It is just a value for storing directly into a register, and nothing more.\r\n On the other hand, displacement value is an offset for some address calculation.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Exercise\r\nRead section 2.1 in Volume 2 for even more details.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Exercise\r\nSkim through section 5.1 in volume 1.\r\n Read chapter 7 in volume 1.\r\n If there are terminologies that you don't understand e.g.\r\n segmentation, don't worry as the terms will be explained in later chapters\r\n or ignored.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Section\r\nUnderstand an instruction in detail\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the instruction reference manual (Volume 2), from chapter 3 onward, every\r\n x86 instruction is documented in detail.\r\n Whenever the precise behavior of an instruction is needed, we always consult\r\n this document first.\r\n However, before using the document, we must know the writing conventions\r\n first.\r\n Every instruction has the following common structure for organizing information\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nOpcode\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\ntable\r\n\\family default\r\n lists all possible opcodes of an assembly instruction.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nEach table contains the following fields, and can have one or more rows:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"1\" columns=\"6\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1.5cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"2.5cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1.2cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"2.3cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3.5cm\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\nOpcode\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\nInstruction\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\nOp/En\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\n64/32-bit Mode\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\nCPUID\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\nFeature flag\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nOpcode \r\n\\family default\r\nshows\r\n\\family typewriter\r\n \r\n\\family default\r\na unique hexadecimal number assigned to an instruction.\r\n There can be more than one opcode for an instruction, each encodes a variant\r\n of the instruction.\r\n For example, one variant requires one operand, but another requires two.\r\n In this column, there can be other notations aside from hexadecimal numbers.\r\n For example, \r\n\\family typewriter\r\n/r \r\n\\family default\r\nindicates that the \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n byte of the instruction contains a \r\n\\family typewriter\r\nreg\r\n\\family default\r\n operand and an \r\n\\family typewriter\r\nr/m\r\n\\family default\r\n operand.\r\n The detail listing is in section \r\n\\emph on\r\n3.1.1.1\r\n\\emph default\r\n and \r\n\\emph on\r\n3.1.1.2\r\n\\emph default\r\n in the Intel's manual, volume 2.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nInstruction\r\n\\family default\r\n gives the syntax of the assembly instruction that a programmer can use\r\n for writing code.\r\n Aside from the mnemonic representation of the opcode, e.g.\r\n \r\n\\family typewriter\r\njmp\r\n\\family default\r\n, other symbols represent operands with specific properties in the instruction.\r\n For example, \r\n\\family typewriter\r\nrel8 \r\n\\family default\r\nrepresents a relative address from 128 bytes before the end of the instruction\r\n to 127 bytes after the end of instruction; similarly \r\n\\family typewriter\r\nrel16/rel32 \r\n\\family default\r\nalso represents relative addresses, but with the operand size of 16/32-bit\r\n instead of 8-bit like \r\n\\family typewriter\r\nrel8\r\n\\family default\r\n.\r\n For a detailed listing, please refer to section\r\n\\family typewriter\r\n 3.1.1.3 \r\n\\family default\r\nof volume 2.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nOp/En\r\n\\family default\r\n is short for \r\n\\series bold\r\n\\emph on\r\nOp\r\n\\series default\r\nerand/\r\n\\series bold\r\nEn\r\n\\series default\r\ncoding\r\n\\emph default\r\n.\r\n An operand encoding specifies how a \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n byte encodes the operands that an instruction requires.\r\n If a variant of an instruction requires operands, then an additional table\r\n named \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nInstruction Operand Encoding\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is added for explaining the operand encoding, with the following structure:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"1\" columns=\"5\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1.2cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3.1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3.1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3.1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3.1cm\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\nOp/En\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\nOperand 1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\nOperand 2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\nOperand 3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\size footnotesize\r\nOperand 4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nMost instructions require one to two operands.\r\n We make use of these instructions for our OS and skip the instructions\r\n that require three or four operands.\r\n The operands can be readable or writable or both.\r\n The symbol \r\n\\family typewriter\r\n(r)\r\n\\family default\r\n denotes a readable operand, and \r\n\\family typewriter\r\n(w)\r\n\\family default\r\n denotes a writable operand.\r\n For example, when \r\n\\family typewriter\r\nOperand 1\r\n\\family default\r\n field contains \r\n\\family typewriter\r\nModRM:r/m (r)\r\n\\family default\r\n, it means the first operand is encoded in \r\n\\family typewriter\r\nr/m\r\n\\family default\r\n field of \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n byte, and is only \r\n\\family typewriter\r\nreadable\r\n\\family default\r\n.\r\n\r\n\\family typewriter\r\n \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n64/32-bit\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nmode\r\n\\family default\r\n indicates whether the opcode sequence is supported in a 64-bit mode and\r\n possibly 32-bit mode.\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nCPUID\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nFeature\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nFlag\r\n\\family default\r\n indicates a particular CPU feature must be available to enable the instruction.\r\n An instruction is invalid if a CPU does not support the required feature.\r\n\\begin_inset Marginal\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nIn Linux, the command:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\ncat /proc/cpuinfo\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\nlists the information of available CPUs and its features in \r\n\\family typewriter\r\nflags \r\n\\family default\r\nfield.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nCompat/Leg\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nMode\r\n\\family default\r\n Many instructions do not have this field, but instead is replaced with\r\n \r\n\\family typewriter\r\nCompat/Leg Mode\r\n\\family default\r\n, which stands for \r\n\\emph on\r\nCompatibility or Legacy Mode\r\n\\emph default\r\n.\r\n This mode enables 64-bit variants of instructions to run normally in 16\r\n or 32-bit mode.\r\n \r\n\\begin_inset Float margintable\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nNotations in \r\n\\family typewriter\r\nCompat/Leg Mode\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"4\" columns=\"2\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"4cm\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nNotation\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSupported\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nI\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nNot supported\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nN.E.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe 64-bit opcode cannot be encoded as it overlaps with existing 32-bit\r\n opcode.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nDescription\r\n\\family default\r\n briefly explains the variant of an instruction in the current row.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nDescription\r\n\\family default\r\n specifies the purpose of the instructions and how an instruction works\r\n in detail.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nOperation\r\n\\family default\r\n is pseudo-code that implements an instruction.\r\n If a description is vague, this section is the next best source to understand\r\n an assembly instruction.\r\n The syntax is described in section \r\n\\emph on\r\n3.1.1.9\r\n\\emph default\r\n in volume 2.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nFlags\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\naffected\r\n\\family default\r\n lists the possible changes to system flags in \r\n\\family typewriter\r\nEFLAGS\r\n\\family default\r\n register.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nExceptions\r\n\\family default\r\n list the possible errors that can occur when an instruction cannot run\r\n correctly.\r\n This section is valuable for OS debugging.\r\n Exceptions fall into one of the following categories:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nProtected Mode Exceptions\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nReal-Address Mode Exception\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nVirtual-8086 Mode Exception\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nFloating-Point Exception\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nSIMD Floating-Point Exception\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nCompatibility Mode Exception\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n64-bit Mode Exception\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFor our OS, we only use \r\n\\emph on\r\nProtected Mode Exceptions\r\n\\emph default\r\n and \r\n\\emph on\r\nReal-Address Mode Exceptions\r\n\\emph default\r\n.\r\n The details are in section \r\n\\emph on\r\n3.1.1.13\r\n\\emph default\r\n and \r\n\\emph on\r\n3.1.1.14\r\n\\emph default\r\n, volume 2.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nExample: \r\n\\family typewriter\r\njmp\r\n\\family default\r\n instruction\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLet's look at our good old \r\n\\family typewriter\r\njmp\r\n\\family default\r\n instruction.\r\n First, the opcode table:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegingroup\r\n\\backslash\r\ntabcolsep=2pt\r\n\\backslash\r\ndef\r\n\\backslash\r\narraystretch{1}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{table*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsetlength{\r\n\\backslash\r\narrayrulewidth}{.2mm}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{\r\n\\backslash\r\ntexttt{jmp} opcode table}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"12\" columns=\"6\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"2.5cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"2.7cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"0.5cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1.5cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1.7cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"7cm\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nOpcode\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nInstruction\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nOp/\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nEn\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\n64-bit Mode\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nCompat/Leg Mode\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\series bold\r\n\\size footnotesize\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEB cb \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP rel8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump short, RIP = RIP + 8-bit displacement sign extended to 64-bits\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nE9 cw\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP rel16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nN.S.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump near, relative, displacement relative to next instruction.\r\n Not supported in 64-bit mode.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nE9 cd\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP rel32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump near, relative, RIP = RIP + 32-bit displacement sign extended to 64-bits\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nFF /4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP r/m16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nM\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nN.S.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump near, absolute indirect, address = zero- extended r/m16.\r\n Not supported in 64-bit mode\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nFF /4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP r/m32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nM\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nN.S.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump near, absolute indirect, address given in r/m32.\r\n Not supported in 64-bit mode\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nFF /4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP r/m64\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nM\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nN.E\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump near, absolute indirect, RIP = 64-Bit offset from register or memory\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEA cd\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP ptr16:16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nInv.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump far, absolute, address given in operand\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nEA cp \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP ptr16:32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nInv.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump far, absolute, address given in operand\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nFF /5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP m16:16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump far, absolute indirect, address given in m16:16\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nFF /5\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP m16:32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump far, absolute indirect, address given in m16:32\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nREX.W + FF /5 \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJMP m16:64\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nD\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nValid\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nN.E.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family sans\r\n\\size footnotesize\r\nJump far, absolute indirect, address given in m16:64\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"jmp-instruction\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nhfill \r\n\\backslash\r\nbreak\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{table*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nendgroup\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEach row lists a variant of \r\n\\family typewriter\r\njmp\r\n\\family default\r\n instruction.\r\n The first column has the opcode \r\n\\family typewriter\r\nEB cb\r\n\\family default\r\n, with an equivalent symbolic form \r\n\\family typewriter\r\njmp rel8\r\n\\family default\r\n.\r\n Here, \r\n\\family typewriter\r\nrel8\r\n\\family default\r\n means 128 bytes offset, counting from the end of the instruction.\r\n The end of an instruction is the next byte after the last byte of an instructio\r\nn.\r\n To make it more concrete, consider this assembly code:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nmain:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color red\r\njmp main\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color blue\r\njmp main2\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  jmp main\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nmain2:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color green\r\njmp 0x1234\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\ngenerates the machine code:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align center\r\n\\begin_inset Float table\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nMemory address of each opcode\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"4\" columns=\"11\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nmain\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nmain2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\downarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\downarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nAddress\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color magenta\r\n02\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n03\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color magenta\r\n04\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n05\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n06\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n07\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n08\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color magenta\r\n09\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nOpcode\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color red\r\neb\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color red\r\nfe\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color blue\r\neb\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color blue\r\n02\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\neb\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nfa\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color green\r\ne9\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color green\r\n2b\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color green\r\n12\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nThe first \r\n\\family typewriter\r\n\\color red\r\njmp main\r\n\\family default\r\n\\color inherit\r\n instruction is generated into \r\n\\family typewriter\r\n\\color red\r\neb fe\r\n\\family default\r\n\\color inherit\r\n and occupies the addresses \r\n\\family typewriter\r\n00\r\n\\family default\r\n and \r\n\\family typewriter\r\n01\r\n\\family default\r\n; the end of the first \r\n\\family typewriter\r\n\\color red\r\njmp main\r\n\\family default\r\n\\color inherit\r\n is at address \r\n\\family typewriter\r\n\\color magenta\r\n02\r\n\\family default\r\n\\color inherit\r\n, past the last byte of the first \r\n\\family typewriter\r\n\\color red\r\njmp main\r\n\\family default\r\n\\color inherit\r\n which is located at the address \r\n\\family typewriter\r\n01\r\n\\family default\r\n.\r\n The value \r\n\\family typewriter\r\n\\color red\r\nfe\r\n\\color inherit\r\n \r\n\\family default\r\nis equivalent to \r\n\\family typewriter\r\n-2\r\n\\family default\r\n, since \r\n\\family typewriter\r\n\\color red\r\neb\r\n\\family default\r\n\\color inherit\r\n opcode uses only a byte (8 bits) for relative addressing.\r\n The offset is \r\n\\family typewriter\r\n-2\r\n\\family default\r\n, and the end address of the first \r\n\\family typewriter\r\n\\color red\r\njmp main\r\n\\family default\r\n\\color inherit\r\n is \r\n\\family typewriter\r\n02\r\n\\family default\r\n, adding them together we get \r\n\\family typewriter\r\n00\r\n\\family default\r\n which is the destination address for jumping to.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nSimilarly, the \r\n\\family typewriter\r\n\\color blue\r\njmp main2\r\n\\family default\r\n\\color inherit\r\n instruction is generated into \r\n\\family typewriter\r\n\\color blue\r\neb 02\r\n\\family default\r\n\\color inherit\r\n, which means the offset is \r\n\\family typewriter\r\n+2\r\n\\family default\r\n; the end address of \r\n\\family typewriter\r\n\\color blue\r\njmp main2\r\n\\family default\r\n\\color inherit\r\n is at \r\n\\family typewriter\r\n\\color magenta\r\n04\r\n\\family default\r\n\\color inherit\r\n, and adding together with the offset we get the destination address is\r\n \r\n\\family typewriter\r\n06\r\n\\family default\r\n, which is the start instruction marked by the label \r\n\\family typewriter\r\nmain2\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe same rule can be applied to \r\n\\family typewriter\r\nrel16 \r\n\\family default\r\nand \r\n\\family typewriter\r\nrel32 \r\n\\family default\r\nencoding.\r\n In the example code, \r\n\\family typewriter\r\n\\color green\r\njmp 0x1234\r\n\\color inherit\r\n \r\n\\family default\r\nuses \r\n\\family typewriter\r\nrel16\r\n\\family default\r\n (which means 2-byte offset) and is generated into \r\n\\family typewriter\r\n\\color green\r\ne9 2b 12\r\n\\family default\r\n\\color inherit\r\n.\r\n As the table \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"jmp-instruction\"\r\n\r\n\\end_inset\r\n\r\n shows, \r\n\\family typewriter\r\ne9\r\n\\family default\r\n opcode takes a \r\n\\family typewriter\r\ncw\r\n\\family default\r\n operand, which is a 2-byte offset (section \r\n\\emph on\r\n3.1.1.1\r\n\\emph default\r\n, volume 2).\r\n Notice one strange issue here: the offset value is \r\n\\family typewriter\r\n\\color green\r\n2b 12\r\n\\family default\r\n\\color inherit\r\n, while it is supposed to be \r\n\\family typewriter\r\n34 12\r\n\\family default\r\n.\r\n There is nothing wrong.\r\n Remember, \r\n\\family typewriter\r\nrel8/rel16/rel32 \r\n\\family default\r\nis an \r\n\\emph on\r\noffset\r\n\\emph default\r\n, not an \r\n\\emph on\r\naddress\r\n\\emph default\r\n.\r\n A offset is a distance from a point.\r\n Since no label is given but a number, the offset is calculated from the\r\n start of a program.\r\n In this case, the start of the program is the address \r\n\\family typewriter\r\n00\r\n\\family default\r\n, the end of \r\n\\family typewriter\r\n\\color green\r\njmp 0x1234\r\n\\family default\r\n \r\n\\color inherit\r\nis the address \r\n\\family typewriter\r\n\\color magenta\r\n09\r\n\\family default\r\n\\color inherit\r\n\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nwhich means 9 bytes was consumed, starting from address 0.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, so the offset is calculated as \r\n\\family typewriter\r\n0x1234 - 0x9 = 0x122b\r\n\\family default\r\n.\r\n That solved the mystery!\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe \r\n\\family typewriter\r\njmp\r\n\\family default\r\n instructions with opcode \r\n\\family typewriter\r\nFF /4\r\n\\family default\r\n enable jumping to a \r\n\\emph on\r\nnear, absolute \r\n\\emph default\r\naddress stored in a general-purpose register or a memory location; or in\r\n short, as written in the description, \r\n\\emph on\r\nabsolute indirect\r\n\\emph default\r\n.\r\n The symbol \r\n\\family typewriter\r\n/4\r\n\\family default\r\n is the column with digit 4 in table \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"mod-rm-16\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe column with the following fields:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nAH\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nSP\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nESP\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nM45\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nXMM4\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n4\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n100\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n For example:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\njmp \r\n\\color blue\r\n[0x1234]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nis generated into:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nff \r\n\\color blue\r\n26\r\n\\color inherit\r\n \r\n\\color blue\r\n34 12\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nSince this is 16-bit code, we use table \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"mod-rm-16\"\r\n\r\n\\end_inset\r\n\r\n.\r\n Looking up the table, \r\n\\family typewriter\r\nModR/M\r\n\\family default\r\n value \r\n\\family typewriter\r\n26 \r\n\\family default\r\nmeans \r\n\\family typewriter\r\ndisp16\r\n\\family default\r\n, which means a 16-bit offset from the start of current index\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nLook at the note under the table.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, which is the base address stored in \r\n\\family typewriter\r\nDS\r\n\\family default\r\n register.\r\n In this case, \r\n\\family typewriter\r\njmp [0x1234] \r\n\\family default\r\nis implicitly understood as \r\n\\family typewriter\r\njmp [ds:\r\n\\color blue\r\n0x1234\r\n\\color inherit\r\n]\r\n\\family default\r\n, which means the destination address is \r\n\\family typewriter\r\n0x1234\r\n\\family default\r\n bytes away from the start of a data segment.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe \r\n\\family typewriter\r\njmp\r\n\\family default\r\n instruction with opcode \r\n\\family typewriter\r\nFF /5\r\n\\family default\r\n enables jumping to a \r\n\\emph on\r\nfar, absolute\r\n\\emph default\r\n address stored in a \r\n\\emph on\r\nmemory location\r\n\\emph default\r\n (as opposed to \r\n\\family typewriter\r\n/4\r\n\\family default\r\n, which means stored in a register); in short, \r\n\\emph on\r\na far pointer\r\n\\emph default\r\n.\r\n To generate such instruction, the keyword \r\n\\family typewriter\r\nfar\r\n\\family default\r\n is needed to tell \r\n\\family typewriter\r\nnasm\r\n\\family default\r\n we are using a far pointer:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\njmp \r\n\\color red\r\nfar\r\n\\color inherit\r\n [eax]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nis generated into:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n67 ff \r\n\\color red\r\n28\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nSince \r\n\\family typewriter\r\n\\color red\r\n28\r\n\\family default\r\n\\color inherit\r\n is the value in the 5th column of the table \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"mod-rm-32\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nRemember the prefix \r\n\\family typewriter\r\n67\r\n\\family default\r\n indicates the instruction is used as 32-bit.\r\n The prefix only added if the default environment is assumed as 16-bit when\r\n generating code by an assembler.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n that refers to \r\n\\family typewriter\r\n[eax]\r\n\\family default\r\n, we successfully generate an instruction for a far jump.\r\n After CPU runs the instruction, the program counter \r\n\\family typewriter\r\neip\r\n\\family default\r\n and code segment register \r\n\\family typewriter\r\ncs \r\n\\family default\r\nis set to the memory address, stored in the memory location that \r\n\\family typewriter\r\neax\r\n\\family default\r\n points to, and CPU starts fetching code from the new address in \r\n\\family typewriter\r\ncs \r\n\\family default\r\nand \r\n\\family typewriter\r\neip\r\n\\family default\r\n.\r\n To make it more concrete, here is an example:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{far \r\n\\backslash\r\ntexttt{jmp} example, with the destination memory stored at address \r\n\\backslash\r\ntexttt{0x1000}, which is stored in \r\n\\backslash\r\ntexttt{eax} to be dereferenced.\r\n After CPU executes the instruction, code segment register \r\n\\backslash\r\ntexttt{cs} and instruction pointer \r\n\\backslash\r\ntexttt{eip} }\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/04/far_jmp_ex.pdf\r\n\tscale 90\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nThe far address consumes total of 6 bytes in size for a 16-bit segment and\r\n 32-bit address, which is encoded as \r\n\\family typewriter\r\nm16:32\r\n\\family default\r\n from the table \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"jmp-instruction\"\r\n\r\n\\end_inset\r\n\r\n.\r\n As can be seen from the figure above, the \r\n\\color blue\r\nblue\r\n\\color inherit\r\n part is a segment address, loaded into \r\n\\family typewriter\r\ncs\r\n\\family default\r\n register with the value \r\n\\family typewriter\r\n\\color blue\r\n0x5678\r\n\\family default\r\n\\color inherit\r\n; the \r\n\\color red\r\nred\r\n\\color inherit\r\n part is the memory address within that segment, loaded into \r\n\\family typewriter\r\neip \r\n\\family default\r\nregister with the value \r\n\\family typewriter\r\n\\color red\r\n0x1234\r\n\\family default\r\n\\color inherit\r\n and start executing from there.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, the \r\n\\family typewriter\r\njmp\r\n\\family default\r\n instructions with \r\n\\family typewriter\r\nEA\r\n\\family default\r\n opcode jump to a direct absolute address.\r\n For example, the instruction:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\njmp \r\n\\color blue\r\n0x5678\r\n\\color inherit\r\n:\r\n\\color red\r\n0x1234\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nis generated into:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nea \r\n\\color red\r\n34 12\r\n\\color inherit\r\n \r\n\\color blue\r\n78 56\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nThe address \r\n\\family typewriter\r\n\\color blue\r\n0x5678\r\n\\color inherit\r\n:\r\n\\color red\r\n0x1234\r\n\\family default\r\n\\color inherit\r\n is right next to the opcode, unlike \r\n\\family typewriter\r\nFF /5\r\n\\family default\r\n instruction that needs an indirect address in \r\n\\family typewriter\r\neax\r\n\\family default\r\n register.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe skip the jump instruction with \r\n\\family typewriter\r\nREX\r\n\\family default\r\n prefix, as it is a 64-bit instruction.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Note Note\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nExplain the remaining sections\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nExamine compiled data\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn this section, we will examine how data definition in C maps to its assembly\r\n form.\r\n The generated code is extracted from \r\n\\family typewriter\r\n.bss\r\n\\family default\r\n section.\r\n That means, the assembly code displayed has no\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nActually, code is just a type of data, and is often used for hijacking into\r\n a running program to execute such code.\r\n However, we have no use for it in this book.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, aside from showing that such a value has an equivalent assembly opcode\r\n that represents an instruction.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe code-assembly listing is not random, but is based on \r\n\\emph on\r\nChapter 4\r\n\\emph default\r\n of Volume 1, \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nData Type\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n.\r\n The chapter lists fundamental data types that x86 hardware operates on,\r\n and through learning the generated assembly code, it can be understood\r\n how close C maps its syntax to hardware, and then a programmer can see\r\n why C is appropriate for OS programming.\r\n The specific \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n command used in this section will be:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump -z -M intel -S -D -j .data -j .bss <object file> | less\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\series bold\r\nNote:\r\n\\series default\r\n zero bytes are hidden with three dot symbols: \r\n\\family typewriter\r\n...\r\n\r\n\\family default\r\n To show all the zero bytes, we add \r\n\\family typewriter\r\n-z\r\n\\family default\r\n option.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nFundamental data types\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe most basic types that x86 architecture works with are based on sizes,\r\n each is twice as large as the previous one: 1 byte (8 bits), 2 bytes (16\r\n bits), 4 bytes (32 bits), 8 bytes (64 bits) and 16 bytes (128 bits).\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure} \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Fundamental Data Types}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Graphics\r\n\tfilename images/04/fundamental_data_types.pdf\r\n\tscale 50\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThese types are simplest: they are just chunks of memory at different sizes\r\n that enables CPU to access memory efficiently.\r\n From the manual, \r\n\\emph on\r\nsection 4.1.1\r\n\\emph default\r\n, volume 1:\r\n\\end_layout\r\n\r\n\\begin_layout Quote\r\nWords, doublewords, and quadwords do not need to be aligned in memory on\r\n natural boundaries.\r\n The natural boundaries for words, double words, and quadwords are even-numbered\r\n addresses, addresses evenly divisible by four, and addresses evenly divisible\r\n by eight, respectively.\r\n However, to improve the performance of programs, data structures (especially\r\n stacks) should be aligned on natural boundaries whenever possible.\r\n The reason for this is that the processor requires two memory accesses\r\n to make an unaligned memory access; aligned accesses require only one memory\r\n access.\r\n A word or doubleword operand that crosses a 4-byte boundary or a quadword\r\n operand that crosses an 8-byte boundary is considered unaligned and requires\r\n two separate memory bus cycles for access.\r\n\\end_layout\r\n\r\n\\begin_layout Quote\r\nSome instructions that operate on double quadwords require memory operands\r\n to be aligned on a natural boundary.\r\n These instructions generate a general-protection exception (\r\n\\family typewriter\r\n#GP\r\n\\family default\r\n) if an unaligned operand is specified.\r\n A natural boundary for a double quadword is any address evenly divisible\r\n by 16.\r\n Other instructions that operate on double quadwords permit unaligned access\r\n (without generating a general-protection exception).\r\n However, additional memory bus cycles are required to access unaligned\r\n data from memory.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn C, the following primitive types (must include \r\n\\family typewriter\r\nstdint.h\r\n\\family default\r\n) maps to the fundamental types:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Full Width\r\n\\begin_inset listings\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdint.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nuint8_t @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries byte|@ = 0x12;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nuint16_t @|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries word|@ = 0x1234;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nuint32_t @|\r\n\\backslash\r\ncolor{green}\r\n\\backslash\r\nbfseries dword|@ = 0x12345678;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nuint64_t @|\r\n\\backslash\r\ncolor{magenta}\r\n\\backslash\r\nbfseries qword|@ = 0x123456789abcdef;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nunsigned __int128 @|\r\n\\backslash\r\ncolor{cyan}\r\n\\backslash\r\nbfseries dqword1|@ =  (__int128) 0x123456789abcdef;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nunsigned __int128 @|\r\n\\backslash\r\ncolor{cyan}\r\n\\backslash\r\nbfseries dqword2|@ =  (__int128) 0x123456789abcdef << 64;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n\\noindent\r\n\\align left\r\n0804a018 <\r\n\\color red\r\nbyte\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a018:       \r\n\\color red\r\n12 00 \r\n\\color inherit\r\n                  adc    al,BYTE PTR [eax]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a01a <\r\n\\color blue\r\nword\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01a:       \r\n\\color blue\r\n34 12\r\n\\color inherit\r\n                   xor    al,0x12\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a01c <\r\n\\color green\r\ndword\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01c:       \r\n\\color green\r\n78 56 \r\n\\color inherit\r\n                  js     804a074 <_end+0x48>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01e:       \r\n\\color green\r\n34 12\r\n\\color inherit\r\n                   xor    al,0x12\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a020 <\r\n\\color magenta\r\nqword\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a020:       \r\n\\color magenta\r\nef \r\n\\color inherit\r\n                     out    dx,eax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a021:       \r\n\\color magenta\r\ncd ab\r\n\\color inherit\r\n                   int    0xab\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a023:       \r\n\\color magenta\r\n89 67 45\r\n\\color inherit\r\n                mov    DWORD PTR [edi+0x45],esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a026:       \r\n\\color magenta\r\n23 01\r\n\\color inherit\r\n                   and    eax,DWORD PTR [ecx]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000000000601040 <\r\n\\color cyan\r\ndqword1\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601040:       \r\n\\color cyan\r\nef\r\n\\color inherit\r\n                      out    dx,eax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601041:       \r\n\\color cyan\r\ncd ab\r\n\\color inherit\r\n                   int    0xab\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601043:       \r\n\\color cyan\r\n89 67 45\r\n\\color inherit\r\n                mov    DWORD PTR [rdi+0x45],esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601046:       \r\n\\color cyan\r\n23 01 \r\n\\color inherit\r\n                  and    eax,DWORD PTR [rcx]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601048:       \r\n\\color cyan\r\n00 00\r\n\\color inherit\r\n                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  60104a:       \r\n\\color cyan\r\n00 00\r\n\\color inherit\r\n                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  60104c:      \r\n\\color cyan\r\n 00 00\r\n\\color inherit\r\n                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  60104e:       \r\n\\color cyan\r\n00 00\r\n\\color inherit\r\n                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000000000601050 <\r\n\\color cyan\r\ndqword2\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601050:       \r\n\\color cyan\r\n00 00\r\n\\color inherit\r\n                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601052:       \r\n\\color cyan\r\n00 00 \r\n\\color inherit\r\n                  add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601054:       \r\n\\color cyan\r\n00 00 \r\n\\color inherit\r\n                  add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601056:       \r\n\\color cyan\r\n00 00 \r\n\\color inherit\r\n                  add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601058:       \r\n\\color cyan\r\nef\r\n\\color inherit\r\n                      out    dx,eax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601059:       \r\n\\color cyan\r\ncd ab \r\n\\color inherit\r\n                  int    0xab\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  60105b:       \r\n\\color cyan\r\n89 67 45\r\n\\color inherit\r\n                mov    DWORD PTR [rdi+0x45],esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  60105e:       \r\n\\color cyan\r\n23 01\r\n\\color inherit\r\n                   and    eax,DWORD PTR [rcx]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\r\n\\family typewriter\r\ngcc\r\n\\family default\r\n generates the variables \r\n\\family typewriter\r\n\\color red\r\nbyte\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color blue\r\nword\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color green\r\ndword\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color magenta\r\nqword\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color cyan\r\ndqword1\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color cyan\r\ndword2\r\n\\family default\r\n\\color inherit\r\n, written earlier, with their respective values highlighted in the same\r\n colors; variables of the same type are also highlighted in the same color.\r\n Since this is data section,\r\n\\family typewriter\r\n \r\n\\family default\r\nthe assembly listing carries no meaning.\r\n When \r\n\\family typewriter\r\nbyte\r\n\\family default\r\n is declared with \r\n\\family typewriter\r\nuint8_t\r\n\\family default\r\n, \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n guarantees that the size of \r\n\\family typewriter\r\nbyte\r\n\\family default\r\n is always 1 byte.\r\n But, an alert reader might notice the \r\n\\family typewriter\r\n\\color red\r\n00\r\n\\family default\r\n\\color inherit\r\n value next to the \r\n\\family typewriter\r\n\\color red\r\n12\r\n\\family default\r\n\\color inherit\r\n value in the \r\n\\family typewriter\r\n\\color red\r\nbyte\r\n\\family default\r\n\\color inherit\r\n variable.\r\n This is normal, as \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n avoid memory misalignment by adding extra \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\npadding bytes\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\npadding bytes\r\n\\emph default\r\n.\r\n To make it easier to see, we look at \r\n\\family typewriter\r\nreadelf\r\n\\family default\r\n output of \r\n\\family typewriter\r\n.data\r\n\\family default\r\n section:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -x .data hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nthe output is (the colors mark which values belong to which variables):\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nHex dump of section '.data':\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  0x00601020 00000000 00000000 00000000 00000000 ................\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  0x00601030 \r\n\\color red\r\n1200\r\n\\color blue\r\n3412\r\n\\color inherit\r\n \r\n\\color green\r\n78563412\r\n\\color inherit\r\n \r\n\\color magenta\r\nefcdab89 67452301\r\n\\color inherit\r\n ..4.xV4.....gE#.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  0x00601040 \r\n\\color cyan\r\nefcdab89 67452301 00000000 00000000\r\n\\color inherit\r\n ....gE#.........\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  0x00601050 \r\n\\color cyan\r\n00000000 00000000 efcdab89 67452301\r\n\\color inherit\r\n ............gE#.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAs can be seen in the \r\n\\family typewriter\r\nreadelf\r\n\\family default\r\n output, variables are allocated storage space according to their types\r\n and in the declared order by the programmer (the colors correspond the\r\n the variables).\r\n Intel is a little-endian machine, which means smaller addresses hold bytes\r\n with smaller values, larger addresses hold byte with larger values.\r\n For example, \r\n\\family typewriter\r\n0x1234\r\n\\family default\r\n is displayed as \r\n\\family typewriter\r\n34 12\r\n\\family default\r\n; that is, \r\n\\family typewriter\r\n34\r\n\\family default\r\n appears first at address \r\n\\family typewriter\r\n0x601032\r\n\\family default\r\n, then \r\n\\family typewriter\r\n12\r\n\\family default\r\n at \r\n\\family typewriter\r\n0x601033\r\n\\family default\r\n.\r\n The decimal values within a byte is unchanged, so we see \r\n\\family typewriter\r\n34\r\n\\family default\r\n \r\n\\family typewriter\r\n12\r\n\\family default\r\n instead of \r\n\\family typewriter\r\n43 21\r\n\\family default\r\n.\r\n This is quite confusing at first, but you will get used to it soon.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\nAlso, isn't it redundant when \r\n\\family typewriter\r\nchar\r\n\\family default\r\n type is always 1 byte already and why do we bother adding \r\n\\family typewriter\r\nint8_t\r\n\\family default\r\n? The truth is, \r\n\\family typewriter\r\nchar\r\n\\family default\r\n type is not guaranteed to be 1 byte in size, but only the minimum of 1\r\n byte in size.\r\n In C, a byte is defined to be the size of a char, and a char is defined\r\n to be smallest addressable unit of the underlying hardware platform.\r\n There are hardware devices that the smallest addressable unit is 16 bit\r\n or even bigger, which means char is 2 bytes in size and a \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nbyte\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n in such platforms is actually 2 units of 8-bit bytes.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNot all architectures support the double quadword type.\r\n Still, \r\n\\family typewriter\r\ngcc \r\n\\family default\r\ndoes provide support for 128-bit number and generate code when a CPU supports\r\n it (that is, a CPU must be 64-bit).\r\n By specifying a variable of type \r\n\\family typewriter\r\n__int128\r\n\\family default\r\n or \r\n\\family typewriter\r\nunsigned __int128\r\n\\family default\r\n, we get a 128-bit variable.\r\n If a CPU does not support 64-bit mode, \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n throws an error.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe data types in C, which represents the fundamental data types, are also\r\n called \r\n\\emph on\r\nunsigned numbers\r\n\\emph default\r\n.\r\n Other than numerical calculations, unsigned numbers are used as a tool\r\n for structuring data in memory; we will see this application later on the\r\n book, when various data structures are organized into bit groups.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn all the examples above, when the value of a variable with smaller size\r\n is assigned to a variable with larger size, the value easily fits in the\r\n larger variable.\r\n On the contrary, the value of a variable with larger size is assigned to\r\n a variable with smaller size, two scenarios occur: \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe value is greater than the maximum value of the variable with smaller\r\n layout, so it needs truncating to the size of the variable and causing\r\n incorrect value.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe value is smaller than the maximum value of the variable with a smaller\r\n layout, so it fits the variable.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHowever, the value might be unknown until runtime and can be value, it is\r\n best not to let such implicit conversion handled by the compiler, but explicitl\r\ny controlled by a programmer.\r\n Otherwise it will cause subtle bugs that are hard to catch as the erroneous\r\n values might rarely be used to reproduce the bugs.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nPointer Data Types\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nPointers are variables that hold memory addresses.\r\n x86 works with 2 types of pointers:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nNear\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\npointer is a 16-bit/32-bit offset within a segment, also called \r\n\\emph on\r\neffective address\r\n\\emph default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nFar\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\npointer is also an offset like a near pointer, but with an explicit segment\r\n selector.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure}  \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Numeric Data Types}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/04/pointer_data_type.pdf\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nC only provides support for near pointers, since far pointers are platform\r\n dependent, such as x86.\r\n In application code, you can assume that the address of current segment\r\n starts at 0, so the offset is actually any memory address from 0 to the\r\n maximum address.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Full Width\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdint.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint8_t i = 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint8_t @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries *p1|@ =  (int8_t *) 0x1234;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint8_t @|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries *p2|@ =  &i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n0000000000601030 <\r\n\\color red\r\np1\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601030:       \r\n\\color red\r\n34 12\r\n\\color inherit\r\n                   xor    al,0x12\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601032:       \r\n\\color red\r\n00 00\r\n\\color inherit\r\n                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601034:       00 00                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601036:       00 00                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000000000601038 <\r\n\\color blue\r\np2\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601038:       \r\n\\color blue\r\n41 10 60\r\n\\color inherit\r\n \r\n\\color blue\r\n00\r\n\\color inherit\r\n             adc    BYTE PTR [r8+0x0],spl\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  60103c:       00 00                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  60103e:       00 00                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDisassembly of section .bss:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000000000601040 <__bss_start>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601040:       00 00                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000000000601041 <i>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color blue\r\n601041\r\n\\color inherit\r\n:       00 00                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601043:       00 00                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601045:       00 00                   add    BYTE PTR [rax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  601047:       00                      .byte 0x0\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nThe pointer \r\n\\family typewriter\r\n\\color red\r\np1\r\n\\family default\r\n\\color inherit\r\n holds a direct address with the value \r\n\\family typewriter\r\n0x1234\r\n\\family default\r\n.\r\n The pointer \r\n\\family typewriter\r\n\\color blue\r\np2\r\n\\family default\r\n\\color inherit\r\n holds the address of the variable \r\n\\family typewriter\r\ni\r\n\\family default\r\n.\r\n Note that both the pointers are 8 bytes in size (or 4-byte, if 32-bit).\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nBit Field Data Type\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\nbit field\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nbit field\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a contiguous sequence of bits.\r\n Bit fields allow data structuring at bit level.\r\n For example, a 32-bit data can hold multiple bit fields that represent\r\n multiples different pieces of information, such as bits 0-4 specifies the\r\n size of a data structure, bit 5-6 specifies permissions and so on.\r\n Data structures at the bit level are common for low-level programming.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure}   \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Numeric Data Types (Source: Figure 4-6, Volume 1}.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/04/bit_field_data_type.pdf\r\n\tscale 78\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstruct bit_field {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data1:8;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data2:8;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data3:8;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data4:8;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstruct bit_field2 {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data1:8;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data2:8;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data3:8;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data4:8;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    char data5:4;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstruct normal_struct {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data1;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data2;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data3;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int data4;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstruct normal_struct @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries ns|@ = {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data1 = @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries 0x12345678|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data2 = @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries 0x9abcdef0|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data3 = @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries 0x12345678|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data4 = @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries 0x9abcdef0|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint @|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries i|@ = 0x12345678;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstruct bit_field @|\r\n\\backslash\r\ncolor{magenta}\r\n\\backslash\r\nbfseries bf|@ = {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data1 = @|\r\n\\backslash\r\ncolor{magenta}\r\n\\backslash\r\nbfseries 0x12|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data2 = @|\r\n\\backslash\r\ncolor{magenta}\r\n\\backslash\r\nbfseries 0x34|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data3 = @|\r\n\\backslash\r\ncolor{magenta}\r\n\\backslash\r\nbfseries 0x56|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data4 = @|\r\n\\backslash\r\ncolor{magenta}\r\n\\backslash\r\nbfseries 0x78|@\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstruct bit_field2 @|\r\n\\backslash\r\ncolor{green}\r\n\\backslash\r\nbfseries bf2|@ = {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data1 = @|\r\n\\backslash\r\ncolor{green}\r\n\\backslash\r\nbfseries 0x12|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data2 = @|\r\n\\backslash\r\ncolor{green}\r\n\\backslash\r\nbfseries 0x34|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data3 = @|\r\n\\backslash\r\ncolor{green}\r\n\\backslash\r\nbfseries 0x56|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data4 = @|\r\n\\backslash\r\ncolor{green}\r\n\\backslash\r\nbfseries 0x78|@,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data5 = @|\r\n\\backslash\r\ncolor{green}\r\n\\backslash\r\nbfseries 0xf|@\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nEach variable and its value are given a unique color in the assembly listing\r\n below:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a018 <\r\n\\color red\r\nns\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a018: \r\n\\color red\r\n78 56\r\n\\color inherit\r\n                   js     804a070 <_end+0x34>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01a: \r\n\\color red\r\n34 12 \r\n\\color inherit\r\n                  xor    al,0x12\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01c: \r\n\\color red\r\nf0 de bc 9a 78 56 34\r\n\\color inherit\r\n    lock fidivr WORD PTR [edx+ebx*4+0x12345678]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a023: \r\n\\color red\r\n12 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a024: \r\n\\color red\r\nf0 de bc 9a 78 56 34 \r\n\\color inherit\r\n   lock fidivr WORD PTR [edx+ebx*4+0x12345678]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a02b: \r\n\\color red\r\n12 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a028 <\r\n\\color blue\r\ni\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a028: \r\n\\color blue\r\n78 56  \r\n\\color inherit\r\n                 js     804a080 <_end+0x44>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a02a:\r\n\\color blue\r\n 34 12  \r\n\\color inherit\r\n                 xor    al,0x12\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a02c <\r\n\\color magenta\r\nbf\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a02c: \r\n\\color magenta\r\n12 34 56 \r\n\\color inherit\r\n               adc    dh,BYTE PTR [esi+edx*2]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color magenta\r\n804a02f\r\n\\color inherit\r\n:\r\n\\color blue\r\n \r\n\\color magenta\r\n78 \r\n\\color inherit\r\n12\r\n\\color magenta\r\n \r\n\\color blue\r\n   \r\n\\color inherit\r\n               js     804a043 <_end+0x7>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a030 <\r\n\\color green\r\nbf2\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a030: \r\n\\color green\r\n12 34 56 \r\n\\color inherit\r\n               adc    dh,BYTE PTR [esi+edx*2]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a033: \r\n\\color green\r\n78 0f  \r\n\\color inherit\r\n                 js     804a044 <_end+0x8>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a035:\r\n\\color green\r\n 00 00 \r\n\\color inherit\r\n                  add    BYTE PTR [eax],al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a037: \r\n\\color green\r\n00\r\n\\color blue\r\n  \r\n\\color inherit\r\n                    .byte 0x0\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nThe sample code creates 4 variables: \r\n\\family typewriter\r\n\\color red\r\nns\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color blue\r\ni\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color magenta\r\nbf\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color green\r\nbf2\r\n\\family default\r\n\\color inherit\r\n.\r\n The definition of \r\n\\family typewriter\r\nnormal_struct\r\n\\family default\r\n and \r\n\\family typewriter\r\nbit_field\r\n\\family default\r\n structs both specify 4 integers.\r\n\r\n\\family typewriter\r\n bit_field\r\n\\family default\r\n specifies additional information next to its member name, separated by\r\n a colon, e.g.\r\n \r\n\\family typewriter\r\n.data1 : 8\r\n\\family default\r\n.\r\n This extra information is the bit width of each bit group.\r\n It means, even though defined as an \r\n\\family typewriter\r\nint\r\n\\family default\r\n, \r\n\\family typewriter\r\n.data1\r\n\\family default\r\n only consumes 8 bit of information.\r\n If additional data members are specified after \r\n\\family typewriter\r\n.data1\r\n\\family default\r\n, two scenarios happen:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nIf the new data members fit within the remaining bits after \r\n\\family typewriter\r\n.data\r\n\\family default\r\n, which are 24 bits\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nSince .data1 is declared as an int, 32 bits are still allocated, but \r\n\\family typewriter\r\n.data1 \r\n\\family default\r\ncan only access 8 bits of information.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, then the total size of \r\n\\family typewriter\r\nbit_field\r\n\\family default\r\n struct is still 4 bytes, or 32 bits.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nIf the new data members don't fit, then the remaining 24 bits (3 bytes)\r\n are still allocated.\r\n However, the new data members are allocated brand new storages, without\r\n using the previous 24 bits.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the example, the 4 data members: \r\n\\family typewriter\r\n.data1\r\n\\family default\r\n,\r\n\\family typewriter\r\n .data2\r\n\\family default\r\n,\r\n\\family typewriter\r\n .data3\r\n\\family default\r\n and\r\n\\family typewriter\r\n .data4\r\n\\family default\r\n, each can access 8 bits of information, and together can access all of\r\n 4 bytes of the integer first declared by \r\n\\family typewriter\r\n.data1\r\n\\family default\r\n.\r\n As can be seen by the generated assembly code, the values of \r\n\\family typewriter\r\nbf\r\n\\family default\r\n are follow natural order as written in the C code: \r\n\\family typewriter\r\n\\color red\r\n12 34 56 78\r\n\\family default\r\n\\color inherit\r\n, since each value is a separate members.\r\n In contrast, the value of \r\n\\family typewriter\r\ni\r\n\\family default\r\n is a number as a whole, so it is subject to the rule of little endianess\r\n and thus contains the value \r\n\\family typewriter\r\n\\color blue\r\n78\r\n\\family default\r\n \r\n\\family typewriter\r\n56 34 12\r\n\\family default\r\n\\color inherit\r\n.\r\n Note that at \r\n\\family typewriter\r\n\\color magenta\r\n804a02f\r\n\\family default\r\n\\color inherit\r\n, is the address of the final byte in\r\n\\color magenta\r\n \r\n\\family typewriter\r\nbf\r\n\\family default\r\n\\color inherit\r\n, but next to it is a number \r\n\\family typewriter\r\n12\r\n\\family default\r\n, despite \r\n\\family typewriter\r\n78\r\n\\family default\r\n is the last number in it.\r\n This extra number \r\n\\family typewriter\r\n12\r\n\\family default\r\n does not belong to the value of \r\n\\family typewriter\r\n\\color magenta\r\nbf\r\n\\family default\r\n\\color inherit\r\n.\r\n \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n is just being confused that \r\n\\family typewriter\r\n78\r\n\\family default\r\n is an opcode; \r\n\\family typewriter\r\n78\r\n\\family default\r\n corresponds to \r\n\\family typewriter\r\njs\r\n\\family default\r\n instruction, and it requires an operand.\r\n For that reason, \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n grabs whatever the next byte after 78 and put it there.\r\n \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n is a tool to display assembly code after all.\r\n A better tool to use is \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n that we will learn in the next chapter.\r\n But for this chapter, \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n suffices.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nUnlike \r\n\\family typewriter\r\nbf\r\n\\family default\r\n, each data member in \r\n\\family typewriter\r\nns\r\n\\family default\r\n is allocated fully as an integer, 4 bytes each, 16 bytes in total.\r\n As we can see, bit field and normal struct are different: bit field structure\r\n data at the bit level, while normal struct works at byte level.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, the struct of \r\n\\family typewriter\r\n\\color green\r\nbf2\r\n\\color inherit\r\n\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nbit_field2\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nis the same of \r\n\\family typewriter\r\n\\color magenta\r\nbf\r\n\\family default\r\n\\color inherit\r\n\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nbit_field\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, except it contains one more data member: \r\n\\family typewriter\r\n.data5\r\n\\family default\r\n, and is defined as a \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nchar\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n For this reason, another 4 bytes are allocated just for \r\n\\family typewriter\r\n.data5\r\n\\family default\r\n, even though it can only access 4 bits of information, and the final value\r\n of \r\n\\family typewriter\r\n\\color green\r\nbf2\r\n\\family default\r\n\\color inherit\r\n is: \r\n\\family typewriter\r\n\\color green\r\n12 34 56 78 0f 00 00 00\r\n\\family default\r\n\\color inherit\r\n.\r\n The remaining 3 bytes must be accessed by the mean of a pointer, or casting\r\n to another data type that can fully access all 4 bytes..\r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nWhat happens when the definition of \r\n\\family typewriter\r\nbit_field\r\n\\family default\r\n struct and \r\n\\family typewriter\r\nbf\r\n\\family default\r\n variable are changed to:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\nstruct bit_field {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    int data1:8;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nstruct bit_field bf = {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    .data1 = 0x1234,\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhat will be the value of \r\n\\family typewriter\r\n.data1\r\n\\family default\r\n?\r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nWhat happens when the definition of \r\n\\family typewriter\r\nbit_field2\r\n\\family default\r\n struct is changed to:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nstruct bit_field2 {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    int data1:8;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    int data5:32;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhat is layout of a variable of type \r\n\\family typewriter\r\nbit_field2?\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nString Data Types\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAlthough share the same name, string as defined by x86 is different than\r\n a string in C.\r\n x86 defines string as \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\ncontinuous sequences of bits, bytes, words, or doublewords\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n.\r\n On the other hand, C defines a string as an array of 1-byte characters\r\n with a zero as the last element of the array to make a \r\n\\emph on\r\nnull-terminated string\r\n\\emph default\r\n.\r\n This implies that strings in x86 are arrays, not C strings.\r\n A programmer can define an array of bytes, words or doublewords with \r\n\\family typewriter\r\nchar\r\n\\family default\r\n or \r\n\\family typewriter\r\nuint8_t\r\n\\family default\r\n, \r\n\\family typewriter\r\nshort\r\n\\family default\r\n or \r\n\\family typewriter\r\nuint16_t\r\n\\family default\r\n and \r\n\\family typewriter\r\nint\r\n\\family default\r\n or \r\n\\family typewriter\r\nuint32_t\r\n\\family default\r\n, except an array of bits.\r\n However, such a feature can be easily implemented, as an array of bits\r\n is essentially any array of bytes, or words or doublewords, but operates\r\n at the bit level.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe following code demonstrates how to define array (string) data types:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdint.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nuint8_t @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries a8[2]|@ = {0x12, 0x34};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nuint16_t @|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries a16[2]|@ = {0x1234, 0x5678};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nuint32_t @|\r\n\\backslash\r\ncolor{magenta}\r\n\\backslash\r\nbfseries a32[2]|@ = {0x12345678, 0x9abcdef0};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nuint64_t @|\r\n\\backslash\r\ncolor{green}\r\n\\backslash\r\nbfseries a64[2]|@ = {0x123456789abcdef0, 0x123456789abcdef0};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n0804a018 <\r\n\\color red\r\na8\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a018: \r\n\\color red\r\n12 34 00\r\n\\color inherit\r\n                adc    dh,BYTE PTR [eax+eax*1]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01b: \r\n\\color red\r\n00\r\n\\color inherit\r\n 34 12                add    BYTE PTR [edx+edx*1],dh\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a01c <\r\n\\color blue\r\na16\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01c: \r\n\\color blue\r\n34 12  \r\n\\color inherit\r\n                 xor    al,0x12\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01e: \r\n\\color blue\r\n78 56 \r\n\\color inherit\r\n                  js     804a076 <_end+0x3a>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a020 <\r\n\\color magenta\r\na32\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a020: \r\n\\color magenta\r\n78 56 \r\n\\color inherit\r\n                  js     804a078 <_end+0x3c>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a022: \r\n\\color magenta\r\n34 12  \r\n\\color inherit\r\n                 xor    al,0x12\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a024: \r\n\\color magenta\r\nf0 de bc 9a \r\n\\color inherit\r\nf0 de bc    lock fidivr WORD PTR [edx+ebx*4-0x65432110]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a02b: 9a\r\n\\color magenta\r\n \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a028 <\r\n\\color green\r\na64\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a028: \r\n\\color green\r\nf0 de bc 9a 78 56 34\r\n\\color inherit\r\n    lock fidivr WORD PTR [edx+ebx*4+0x12345678]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a02f: \r\n\\color green\r\n12\r\n\\color inherit\r\n \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a030: \r\n\\color green\r\nf0 de bc 9a 78 56 34\r\n\\color inherit\r\n    lock fidivr WORD PTR [edx+ebx*4+0x12345678]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a037: \r\n\\color green\r\n12 \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nDespite \r\n\\family typewriter\r\n\\color red\r\na8\r\n\\family default\r\n\\color inherit\r\n is an array with 2 elements, each is 1-byte long, but it is still allocated\r\n with 4 bytes.\r\n Again, to ensure natural alignment for best performance, \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n pads extra zero bytes.\r\n As shown in the assembly listing, the actual value of \r\n\\family typewriter\r\n\\color red\r\na8\r\n\\family default\r\n\\color inherit\r\n is \r\n\\family typewriter\r\n12 34 00 00\r\n\\family default\r\n, with \r\n\\family typewriter\r\n\\color red\r\na8\r\n\\color inherit\r\n[0]\r\n\\family default\r\n equals to \r\n\\family typewriter\r\n12\r\n\\family default\r\n and \r\n\\family typewriter\r\n\\color red\r\na8\r\n\\color inherit\r\n[1]\r\n\\family default\r\n equals to \r\n\\family typewriter\r\n34\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen it comes \r\n\\family typewriter\r\n\\color blue\r\na16\r\n\\family default\r\n\\color inherit\r\n with 2 elements, each is 2-byte long.\r\n Since 2 elements are 4 bytes in total, which is in the natural alignment,\r\n \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n pads no byte.\r\n The value of \r\n\\family typewriter\r\n\\color blue\r\na16\r\n\\family default\r\n\\color inherit\r\n is \r\n\\family typewriter\r\n34 12 78 56\r\n\\family default\r\n, with \r\n\\family typewriter\r\n\\color blue\r\na16\r\n\\color inherit\r\n[0]\r\n\\family default\r\n equals to \r\n\\family typewriter\r\n34 12\r\n\\family default\r\n and \r\n\\family typewriter\r\n\\color blue\r\na16\r\n\\color inherit\r\n[1]\r\n\\family default\r\n equals to \r\n\\family typewriter\r\n78 56\r\n\\family default\r\n.\r\n Note that, objdump is confused again, as \r\n\\family typewriter\r\nde\r\n\\family default\r\n is the opcode for the instruction \r\n\\family typewriter\r\nfidivr\r\n\\family default\r\n (short of reverse divide) that requires another operand, so \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n grabs whatever the next bytes that makes sense to it for creating \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nan operand\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n.\r\n Only the highlighted values belong to \r\n\\family typewriter\r\n\\color magenta\r\na32\r\n\\family default\r\n\\color inherit\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNext is \r\n\\family typewriter\r\n\\color magenta\r\na32\r\n\\family default\r\n\\color inherit\r\n, with 2 elements, 4 bytes each.\r\n Similar to above arrays, the value of \r\n\\family typewriter\r\n\\color magenta\r\na32\r\n\\color inherit\r\n[0]\r\n\\family default\r\n is \r\n\\family typewriter\r\n78 56 34 12\r\n\\family default\r\n, the value of \r\n\\family typewriter\r\n\\color magenta\r\na32\r\n\\color inherit\r\n[1]\r\n\\family default\r\n is \r\n\\family typewriter\r\nf0 de bc 9a\r\n\\family default\r\n, exactly what is assigned in the C code.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally is \r\n\\family typewriter\r\n\\color green\r\na64\r\n\\family default\r\n\\color inherit\r\n, also with 2 elements, but 8 bytes each.\r\n The total size of \r\n\\family typewriter\r\n\\color green\r\na64\r\n\\family default\r\n\\color inherit\r\n is 16 bytes, which is in the natural alignment, therefore no padding bytes\r\n added.\r\n The values of both \r\n\\family typewriter\r\n\\color green\r\na64\r\n\\color inherit\r\n[0]\r\n\\family default\r\n and \r\n\\family typewriter\r\n\\color green\r\na64\r\n\\color inherit\r\n[1]\r\n\\family default\r\n are the same: \r\n\\family typewriter\r\nf0 de bc 9a 78 56 34 12\r\n\\family default\r\n, that got misinterpreted to \r\n\\family typewriter\r\nfidivr\r\n\\family default\r\n instruction.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\na8\r\n\\family default\r\n, \r\n\\family typewriter\r\na16\r\n\\family default\r\n, \r\n\\family typewriter\r\na32\r\n\\family default\r\n and \r\n\\family typewriter\r\na64 \r\n\\family default\r\nmemory layouts\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color red\r\na8\r\n\\color inherit\r\n: \r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"1\" columns=\"1\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n12 |\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n34 \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color blue\r\na16\r\n\\color inherit\r\n: \r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"1\" columns=\"1\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n34 12\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n|\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n78 56\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color magenta\r\na32\r\n\\color inherit\r\n: \r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"1\" columns=\"1\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n78 56 34 12\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n|\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nf0 de bc 9a\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color green\r\na64\r\n\\color inherit\r\n: \r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"1\" columns=\"1\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nf0 de bc 9a 78 56 34 12\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n|\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nf0 de bc 9a 78 56 34 12\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHowever, beyond one-dimensional arrays that map directly to hardware string\r\n type, C provides its own syntax for multi-dimensional arrays:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdint.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nuint8_t @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries a2[2][2]|@ = {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    {0x12, 0x34},\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    {0x56, 0x78}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nuint8_t @|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries a3[2][2][2]|@ = {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    {{0x12, 0x34},\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n     {0x56, 0x78}},\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    {{0x9a, 0xbc},\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n     {0xde, 0xff}},\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n0804a018 <\r\n\\color red\r\na2\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a018: \r\n\\color red\r\n12 34 56\r\n\\color inherit\r\n                adc    dh,BYTE PTR [esi+edx*2]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01b: \r\n\\color red\r\n78\r\n\\color inherit\r\n 12                   js     804a02f <_end+0x7>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0804a01c <\r\n\\color blue\r\na3\r\n\\color inherit\r\n>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01c: \r\n\\color blue\r\n12 34 56 \r\n\\color inherit\r\n               adc    dh,BYTE PTR [esi+edx*2]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a01f:\r\n\\color blue\r\n 78 9a \r\n\\color inherit\r\n                  js     8049fbb <_DYNAMIC+0xa7>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a021: \r\n\\color blue\r\nbc\r\n\\color inherit\r\n                      .byte 0xbc\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804a022: \r\n\\color blue\r\nde ff\r\n\\color inherit\r\n                   fdivrp st(7),st\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nTechnically, multi-dimensional arrays are like normal arrays: in the end,\r\n the total size is translated into flat allocated bytes.\r\n A 2 x 2 array is allocated with 4 bytes; a \r\n\\begin_inset Formula $2\\times2\\times2$\r\n\\end_inset\r\n\r\n array is allocated with 8 bytes, as can be seen in the assembly listing\r\n of \r\n\\family typewriter\r\n\\color red\r\na2\r\n\\color inherit\r\n\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nAgain, \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n is confused and put the number \r\n\\family typewriter\r\n12\r\n\\family default\r\n next to 78 in \r\n\\family typewriter\r\na3\r\n\\family default\r\n listing.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n and \r\n\\family typewriter\r\n\\color blue\r\na3\r\n\\family default\r\n\\color inherit\r\n.\r\n In low-level assembly code, the representation is the same between \r\n\\family typewriter\r\na[4]\r\n\\family default\r\n and \r\n\\family typewriter\r\na[2][2]\r\n\\family default\r\n.\r\n However, in high-level C code, the difference is tremendous.\r\n The syntax of multi-dimensional array enables a programmer to think with\r\n higher level concepts, instead of translating manually from high-level\r\n concepts to low-level code and work with high-level concepts in his head\r\n at the same time.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nThe following two-dimensional array can hold a list of 2 names with the\r\n length of 10:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nchar names[2][10] = {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    \"John Doe\",\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    \"Jane Doe\"\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n};\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo access a name, we simply adjust the column index\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe left index is called column index since it changes the index based on\r\n a column.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n e.g.\r\n \r\n\\family typewriter\r\nnames[0]\r\n\\family default\r\n, \r\n\\family typewriter\r\nnames[1]\r\n\\family default\r\n.\r\n To access individual character within a name, we use the row index\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nSame with column index, the right index is called row index since it changes\r\n the index based on a row.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n e.g.\r\n \r\n\\family typewriter\r\nnames[0][0] \r\n\\family default\r\ngives the character \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nJ\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n, \r\n\\family typewriter\r\nnames[0][1]\r\n\\family default\r\n gives the character \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\no\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n and so on.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWithout such syntax, we need to create a 20-byte array e.g.\r\n \r\n\\family typewriter\r\nnames[20]\r\n\\family default\r\n, and whenever we want to access a character e.g.\r\n to check if the names contains with a number in it, we need to calculate\r\n the index manually.\r\n It would be distracting, since we constantly need to switch thinkings between\r\n the actual problem and the translate problem.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSince this is a repeating pattern, C abstracts away this problem with the\r\n syntax for define and manipulating multi-dimensional array.\r\n Through this example, we can clearly see the power of abstraction through\r\n language can give us.\r\n It would be ideal if a programmer is equipped with such power to define\r\n whatever syntax suitable for a problem at hands.\r\n Not many languages provide such capacity.\r\n Fortunately, through C macro, we can partially achieve that goal .\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nIn all cases, an array is guaranteed to generate contiguous bytes of memory,\r\n regardless of the dimensions it has.\r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nWhat is the difference between a multi-dimensional array and an array of\r\n pointers, or even pointers of pointers?\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nExamine compiled code\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis section will explore how compiler transform high level code into assembly\r\n code that CPU can execute, and see how common assembly patterns help to\r\n create higher level syntax.\r\n \r\n\\family typewriter\r\n-S \r\n\\family default\r\noption is added to \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n to better demonstrate the connection between high and low level code.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn this section, the option \r\n\\family typewriter\r\n--no-show-raw-insn\r\n\\family default\r\n is added to objdump command to omit the opcodes for clarity:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump --no-show-raw-insn -M intel -S -D <object file> | less \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nData Transfer\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nPrevious section explores how various types of data are created, and how\r\n they are laid out in memory.\r\n Once memory storages are allocated for variables, they must be accessible\r\n and writable.\r\n Data transfer instructions move data (bytes, words, doublewords or quadwords)\r\n between memory and registers, and between registers, effectively read from\r\n a storage source and write to another storage source.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdint.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint32_t i = 0x12345678;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        int j = i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        int k = 0xabcdef;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n080483db <main>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#include <stdint.h>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint32_t i = 0x12345678;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483db:       push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n80483dc:       mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483de:       sub    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n        int j = i;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color blue\r\n80483e1:     \r\n\\color inherit\r\n  \r\n\\color blue\r\nmov    eax,ds:0x804a018\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color blue\r\n80483e6:      \r\n\\color inherit\r\n \r\n\\color blue\r\nmov    DWORD PTR [ebp-0x8],eax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n        int k = 0xabcdef;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color magenta\r\n80483e9:       mov    DWORD PTR [ebp-0x4],0xabcdef\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n        return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color green\r\n80483f0:       mov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f5:       leave  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f6:       ret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f7:       xchg   ax,ax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f9:       xchg   ax,ax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483fb:       xchg   ax,ax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483fd:       xchg   ax,ax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483ff:       nop\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nThe general data movement is performed with the \r\n\\family typewriter\r\nmov\r\n\\family default\r\n instruction.\r\n Note that despite the instruction being called \r\n\\family typewriter\r\nmov\r\n\\family default\r\n, it actually copies data from one destination to another.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\color red\r\nThe red\r\n\\color inherit\r\n instruction copies data from the register \r\n\\family typewriter\r\n\\color red\r\nesp\r\n\\family default\r\n\\color inherit\r\n to the register \r\n\\family typewriter\r\n\\color red\r\nebp\r\n\\family default\r\n\\color inherit\r\n.\r\n This \r\n\\family typewriter\r\nmov\r\n\\family default\r\n instruction moves data between registers and is assigned the opcode \r\n\\family typewriter\r\n89\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\color blue\r\nThe blue\r\n\\color inherit\r\n instructions copies data from one memory location (the \r\n\\family typewriter\r\ni\r\n\\family default\r\n variable) to another (the \r\n\\family typewriter\r\nj\r\n\\family default\r\n variable).\r\n There exists no data movement from memory to memory; it requires two \r\n\\family typewriter\r\nmov\r\n\\family default\r\n instructions, one for copying the data from a memory location to a register,\r\n and one for copying the data from the register to the destination memory\r\n location.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe \r\n\\color magenta\r\npink\r\n\\color inherit\r\n instruction copies an immediate value into memory.\r\n Finally, the \r\n\\color green\r\ngreen\r\n\\color inherit\r\n instruction copies immediate data into a register.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nExpressions\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint expr(int i, int j)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int add            = i + j;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int sub            = i - j;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int mul            = i * j;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int div            = i / j;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int mod            = i % j;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int neg            = -i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int and            = i & j;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int or             = i | j;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int xor            = i ^ j;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int not            = ~i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int shl            = i << 8;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int shr            = i >> 8;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    char equal1        = (i == j);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int equal2         = (i == j);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    char greater       = (i > j);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    char less          = (i < j);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    char greater_equal = (i >= j);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    char less_equal    = (i <= j);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int logical_and    = i && j;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int logical_or     = i || j;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    ++i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    --i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int i1             = i++;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int i2             = ++i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int i3             = i--;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int i4             = --i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nThe full assembly listing is really long.\r\n For that reason, we examine expression by expression.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nadd\r\n\\color inherit\r\n = i + j;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n 80483e1:       mov    edx,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e4:       mov    eax,DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n 80483e7:       add    eax,edx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e9:       mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x34]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe assembly code is straight forward: variable \r\n\\family typewriter\r\ni\r\n\\family default\r\n and \r\n\\family typewriter\r\nj\r\n\\family default\r\n are stored in \r\n\\family typewriter\r\neax\r\n\\family default\r\n and \r\n\\family typewriter\r\nedx\r\n\\family default\r\n respectively, then added together with the \r\n\\family typewriter\r\n\\color red\r\nadd\r\n\\family default\r\n\\color inherit\r\n instruction, and the final result is stored into \r\n\\family typewriter\r\neax\r\n\\family default\r\n.\r\n Then, the result is saved into the local variable \r\n\\family typewriter\r\n\\color blue\r\nadd\r\n\\family default\r\n\\color inherit\r\n, which is at the location \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x34]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nsub\r\n\\color inherit\r\n = i - j;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n80483ec:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n80483ef:       sub    eax,DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80483f2:       mov    DWORD PTR\r\n\\color blue\r\n [ebp-0x30]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to \r\n\\family typewriter\r\nadd\r\n\\family default\r\n instruction, x86 provides a \r\n\\family typewriter\r\n\\color red\r\nsub\r\n\\family default\r\n\\color inherit\r\n instruction for subtraction.\r\n Hence, \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n translates a subtraction into \r\n\\family typewriter\r\n\\color red\r\nsub\r\n\\family default\r\n\\color inherit\r\n instruction, with \r\n\\family typewriter\r\neax\r\n\\family default\r\n is reloaded with \r\n\\family typewriter\r\ni\r\n\\family default\r\n, as \r\n\\family typewriter\r\neax\r\n\\family default\r\n still carries the result from previous expression.\r\n Then, \r\n\\family typewriter\r\nj\r\n\\family default\r\n is subtracted from \r\n\\family typewriter\r\ni\r\n\\family default\r\n.\r\n After the subtraction, the value is saved into the variable \r\n\\family typewriter\r\n\\color blue\r\nsub\r\n\\family default\r\n\\color inherit\r\n, at location \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x30]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nmul\r\n\\color inherit\r\n = i * j;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n80483f5:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n80483f8:       imul   eax,DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80483fc:       mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x34]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to \r\n\\family typewriter\r\nsub\r\n\\family default\r\n instruction, only \r\n\\family typewriter\r\neax\r\n\\family default\r\n is reloaded, since it carries the result of previous calculation.\r\n \r\n\\family typewriter\r\n\\color red\r\nimul\r\n\\family default\r\n\\color inherit\r\n performs signed multiply\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nUnsigned multiply is perform by \r\n\\family typewriter\r\nmul\r\n\\family default\r\n instruction.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n \r\n\\family typewriter\r\neax\r\n\\family default\r\n is first loaded with \r\n\\family typewriter\r\ni\r\n\\family default\r\n, then is multiplied with \r\n\\family typewriter\r\nj\r\n\\family default\r\n and stored the result back into \r\n\\family typewriter\r\neax\r\n\\family default\r\n, then stored into the variable \r\n\\family typewriter\r\n\\color blue\r\nmul\r\n\\family default\r\n\\color inherit\r\n at location \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x34]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\ndiv\r\n\\color inherit\r\n = i / j;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n80483ff:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048402:       cdq    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048403:       idiv   DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048406:       mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x30]\r\n\\color inherit\r\n,\r\n\\color red\r\neax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to \r\n\\family typewriter\r\nimul\r\n\\family default\r\n, \r\n\\family typewriter\r\n\\color red\r\nidiv\r\n\\family default\r\n\\color inherit\r\n performs sign divide.\r\n But, different from \r\n\\family typewriter\r\nimul\r\n\\family default\r\n above \r\n\\family typewriter\r\n\\color red\r\nidiv\r\n\\family default\r\n\\color inherit\r\n only takes one operand:\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nFirst, \r\n\\family typewriter\r\ni\r\n\\family default\r\n is reloaded into \r\n\\family typewriter\r\neax\r\n\\family default\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nThen, \r\n\\family typewriter\r\ncdq\r\n\\family default\r\n converts the double word value in \r\n\\family typewriter\r\neax\r\n\\family default\r\n into a quadword value stored in the pair of registers \r\n\\family typewriter\r\nedx:eax\r\n\\family default\r\n, by copying the signed (bit 31\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nth\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n) of the value in \r\n\\family typewriter\r\neax\r\n\\family default\r\n into every bit position in \r\n\\family typewriter\r\nedx\r\n\\family default\r\n.\r\n The pair \r\n\\family typewriter\r\nedx:eax\r\n\\family default\r\n is the dividend, which is the variable i, and the operand to \r\n\\family typewriter\r\nidiv\r\n\\family default\r\n is the divisor, which is the variable \r\n\\family typewriter\r\nj\r\n\\family default\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nAfter the calculation, the result is stored into the pair \r\n\\family typewriter\r\nedx:eax\r\n\\family default\r\n registers, with the quotient in \r\n\\family typewriter\r\neax\r\n\\family default\r\n and remainder in \r\n\\family typewriter\r\nedx\r\n\\family default\r\n.\r\n The quotient is stored in the variable \r\n\\family typewriter\r\n\\color blue\r\ndiv\r\n\\family default\r\n\\color inherit\r\n, at location \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x30]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nmod\r\n\\color inherit\r\n = i % j;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n8048409:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n804840c:       cdq    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n804840d:       idiv   DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048410:       mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x2c]\r\n\\color inherit\r\n,\r\n\\color red\r\nedx\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe same \r\n\\family typewriter\r\nidiv\r\n\\family default\r\n instruction also performs the modulo operation, since it also calculates\r\n a remainder and stores in the variable \r\n\\family typewriter\r\n\\color blue\r\nmod\r\n\\family default\r\n\\color inherit\r\n, at location \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x2c]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nneg\r\n\\color inherit\r\n = -i;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n8048413:       mov    eax,DWORD PTR [ebp+0x8] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048416:\r\n\\color inherit\r\n \r\n\\color red\r\n      neg    eax       \r\n\\color inherit\r\n              \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048418:       mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x28]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color red\r\nneg\r\n\\family default\r\n\\color inherit\r\n replaces the value of operand (the destination operand) with its two's\r\n complement (this operation is equivalent to subtracting the operand from\r\n 0).\r\n In this example, the value \r\n\\family typewriter\r\ni\r\n\\family default\r\n in \r\n\\family typewriter\r\neax\r\n\\family default\r\n is replaced replaced with \r\n\\family typewriter\r\n-i \r\n\\family default\r\nusing \r\n\\family typewriter\r\n\\color red\r\nneg\r\n\\family default\r\n\\color inherit\r\n instruction.\r\n Then, the new value is stored in the variable neg at \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x28]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nand\r\n\\color inherit\r\n = i & j;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n804841b:        mov    eax,DWORD PTR [ebp+0x8] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n804841e:\r\n\\color inherit\r\n \r\n\\color red\r\n       and    eax,DWORD PTR [ebp+0xc]\r\n\\color inherit\r\n \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048421:        mov    DWORD PTR [ebp-0x24],eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color red\r\nand\r\n\\family default\r\n\\color inherit\r\n performs a bitwise \r\n\\family typewriter\r\nAND\r\n\\family default\r\n operation on two operands, and stores the result in the destination operand,\r\n which is the variable \r\n\\family typewriter\r\n\\color blue\r\nand\r\n\\family default\r\n\\color inherit\r\n at \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x24]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nor\r\n\\color inherit\r\n = i | j;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n8048424:       mov    eax,DWORD PTR [ebp+0x8] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048427:       or     eax,DWORD PTR [ebp+0xc] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n804842a:       mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x20]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to\r\n\\family typewriter\r\n and \r\n\\family default\r\ninstruction,\r\n\\family typewriter\r\n \r\n\\color red\r\nor\r\n\\family default\r\n\\color inherit\r\n performs a bitwise \r\n\\family typewriter\r\nOR\r\n\\family default\r\n operation on two operands, and stores the result in the destination operand,\r\n which is the variable \r\n\\family typewriter\r\n\\color blue\r\nor\r\n\\family default\r\n\\color inherit\r\n at \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x20]\r\n\\family default\r\n\\color inherit\r\n in this case.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nxor\r\n\\color inherit\r\n = i ^ j;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n804842d:       mov    eax,DWORD PTR [ebp+0x8] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048430:       xor    eax,DWORD PTR [ebp+0xc] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048433:       mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x1c]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to\r\n\\family typewriter\r\n and/or \r\n\\family default\r\ninstruction,\r\n\\family typewriter\r\n \r\n\\color red\r\nxor\r\n\\family default\r\n\\color inherit\r\n performs a bitwise \r\n\\family typewriter\r\nXOR\r\n\\family default\r\n operation on two operands, and stores the result in the destination operand,\r\n which is the variable \r\n\\family typewriter\r\n\\color blue\r\nxor\r\n\\family default\r\n\\color inherit\r\n at \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x1c]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nnot\r\n\\color inherit\r\n = ~i;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n8048436:       mov    eax,DWORD PTR [ebp+0x8] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048439:\r\n\\color inherit\r\n \r\n\\color red\r\n      not    eax     \r\n\\color inherit\r\n                \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n804843b:       mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x18]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color red\r\nnot\r\n\\family default\r\n\\color inherit\r\n performs a bitwise \r\n\\family typewriter\r\nNOT\r\n\\family default\r\n operation (each 1 is set to 0, and each 0 is set to 1) on the destination\r\n operand and stores the result in the destination operand location, which\r\n is the variable \r\n\\family typewriter\r\n\\color blue\r\nnot\r\n\\family default\r\n\\color inherit\r\n at \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x18]\r\n\\family default\r\n\\color inherit\r\n.\r\n \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nshl\r\n\\color inherit\r\n = i << 8;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n804843e:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048441:\t   shl    eax,0x8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048444:\t   mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x14]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color red\r\nshl\r\n\\family default\r\n\\color inherit\r\n (shift logical left) shifts the bits in the destination operand to the\r\n left by the number of bits specified in the source operand.\r\n In this case, \r\n\\family typewriter\r\neax\r\n\\family default\r\n stores i and \r\n\\family typewriter\r\n\\color red\r\nshl\r\n\\family default\r\n\\color inherit\r\n shifts \r\n\\family typewriter\r\neax\r\n\\family default\r\n by 8 bits to the left.\r\n A different name for \r\n\\family typewriter\r\n\\color red\r\nshl\r\n\\family default\r\n\\color inherit\r\n is \r\n\\family typewriter\r\nsal\r\n\\family default\r\n \r\n\\emph on\r\n(\r\n\\emph default\r\nshift arithmetic left).\r\n Both can be used synonymous.\r\n Finally, the result is stored in the variable \r\n\\family typewriter\r\n\\color blue\r\nshl\r\n\\family default\r\n\\color inherit\r\n at \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x14]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHere is a visual demonstration of \r\n\\family typewriter\r\nshl\r\n\\family default\r\n/\r\n\\family typewriter\r\nsal \r\n\\family default\r\nand \r\n\\family typewriter\r\nshr\r\n\\family default\r\n instructions:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Shift Instructions (\r\n\\backslash\r\ntextcolor{red}{red} is the start bit, \r\n\\backslash\r\ntextcolor{blue}{blue} is the end bit.)}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[SHL/SAL  (Source: Figure 7-6, Volume 1)]{\r\n\\backslash\r\nlabel{shr}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.55]{images/04/shl_sal}}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nhfill{}\r\n\\backslash\r\nsubfloat[SHR  (Source: Figure 7-7, Volume 1)]{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nincludegraphics[scale=0.55]{images/04/shr}}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nhfill\r\n\\backslash\r\nbreak\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter shifting to the left, the right most bit is set for Carry Flag in\r\n \r\n\\family typewriter\r\nEFLAGS\r\n\\family default\r\n register.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nshr\r\n\\color inherit\r\n = i >> 8;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n8048447:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n804844a:   \tsar    eax,0x8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n804844d:   \tmov    DWORD PTR \r\n\\color blue\r\n[ebp-0x10]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color red\r\nsar\r\n\\family default\r\n\\color inherit\r\n is similar to \r\n\\family typewriter\r\nshl/sal\r\n\\family default\r\n, but shift bits to the right and extends the sign bit.\r\n For right shift, \r\n\\family typewriter\r\nshr\r\n\\family default\r\n and \r\n\\family typewriter\r\n\\color red\r\nsar\r\n\\family default\r\n\\color inherit\r\n are two different instructions.\r\n \r\n\\family typewriter\r\nshr\r\n\\family default\r\n differs to \r\n\\family typewriter\r\n\\color red\r\nsar\r\n\\color inherit\r\n \r\n\\family default\r\nis that it does not extend the sign bit.\r\n Finally, the result is stored in the variable \r\n\\family typewriter\r\n\\color blue\r\nshr\r\n\\family default\r\n\\color inherit\r\n at \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x10]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the figure \r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nref{shr}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n(b), notice that initially, the sign bit is 1, but after 1-bit and 10-bit\r\n shiftings, the shifted-out bits are filled with zeros.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nSAR Instruction Operation (Source: Figure 7-8, Volume 1)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/04/sar.pdf\r\n\tscale 55\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWith \r\n\\family typewriter\r\n\\color red\r\nsar\r\n\\family default\r\n\\color inherit\r\n, the sign bit (the most significant bit) is preserved.\r\n That is, if the sign bit is 0, the new bits always get the value 0; if\r\n the sign bit is 1, the new bits always get the value 1.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nchar \r\n\\color blue\r\nequal1\r\n\\color inherit\r\n = (i == j);\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n8048450:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048453:\r\n\\color inherit\r\n \r\n\\color red\r\n      cmp    eax,DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048456:       sete   al         \r\n\\color inherit\r\n            \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048459:       mov    BYTE PTR \r\n\\color blue\r\n[ebp-0x41]\r\n\\color inherit\r\n,al\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color red\r\ncmp\r\n\\family default\r\n\\color inherit\r\n and variants of the variants of \r\n\\family typewriter\r\n\\color red\r\nset\r\n\\family default\r\n\\color inherit\r\n instructions make up all the logical comparisons.\r\n In this expression, \r\n\\family typewriter\r\n\\color red\r\ncmp\r\n\\family default\r\n\\color inherit\r\n compares variable \r\n\\family typewriter\r\ni\r\n\\family default\r\n and \r\n\\family typewriter\r\nj\r\n\\family default\r\n; then \r\n\\family typewriter\r\n\\color red\r\nsete\r\n\\family default\r\n\\color inherit\r\n stores the value 1 to \r\n\\family typewriter\r\nal\r\n\\family default\r\n register if the comparison from \r\n\\family typewriter\r\n\\color red\r\ncmp\r\n\\family default\r\n\\color inherit\r\n earlier is equal, or stores 0 otherwise.\r\n The general name for variants of \r\n\\family typewriter\r\n\\color red\r\nset\r\n\\family default\r\n\\color inherit\r\n instruction is called \r\n\\family typewriter\r\nSETcc\r\n\\family default\r\n.\r\n The suffix \r\n\\family typewriter\r\ncc\r\n\\family default\r\n denotes the condition being tested for in \r\n\\family typewriter\r\nEFLAGS\r\n\\family default\r\n register.\r\n Appendix B in volume 1, \r\n\\emph on\r\n“EFLAGS Condition Codes”\r\n\\emph default\r\n, lists the conditions it is possible to test for with this instruction.\r\n Finally, the result is stored in the variable \r\n\\family typewriter\r\n\\color blue\r\nequal1\r\n\\family default\r\n\\color inherit\r\n at \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x41]\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint equal2 = (i == j);\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n804845c:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n804845f:       cmp    eax,DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048462:       sete   al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048465:       movzx  eax,al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048468:       mov    DWORD PTR [ebp-0xc],eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to equality comparison, this expression also compares for equality,\r\n with an exception that the result is stored in an int type.\r\n For that reason, one more instruction is a added: \r\n\\family typewriter\r\n\\color red\r\nmovzx\r\n\\family default\r\n\\color inherit\r\n instruction, a variant of \r\n\\family typewriter\r\nmov\r\n\\family default\r\n that copies the result into a destination operand and fills the remaining\r\n bytes with 0.\r\n In this case, since \r\n\\family typewriter\r\neax \r\n\\family default\r\nis 4-byte wide, after copying the first byte in \r\n\\family typewriter\r\nal\r\n\\family default\r\n, the remaining bytes of \r\n\\family typewriter\r\neax\r\n\\family default\r\n are filled with 0 to ensure the \r\n\\family typewriter\r\neax\r\n\\family default\r\n carries the same value as \r\n\\family typewriter\r\nal\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nmovzx\r\n\\family default\r\n instruction\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\neax\r\n\\family default\r\n before \r\n\\family typewriter\r\nmovzx\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"1\" columns=\"4\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n12\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n34\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n56\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n78\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nafter \r\n\\family typewriter\r\nmovzx eax, al\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"1\" columns=\"4\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n78\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nchar greater = (i > j);\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n804846b:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n804846e:       cmp    eax,DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048471:       setg   al \r\n\\color inherit\r\n                    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048474:       mov    BYTE PTR [ebp-0x40],al \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to equality comparison, but used \r\n\\family typewriter\r\n\\color red\r\nsetg\r\n\\family default\r\n\\color inherit\r\n for \r\n\\series bold\r\ng\r\n\\series default\r\nreater comparison instead.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nchar less = (i < j);\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n8048477:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n804847a:\r\n\\color inherit\r\n \r\n\\color red\r\n      cmp    eax,DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n804847d:       setl   al   \r\n\\color inherit\r\n                  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048480:       mov    BYTE PTR [ebp-0x3f],al \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nApplied\r\n\\family typewriter\r\n \r\n\\color red\r\nsetl\r\n\\family default\r\n\\color inherit\r\n for\r\n\\series bold\r\n l\r\n\\series default\r\ness comparison.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nchar greater_equal = (i >= j);\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n8048483:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048486:\r\n\\color inherit\r\n \r\n\\color red\r\n      cmp    eax,DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048489:\r\n\\color inherit\r\n \r\n\\color red\r\n      setge  al    \r\n\\color inherit\r\n                 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n804848c:       mov    BYTE PTR [ebp-0x3e],al \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nApplied \r\n\\family typewriter\r\n\\color red\r\nsetge\r\n\\family default\r\n\\color inherit\r\n for \r\n\\series bold\r\ng\r\n\\series default\r\nreater or \r\n\\series bold\r\ne\r\n\\series default\r\nqual comparison.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nchar less_equal = (i <= j);\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n804848f:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048492:       cmp    eax,DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n8048495:\r\n\\color inherit\r\n \r\n\\color red\r\n      setle  al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048498:       mov    BYTE PTR [ebp-0x3d],al\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nApplied \r\n\\family typewriter\r\n\\color red\r\nsetle\r\n\\family default\r\n\\color inherit\r\n for \r\n\\series bold\r\nl\r\n\\series default\r\ness than or \r\n\\series bold\r\ne\r\n\\series default\r\nqual comparison.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color blue\r\nlogical_and\r\n\\color inherit\r\n = (i && j);\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n804849b:       cmp    DWORD PTR [ebp+0x8],0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n804849f:       je \r\n\\color blue\r\n    \r\n\\color magenta\r\n80484ae\r\n\\color blue\r\n \r\n\\color magenta\r\n<expr+0xd3>\r\n\\color blue\r\n  \r\n\\color inherit\r\n  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n80484a1:       cmp    DWORD PTR [ebp+0xc],0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484a5:       je\r\n\\color blue\r\n     \r\n\\color magenta\r\n80484ae <expr+0xd3>   \r\n\\color blue\r\n \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n80484a7:       mov    eax,0x1      \r\n\\color inherit\r\n          \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484ac:       jmp    \r\n\\color green\r\n80484b3 <expr+0xd8>   \r\n\\color inherit\r\n \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color magenta\r\n80484ae: \r\n\\color inherit\r\n \r\n\\color magenta\r\n     mov    eax,0x0    \r\n\\color inherit\r\n            \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color green\r\n80484b3:\r\n\\color inherit\r\n       mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x8]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLogical \r\n\\family typewriter\r\nAND\r\n\\family default\r\n operator \r\n\\family typewriter\r\n&&\r\n\\family default\r\n is one of the syntaxes that is made entirely in software\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nThat is, there is no equivalent assembly instruction implemented in hardware.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n with simpler instructions.\r\n The algorithm from the assembly code is simple:\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nFirst, check if \r\n\\family typewriter\r\ni\r\n\\family default\r\n is \r\n\\family typewriter\r\n0\r\n\\family default\r\n with the instruction at \r\n\\family typewriter\r\n\\color red\r\n0x804849b\r\n\\family default\r\n\\color inherit\r\n.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Enumerate\r\nIf true, jump to \r\n\\family typewriter\r\n\\color magenta\r\n0x80484ae\r\n\\color inherit\r\n \r\n\\family default\r\nand set eax to 0.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nSet the variable \r\n\\family typewriter\r\nlogical\r\n\\family default\r\n_and to 0, as it is the next instruction after \r\n\\family typewriter\r\n\\color magenta\r\n0x80484ae\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Enumerate\r\nIf \r\n\\family typewriter\r\ni\r\n\\family default\r\n is not \r\n\\family typewriter\r\n0\r\n\\family default\r\n, check if \r\n\\family typewriter\r\nj\r\n\\family default\r\n is \r\n\\family typewriter\r\n0\r\n\\family default\r\n with the instruction at \r\n\\family typewriter\r\n\\color red\r\n0x80484a1\r\n\\family default\r\n\\color inherit\r\n.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Enumerate\r\nIf true, jump to \r\n\\family typewriter\r\n\\color magenta\r\n0x80484ae\r\n\\color inherit\r\n \r\n\\family default\r\nand set eax to 0.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nSet the variable \r\n\\family typewriter\r\nlogical\r\n\\family default\r\n_and to 0, as it is the next instruction after \r\n\\family typewriter\r\n\\color magenta\r\n0x80484ae\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Enumerate\r\nIf both \r\n\\family typewriter\r\ni\r\n\\family default\r\n and \r\n\\family typewriter\r\nj\r\n\\family default\r\n are not \r\n\\family typewriter\r\n0\r\n\\family default\r\n, the result is certainly \r\n\\family typewriter\r\n1\r\n\\family default\r\n, or \r\n\\family typewriter\r\ntrue\r\n\\family default\r\n.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Enumerate\r\nSet it accordingly with the instruction at \r\n\\family typewriter\r\n\\color red\r\n0x80484a7\r\n\\family default\r\n\\color inherit\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nThen jump to the instruction at \r\n\\family typewriter\r\n\\color green\r\n0x80484b3\r\n\\family default\r\n\\color inherit\r\n to set the variable \r\n\\family typewriter\r\n\\color blue\r\nlogical_and\r\n\\family default\r\n\\color inherit\r\n at\r\n\\color blue\r\n \r\n\\family typewriter\r\n[ebp-0x8]\r\n\\family default\r\n\\color inherit\r\n to 1.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint logical_or = (i || j);\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n80484b6:       cmp    DWORD PTR [ebp+0x8],0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484ba:       jne    80484c2 <expr+0xe7>    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484bc:       cmp    DWORD PTR [ebp+0xc],0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484c0:       je     80484c9 <expr+0xee>    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484c2:       mov    eax,0x1                \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484c7:       jmp    80484ce <expr+0xf3>    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484c9:       mov    eax,0x0                \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484ce:       mov    DWORD PTR [ebp-0x4],eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLogical \r\n\\family typewriter\r\nOR\r\n\\family default\r\n operator\r\n\\family typewriter\r\n ||\r\n\\family default\r\n is similar to logical and above.\r\n Understand the algorithm is left as an exercise for readers.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\n++i; \r\n\\family default\r\nand\r\n\\family typewriter\r\n --i; \r\n\\family default\r\n(or \r\n\\family typewriter\r\ni++\r\n\\family default\r\n and \r\n\\family typewriter\r\ni--\r\n\\family default\r\n)\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n80484d1: \r\n\\color red\r\n      add    DWORD PTR [ebp+0x8],0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484d5: \r\n\\color red\r\n      sub    DWORD PTR [ebp+0x8],0x1\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe syntax of increment and decrement is similar to logical \r\n\\family typewriter\r\nAND\r\n\\family default\r\n and logical \r\n\\family typewriter\r\nOR\r\n\\family default\r\n in that it is made from existing instruction, that is \r\n\\family typewriter\r\nadd\r\n\\family default\r\n.\r\n The difference is that the CPU actually does has a built-in instruction,\r\n but \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n decided not to use the instruction because \r\n\\family typewriter\r\ninc\r\n\\family default\r\n and \r\n\\family typewriter\r\ndec\r\n\\family default\r\n cause a \r\n\\emph on\r\npartial flag register stall\r\n\\emph default\r\n, occurs when an instruction modifies a part of the flag register and the\r\n following instruction is dependent on the outcome of the flags (\r\n\\emph on\r\nsection 3.5.2.6\r\n\\emph default\r\n, \r\n\\begin_inset CommandInset citation\r\nLatexCommand citealt\r\nbefore \"Optimization Manual,\"\r\nkey \"intel_opt_manual\"\r\nliteral \"true\"\r\n\r\n\\end_inset\r\n\r\n).\r\n The manual even suggests that \r\n\\family typewriter\r\ninc\r\n\\family default\r\n and \r\n\\family typewriter\r\ndec\r\n\\family default\r\n should be replaced with \r\n\\family typewriter\r\nadd\r\n\\family default\r\n and \r\n\\family typewriter\r\nsub\r\n\\family default\r\n instructions (section \r\n\\emph on\r\n3.5.1.1\r\n\\emph default\r\n, \r\n\\begin_inset CommandInset citation\r\nLatexCommand citealt\r\nbefore \"Optimization Manual,\"\r\nkey \"intel_opt_manual\"\r\nliteral \"true\"\r\n\r\n\\end_inset\r\n\r\n).\r\n \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint \r\n\\color green\r\ni1\r\n\\color inherit\r\n = i++;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n80484d9\r\n\\color inherit\r\n:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n80484dc:       lea    edx,[eax+0x1]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n80484df\r\n\\color inherit\r\n:       mov    DWORD PTR [ebp+0x8],edx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n80484e2\r\n\\color inherit\r\n:       mov    DWORD PTR \r\n\\color green\r\n[ebp-0x10]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFirst, \r\n\\family typewriter\r\ni\r\n\\family default\r\n is copied into \r\n\\family typewriter\r\neax\r\n\\family default\r\n at \r\n\\family typewriter\r\n\\color blue\r\n80484d9\r\n\\family default\r\n\\color inherit\r\n.\r\n Then, the value of \r\n\\family typewriter\r\neax + 0x1\r\n\\family default\r\n is copied into \r\n\\family typewriter\r\nedx\r\n\\family default\r\n as an \r\n\\emph on\r\neffective address\r\n\\emph default\r\n at \r\n\\family typewriter\r\n\\color red\r\n80484dc\r\n\\family default\r\n\\color inherit\r\n.\r\n The \r\n\\family typewriter\r\nlea\r\n\\family default\r\n (\r\n\\emph on\r\nload effective address\r\n\\emph default\r\n) instruction copies a memory address into a register.\r\n According to Volume 2, the source operand is a memory address specified\r\n with one of the processors addressing modes.\r\n This means, the source operand must be specified by the addressing modes\r\n defined in 16-bit/32-bit ModR/M Byte tables, \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"mod-rm-16\"\r\n\r\n\\end_inset\r\n\r\n and \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"mod-rm-32\"\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter loading the incremented value into \r\n\\family typewriter\r\nedx\r\n\\family default\r\n, the value of \r\n\\family typewriter\r\ni\r\n\\family default\r\n is increased by 1 at \r\n\\family typewriter\r\n\\color blue\r\n80484df\r\n\\family default\r\n\\color inherit\r\n.\r\n Finally, the \r\n\\emph on\r\nprevious\r\n\\emph default\r\n \r\n\\family typewriter\r\ni\r\n\\family default\r\n value is stored back to \r\n\\family typewriter\r\n\\color green\r\ni1\r\n\\family default\r\n\\color inherit\r\n at \r\n\\family typewriter\r\n\\color green\r\n[ebp-0x8]\r\n\\family default\r\n\\color inherit\r\n by the instruction at \r\n\\family typewriter\r\n\\color blue\r\n80484e2\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint i2 = ++i;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n80484e5:       add    DWORD PTR [ebp+0x8],0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484e9:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484ec:       mov    DWORD PTR [ebp-0xc],eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe primary differences between this increment syntax and the previous one\r\n are:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n\\color red\r\nadd\r\n\\family default\r\n\\color inherit\r\n is used instead of \r\n\\family typewriter\r\nlea \r\n\\family default\r\nto increase \r\n\\family typewriter\r\ni\r\n\\family default\r\n directly.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nthe newly incremented \r\n\\family typewriter\r\ni\r\n\\family default\r\n is stored into \r\n\\family typewriter\r\ni2\r\n\\family default\r\n instead of the old value.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nthe expression only costs 3 instructions instead of 4.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis prefix-increment syntax is faster than the post-fix one used previously.\r\n It might not matter much which version to use if the increment is only\r\n used once or a few hundred times in a small loop, but it matters when a\r\n loop runs millions or more times.\r\n Also, depends on different circumstances, it is more convenient to use\r\n one over the other e.g.\r\n if \r\n\\family typewriter\r\ni\r\n\\family default\r\n is an index for accessing an array, we want to use the old value for accessing\r\n previous array element and newly incremented \r\n\\family typewriter\r\ni\r\n\\family default\r\n for current element.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression: \r\n\\family typewriter\r\nint i3 = i--;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n80484ef:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484f2:       lea    edx,[eax-0x1]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484f5:       mov    DWORD PTR [ebp+0x8],edx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484f8:       mov    DWORD PTR [ebp-0x8],eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to \r\n\\family typewriter\r\ni++\r\n\\family default\r\n syntax, and is left as an exercise to readers.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nExpression:\r\n\\family typewriter\r\n int i4 = --i;\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n80484fb:       sub    DWORD PTR [ebp+0x8],0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n80484ff:       mov    eax,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8048502:       mov    DWORD PTR [ebp-0x4],eax\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to \r\n\\family typewriter\r\n++i\r\n\\family default\r\n syntax, and is left as an exercise to readers.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Exercise\r\nRead section \r\n\\emph on\r\n3.5.2.4\r\n\\emph default\r\n, \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nPartial Register Stalls\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n to understand register stalls in general.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Exercise\r\nRead the sections from \r\n\\emph on\r\n7.3.1\r\n\\emph default\r\n to \r\n\\emph on\r\n7.3.7\r\n\\emph default\r\n in volume 1.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nStack\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA stack is a contiguous array of memory locations that holds a collection\r\n of discrete data.\r\n When a new element is added, a stack \r\n\\emph on\r\ngrows down\r\n\\emph default\r\n in memory toward lesser addresses, and \r\n\\emph on\r\nshrinks up\r\n\\emph default\r\n toward greater addresses when an element is removed.\r\n x86 uses the \r\n\\family typewriter\r\nesp\r\n\\family default\r\n register to point to the top of the stack, at the newest element.\r\n A stack can be originated anywhere in main memory, as \r\n\\family typewriter\r\nesp\r\n\\family default\r\n can be set to any memory address.\r\n x86 provides two operations for manipulating stacks:\r\n\\family typewriter\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\npush\r\n\\family default\r\n instruction and its variants add a new element on top of the stack\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\npop\r\n\\family default\r\n instructions and its variants remove the top-most element from the stack.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Stack operations}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[Initial state at address \r\n\\backslash\r\ntexttt{0x10004}] {\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"5\" columns=\"4\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"0.35cm\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10002\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10003\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10004\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color red\r\n12\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\leftarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nesp\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hspace*{\\fill}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[After executing \r\n\\backslash\r\ntexttt{push word 0x5678}] {\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"5\" columns=\"4\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"0.35cm\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10002\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color blue\r\n78\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\leftarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nesp\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10003\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color blue\r\n56\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10004\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color red\r\n12\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hspace*{\\fill}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nsubfloat[After executing \r\n\\backslash\r\ntexttt{pop word}] {\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"5\" columns=\"4\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"0.35cm\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10001\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10002\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10003\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10004\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color red\r\n12\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\leftarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nesp\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nAutomatic variables\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLocal variables are variables that exist within a scope.\r\n A scope is delimited by a pair of braces: \r\n\\family typewriter\r\n{..}\r\n\\family default\r\n.\r\n The most common scope to define local variables is at function scope.\r\n However, scope can be unnamed, and variables created inside an unnamed\r\n scope do not exist outside of its scope and its inner scope.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nFunction scope:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid foo() {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int a;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int b;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\na\r\n\\family default\r\n and \r\n\\family typewriter\r\nb\r\n\\family default\r\n are variables local to the function \r\n\\family typewriter\r\nfoo\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nUnnamed scope:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint foo() {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        int a = 1;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        int b = 2;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n            return i = a + b;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\na\r\n\\family default\r\n and \r\n\\family typewriter\r\nb\r\n\\family default\r\n are local to where it is defined and local into its inner child scope that\r\n return \r\n\\family typewriter\r\ni = a + b\r\n\\family default\r\n.\r\n However, they do not exist at the function scope that creates \r\n\\family typewriter\r\ni\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nWhen a local variable is created, it is pushed on the stack; when a local\r\n variable goes out of scope, it is pop out of the stack, thus destroyed.\r\n When an argument is passed from a caller to a callee, it is pushed on the\r\n stack; when a callee returns to the caller, the arguments are popped out\r\n the stack.\r\n The local variables and arguments are automatically allocated upon enter\r\n a function and destroyed after exiting a function, that's why it's called\r\n \r\n\\emph on\r\nautomatic variables\r\n\\emph default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA base frame pointer points to the start of the current function frame,\r\n and is kept in \r\n\\family typewriter\r\nebp \r\n\\family default\r\nregister.\r\n Whenever a function is called, it is allocated with its own dedicated storage\r\n on stack, called \r\n\\emph on\r\nstack frame\r\n\\emph default\r\n.\r\n A stack frame is where all local variables and arguments of a function\r\n are placed on a stack\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nData and only data are exclusively allocated on stack for every stack frame.\r\n No code resides here.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen a function needs a local variable or an argument, it uses \r\n\\family typewriter\r\nebp\r\n\\family default\r\n to access a variable:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nAll local variables are allocated after the \r\n\\family typewriter\r\nebp\r\n\\family default\r\n pointer.\r\n Thus, to access a local variable, a number is subtracted from \r\n\\family typewriter\r\nebp\r\n\\family default\r\n to reach the location of the variable.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nAll arguments are allocated before \r\n\\family typewriter\r\nebp\r\n\\family default\r\n pointer.\r\n To access an argument, a number is added to \r\n\\family typewriter\r\nebp\r\n\\family default\r\n to reach the location of the argument.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe \r\n\\family typewriter\r\nebp\r\n\\family default\r\n itself pointer points to the return address of its caller.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Function arguments and local variables}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"3\" columns=\"15\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"2.7cm\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nPrevious Frame\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nCurrent Frame\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nFunction Arguments \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nebp\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nLocal variables\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nA1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nA2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nA3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\n........\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nAn\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\" width=\"2.7cm\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nReturn Address\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nOld ebp\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nL1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nL2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nL3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\n........\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nLn\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nA\r\n\\family default\r\n = Argument\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nL\r\n\\family default\r\n = Local Variable\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHere is an example to make it more concrete:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint add(int @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries a|@, int @|\r\n\\backslash\r\ncolor{green}\r\n\\backslash\r\nbfseries b|@) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int @|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries i|@ = @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries a|@ + @|\r\n\\backslash\r\ncolor{green}\r\n\\backslash\r\nbfseries b|@;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n080483db <add>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#include <stdint.h>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint add(int \r\n\\color red\r\na\r\n\\color inherit\r\n, int \r\n\\color green\r\nb\r\n\\color inherit\r\n) {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483db:       push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483dc:       mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483de:       \r\n\\color magenta\r\nsub    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    int \r\n\\color blue\r\ni\r\n\\color inherit\r\n = \r\n\\color red\r\na\r\n\\color inherit\r\n + \r\n\\color green\r\nb\r\n\\color inherit\r\n;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e1:       mov    edx,DWORD PTR\r\n\\color red\r\n [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e4:       mov    eax,DWORD PTR \r\n\\color green\r\n[ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e7:       add    eax,edx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e9:       mov    DWORD PTR \r\n\\color blue\r\n[ebp-0x4]\r\n\\color inherit\r\n,eax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    return \r\n\\color blue\r\ni\r\n\\color inherit\r\n;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483ec:       mov    eax,DWORD PTR \r\n\\color blue\r\n[ebp-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483ef:       leave  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f0:       ret    \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nIn the assembly listing, \r\n\\family typewriter\r\n\\color blue\r\n[ebp-0x4]\r\n\\family default\r\n\\color inherit\r\n is the local variable \r\n\\family typewriter\r\n\\color blue\r\ni\r\n\\family default\r\n\\color inherit\r\n, since it is allocated \r\n\\emph on\r\nafter \r\n\\family typewriter\r\n\\emph default\r\nebp\r\n\\family default\r\n, with the length of 4 bytes (an \r\n\\family typewriter\r\nint\r\n\\family default\r\n).\r\n On the other hand,\r\n\\family typewriter\r\n \r\n\\color red\r\na\r\n\\family default\r\n\\color inherit\r\n and \r\n\\family typewriter\r\n\\color green\r\nb\r\n\\family default\r\n\\color inherit\r\n are arguments and can be accessed with \r\n\\family typewriter\r\nebp\r\n\\family default\r\n: \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n\\color red\r\n[ebp+0x8]\r\n\\color inherit\r\n \r\n\\family default\r\naccesses \r\n\\family typewriter\r\n\\color red\r\na\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n\\color green\r\n[ebp+0xc]\r\n\\family default\r\n\\color inherit\r\n access \r\n\\family typewriter\r\n\\color green\r\nb\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFor accessing arguments, the rule is that the closer a variable on stack\r\n to \r\n\\family typewriter\r\nebp\r\n\\family default\r\n, the closer it is to a function name.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Function arguments and local variables in memory}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"9\" columns=\"17\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nebp+0xc\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nebp+0x8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nebp+0x4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nebp\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\begin_inset Formula $\\downarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\downarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\downarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\downarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n02\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n03\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n04\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n05\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n06\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n07\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n08\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n09\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0a\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0b\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0c\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0d\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0e\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0f\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0x10000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\color green\r\nb\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\color red\r\na\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nReturn Address\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nOld ebp\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nebp+0x8\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nebp+0x4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\downarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\downarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n02\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n03\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n04\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n05\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n06\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n07\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n08\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n09\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0a\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0b\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0c\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0d\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0e\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0f\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n0xffe0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nN\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color blue\r\ni\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nN\r\n\\family default\r\n = Next local variable starts here\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFrom the figure, we can see that \r\n\\family typewriter\r\na\r\n\\family default\r\n and \r\n\\family typewriter\r\nb\r\n\\family default\r\n are laid out in memory with the exact order as written in C, relative to\r\n the return address.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nFunction Call and Return\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"subsec:Function-Call-and\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint add(int a, int b) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int local = 0x12345;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return a + b;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    add(1,1);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nFor every function call, \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n pushes arguments on the stack in reversed order with the \r\n\\family typewriter\r\n\\color red\r\npush\r\n\\family default\r\n\\color inherit\r\n instructions.\r\n That is, the arguments pushed on stack are in reserved order as it is written\r\n in high level C code, to ensure the relative order between arguments, as\r\n seen in previous section how function arguments and local variables are\r\n laid out.\r\n Then, \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n generates a \r\n\\family typewriter\r\n\\color blue\r\ncall\r\n\\family default\r\n\\color inherit\r\n instruction, which then implicitly pushes a return address before transferring\r\n the control to \r\n\\family typewriter\r\nadd\r\n\\family default\r\n function:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n080483f2 <main>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f2:       push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f3:       mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    add(1,2);\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n80483f5:       push   0x2\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n 80483f7:       push   0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n 80483f9:       call   80483db <add>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color green\r\n80483fe:       add    esp,0x8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048401:       mov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color magenta\r\n 8048406:       leave \r\n\\color red\r\n \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color cyan\r\n 8048407:       ret  \r\n\\color inherit\r\n  \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nUpon finishing the call to \r\n\\family typewriter\r\nadd\r\n\\family default\r\n function, the stack is restored by adding \r\n\\family typewriter\r\n0x8\r\n\\family default\r\n to stack pointer \r\n\\family typewriter\r\nesp \r\n\\family default\r\n(which is equivalent to 2 \r\n\\family typewriter\r\npop\r\n\\family default\r\n instructions).\r\n Finally, a \r\n\\family typewriter\r\nleave\r\n\\family default\r\n instruction is executed and main returns with a \r\n\\family typewriter\r\nret\r\n\\family default\r\n instruction.\r\n A \r\n\\family typewriter\r\nret\r\n\\family default\r\n instruction transfers the program execution back to the caller to the instructi\r\non right after the \r\n\\family typewriter\r\ncall\r\n\\family default\r\n instruction, the \r\n\\family typewriter\r\nadd\r\n\\family default\r\n instruction.\r\n The reason \r\n\\family typewriter\r\nret\r\n\\family default\r\n can return to such location is that the return address implicitly pushed\r\n by the \r\n\\family typewriter\r\ncall\r\n\\family default\r\n instruction, which is the address right after the \r\n\\family typewriter\r\ncall\r\n\\family default\r\n instruction; whenever the CPU executes \r\n\\family typewriter\r\nret\r\n\\family default\r\n instruction, it retrieves the return address that sits right after all\r\n the arguments on the stack:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAt the end of a function, \r\n\\family typewriter\r\ngcc \r\n\\family default\r\nplaces a \r\n\\family typewriter\r\nleave\r\n\\family default\r\n instruction to clean up all spaces allocated for local variables and restore\r\n the frame pointer to frame pointer of the caller.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n080483db <add>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint add(int a, int b) {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n80483db\r\n\\color inherit\r\n:       \r\n\\color red\r\npush   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n80483dc\r\n\\color inherit\r\n:       \r\n\\color red\r\nmov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483de:       sub    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    int \r\n\\color blue\r\nlocal\r\n\\color inherit\r\n = \r\n\\color magenta\r\n0x12345\r\n\\color inherit\r\n;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e1:       \r\n\\color blue\r\nDWORD PTR [ebp-0x4]\r\n\\color magenta\r\n,0x12345\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    return a + b;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e8:       mov    edx,DWORD PTR [ebp+0x8]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483eb:       mov    eax,DWORD PTR [ebp+0xc]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483ee:       \r\n\\color red\r\nadd    eax,edx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n80483f0\r\n\\color inherit\r\n:       \r\n\\color red\r\nleave\r\n\\color inherit\r\n  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n80483f1\r\n\\color inherit\r\n:       \r\n\\color red\r\nret\r\n\\color inherit\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nThe above code that \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n generated for function calling is actually the standard method x86 defined.\r\n Read chapter 6, \r\n\\emph on\r\n\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nProduce Calls, Interrupts, and Exceptions\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n,\r\n\\emph default\r\n Intel manual volume 1.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nLoop\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLoop is simply resetting the instruction pointer to an already executed\r\n instruction and starting from there all over again.\r\n A loop is just one application of \r\n\\family typewriter\r\njmp\r\n\\family default\r\n instruction.\r\n However, because looping is a pervasive pattern, it earned its own syntax\r\n in C.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    for (int i = 0; i < 10; i++) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n080483db <main>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483db:       push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483dc:       mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483de:       sub    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    for (\r\n\\color red\r\nint i = 0\r\n\\color inherit\r\n; \r\n\\color green\r\ni < 10\r\n\\color inherit\r\n; i++) {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n 80483e1:       \r\n\\color inherit\r\nmov \r\n\\color red\r\n   DWORD PTR [ebp-0x4],0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color green\r\n 80483e8:\r\n\\color inherit\r\n \r\n\\color green\r\n      jmp    80483ee <main+0x13>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n 80483ea:\r\n\\color inherit\r\n \r\n\\color blue\r\n      add    DWORD PTR [ebp-0x4],0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color green\r\n 80483ee:       cmp    DWORD PTR [ebp-0x4],0x9\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color green\r\n 80483f2:       jle    \r\n\\color blue\r\n80483ea\r\n\\color green\r\n <main+0xf>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    }\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f4: b8 00 00 00 00        mov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f9: c9                    leave\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483fa: c3                    ret\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483fb: 66 90                 xchg   ax,ax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483fd: 66 90                 xchg   ax,ax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483ff: 90                    nop\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe colors mark corresponding high level code to assembly code:\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nThe \r\n\\color red\r\nred\r\n\\color inherit\r\n instruction initialize \r\n\\family typewriter\r\ni\r\n\\family default\r\n to \r\n\\family typewriter\r\n0\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nThe \r\n\\color green\r\ngreen\r\n\\color inherit\r\n instructions compare i to 10 by using \r\n\\family typewriter\r\njle\r\n\\family default\r\n and compare it to \r\n\\family typewriter\r\n9\r\n\\family default\r\n.\r\n If true, jump to \r\n\\family typewriter\r\n\\color blue\r\n80483ea\r\n\\family default\r\n\\color inherit\r\n for another iteration.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nThe \r\n\\color blue\r\nblue\r\n\\color inherit\r\n instruction increase i by 1, making the loop able to terminate once the\r\n terminate condition is satisfied.\r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nWhy does the increment instruction (the \r\n\\color blue\r\nblue\r\n\\color inherit\r\n instruction) appears before the compare instructions (the \r\n\\color green\r\ngreen\r\n\\color inherit\r\n instructions)?\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Exercise\r\nWhat assembly code can be generated for \r\n\\family typewriter\r\nwhile\r\n\\family default\r\n and \r\n\\family typewriter\r\ndo...while\r\n\\family default\r\n?\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nConditional\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAgain, conditional in C with \r\n\\family typewriter\r\nif...else...\r\n\r\n\\family default\r\n construct is just another application of \r\n\\family typewriter\r\njmp\r\n\\family default\r\n instruction under the hood.\r\n It is also a pervasive pattern that earned its own syntax in C.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSource\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int i = 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    if (argc) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        i = 1;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    } else {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        i = 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nAssembly\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\nint main(int argc, char *argv[]) {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483db:        push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483dc:        mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483de:        sub    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    int i = 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e1:        mov    DWORD PTR [ebp-0x4],0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   \r\n\\color red\r\n if (argc) {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n80483e8\r\n\\color inherit\r\n:       \r\n\\color red\r\n cmp    DWORD PTR [ebp+0x8],0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n80483ec\r\n\\color inherit\r\n:        \r\n\\color red\r\nje\r\n\\color inherit\r\n     \r\n\\color blue\r\n80483f7\r\n\\color inherit\r\n <main+0x1c>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     \r\n\\color red\r\n   i = 1;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n80483ee\r\n\\color inherit\r\n:        \r\n\\color red\r\nmov    DWORD PTR [ebp-0x4],0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f5:  \r\n\\color red\r\n      jmp\r\n\\color inherit\r\n    \r\n\\color green\r\n80483fe\r\n\\color inherit\r\n <main+0x23>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    \r\n\\color blue\r\n} else {\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     \r\n\\color blue\r\n   i = 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color blue\r\n80483f7\r\n\\color inherit\r\n:        \r\n\\color blue\r\nmov    DWORD PTR [ebp-0x4],0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n    }\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    \r\n\\color green\r\nreturn 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color green\r\n80483fe\r\n\\color inherit\r\n:        \r\n\\color green\r\nmov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048403:        leave  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048404:        ret    \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe generated assembly code follows the same order as the corresponding\r\n high level syntax:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\color red\r\nred\r\n\\color inherit\r\n instructions represents \r\n\\family typewriter\r\nif\r\n\\family default\r\n branch.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\color blue\r\nblue\r\n\\color inherit\r\n instructions represents \r\n\\family typewriter\r\nelse\r\n\\family default\r\n branch.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\color green\r\ngreen\r\n\\color inherit\r\n instruction is the exit point for both \r\n\\family typewriter\r\nif\r\n\\family default\r\n and \r\n\\family typewriter\r\nelse\r\n\\family default\r\n branch.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nif\r\n\\family default\r\n branch first compares whether \r\n\\family typewriter\r\nargc\r\n\\family default\r\n is \r\n\\emph on\r\nfalse\r\n\\emph default\r\n (equal to 0) with \r\n\\family typewriter\r\ncmp \r\n\\family default\r\ninstruction.\r\n If true, it proceeds to \r\n\\family typewriter\r\nelse\r\n\\family default\r\n branch at \r\n\\family typewriter\r\n\\color blue\r\n80483f7\r\n\\family default\r\n\\color inherit\r\n.\r\n Otherwise, \r\n\\family typewriter\r\nif\r\n\\family default\r\n branch continues with the code of its branch, which is the next instruction\r\n at \r\n\\family typewriter\r\n\\color red\r\n80483ee\r\n\\color inherit\r\n \r\n\\family default\r\nfor copying \r\n\\family typewriter\r\n1\r\n\\family default\r\n to \r\n\\family typewriter\r\ni\r\n\\family default\r\n.\r\n\r\n\\family typewriter\r\n \r\n\\family default\r\nFinally, it skips over \r\n\\family typewriter\r\nelse\r\n\\family default\r\n branch and proceeds to \r\n\\family typewriter\r\n\\color green\r\n80483fe\r\n\\family default\r\n\\color inherit\r\n, which is the next instruction pasts the \r\n\\family typewriter\r\nif..else...\r\n\r\n\\family default\r\n construct.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nelse\r\n\\family default\r\n branch is entered when \r\n\\family typewriter\r\ncmp\r\n\\family default\r\n instruction from \r\n\\family typewriter\r\nif\r\n\\family default\r\n branch is true.\r\n \r\n\\family typewriter\r\nelse\r\n\\family default\r\n branch starts at \r\n\\family typewriter\r\n\\color blue\r\n80483f7\r\n\\family default\r\n\\color inherit\r\n, which is the first instruction of \r\n\\family typewriter\r\nelse\r\n\\family default\r\n branch.\r\n The instruction copies \r\n\\family typewriter\r\n0\r\n\\family default\r\n to \r\n\\family typewriter\r\ni\r\n\\family default\r\n, and proceeds naturally to the next instruction pasts the \r\n\\family typewriter\r\nif...else...\r\n\r\n\\family default\r\n construct without any jump.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Chapter\r\nThe Anatomy of a Program\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"chap:The-Anatomy-of-a-program\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEvery program consists of code and data, and only those two components made\r\n up a program.\r\n However, if a program consists purely code and data of its own, from the\r\n perspective of an operating system (as well as human), it does not know\r\n in a program, which block of binary is a program and which is just raw\r\n data, where in the program to start execution, which region of memory should\r\n be protected and which is free to modify.\r\n For that reason, each program carries extra metadata to communicate with\r\n the operating system how to handle the program.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen a source file is compiled, the generated machine code is stored into\r\n an \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nobject file\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nobject file\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\nobject file\r\n\\emph default\r\n, which is just a block of binary.\r\n One or more object files can be combined to produce an \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nexecutable binary\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nexecutable binary\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\nexecutable binary\r\n\\emph default\r\n, which is a complete program runnable in an operating system.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nreadelf\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is a program that recognizes and displays the ELF metadata of a binary\r\n file, be it an object file or an executable binary.\r\n \r\n\\series bold\r\n\\emph on\r\nELF\r\n\\series default\r\n\\emph default\r\n, or \r\n\\series bold\r\n\\emph on\r\nE\r\n\\series default\r\nxecutable and \r\n\\series bold\r\nL\r\n\\series default\r\ninkable \r\n\\series bold\r\nF\r\n\\series default\r\normat\r\n\\emph default\r\n, is the content at the very beginning of an executable to provide an operating\r\n system necessary information to load into main memory and run the executable.\r\n ELF can be thought of similar to the table of contents of a book.\r\n In a book, a table of contents list the page numbers of the main sections,\r\n subsections, sometimes even figures and tables for easy lookup.\r\n Similarly, ELF lists various sections used for code and data, and the memory\r\n addresses of each symbol along with other information.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAn ELF binary is composed of:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nAn \r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nELF header\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nELF header\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nELF header\r\n\\emph default\r\n: the very first section of an executable that describes the file's organization.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nA \r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nProgram header table\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nprogram header table\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nprogram header table\r\n\\emph default\r\n: is an array of fixed-size structures that describes segments of an executable.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nA \r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSection header table\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nsection header table\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nsection header table\r\n\\emph default\r\n: is an array of fixed-size structures that describes sections of an executable.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\emph on\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSegments and section\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nSegments and section\r\n\\emph default\r\ns\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nSegments and section\r\n\\emph default\r\ns are the main content of an ELF binary, which are the code and data, divided\r\n into chunks of different purposes.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\nsegment\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nsegment\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a composition of zero or more sections and is directly loaded by an\r\n operating system at runtime.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\nsection\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nsection\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a block of binary that is either:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nactual program code and data that is available in memory when a program\r\n runs.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nmetadata about other sections used only in the linking process, and disappear\r\n from the final executable.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLinker uses sections to build segments.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nELF - Linking View vs Executable View (Source: Wikipedia)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/05/Elf-layout--en.pdf\r\n\tscale 30\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLater we will compile our kernel as an ELF executable with GCC, and explicitly\r\n specify how segments are created and where they are loaded in memory through\r\n the use a \r\n\\emph on\r\nlinker script\r\n\\emph default\r\n, a text file to instruct how a linker should generate a binary.\r\n For now, we will examine the anatomy of an ELF executable in detail.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nReference documents: \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe \r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nELF specification\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nELF specification is bundled as a \r\n\\family typewriter\r\nman\r\n\\family default\r\n page in Linux:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ man elf\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIt is a useful resource to understand and implement ELF.\r\n However, it will be much easier to use after you finish this chapter, as\r\n the specification mixes implementation details in it.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe default specification is a generic one, in which every ELF implementation\r\n follows.\r\n However, each platform provides extra features unique to it.\r\n The ELF specification for x86 is currently maintained on Github by H.J.\r\n Lu: \r\n\\begin_inset Flex URL\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttps://github.com/hjl-tools/x86-psABI/wiki/X86-psABI\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nPlatform-dependent details are referred to as \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nprocessor specific\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n in the generic ELF specification.\r\n We will not explore these details, but study the generic details, which\r\n are enough for crafting an ELF binary image for our operating system.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nELF header\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo see the information of an ELF header:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -h hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nThe output:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nELF Header:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Class:                             ELF64   \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Data:                              2's complement, little endian\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Version:                           1 (current)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  OS/ABI:                            UNIX - System V\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  ABI Version:                       0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type:                              EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Machine:                           Advanced Micro Devices X86-64\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Version:                           0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Entry point address:               0x400430\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Start of program headers:          64 (bytes into file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Start of section headers:          6648 (bytes into file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Flags:                             0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of this header:               64 (bytes)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of program headers:           56 (bytes)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Number of program headers:         9\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of section headers:           64 (bytes)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Number of section headers:         31\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSection header string table index: 28\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLet's go through each field:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMagic\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nDisplays the raw bytes that uniquely addresses a file is an ELF executable\r\n binary.\r\n Each byte gives a brief information.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the example, we have the following magic bytes:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\\noindent\r\n\\align left\r\nMagic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nExamine byte by byte:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset space \\quad{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"12\" columns=\"2\">\r\n<features booktabs=\"true\" tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"60col%\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nByte\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n7f 45 4c 46\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPredefined values.\r\n The first byte is always \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n7F\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, the remaining 3 bytes represent the string \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nELF\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n02\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSee \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nClass\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n field below.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSee \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nData\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n field below.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSee \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nVersion\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n field below.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSee \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nOS/ABI\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n field below.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n00 00 00 00 00 00 00 00\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPadding bytes.\r\n These bytes are unused and are always set to 0.\r\n Padding bytes are added for proper alignment, and is reserved for future\r\n use when more information is needed.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nClass\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nA byte in \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMagic\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n field.\r\n It specifies the class or capacity of a file.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nPossible values:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset space \\quad{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"4\" columns=\"2\">\r\n<features booktabs=\"true\" tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0pt\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nValue\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nInvalid class\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n32-bit objects\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n2\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n64-bit objects\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nData\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nA byte in \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMagic\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n field.\r\n It specifies the data encoding of the processor-specific data in the object\r\n file.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nPossible values:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset space \\quad{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"4\" columns=\"2\">\r\n<features booktabs=\"true\" tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0pt\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nValue\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nInvalid data encoding\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nLittle endian, 2's complement\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n2\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nBig endian, 2's complement\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nVersion\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nA byte in \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMagic\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n It specifies the ELF header version number.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nPossible values:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset space \\quad{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"3\" columns=\"2\">\r\n<features booktabs=\"true\" tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0pt\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" bottomline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nValue\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nInvalid version\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nCurrent version\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nOS/ABI\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nA byte in \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMagic\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n field.\r\n It specifies the target operating system \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nABI\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n Originally, it was a padding byte.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nPossible values: Refer to the latest ABI document, as it is a long list\r\n of different operating systems.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nType\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nIdentifies the object file type.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset space \\quad{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"8\" columns=\"2\">\r\n<features booktabs=\"true\" tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0pt\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nValue\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nNo file type\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nRelocatable file\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n2\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nExecutable file\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n3\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nShared object file\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n4\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nCore file\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0xff00\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nProcessor specific, lower bound\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0xffff\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nProcessor specific, upper bound\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe values from \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0xff00\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n to \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0xffff\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n are reserved for a processor to define additional file types meaningful\r\n to it.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMachine\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nSpecifies the required architecture value for an ELF file e.g.\r\n x86_64, MIPS, SPARC, etc.\r\n In the example, the machine is of \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nx86_64\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n architecture.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nPossible values: Please refer to the latest ABI document, as it is a long\r\n list of different architectures.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nVersion\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nSpecifies the version number of the current \r\n\\emph on\r\nobject file\r\n\\emph default\r\n (not the version of the ELF header, as the above \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nVersion\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n field specified).\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nEntry\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\npoint\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\naddress\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nSpecifies the memory address where the very first code to be executed.\r\n The address of \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nmain\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n function is the default in a normal application program, but it can be\r\n any function by explicitly specifying the function name to \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n.\r\n For the operating system we are going to write, this is the single most\r\n important field that we need to retrieve to bootstrap our kernel, and everythin\r\ng else can be ignored.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nStart\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nof\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nprogram\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nheaders\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nThe offset of the program header table, in bytes.\r\n In the example, this number is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n64\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n bytes, which means the \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n65th\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n byte, or \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n<start address> + 64\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, is the start address of the program header table.\r\n That is, if a program is loaded at address \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0x10000\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n in memory, then the start address is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0x10000\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n (the very first byte of \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMagic\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n field, where the value \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0x7f\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n resides) and the start address of program header table is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0x10000 + 0x40 = 0x10040\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nStart\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nof\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nsection\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nheaders\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nThe offset of the section header table in bytes, similar to the start of\r\n program headers.\r\n In the example, it is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n6648\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n bytes into file.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nFlags\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nHold processor-specific flags associated with the file.\r\n When the program is loaded, in a x86 machine, \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nEFLAGS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n register is set according to this value.\r\n In the example, the value is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n0x0\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, which means \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nEFLAGS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n register is in a clear state.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nof\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nthis\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nheader\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nSpecifies the total size of ELF header's size in bytes.\r\n In the example, it is \r\n\\family typewriter\r\n64\r\n\\family default\r\n bytes, which is equivalent to Start of program headers.\r\n Note that these two numbers are not necessarily equivalent, as program\r\n header table might be placed far away from the ELF header.\r\n The only fixed component in the ELF executable binary is the ELF header,\r\n which appears at the very beginning of the file.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nof\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nprogram\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nheaders\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nSpecifies the size of \r\n\\emph on\r\neach\r\n\\emph default\r\n program header in bytes.\r\n In the example, it is \r\n\\family typewriter\r\n64\r\n\\family default\r\n bytes.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nNumber\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nof\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nprogram\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nheaders\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nSpecifies the total number of program headers.\r\n In the example, the file has a total of \r\n\\family typewriter\r\n9\r\n\\family default\r\n program headers.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSize\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nof\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nsection\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nheaders\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nSpecifies the size of \r\n\\emph on\r\neach\r\n\\emph default\r\n section header in bytes.\r\n In the example, it is 64 bytes.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nNumber\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nof\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nsection\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nheaders\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nSpecifies the total number of section headers.\r\n In the example, the file has a total of \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n31\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n section headers.\r\n In a section header table, the first entry in the table is always an empty\r\n section.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSection\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nheader\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nstring\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\ntable\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nindex\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nSpecifies the index of the header in the section header table that points\r\n to the section that holds all null-terminated strings.\r\n In the example, the index is \r\n\\family typewriter\r\n28\r\n\\family default\r\n, which means it's the 28\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nth\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n entry of the table.\r\n \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Section\r\nSection header table\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAs we know already, code and data compose a program.\r\n However, not all types of code and data have the same purpose.\r\n For that reason, instead of a big chunk of code and data, they are divided\r\n into smaller chunks, and each chunk must satisfy these conditions (according\r\n to gABI):\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nEvery section in an object file has exactly one section header describing\r\n it.\r\n But, section headers may exist that do not have a section.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nEach section occupies one contiguous (possibly empty) sequence of bytes\r\n within a file.\r\n That means, there's no two regions of bytes that are the same section.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nSections in a file may not overlap.\r\n No byte in a file resides in more than one section.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nAn object file may have inactive space.\r\n The various headers and the sections might not \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\ncover\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n every byte in an object file.\r\n The contents of the inactive data are unspecified.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo get all the headers from an executable binary e.g.\r\n \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nhello\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, use the following command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -S hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nHere is a sample output (do not worry if you don't understand the output.\r\n Just skim to get your eyes familiar with it.\r\n We will dissect it soon enough):\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 31 section headers, starting at offset 0x19c8:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSection Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [Nr] Name              Type             Address           Offset\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       Size              EntSize          Flags  Link  Info  Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 0]                   NULL             0000000000000000  00000000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000000  0000000000000000           0     0     0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 1] .interp           PROGBITS         0000000000400238  00000238\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       000000000000001c  0000000000000000   A       0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 2] .note.ABI-tag     NOTE             0000000000400254  00000254\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000020  0000000000000000   A       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 3] .note.gnu.build-i NOTE             0000000000400274  00000274\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000024  0000000000000000   A       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 4] .gnu.hash         GNU_HASH         0000000000400298  00000298\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       000000000000001c  0000000000000000   A       5     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 5] .dynsym           DYNSYM           00000000004002b8  000002b8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000048  0000000000000018   A       6     1     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 6] .dynstr           STRTAB           0000000000400300  00000300\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000038  0000000000000000   A       0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 7] .gnu.version      VERSYM           0000000000400338  00000338\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000006  0000000000000002   A       5     0     2\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 8] .gnu.version_r    VERNEED          0000000000400340  00000340\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000020  0000000000000000   A       6     1     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 9] .rela.dyn         RELA             0000000000400360  00000360\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000018  0000000000000018   A       5     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [10] .rela.plt         RELA             0000000000400378  00000378\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000018  0000000000000018  AI       5    24     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [11] .init             PROGBITS         0000000000400390  00000390\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       000000000000001a  0000000000000000  AX       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [12] .plt              PROGBITS         00000000004003b0  000003b0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000020  0000000000000010  AX       0     0     16\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [13] .plt.got          PROGBITS         00000000004003d0  000003d0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000008  0000000000000000  AX       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [14] .text             PROGBITS         00000000004003e0  000003e0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000192  0000000000000000  AX       0     0     16\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [15] .fini             PROGBITS         0000000000400574  00000574\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000009  0000000000000000  AX       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [16] .rodata           PROGBITS         0000000000400580  00000580\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000004  0000000000000004  AM       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [17] .eh_frame_hdr     PROGBITS         0000000000400584  00000584\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       000000000000003c  0000000000000000   A       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [18] .eh_frame         PROGBITS         00000000004005c0  000005c0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000114  0000000000000000   A       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [19] .init_array       INIT_ARRAY       0000000000600e10  00000e10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000008  0000000000000000  WA       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [20] .fini_array       FINI_ARRAY       0000000000600e18  00000e18\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000008  0000000000000000  WA       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [21] .jcr              PROGBITS         0000000000600e20  00000e20\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000008  0000000000000000  WA       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [22] .dynamic          DYNAMIC          0000000000600e28  00000e28\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       00000000000001d0  0000000000000010  WA       6     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [23] .got              PROGBITS         0000000000600ff8  00000ff8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000008  0000000000000008  WA       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [24] .got.plt          PROGBITS         0000000000601000  00001000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000020  0000000000000008  WA       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [25] .data             PROGBITS         0000000000601020  00001020\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000010  0000000000000000  WA       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [26] .bss              NOBITS           0000000000601030  00001030\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000008  0000000000000000  WA       0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [27] .comment          PROGBITS         0000000000000000  00001030\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000034  0000000000000001  MS       0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [28] .shstrtab         STRTAB           0000000000000000  000018b6\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       000000000000010c  0000000000000000           0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [29] .symtab           SYMTAB           0000000000000000  00001068\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000648  0000000000000018          30    47     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [30] .strtab           STRTAB           0000000000000000  000016b0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000206  0000000000000000           0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nKey to Flags:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  W (write), A (alloc), X (execute), M (merge), S (strings), l (large)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  O (extra OS processing required) o (OS specific), p (processor specific)\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe first line:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm,width=12.3cm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 31 section headers, starting at offset 0x19c8\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nsummarizes the total number of sections in the file, and where the address\r\n where it starts.\r\n Then, comes the listing section by section with the following header, is\r\n also the format of each section output:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[Nr] Name              Type             Address           Offset\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     Size              EntSize          Flags  Link  Info  Align\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEach section has two lines with different fields:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nNr\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n The index of each section.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nName\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n The name of each section.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nType\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n This field (in a section header) identifies the type of each section.\r\n Types are used to classify sections.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nAddress\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n The starting \r\n\\emph on\r\nvirtual\r\n\\emph default\r\n address of each section.\r\n Note that the addresses are virtual only when a program runs in an OS with\r\n support for virtual memory enabled.\r\n In our OS, we run on the bare metal, the addresses will all be physical.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nOffset\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\noffset\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a distance in bytes, from the first byte of a file to the start of an\r\n object, such as a section or a segment in the context of an ELF binary\r\n file.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nSize\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n The size in bytes of each section.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nEntSize\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n Some sections hold a table of fixed-size entries, such as a symbol table.\r\n For such a section, this member gives the size in bytes of each entry.\r\n The member contains 0 if the section does not hold a table of fixed-size\r\n entries.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nFlags\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\n \r\n\\emph default\r\ndescribes attributes of a section.\r\n Flags together with a type defines the purpose of a section.\r\n Two sections can be of the same type, but serve different purposes.\r\n For example, even though \r\n\\family typewriter\r\n.data\r\n\\family default\r\n and \r\n\\family typewriter\r\n.text\r\n\\family default\r\n share the same type, \r\n\\family typewriter\r\n.data\r\n\\family default\r\n holds the initialized data of a program while \r\n\\family typewriter\r\n.text \r\n\\family default\r\nholds executable instructions of a program.\r\n For that reason, \r\n\\family typewriter\r\n.data\r\n\\family default\r\n is given read and write permission, but not executable.\r\n Any attempt to execute code in \r\n\\family typewriter\r\n.data\r\n\\family default\r\n is denied by the running OS: in Linux, such invalid section usage gives\r\n a \r\n\\emph on\r\nsegmentation fault\r\n\\emph default\r\n.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nELF gives information to enable an OS with such protection mechanism.\r\n However, running on bare metal, nothing can prevent from doing anything.\r\n Our OS can execute code in data section, and vice versa, writing to code\r\n section.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"17\" columns=\"2\">\r\n<features islongtable=\"true\" longtabularalignment=\"left\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"1cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"14cm\">\r\n<row endfirsthead=\"true\" caption=\"true\">\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" usebox=\"none\" width=\"16cm\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nSection Flags\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nFlag\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescriptions\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nW\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nBytes in this section are writable during execution.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nMemory is allocated for this section during process execution.\r\n Some control sections do not reside in the memory image of an object file;\r\n this attribute is off for those sections.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe section contains executable instructions.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nM\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\nThe data \r\n\\family default\r\n\\series default\r\n\\shape default\r\n\\size default\r\n\\emph on\r\n\\bar default\r\n\\strikeout default\r\n\\uuline default\r\n\\uwave default\r\n\\noun default\r\n\\color inherit\r\nin the section\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n may be merged to eliminate duplication.\r\n \r\n\\family default\r\n\\series default\r\n\\shape default\r\n\\size default\r\n\\emph default\r\n\\bar default\r\n\\strikeout default\r\n\\uuline default\r\n\\uwave default\r\n\\noun default\r\n\\color inherit\r\nEach element in the section is compared against other elements in sections\r\n with the same name, type and flags.\r\n Elements that would have identical values at program run-time may be merged.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nS\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe data elements in the section consist of null-terminated character strings.\r\n The size of each character is specified in the section header's \r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nEntSize\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n field.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nl\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSpecific large section for x86_64 architecture.\r\n This flag is not specified in the Generic ABI but in x86_64 ABI.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nI\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe \r\n\\emph on\r\n\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nInfo\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n field of this section header holds an index of a section header.\r\n Otherwise, the number is the index of something else.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPreserve section ordering when linking.\r\n If this section is combined with other sections in the output file, it\r\n must appear in the same relative order with respect to those sections,\r\n as the linked-to section appears with respect to sections the linked-to\r\n section is combined with.\r\n Apply when the \r\n\\family typewriter\r\n\\series bold\r\n\\emph on\r\nLink\r\n\\family default\r\n\\series default\r\n\\emph default\r\n field of this section's header references another section (the linked-to\r\n section)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nG\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThis section is a member (perhaps the only one) of a section group.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nT\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThis section holds \r\n\\series bold\r\nT\r\n\\series default\r\nhread-\r\n\\series bold\r\nL\r\n\\series default\r\nocal\r\n\\series bold\r\n S\r\n\\series default\r\ntorage, meaning that each thread has its own distinct instance of this data.\r\n A thread is a distinct execution flow of code.\r\n A program can have multiple threads that pack different pieces of code\r\n and execute separately, at the same time.\r\n We will learn more about threads when writing our kernel.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nLink editor is to exclude this section from executable and shared library\r\n that it builds when those objects are not to be further relocated.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nx \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nUnknown flag to \r\n\\family typewriter\r\nreadelf\r\n\\family default\r\n.\r\n It happens because the linking process can be done manually with a linker\r\n like \r\n\\family typewriter\r\nGNU ld\r\n\\family default\r\n (we will later later).\r\n That is, section flags can be specified manually, and some flags are for\r\n a customized ELF that the open-source\r\n\\family typewriter\r\n readelf\r\n\\family default\r\n doesn't know of.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nO\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThis section requires special OS-specific processing (beyond the standard\r\n linking rules) to avoid incorrect behavior.\r\n A link editor encounters sections whose headers contain OS-specific values\r\n it does not recognize by Type or Flags values defined by ELF standard,\r\n the link editor should combine those sections.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\no\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nAll bits included in this flag are reserved for operating system-specific\r\n semantics.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\np\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nAll bits included in this flag are reserved for processor-specific semantics.\r\n If meanings are specified, the processor supplement explains them.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\emph on\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nLink\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nand\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nInfo\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n \r\n\\emph default\r\nare numbers that references the indexes of sections, symbol table entries,\r\n hash table entries.\r\n \r\n\\family typewriter\r\nLink\r\n\\family default\r\n field only holds the index of a section, while \r\n\\family typewriter\r\nInfo\r\n\\family default\r\n field holds an index of a section, a symbol table entry or a hash table\r\n entry, depends on the type of a section.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nLater when writing our OS, we will handcraft the kernel image by explicitly\r\n linking the object files (produced by \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n) through a linker script.\r\n We will specify the memory layout of sections by specifying at what addresses\r\n they will appear in the final image.\r\n But we will not assign any section flag and let the linker take care of\r\n it.\r\n Nevertheless, knowing which flag does what is useful.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\emph on\r\nAlign\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is a value that enforces the offset of a section should be divisible by\r\n the value.\r\n Only 0 and positive integral powers of two are allowed.\r\n Values 0 and 1 mean the section has no alignment constraint.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nOutput of \r\n\\family typewriter\r\n.interp\r\n\\family default\r\n section:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[Nr] Name              Type             Address           Offset\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     Size              EntSize          Flags  Link  Info  Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[ 1] .interp           PROGBITS         0000000000400238  00000238\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     000000000000001c  0000000000000000   A       0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nNr\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n1\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nType\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\nPROGBITS\r\n\\family default\r\n, which means this section is part of the program.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nAddress\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n0x0000000000400238\r\n\\family default\r\n, which means the program is loaded at this virtual memory address at runtime.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nOffset\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n0x00000238\r\n\\family default\r\n \r\n\\emph on\r\nbytes\r\n\\emph default\r\n into file.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nSize\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n0x000000000000001c\r\n\\family default\r\n in bytes.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nEntSize\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n0\r\n\\family default\r\n, which means this section does not have any fixed-size entry.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nFlags\r\n\\family default\r\n\\emph default\r\n are \r\n\\family typewriter\r\nA\r\n\\family default\r\n (Allocatable), which means this section consumes memory at runtime.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nInfo\r\n\\emph default\r\n and Link\r\n\\family default\r\n are \r\n\\family typewriter\r\n0\r\n\\family default\r\n and \r\n\\family typewriter\r\n0\r\n\\family default\r\n, which means this section links to no section or entry in any table.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nAlign\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n1\r\n\\family default\r\n, which means no alignment.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nOutput of the \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[14] .text             PROGBITS         00000000004003e0  000003e0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000192  0000000000000000  AX       0     0     16\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nNr\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n14\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nType\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\nPROGBITS\r\n\\family default\r\n, which means this section is part of the program.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nAddress\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n0x00000000004003e0\r\n\\family default\r\n, which means the program is loaded at this virtual memory address at runtime.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nOffset\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n0x000003e0\r\n\\family default\r\n \r\n\\emph on\r\nbytes\r\n\\emph default\r\n into file.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nSize\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n0x0000000000000192\r\n\\family default\r\n in bytes.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nEntSize\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n0\r\n\\family default\r\n, which means this section does not have any fixed-size entry.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nFlags\r\n\\family default\r\n\\emph default\r\n are \r\n\\family typewriter\r\nA\r\n\\family default\r\n (Allocatable) and \r\n\\family typewriter\r\nX\r\n\\family default\r\n (Executable), which means this section consumes memory and can be executed\r\n as code at runtime.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nInfo\r\n\\emph default\r\n and Link\r\n\\family default\r\n are \r\n\\family typewriter\r\n0\r\n\\family default\r\n and \r\n\\family typewriter\r\n0\r\n\\family default\r\n, which means this section links to no section or entry in any table.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\emph on\r\nAlign\r\n\\family default\r\n\\emph default\r\n is \r\n\\family typewriter\r\n16\r\n\\family default\r\n, which means the starting address of the section should be divisible by\r\n \r\n\\family typewriter\r\n16\r\n\\family default\r\n, or \r\n\\family typewriter\r\n0x10\r\n\\family default\r\n.\r\n Indeed, it is: \r\n\\begin_inset Formula $\\mathtt{0x3e0/0x10=0x3e}$\r\n\\end_inset\r\n\r\n.\r\n \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Section\r\nUnderstand Section in-depth\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn this section, we will learn different details of section types and the\r\n purposes of special sections e.g.\r\n \r\n\\family typewriter\r\n.bss, .text, .data\r\n\\family default\r\n, etc, by looking at each section one by one.\r\n We will also examine the content of each section as a hexdump with the\r\n commands:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -x <section name|section number> <file>\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFor example, if you want to examine the content of section with index 25\r\n (the \r\n\\family typewriter\r\n.bss\r\n\\family default\r\n section in the sample output) in the file \r\n\\family typewriter\r\nhello\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -x 25 hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEquivalently, using name instead of index works:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{tcolorbox}[enlarge top initially by=5mm]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -x .data hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{tcolorbox}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf a section contains strings e.g.\r\n string symbol table, the flag \r\n\\family typewriter\r\n-x \r\n\\family default\r\ncan be replaced with\r\n\\family typewriter\r\n -p\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nNULL\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n marks a section header as inactive and does not have an associated section.\r\n \r\n\\family typewriter\r\nNULL\r\n\\family default\r\n section is always the first entry of section header table.\r\n It means, any useful section starts from 1.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nThe sample output of \r\n\\family typewriter\r\nNULL\r\n\\family default\r\n section:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[Nr] Name             Type             Address           Offset\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     Size             EntSize          Flags  Link  Info  Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[ 0]                  NULL             0000000000000000 00000000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0000000000000000 0000000000000000           0     0     0\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nExamining the content, the section is empty:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section '' has no data to dump.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nNOTE\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n marks a section with special information that other programs will check\r\n for conformance, compatibility, etc, by a vendor or a system builder.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nIn the sample output, we have 2 \r\n\\family typewriter\r\nNOTE\r\n\\family default\r\n sections:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[Nr] Name              Type             Address           Offset\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     Size              EntSize          Flags  Link  Info  Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[ 2] .note.ABI-tag     NOTE             0000000000400254  00000254\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0000000000000020  0000000000000000   A       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[ 3] .note.gnu.build-i NOTE             0000000000400274  00000274        \r\n  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0000000000000024  0000000000000000   A       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nExamine 2nd section with the command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -x 2 hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nwe have:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nHex dump of section '.note.ABI-tag':\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  0x00400254 04000000 10000000 01000000 474e5500 ............GNU.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  0x00400264 00000000 02000000 06000000 20000000 ............\r\n ...\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nPROGBITS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n indicates a section holding the main content of a program, either code\r\n or data.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nThere are many PROGBITS sections:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [Nr] Name              Type             Address           Offset\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       Size              EntSize          Flags  Link  Info  Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 1] .interp           PROGBITS         0000000000400238  00000238\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       000000000000001c  0000000000000000   A       0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  ...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [11] .init             PROGBITS         0000000000400390  00000390\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       000000000000001a  0000000000000000  AX       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [12] .plt              PROGBITS         00000000004003b0  000003b0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000020  0000000000000010  AX       0     0     16\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [13] .plt.got          PROGBITS         00000000004003d0  000003d0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000008  0000000000000000  AX       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [14] .text             PROGBITS         00000000004003e0  000003e0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000192  0000000000000000  AX       0     0     16\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [15] .fini             PROGBITS         0000000000400574  00000574\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000009  0000000000000000  AX       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [16] .rodata           PROGBITS         0000000000400580  00000580\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000004  0000000000000004  AM       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [17] .eh_frame_hdr     PROGBITS         0000000000400584  00000584\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       000000000000003c  0000000000000000   A       0     0     4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [18] .eh_frame         PROGBITS         00000000004005c0  000005c0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000114  0000000000000000   A       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  ...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [23] .got              PROGBITS         0000000000600ff8  00000ff8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000008  0000000000000008  WA       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [24] .got.plt          PROGBITS         0000000000601000  00001000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000020  0000000000000008  WA       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [25] .data             PROGBITS         0000000000601020  00001020\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000010  0000000000000000  WA       0     0     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [27] .comment          PROGBITS         0000000000000000  00001030\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n       0000000000000034  0000000000000001  MS       0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nFor our operating system, we only need the following section:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n.text\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nThis section holds all the compiled code of a program.\r\n \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n.data\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nThis section holds the initialized data of a program.\r\n Since the data are initialized with actual values, \r\n\\family typewriter\r\ngcc \r\n\\family default\r\nallocates the section with actual byte in the executable binary.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n.rodata\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nThis section holds read-only data, such as fixed-size strings in a program,\r\n e.g.\r\n \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nHello World\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n, and others.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n.bss\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nThis section, shorts for \r\n\\series bold\r\n\\emph on\r\nB\r\n\\series default\r\nlock \r\n\\series bold\r\nS\r\n\\series default\r\ntarted by \r\n\\series bold\r\nS\r\n\\series default\r\nymbol\r\n\\emph default\r\n, holds uninitialized data of a program.\r\n Unlike other sections, no space is allocated for this section in the image\r\n of the executable binary on disk.\r\n The section is allocated only when the program is loaded into main memory.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nOther sections are mainly needed for dynamic linking, that is code linking\r\n at runtime for sharing between many programs.\r\n To enable such feature, an OS as a runtime environment must be presented.\r\n Since we run our OS on bare metal, we are effectively creating such environment.\r\n For simplicity, we won't add dynamic linking to our OS.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nSYMTAB\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nand\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nDYNSYM\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n These sections hold symbol table.\r\n A \r\n\\emph on\r\nsymbol table\r\n\\emph default\r\n is an array of entries that describe symbols in a program.\r\n A \r\n\\emph on\r\nsymbol \r\n\\emph default\r\nis a name assigned to an entity in a program.\r\n The types of these entities are also the types of symbols, and these are\r\n the possible types of an entity:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nIn the sample output, section 5 and 29 are symbol tables:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[Nr] Name              Type             Address           Offset\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     Size              EntSize          Flags  Link  Info  Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[ 5] .dynsym           DYNSYM           00000000004002b8  000002b8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0000000000000048  0000000000000018   A       6     1     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[29] .symtab           SYMTAB           0000000000000000  00001068\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0000000000000648  0000000000000018          30    47     8\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo show the symbol table:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -s hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOutput consists of 2 symbol tables, corresponding to the two sections above,\r\n \r\n\\family typewriter\r\n.dynsym \r\n\\family default\r\nand \r\n\\family typewriter\r\n.symtab\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\nSymbol table '.dynsym' contains 4 entries:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n   Num:    Value          Size Type    Bind   Vis      Ndx Name\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n     1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.2.5\r\n (2)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLI\r\nBC_2.2.5 (2)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n     3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\nSymbol table '.symtab' contains 67 entries:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n   Num:    Value          Size Type    Bind   Vis      Ndx Name\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n    ..........................................\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n    59: 0000000000601040     0 NOTYPE  GLOBAL DEFAULT   26 _end\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n    60: 0000000000400430    42 FUNC    GLOBAL DEFAULT   14 _start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n    61: 0000000000601038     0 NOTYPE  GLOBAL DEFAULT   26 __bss_start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n    62: 0000000000400526    32 FUNC    GLOBAL DEFAULT   14 main\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n    63: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n    64: 0000000000601038     0 OBJECT  GLOBAL HIDDEN    25 __TMC_END__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n    65: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneT\r\nable\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size small\r\n    66: 00000000004003c8     0 FUNC    GLOBAL DEFAULT   11 _init\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nTLS\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n\tThe symbol is associated with a Thread-Local Storage entity.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nNum \r\n\\family default\r\nis the index of an entry in a table.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nValue \r\n\\family default\r\nis the virtual memory address where the symbol is located.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nSize \r\n\\family default\r\nis the size of the entity associated with a symbol.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nType \r\n\\family default\r\nis a symbol type according to table.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nNOTYPE\r\n\\family default\r\n The type of a symbol is not specified.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nOBJECT\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n\tThe symbol is associated with a data object.\r\n In C, any variable definition is of \r\n\\family typewriter\r\n\\series default\r\n\\shape default\r\n\\size default\r\n\\emph default\r\n\\bar default\r\n\\strikeout default\r\n\\uuline default\r\n\\uwave default\r\n\\noun default\r\n\\color inherit\r\nOBJECT\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n type.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nFUNC\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n The symbol is associated with a function or other executable code.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nSECTION\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n\tThe symbol is associated with a section, and exists primarily for relocation.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nFILE\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n The symbol is the name of a source file associated with an executable binary.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nCOMMON\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n\tThe symbol labels an uninitialized variable.\r\n That is, when a variable in C is defined as global variable without an\r\n initial value, or as an external variable using the extern keyword.\r\n In other words, these variables stay in \r\n\\family typewriter\r\n\\series default\r\n\\shape default\r\n\\size default\r\n\\emph default\r\n\\bar default\r\n\\strikeout default\r\n\\uuline default\r\n\\uwave default\r\n\\noun default\r\n\\color inherit\r\n.bss\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n section.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nBind \r\n\\family default\r\nis the scope of a symbol.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nLOCAL\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n are symbols that are only visible in the object files that defined them.\r\n In C, the \r\n\\family typewriter\r\nstatic \r\n\\family default\r\nmodifier marks a symbol (e.g.\r\n a variable/function) as local to only the file that defines it.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nIf we define variables and functions with \r\n\\family typewriter\r\nstatic\r\n\\family default\r\n modifer:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"title={hello.c}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstatic int global_static_var = 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstatic void local_func() {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    static int local_static_var = 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Example\r\nThen we get the \r\n\\family typewriter\r\nstatic\r\n\\family default\r\n variables listed as local symbols after compiling:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -s hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSymbol table '.dynsym' contains 5 entries:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   Num:    Value  Size Type    Bind   Vis      Ndx Name\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     1: 00000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.0 (2)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     2: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     3: 00000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.0\r\n (2)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     4: 080484bc     4 OBJECT  GLOBAL DEFAULT   16 _IO_stdin_used\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSymbol table '.symtab' contains 72 entries:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   Num:    Value  Size Type    Bind   Vis      Ndx Name\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n      .........\r\n output omitted .........\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout LyX-Code\r\n    38: 0804a020     4 \r\n\\color magenta\r\nOBJECT\r\n\\color inherit\r\n  \r\n\\color blue\r\nLOCAL\r\n\\color inherit\r\n  DEFAULT   26 \r\n\\color red\r\nglobal_static_var\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    39: 0804840b     6 \r\n\\color magenta\r\nFUNC\r\n\\color inherit\r\n    \r\n\\color blue\r\nLOCAL\r\n\\color inherit\r\n  DEFAULT   14 \r\n\\color red\r\nlocal_func\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    40: 0804a024     4 \r\n\\color magenta\r\nOBJECT\r\n\\color inherit\r\n  \r\n\\color blue\r\nLOCAL\r\n\\color inherit\r\n  DEFAULT   26 \r\n\\color red\r\nlocal_static_var.1938\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n .........\r\n output omitted .........\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nGLOBAL\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\tare symbols that are accessible by other object files when linking together.\r\n These symbols are primarily non-\r\n\\family typewriter\r\n\\series default\r\n\\shape default\r\n\\size default\r\n\\emph default\r\n\\bar default\r\n\\strikeout default\r\n\\uuline default\r\n\\uwave default\r\n\\noun default\r\n\\color inherit\r\nstatic\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n functions and non-\r\n\\family typewriter\r\n\\series default\r\n\\shape default\r\n\\size default\r\n\\emph default\r\n\\bar default\r\n\\strikeout default\r\n\\uuline default\r\n\\uwave default\r\n\\noun default\r\n\\color inherit\r\nstatic\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n global data.\r\n The \r\n\\family typewriter\r\n\\series default\r\n\\shape default\r\n\\size default\r\n\\emph default\r\n\\bar default\r\n\\strikeout default\r\n\\uuline default\r\n\\uwave default\r\n\\noun default\r\n\\color inherit\r\nextern\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n modifier marks a symbol as externally defined elsewhere but is accessible\r\n in the final executable binary, so an \r\n\\family typewriter\r\n\\series default\r\n\\shape default\r\n\\size default\r\n\\emph default\r\n\\bar default\r\n\\strikeout default\r\n\\uuline default\r\n\\uwave default\r\n\\noun default\r\n\\color inherit\r\nextern\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n variable is also considered \r\n\\family typewriter\r\n\\series default\r\n\\shape default\r\n\\size default\r\n\\emph default\r\n\\bar default\r\n\\strikeout default\r\n\\uuline default\r\n\\uwave default\r\n\\noun default\r\n\\color inherit\r\nGLOBAL\r\n\\family roman\r\n\\series medium\r\n\\shape up\r\n\\size normal\r\n\\emph off\r\n\\bar no\r\n\\strikeout off\r\n\\uuline off\r\n\\uwave off\r\n\\noun off\r\n\\color none\r\n.\r\n\\family default\r\n\\series default\r\n\\shape default\r\n\\size default\r\n\\bar default\r\n\\color inherit\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nSimilar to the \r\n\\family typewriter\r\nLOCAL\r\n\\family default\r\n example above, the output lists many \r\n\\family typewriter\r\nGLOBAL\r\n\\family default\r\n symbols such as \r\n\\family typewriter\r\nmain\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\nNum:    Value  Size Type    Bind   Vis      Ndx Name\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.........\r\n output omitted .........\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 66: 080483e1    10 \r\n\\color magenta\r\nFUNC\r\n\\color inherit\r\n    \r\n\\color blue\r\nGLOBAL\r\n\\color inherit\r\n DEFAULT   14 \r\n\\color red\r\nmain\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.........\r\n output omitted .........\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nWEAK\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n are symbols whose definitions can be redefined.\r\n Normally, a symbol with multiple definitions are reported as an error by\r\n a compiler.\r\n However, this constraint is lax when a definition is explicitly marked\r\n as weak, which means the default implementation can be replaced by a different\r\n definition at link time.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nSuppose we have a default implementation of the function \r\n\\family typewriter\r\nadd\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n__attribute__((weak)) int add(int a, int b) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"warning: function is not implemented.\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"add(1,2) is %d\r\n\\backslash\r\nn\", add(1,2));\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n__attribute__((weak))\r\n\\family default\r\n is a \r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nfunction attribute\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nfunction attribute.\r\n A \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nfunction attribute\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nfunction attribute\r\n\\emph default\r\n is extra information for a compiler to handle a function differently from\r\n a normal function.\r\n In this example, \r\n\\family typewriter\r\nweak\r\n\\family default\r\n attribute makes the function \r\n\\family typewriter\r\nadd \r\n\\family default\r\na weak function,which means the default implementation can be replaced by\r\n a different definition at link time.\r\n Function attribute is a feature of a compiler, not standard C.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf we do not supply a different function definition in a different file\r\n (must be in a different file, otherwise \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n reports as an error), then the default implementation is applied.\r\n When the function \r\n\\family typewriter\r\nadd\r\n\\family default\r\n is called, it only prints the message: \r\n\\family typewriter\r\n\"warning: function not implemented\"\r\n\\family default\r\nand returns 0:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ./hello \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nwarning: function is not implemented.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nadd(1,2) is 0\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHowever, if we supply a different definition in another file e.g.\r\n \r\n\\family typewriter\r\nmath.c\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=C,tabsize=4,title={\\texttt{math.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint add(int a, int b) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return a + b;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nand compile the two files together:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc math.c hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, when running \r\n\\family typewriter\r\nhello\r\n\\family default\r\n, no warning message is printed and the correct value is returned.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWeak symbol is a mechanism to provide a default implementation, but replaceable\r\n when a better implementation is available (e.g.\r\n more specialized and optimized) at link-time.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nVis \r\n\\family default\r\nis the visibility of a symbol.\r\n The following values are available:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"6\" columns=\"2\">\r\n<features islongtable=\"true\" longtabularalignment=\"center\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"2.5cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"13.5cm\">\r\n<row caption=\"true\">\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" usebox=\"none\" width=\"16cm\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nSymbol Visibility\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nValue\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nDEFAULT\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe visibility is specified by the binding type of asymbol.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nGlobal and weak symbols are visible outside of their defining component\r\n (executable file or shared object).\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nLocal symbols are hidden.\r\n See \r\n\\family typewriter\r\nHIDDEN\r\n\\family default\r\n below.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nHIDDEN\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nA symbol is hidden when the name is not visible to any other program outside\r\n of its running program.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nPROTECTED\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nA symbol is protected when it is shared outside of its running program or\r\n shared libary and cannot be overridden.\r\n That is, there can only be one definition for this symbol across running\r\n programs that use it.\r\n No program can define its own definition of the same symbol.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nINTERNAL\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nVisibility is processor-specific and is defined by processor-specific ABI.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nNdx \r\n\\family default\r\nis the index of a section that the symbol is in.\r\n Aside from fixed index numbers that represent section indexes, index has\r\n these special values:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"8\" columns=\"2\">\r\n<features islongtable=\"true\" longtabularalignment=\"center\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"2cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"14cm\">\r\n<row newpage=\"true\" caption=\"true\">\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" usebox=\"none\" width=\"16cm\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nSymbol Index\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nValue\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nABS\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe index will not be changed by any symbol relocation.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nCOM\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe index refers to an unallocated common block.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nUND\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe symbol is undefined in the current object file, which means the symbol\r\n depends on the actual definition in another file.\r\n Undefined symbols appears when the object file refers to symbols that are\r\n available at runtime, from shared library.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nLORESERVE\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nHIRESERVE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nLORESERVE\r\n\\family default\r\n is the lower boundary of the reserve indexes.\r\n Its value is \r\n\\family typewriter\r\n0xff00\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nHIREVERSE\r\n\\family default\r\n is the upper boundary of the reserve indexes.\r\n Its value is \r\n\\family typewriter\r\n0xffff\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\nThe operating system reserves exclusive indexes between \r\n\\family typewriter\r\nLORESERVE\r\n\\family default\r\n and \r\n\\family typewriter\r\nHIRESERVE\r\n\\family default\r\n, which do not map to any actual section header.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nXINDEX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe index is larger than \r\n\\family typewriter\r\nLORESERVE\r\n\\family default\r\n.\r\n The actual value will be contained in the section \r\n\\family typewriter\r\nSYMTAB_SHNDX\r\n\\family default\r\n, where each entry is a mapping between a symbol, whose \r\n\\family typewriter\r\nNdx\r\n\\family default\r\n field is a \r\n\\family typewriter\r\nXINDEX\r\n\\family default\r\n value, and the actual index value.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nOthers\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSometimes, values such as \r\n\\family typewriter\r\nANSI_COM\r\n\\family default\r\n, \r\n\\family typewriter\r\nLARGE_COM\r\n\\family default\r\n, \r\n\\family typewriter\r\nSCOM\r\n\\family default\r\n, \r\n\\family typewriter\r\nSUND \r\n\\family default\r\nappear.\r\n This means that the index is processor-specific.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nName \r\n\\family default\r\nis the symbol name.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nA C application program always starts from symbol \r\n\\family typewriter\r\nmain\r\n\\family default\r\n.\r\n The entry for \r\n\\family typewriter\r\nmain\r\n\\family default\r\n in the symbol table in \r\n\\family typewriter\r\n.symtab\r\n\\family default\r\n section is:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nNum:                Value  Size Type    Bind   Vis      Ndx Name\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 62:     0000000000400526    32 FUNC    GLOBAL DEFAULT   14 main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nThe entry shows that:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nmain\r\n\\family default\r\n is the 62\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nth\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n entry in the table.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nmain \r\n\\family default\r\nstarts at address \r\n\\family typewriter\r\n0x0000000000400526\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nmain \r\n\\family default\r\nconsumes 32 bytes.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nmain \r\n\\family default\r\nis a function.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nmain \r\n\\family default\r\nis in global scope.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nmain \r\n\\family default\r\nis visible to other object files that use it.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nmain \r\n\\family default\r\nis inside the 14\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nth\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n section, which is \r\n\\family typewriter\r\n.text\r\n\\family default\r\n.\r\n This is logical, since \r\n\\family typewriter\r\n.text\r\n\\family default\r\n holds all program code.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nSTRTAB\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nhold a table of null-terminated strings, called \r\n\\emph on\r\nstring table\r\n\\emph default\r\n.\r\n The first and last byte of this section is always a NULL character.\r\n A string table section exists because a string can be reused by more than\r\n one section to represent symbol and section names, so a program like \r\n\\family typewriter\r\nreadelf\r\n\\family default\r\n or \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n can display various objects in a program, e.g.\r\n variable, functions, section names, in a human-readable text instead of\r\n its raw hex address.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nIn the sample output, section \r\n\\family typewriter\r\n28\r\n\\family default\r\n and \r\n\\family typewriter\r\n30\r\n\\family default\r\n are of \r\n\\family typewriter\r\nSTRTAB\r\n\\family default\r\n type:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[Nr] Name              Type             Address           Offset\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     Size              EntSize          Flags  Link  Info  Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[28] .shstrtab         STRTAB           0000000000000000  000018b6\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     000000000000010c  0000000000000000           0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[30] .strtab           STRTAB           0000000000000000  000016b0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0000000000000206  0000000000000000           0     0     1\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n.shstrtab \r\n\\family default\r\nholds all the section names.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n.strtab \r\n\\family default\r\nholds the symbols e.g.\r\n variable names, function names, struct names, etc., in a C program, but\r\n not fixed-size null-terminated C strings; the C strings are kept in \r\n\\family typewriter\r\n.rodata\r\n\\family default\r\n section.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nStrings in those\r\n\\family typewriter\r\n \r\n\\family default\r\nsection can be inspected with the command:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -p 29 hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe output shows all the section names, with the offset (also the string\r\n index) into \r\n\\family typewriter\r\n.shstrtab\r\n\\family default\r\n the table to the left:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nString dump of section '.shstrtab':  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [     1]  .symtab\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [     9]  .strtab\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    11]  .shstrtab\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    1b]  .interp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    23]  .note.ABI-tag\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    31]  .note.gnu.build-id\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    44]  .gnu.hash\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    4e]  .dynsym\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    56]  .dynstr\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    5e]  .gnu.version\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    6b]  .gnu.version_r\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    7a]  .rela.dyn\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    84]  .rela.plt\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    8e]  .init\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    94]  .plt.got\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    9d]  .text\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    a3]  .fini\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    a9]  .rodata\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    b1]  .eh_frame_hdr\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    bf]  .eh_frame\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    c9]  .init_array\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    d5]  .fini_array\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    e1]  .jcr\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    e6]  .dynamic\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    ef]  .got.plt\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    f8]  .data\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    fe]  .bss\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   103]  .comment\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe actual implementation of a string table is a contiguous array of null-termin\r\nated strings.\r\n The index of a string is the position of its first character in the array.\r\n For example, in the above string table, \r\n\\family typewriter\r\n.symtab\r\n\\family default\r\n is at index 1 in the array (NULL character is at index 0).\r\n The length of \r\n\\family typewriter\r\n.symtab\r\n\\family default\r\n is 7, plus the NULL character, which occurs 8 bytes in total.\r\n So, \r\n\\family typewriter\r\n.strtab \r\n\\family default\r\nstarts at index 9, and so on.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{String table in memory of \r\n\\backslash\r\ntexttt{.shstrtab.} A \r\n\\backslash\r\ntextcolor{red}{red} number is the starting index of a string.}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"6\" columns=\"17\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\color red\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n02\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n03\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n04\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n05\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n06\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n07\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n08\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\color red\r\n09\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0a\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0b\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0c\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0d\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0e\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0f\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n00000000\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\r\n\\backslash\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\ns\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\ny\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nm\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nt\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\na\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nb\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\r\n\\backslash\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\ns\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nt\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nr\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nt\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\na\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nb\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n00\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\color red\r\n01\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n02\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n03\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n04\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n05\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n06\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n07\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n08\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n09\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0a\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n\\color red\r\n0b\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0c\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0d\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0e\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n0f\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\n00000010\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\r\n\\backslash\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\ns\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nh\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\ns\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nt\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nr\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nt\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\na\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nb\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\r\n\\backslash\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\ni\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nn\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nt\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\ne\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n....\r\n and so on ....\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset VSpace bigskip\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilarly, the output of \r\n\\family typewriter\r\n.strtab\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nString dump of section '.strtab':\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [     1]  crtstuff.c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [     c]  __JCR_LIST__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    19]  deregister_tm_clones\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    2e]  __do_global_dtors_aux\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    44]  completed.7585\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    53]  __do_global_dtors_aux_fini_array_entry\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    7a]  frame_dummy\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    86]  __frame_dummy_init_array_entry\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    a5]  \r\n\\series bold\r\n\\size larger\r\nhello.c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    ad]  __FRAME_END__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    bb]  __JCR_END__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    c7]  __init_array_end\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    d8]  _DYNAMIC\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    e1]  __init_array_start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [    f4]  __GNU_EH_FRAME_HDR\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   107]  _GLOBAL_OFFSET_TABLE_\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   11d]  __libc_csu_fini\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   12d]  _ITM_deregisterTMCloneTable\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   149]  j\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   14b]  _edata\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   152]  __libc_start_main@@GLIBC_2.2.5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   171]  __data_start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   17e]  __gmon_start__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   18d]  __dso_handle\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   19a]  _IO_stdin_used\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   1a9]  __libc_csu_init\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   1b9]  __bss_start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   1c5]  \r\n\\series bold\r\n\\size larger\r\nmain\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   1ca]  _Jv_RegisterClasses\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   1de]  __TMC_END__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [   1ea]  _ITM_registerTMCloneTable\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nHASH\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nholds a symbol hash table, which supports symbol table access.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nDYNAMIC\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nholds information for dynamic linking.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nNOBITS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nis similar to \r\n\\family typewriter\r\nPROGBITS\r\n\\family default\r\n but occupies no space.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\n.bss\r\n\\family default\r\n section holds uninitialized data, which means the bytes in the section\r\n can have any value.\r\n Until a operating system actually loads the section into main memory, there\r\n is no need to allocate space for the binary image on disk to reduce the\r\n size of a binary file.\r\n Here is the details of \r\n\\family typewriter\r\n.bss\r\n\\family default\r\n from the example output:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[Nr] Name              Type             Address           Offset\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     Size              EntSize          Flags  Link  Info  Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[26] .bss              NOBITS           0000000000601038  \r\n\\color magenta\r\n00001038\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     \r\n\\color red\r\n0000000000000008\r\n\\color inherit\r\n  0000000000000000  WA       0     0     1   \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[27] .comment          PROGBITS         0000000000000000  \r\n\\color magenta\r\n00001038\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0000000000000034  0000000000000001  MS       0     0     1 \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the above output, the \r\n\\color red\r\nsize\r\n\\color inherit\r\n of the section is only 8 bytes, while the \r\n\\color magenta\r\noffsets\r\n\\color inherit\r\n of both sections are the same, which means \r\n\\family typewriter\r\n.bss\r\n\\family default\r\n consumes no byte of the executable binary on disk.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNotice that the \r\n\\family typewriter\r\n.comment\r\n\\family default\r\n section has no starting address.\r\n This means that this section is discarded when the executable binary is\r\n loaded into memory.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nREL\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nholds relocation entries without explicit addends.\r\n This type will be explained in details in \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"sec:Understand-relocations-with-readelf\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nRELA\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nholds relocation entries with explicit addends.\r\n This type will be explained in details in \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"sec:Understand-relocations-with-readelf\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nINIT_ARRAY\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nis an array of function pointers for program initialization.\r\n When an application program runs, before getting to\r\n\\family typewriter\r\n main()\r\n\\family default\r\n, initialization code in \r\n\\family typewriter\r\n.init\r\n\\family default\r\n and this section are executed first.\r\n The first element in this array is an ignored function pointer.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nIt might not make sense when we can include initialization code in the \r\n\\family typewriter\r\nmain()\r\n\\family default\r\n function.\r\n However, for shared object files where there are no \r\n\\family typewriter\r\nmain()\r\n\\family default\r\n, this section ensures that the initialization code from an object file\r\n executes before any other code to ensure a proper environment for main\r\n code to run properly.\r\n It also makes an object file more modularity, as the main application code\r\n needs not to be responsible for initializing a proper environment for using\r\n a particular object file, but the object file itself.\r\n Such a clear division makes code cleaner.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHowever, we will not use any \r\n\\family typewriter\r\n.init\r\n\\family default\r\n and\r\n\\family typewriter\r\n INIT_ARRAY \r\n\\family default\r\nsections in our operating system, for simplicity, as initializing an environment\r\n is part of the operating-system domain.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nTo use the \r\n\\family typewriter\r\nINIT_ARRAY\r\n\\family default\r\n, we simply mark a function with the attribute \r\n\\family typewriter\r\nconstructor\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n__attribute__((constructor)) static void init1(){\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n__attribute__((constructor)) static void init2(){\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"hello world\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe program automatically calls the constructor without explicitly invoking\r\n it:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ./hello \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ninit1\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ninit2\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nhello world\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nOptionally, a constructor can be assigned with a priority from 101 onward.\r\n The priorities from 0 to 100 are reserved for \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n.\r\n If we want \r\n\\family typewriter\r\ninit2\r\n\\family default\r\n to run before \r\n\\family typewriter\r\ninit1\r\n\\family default\r\n, we give it a higher priority:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n__attribute__((constructor(102))) static void init1(){\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n__attribute__((constructor(101))) static void init2(){\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"hello world\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe call order should be exactly as specified:\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Example\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\n$ gcc -m32 hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\n$ ./hello\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\ninit2\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\ninit1\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\nhello world\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWe can add initialization functions using another method:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid init1() {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid init2() {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n/* Without typedef, init is a definition of a function pointer.\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n   With typedef, init is a declaration of a type.*/\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\ntypedef void (*init)();\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n__attribute__((section(\".init_array\"))) init init_arr[2] = {init1, init2};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"hello world!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe attribute \r\n\\family typewriter\r\nsection(\r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\n...\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n)\r\n\\family default\r\n put a function into a particular section rather than the default \r\n\\family typewriter\r\n.text\r\n\\family default\r\n.\r\n In this example, it is \r\n\\family typewriter\r\n.init_array\r\n\\family default\r\n.\r\n The section name is not necessary the same as the standard header in an\r\n ELF file (such as \r\n\\family typewriter\r\n.text\r\n\\family default\r\n or \r\n\\family typewriter\r\n.init_array\r\n\\family default\r\n, but can be anything.\r\n Non-standard section names are often used for controlling the final binary\r\n layout of a compiled program.\r\n We will explore this techinque in more details when learning the \r\n\\family typewriter\r\nGNU ld\r\n\\family default\r\n linker and the linking process.\r\n Again, the program automatically calls the constructors without explicitly\r\n invoking it:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ./hello \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ninit1\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\ninit2\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\nhello world!\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nFINI_ARRAY\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nis an array of function pointers for program termination, called after exiting\r\n \r\n\\family typewriter\r\nmain()\r\n\\family default\r\n.\r\n If the application terminate abnormally, such as through \r\n\\family typewriter\r\nabort()\r\n\\family default\r\n call or a crash, the \r\n\\family typewriter\r\n.finit_array\r\n\\family default\r\n is ignored.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nA destructor is automatically called after exiting main(), if one or more\r\n available:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n__attribute__((destructor)) static void destructor(){\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"hello world\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ./hello \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nhello world\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ndestructor\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nPREINIT_ARRAY\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nis an array of function pointers that are invoked before all other initializatio\r\nn functions in \r\n\\family typewriter\r\nINIT_ARRAY\r\n\\family default\r\n.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nTo use the \r\n\\family typewriter\r\n.preinit_array\r\n\\family default\r\n, the only way to put functions into this section is to use the attribute\r\n \r\n\\family typewriter\r\nsection()\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid preinit1() {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid preinit2() {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid init1() {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid init2() {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"%s\r\n\\backslash\r\nn\", __FUNCTION__);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\ntypedef void (*preinit)();\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\ntypedef void (*init)();\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n__attribute__((section(\".preinit_array\"))) preinit preinit_arr[2] = {preinit1,\r\n preinit2};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n__attribute__((section(\".preinit_array\"))) init init_arr[2] = {init1, init2};\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"hello world!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 hello2.c -o hello2\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ./hello2\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\npreinit1\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\npreinit2\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ninit1\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ninit2\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nhello world!\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nGROUP\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\ndefines a section group, which is the same section that appears in different\r\n object files but when merged into the final executable binary file, only\r\n one copy is kept and the rest in other object files are discarded.\r\n This section is only relevant in C++ object files, so we will not examine\r\n further.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nSYMTAB_SHNDX\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nis a section containing extended section indexes, that are associated with\r\n a symbol table.\r\n This section only appears when the \r\n\\family typewriter\r\nNdx\r\n\\family default\r\n value of an entry in the symbol table exceeds the \r\n\\family typewriter\r\nLORESERVE\r\n\\family default\r\n value.\r\n This section then maps between a symbol and an actual index value of a\r\n section header.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nUpon understanding section types, we can understand the number in \r\n\\family typewriter\r\nLink\r\n\\emph on\r\n \r\n\\family default\r\n\\emph default\r\nand \r\n\\family typewriter\r\nInfo\r\n\\emph on\r\n \r\n\\family default\r\n\\emph default\r\nfields:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{table*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{The meannings of \r\n\\backslash\r\ntexttt{\r\n\\backslash\r\ntextbf{\r\n\\backslash\r\nemph{Link}}} and \r\n\\backslash\r\ntexttt{\r\n\\backslash\r\ntextbf{\r\n\\backslash\r\nemph{Info}} depend on section types.} interpretation}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"7\" columns=\"3\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"3cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"6cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"6cm\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nType\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nLink\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nInfo\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nDYNAMIC\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nEntries in this section uses the section index of the dynamic string table.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nHASH\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nGNU_HASH\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe section index of the symbol table to which the hash table applies.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n0\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nREL\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nRELA\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe section index of the associated symbol table.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe section index to which the relocation applies.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nSYMTAB\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nDYNSYM\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe section index of the associated string table.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nOne greater than the symbol table index of the last local symbol.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nGROUP\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe section index of the associated symbol table.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe symbol index of an entry in the associated symbol table.\r\n The name of the specified symbol table entry provides a signature for the\r\n section group.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nSYMTAB_SHNDX\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThe section header index of the associated symbol table.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nhfill\r\n\\backslash\r\nbreak\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{table*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nVerify that the value of the \r\n\\family typewriter\r\nLink\r\n\\family default\r\n field of a \r\n\\family typewriter\r\nSYMTAB\r\n\\family default\r\n section is the index of a \r\n\\family typewriter\r\nSTRTAB\r\n\\family default\r\n section.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nVerify that the value of the \r\n\\family typewriter\r\nInfo\r\n\\family default\r\n field of a \r\n\\family typewriter\r\nSYMTAB\r\n\\family default\r\n section is the index of last local symbol + 1.\r\n It means, in the symbol table, from the index listed by \r\n\\family typewriter\r\nInfo\r\n\\family default\r\n field onward, no local symbol appears.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nVerify that the value of the \r\n\\family typewriter\r\nInfo\r\n\\family default\r\n field of a \r\n\\family typewriter\r\nREL\r\n\\family default\r\n section is the index of the \r\n\\family typewriter\r\nSYMTAB\r\n\\family default\r\n section.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nVerify that the value of the \r\n\\family typewriter\r\nLink\r\n\\family default\r\n field of a \r\n\\family typewriter\r\nREL\r\n\\family default\r\n section is the index of the section where relocation is applied.\r\n For example.\r\n if the section is \r\n\\family typewriter\r\n.rel.text\r\n\\family default\r\n, then the relocating section should be \r\n\\family typewriter\r\n.text\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nProgram header table\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"sec:Program-header-table\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\nprogram header table\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nprogram header table\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is an array of program headers that defines the memory layout of a program\r\n at runtime.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\nprogram header\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nprogram header\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a description of a program segment.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\nprogram segment\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nprogram segment\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a collection of related sections.\r\n A segment contains zero or more sections.\r\n An operating system when loading a program, \r\n\\emph on\r\nonly use segments\r\n\\emph default\r\n, not sections.\r\n To see the information of a program header table, we use the \r\n\\family typewriter\r\n-l \r\n\\family default\r\noption with \r\n\\family typewriter\r\nreadelf:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\r\n\\family typewriter\r\n$ readelf -l <binary file>\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to a section, a program header also has types:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nPHDR \r\n\\family default\r\nspecifies the location and size of the program header table itself, both\r\n in the file and in the memory image of the program\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nINTERP\r\n\\family default\r\n\\series bold\r\n \r\n\\series default\r\nspecifies the location and size of a null-terminated path name to invoke\r\n as an interpreter for linking runtime libraries.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nLOAD \r\n\\family default\r\nspecifies a loadable segment.\r\n That is, this segment is loaded into main memory.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\series bold\r\nDYNAMIC \r\n\\family default\r\n\\series default\r\nspecifies dynamic linking information.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nNOTE \r\n\\family default\r\nspecifies the location and size of auxiliary information.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nTLS \r\n\\family default\r\nspecifies the \r\n\\emph on\r\nThread-Local Storage template\r\n\\emph default\r\n, which is formed from the combination of all sections with the flag \r\n\\family typewriter\r\nTLS\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\nGNU_STACK\r\n\\family default\r\n indicates whether the program's stack should be made executable or not.\r\n Linux kernel uses this type.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA segment also has permission, which is a combination of these 3 values:\r\n\\begin_inset Float margintable\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nSegment Permission\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"4\" columns=\"2\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nPermission\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nR\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nReadable\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nW\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nWritable\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nExecutable\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nRead (\r\n\\family typewriter\r\nR\r\n\\family default\r\n)\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nWrite (\r\n\\family typewriter\r\nW\r\n\\family default\r\n)\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nExecute (\r\n\\family typewriter\r\nE\r\n\\family default\r\n)\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"-4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nThe command to get the program header table:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\r\n\\family typewriter\r\n$ readelf -l hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOutput:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x400430\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 9 program headers, starting at offset 64\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset             VirtAddr           PhysAddr\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n                 FileSiz            MemSiz              Flags  Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n                 0x00000000000001f8 0x00000000000001f8  R E    8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n                 0x000000000000001c 0x000000000000001c  R      1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color red\r\nLOAD\r\n\\color inherit\r\n           0x0000000000000000 0x0000000000400000 0x0000000000400000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n                 0x000000000000070c 0x000000000000070c  R E    200000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color red\r\nLOAD\r\n\\color inherit\r\n           0x0000000000000e10 0x0000000000600e10 0x0000000000600e10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n                 0x0000000000000228 0x0000000000000230  RW     200000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  DYNAMIC        0x0000000000000e28 0x0000000000600e28 0x0000000000600e28\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n                 0x00000000000001d0 0x00000000000001d0  RW     8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n                 0x0000000000000044 0x0000000000000044  R      4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  GNU_EH_FRAME   0x00000000000005e4 0x00000000004005e4 0x00000000004005e4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n                 0x0000000000000034 0x0000000000000034  R      4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n                 0x0000000000000000 0x0000000000000000  RW     10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  GNU_RELRO      0x0000000000000e10 0x0000000000600e10 0x0000000000600e10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n                 0x00000000000001f0 0x00000000000001f0  R      1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .interp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .plt.got .text .fini\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.rodata .eh_frame_hdr .eh_frame \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   03     .init_array .fini_array .jcr .dynamic .got .got.plt .data .bss \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   04     .dynamic \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   05     .note.ABI-tag .note.gnu.build-id \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   06     .eh_frame_hdr \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   07     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   08     .init_array .fini_array .jcr .dynamic .got \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the sample output, \r\n\\family typewriter\r\nLOAD \r\n\\family default\r\nsegment appears twice:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nLOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n               0x000000000000070c 0x000000000000070c  R E    200000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nLOAD           0x0000000000000e10 0x0000000000600e10 0x0000000000600e10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n               0x0000000000000228 0x0000000000000230  RW     200000\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhy? Notice the permission: \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nthe upper \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n has Read and Execute permission.\r\n This is a \r\n\\emph on\r\ntext\r\n\\emph default\r\n segment.\r\n A text segment contains read-only instructions and read-only data.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nthe lower \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n has Read and Write permission.\r\n This is a \r\n\\emph on\r\ndata\r\n\\emph default\r\n segment.\r\n It means that this segment can be read and written to, but is not allowed\r\n to be used as executable code, for security reason.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, \r\n\\family typewriter\r\nLOAD \r\n\\family default\r\ncontains the following sections:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .plt.got .text .fini \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.rodata .eh_frame_hdr .eh_frame \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   03     .init_array .fini_array .jcr .dynamic .got .got.plt .data .bss \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe first number is the index of a program header in program header table,\r\n and the remaining text is the list of all sections within a segment.\r\n Unfortunately, \r\n\\family typewriter\r\nreadelf\r\n\\family default\r\n does not print the index, so a user needs to keep track manually which\r\n segment is of which index.\r\n First segment starts at index 0, second at index 1 and so on.\r\n \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n are segments at index 2 and 3.\r\n As can be seen from the two lists of sections, most sections are loadable\r\n and is available at runtime.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nSegments vs sections\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAs mentioned earlier, an operating system loads program segments, not sections.\r\n However, a question arises: Why doesn't the operating system use sections\r\n instead? After all, a section also contains similar information to a program\r\n segment, such as the type, the virtual memory address to be loaded, the\r\n size, the attributes, the flags and align.\r\n As explained before, a segment is the perspective of an operating system,\r\n while a section is the perspective of a linker.\r\n To understand why, looking into the structure of a segment, we can easily\r\n see:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nA segment is a collection of sections.\r\n It means that sections are logically grouped together by their attributes.\r\n For example, all sections in a \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment are always loaded by the operating system; all sections have the\r\n same permission, either a \r\n\\family typewriter\r\nRE \r\n\\family default\r\n(Read + Execute) for executable sections, or \r\n\\family typewriter\r\nRW\r\n\\family default\r\n (Read + Write) for data sections.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nBy grouping sections into a segment, it is easier for an operating system\r\n to batch load sections just once by loading the start and end of a segment,\r\n instead of loading section by section.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nSince a segment is for loading a program and a section is for linking a\r\n program, all the sections in a segment is \r\n\\emph on\r\nwithin its start and end virtual memory addresses of a segment\r\n\\emph default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo see the last point clearer, consider an example of linking two object\r\n files.\r\n Suppose we have two source files:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"Hello World\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nand:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=C,tabsize=4,title={\\texttt{math.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint add(int a, int b) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return a + b;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow, compile the two source files as \r\n\\emph on\r\nobject files\r\n\\emph default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 -c math.c \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 -c hello.c\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, we check the sections of \r\n\\family typewriter\r\nmath.o\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -S math.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 11 section headers, starting at offset 0x1a8:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSection Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk\r\n Inf Al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 0]                   NULL            \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000000 000000 00      0   0  0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 1] .text             PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000034 00000d 00  AX  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 2] .data             PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000041 000000 00  WA  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 3] .bss              NOBITS          \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000041 000000 00  WA  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 4] .comment          PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000041 000035 01  MS  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 5] .note.GNU-stack   PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000076 000000 00      0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 6] .eh_frame         PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000078 000038 00   A  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 7] .rel.eh_frame     REL             \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 00014c 000008 08   I  9   6  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 8] .shstrtab         STRTAB          \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000154 000053 00      0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 9] .symtab           SYMTAB          \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 0000b0 000090 10     10   8  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [10] .strtab           STRTAB          \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000140 00000c 00      0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nKey to Flags:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  W (write), A (alloc), X (execute), M (merge), S (strings)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  O (extra OS processing required) o (OS specific), p (processor specific)\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAs shown in the output, all the section virtual memory addresses of every\r\n section are set to 0.\r\n At this stage, each object file is simply a \r\n\\emph on\r\nblock of binary\r\n\\emph default\r\n that contains code and data.\r\n Its existence is to serve as a material container for the final product,\r\n which is the executable binary.\r\n As such, the virtual addresses in \r\n\\family typewriter\r\nhello.o\r\n\\family default\r\n are all zeroes.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNo segment exists at this stage:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l math.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nThere are no program headers in this file.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe same happens to other object file:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 13 section headers, starting at offset 0x224:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSection Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk\r\n Inf Al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 0]                   NULL            \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000000 000000 00      0   0  0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 1] .text             PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000034 00002e 00  AX  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 2] .rel.text         REL             \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 0001ac 000010 08   I 11   1  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 3] .data             PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000062 000000 00  WA  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 4] .bss              NOBITS          \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000062 000000 00  WA  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 5] .rodata           PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000062 00000c 00   A  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 6] .comment          PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 00006e 000035 01  MS  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 7] .note.GNU-stack   PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 0000a3 000000 00      0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 8] .eh_frame         PROGBITS        \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 0000a4 000044 00   A  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 9] .rel.eh_frame     REL             \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 0001bc 000008 08   I 11   8  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [10] .shstrtab         STRTAB          \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 0001c4 00005f 00      0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [11] .symtab           SYMTAB          \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 0000e8 0000b0 10     12   9  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [12] .strtab           STRTAB          \r\n\\color red\r\n00000000\r\n\\color inherit\r\n 000198 000013 00      0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nKey to Flags:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  W (write), A (alloc), X (execute), M (merge), S (strings)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  O (extra OS processing required) o (OS specific), p (processor specific)\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l hello.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nThere are no program headers in this file.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOnly when object files are combined into a final executable binary, sections\r\n are fully realized:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 math.o hello.o -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -S hello.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 31 section headers, starting at offset 0x1804:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSection Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk\r\n Inf Al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 0]                   NULL            00000000 000000 000000 00     \r\n 0   0  0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 1] .interp           PROGBITS        \r\n\\color green\r\n08048154\r\n\\color inherit\r\n 000154 000013 00   A  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 2] .note.ABI-tag     NOTE            \r\n\\color green\r\n08048168\r\n\\color inherit\r\n 000168 000020 00   A  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 3] .note.gnu.build-i NOTE            \r\n\\color green\r\n08048188\r\n\\color inherit\r\n 000188 000024 00   A  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 4] .gnu.hash         GNU_HASH        \r\n\\color green\r\n080481ac\r\n\\color inherit\r\n 0001ac 000020 04   A  5   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 5] .dynsym           DYNSYM          \r\n\\color green\r\n080481cc\r\n\\color inherit\r\n 0001cc 000050 10   A  6   1  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 6] .dynstr           STRTAB          \r\n\\color green\r\n0804821c\r\n\\color inherit\r\n 00021c 00004a 00   A  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 7] .gnu.version      VERSYM          \r\n\\color green\r\n08048266\r\n\\color inherit\r\n 000266 00000a 02   A  5   0  2\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 8] .gnu.version_r    VERNEED         \r\n\\color green\r\n08048270\r\n\\color inherit\r\n 000270 000020 00   A  6   1  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 9] .rel.dyn          REL             \r\n\\color green\r\n08048290\r\n\\color inherit\r\n 000290 000008 08   A  5   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [10] .rel.plt          REL             \r\n\\color green\r\n08048298\r\n\\color inherit\r\n 000298 000010 08  AI  5  24  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [11] .init             PROGBITS        \r\n\\color green\r\n080482a8\r\n\\color inherit\r\n 0002a8 000023 00  AX  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [12] .plt              PROGBITS        \r\n\\color green\r\n080482d0\r\n\\color inherit\r\n 0002d0 000030 04  AX  0   0 16\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [13] .plt.got          PROGBITS        \r\n\\color green\r\n08048300\r\n\\color inherit\r\n 000300 000008 00  AX  0   0  8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [14] .text             PROGBITS        \r\n\\color green\r\n08048310\r\n\\color inherit\r\n 000310 0001a2 00  AX  0   0 16\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [15] .fini             PROGBITS        \r\n\\color green\r\n080484b4\r\n\\color inherit\r\n 0004b4 000014 00  AX  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [16] .rodata           PROGBITS        \r\n\\color green\r\n080484c8\r\n\\color inherit\r\n 0004c8 000014 00   A  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [17] .eh_frame_hdr     PROGBITS        \r\n\\color green\r\n080484dc\r\n\\color inherit\r\n 0004dc 000034 00   A  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [18] .eh_frame         PROGBITS        \r\n\\color green\r\n08048510\r\n\\color inherit\r\n 000510 0000ec 00   A  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [19] .init_array       INIT_ARRAY      \r\n\\color green\r\n08049f08\r\n\\color inherit\r\n 000f08 000004 00  WA  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [20] .fini_array       FINI_ARRAY      \r\n\\color green\r\n08049f0c\r\n\\color inherit\r\n 000f0c 000004 00  WA  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [21] .jcr              PROGBITS        \r\n\\color green\r\n08049f10\r\n\\color inherit\r\n 000f10 000004 00  WA  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [22] .dynamic          DYNAMIC         \r\n\\color green\r\n08049f14\r\n\\color inherit\r\n 000f14 0000e8 08  WA  6   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [23] .got              PROGBITS        \r\n\\color green\r\n08049ffc\r\n\\color inherit\r\n 000ffc 000004 04  WA  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [24] .got.plt          PROGBITS        \r\n\\color green\r\n0804a000\r\n\\color inherit\r\n 001000 000014 04  WA  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [25] .data             PROGBITS        \r\n\\color green\r\n0804a014\r\n\\color inherit\r\n 001014 000008 00  WA  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [26] .bss              NOBITS          \r\n\\color green\r\n0804a01c\r\n\\color inherit\r\n 00101c 000004 00  WA  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [27] .comment          PROGBITS        00000000 00101c 000034 01  MS  0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [28] .shstrtab         STRTAB          00000000 0016f8 00010a 00      0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [29] .symtab           SYMTAB          00000000 001050 000470 10     30\r\n  48  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [30] .strtab           STRTAB          00000000 0014c0 000238 00      0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nKey to Flags:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  W (write), A (alloc), X (execute), M (merge), S (strings)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  O (extra OS processing required) o (OS specific), p (processor specific)\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEvery loadable section is assigned an address, highlighted in \r\n\\color green\r\ngreen\r\n\\color inherit\r\n.\r\n The reason each section got its own address is that in reality, \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n \r\n\\emph on\r\ndoes not combine an object by itself, but invokes the linker\r\n\\emph default\r\n \r\n\\family typewriter\r\nld\r\n\\family default\r\n.\r\n The linker \r\n\\family typewriter\r\nld\r\n\\family default\r\n uses the default script that it can find in the system to build the executable\r\n binary.\r\n In the default script, a segment is assigned a starting address \r\n\\family typewriter\r\n0x8048000\r\n\\family default\r\n and sections belong to it.\r\n Then:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Formula $\\mathtt{1^{st}\\,section\\,address=starting\\,segment\\,address+section\\,offset=0x8048000+0x154=0x08048154}$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Formula $\\mathtt{2^{nd}\\,section\\,address=starting\\,segment\\,address+section\\,offset=0x8048000+0x168=0x08048168}$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nand so on until the last loadable section.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIndeed, the end address of a segment is also the end address of the final\r\n section.\r\n We can see this by listing all the segments:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAnd check, for example, \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment which starts at \r\n\\family typewriter\r\n\\color red\r\n0x08048000\r\n\\family default\r\n\\color inherit\r\n and end at \r\n\\begin_inset Formula $\\mathtt{0x08048000+0x005fc=0x080485fc}$\r\n\\end_inset\r\n\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x8048310\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 9 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000034 0x08048034 0x08048034 0x00120 0x00120 R E 0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  INTERP         0x000154 0x08048154 0x08048154 0x00013 0x00013 R   0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n      [Requesting program interpreter: /lib/ld-linux.so.2]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x000000 0x08048000 \r\n\\color red\r\n0x08048000\r\n\\color inherit\r\n 0x005fc \r\n\\color blue\r\n0x005fc\r\n\\color inherit\r\n R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x000f08 0x08049f08 0x08049f08 0x00114 0x00118 RW  0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  DYNAMIC        0x000f14 0x08049f14 0x08049f14 0x000e8 0x000e8 RW  0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  NOTE           0x000168 0x08048168 0x08048168 0x00044 0x00044 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  GNU_EH_FRAME   0x0004dc 0x080484dc 0x080484dc 0x00034 0x00034 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  GNU_RELRO      0x000f08 0x08049f08 0x08049f08 0x000f8 0x000f8 R   0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .interp \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .plt.got .text .fini \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.rodata .eh_frame_hdr .eh_frame \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   03     .init_array .fini_array .jcr .dynamic .got .got.plt .data .bss \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   04     .dynamic \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   05     .note.ABI-tag .note.gnu.build-id \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   06     .eh_frame_hdr \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   07     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   08     .init_array .fini_array .jcr .dynamic .got \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe last section in the first \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment is \r\n\\family typewriter\r\n.eh_frame\r\n\\family default\r\n.\r\n The \r\n\\family typewriter\r\n.eh_frame\r\n\\family default\r\n section starts at \r\n\\family typewriter\r\n0x0804851\r\n\\family default\r\n because the start address is \r\n\\family typewriter\r\n0x08048000\r\n\\family default\r\n, the offset into the file is \r\n\\family typewriter\r\n0x510\r\n\\family default\r\n.\r\n The end address of \r\n\\family typewriter\r\n.eh_frame\r\n\\family default\r\n should be: \r\n\\begin_inset Formula $\\mathtt{0x08048000+0x510+0xec=0x080485fc}$\r\n\\end_inset\r\n\r\n because the segment size is \r\n\\family typewriter\r\n0x\r\n\\family default\r\nec.\r\n This is exactly the same as the end address of the first \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment above: \r\n\\begin_inset Formula $\\mathtt{0x08048000+0x5ec=0x080485fc}$\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nChapter \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"chap:Linking-and-loading\"\r\n\r\n\\end_inset\r\n\r\n will explore this whole process in detail.\r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nRuntime inspection and debug\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA\r\n\\emph on\r\n \r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\ndebugger\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\ndebugger\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\ndebugger\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a program that allows inspection of a running program.\r\n A debugger can start and run a program then stop at a specific line for\r\n examining the state of the program at that point.\r\n The point where the debugger stop (but not halt) is called a \r\n\\emph on\r\nbreakpoint\r\n\\emph default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe will be using the \r\n\\series bold\r\n\\emph on\r\nGDB -\r\n\\series default\r\n \r\n\\series bold\r\nG\r\n\\series default\r\nNU\r\n\\series bold\r\n D\r\n\\series default\r\ne\r\n\\series bold\r\nb\r\n\\series default\r\nugger\r\n\\emph default\r\n for debugging our kernel.\r\n \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\emph on\r\ngdb\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is the program name.\r\n \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\emph on\r\ngdb\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n can do four main kinds of things:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nStart your program, specifying anything that might affect its behavior.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nMake your program stop on specified conditions.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nExamine what has happened, when your program has stopped\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nChange things in your program, so you can experiment with correcting the\r\n effects of one bug and go on to learn about another\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nA sample program\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThere must be an existing program for debugging.\r\n The good old \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nHello World\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n program suffices for the educational purpose in this chapter:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe compile it with debugging information with the option \r\n\\family typewriter\r\n-g\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 -g hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, we start gdb with the program as argument:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\r\n\\family typewriter\r\n$ gdb hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nStatic inspection of a program\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBefore inspecting a program at runtime, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n loads it first.\r\n Upon loading into memory (but without running), a lot of useful information\r\n can be retrieve for inspection.\r\n The commands in this section can be used before the program runs.\r\n However, they are also usable when the program runs and can display even\r\n more information.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nCommand:\r\n\\family typewriter\r\n info target/info file/info files\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command prints the information of the target being debugged.\r\n A \r\n\\emph on\r\ntarget\r\n\\emph default\r\n is the debugging program.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nThe output of the command from \r\n\\family typewriter\r\nhello\r\n\\family default\r\n program, a local target in detail:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ninfo target\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSymbols from \"/tmp/hello\".\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nLocal exec file:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t`/tmp/hello', file type elf32-i386.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\tEntry point: 0x8048310\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08048154 - 0x08048167 is .interp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08048168 - 0x08048188 is .note.ABI-tag\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08048188 - 0x080481ac is .note.gnu.build-id\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x080481ac - 0x080481cc is .gnu.hash\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x080481cc - 0x0804821c is .dynsym\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x0804821c - 0x08048266 is .dynstr\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08048266 - 0x08048270 is .gnu.version\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08048270 - 0x08048290 is .gnu.version_r\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08048290 - 0x08048298 is .rel.dyn\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08048298 - 0x080482a8 is .rel.plt\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x080482a8 - 0x080482cb is .init\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x080482d0 - 0x08048300 is .plt\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08048300 - 0x08048308 is .plt.got\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08048310 - 0x080484a2 is .text\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x080484a4 - 0x080484b8 is .fini\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x080484b8 - 0x080484cd is .rodata\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x080484d0 - 0x080484fc is .eh_frame_hdr\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x080484fc - 0x080485c8 is .eh_frame\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08049f08 - 0x08049f0c is .init_array\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08049f0c - 0x08049f10 is .fini_array\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08049f10 - 0x08049f14 is .jcr\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08049f14 - 0x08049ffc is .dynamic\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x08049ffc - 0x0804a000 is .got\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x0804a000 - 0x0804a014 is .got.plt\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x0804a014 - 0x0804a01c is .data\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t0x0804a01c - 0x0804a020 is .bss\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe output displayed reports:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nPath of a symbol file.\r\n A \r\n\\emph on\r\nsymbol file\r\n\\emph default\r\n is the file that contains the debugging information.\r\n Usually, this is the same file as the binary, but it is common to separate\r\n between an executable binary and its debugging information into 2 files,\r\n especially for remote debugging.\r\n In the example, it is this line:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\nSymbols from \"/tmp/hello\".\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nThe path of the debugging program and its file type.\r\n In the example, it is this line:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\nLocal exec file:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\t`/tmp/hello', file type elf32-i386.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nThe entry point to the debugging program.\r\n That is, the very first code the program runs.\r\n In the example, it is this line:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\nEntry point: 0x8048310\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nA list of sections with its starting and ending addresses.\r\n In the example, it is the remaining output.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nIf the debugging program runs in a different machine, it is a remote target\r\n and \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n only prints a brief information:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ninfo target\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nRemote serial target in gdb-specific protocol:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDebugging a target over a serial line.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nmaint info sections\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command is similar to \r\n\\family typewriter\r\ninfo targe\r\n\\family default\r\nt but give extra information about program sections, specifically the file\r\n offset and the flags of each section.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nHere is the output when running against \r\n\\family typewriter\r\nhello\r\n\\family default\r\n program:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nmaint info sections\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\nExec file:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n    `/tmp/hello', file type elf64-x86-64.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [0]     0x00400238->0x00400254 at 0x00000238: \r\n\\color blue\r\n.interp\r\n\\color inherit\r\n ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [1]     0x00400254->0x00400274 at 0x00000254: \r\n\\color blue\r\n.note.ABI-tag\r\n\\color inherit\r\n ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [2]     0x00400274->0x00400298 at 0x00000274: \r\n\\color blue\r\n.note.gnu.build-id\r\n\\color inherit\r\n ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [3]     0x00400298->0x004002b4 at 0x00000298: \r\n\\color blue\r\n.gnu.hash\r\n\\color inherit\r\n ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [4]     0x004002b8->0x00400318 at 0x000002b8: \r\n\\color blue\r\n.dynsym \r\n\\color inherit\r\nALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [5]     0x00400318->0x00400355 at 0x00000318: \r\n\\color blue\r\n.dynstr\r\n\\color inherit\r\n ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [6]     0x00400356->0x0040035e at 0x00000356: \r\n\\color blue\r\n.gnu.version\r\n\\color inherit\r\n ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [7]     0x00400360->0x00400380 at 0x00000360: \r\n\\color blue\r\n.gnu.version_r\r\n\\color inherit\r\n ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n....remaining output omitted....\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nThe output is similar to \r\n\\family typewriter\r\ninfo target\r\n\\family default\r\n, but with more details.\r\n Next to the \r\n\\color blue\r\nsection names\r\n\\color inherit\r\n are the section flags, which are attributes of a section.\r\n Here, we can see that the sections with \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n flag are from \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment.\r\n The command can be combined with the section flags for filtered outputs:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nALLOBJ displays sections for all loaded object files, including shared libraries.\r\n Shared libraries are only displayed when the program is already running.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nsection\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nnames displays only named sections.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nThe command:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nmaint info sections \r\n\\color blue\r\n.text .data .bss\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\nonly displays \r\n\\family typewriter\r\n\\color blue\r\n.text\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color blue\r\n.data\r\n\\family default\r\n\\color inherit\r\n and \r\n\\family typewriter\r\n\\color blue\r\n.bss\r\n\\family default\r\n \r\n\\color inherit\r\nsections:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\nExec file:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n    `/tmp/hello', file type elf64-x86-64.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [13]     0x00400430->0x004005c2 at 0x00000430: \r\n\\color blue\r\n.text\r\n\\color inherit\r\n ALLOC LOAD READONLY CODE HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [24]     0x00601028->0x00601038 at 0x00001028: \r\n\\color blue\r\n.data\r\n\\color inherit\r\n ALLOC LOAD DATA HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [25]     0x00601038->0x00601040 at 0x00001038: \r\n\\color blue\r\n.bss\r\n\\color inherit\r\n ALLOC\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\nsection-flags displays only sections with specified section flags.\r\n Note that these section flags are specific to \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n, though it is based on the section attributes defined previously.\r\n Currently, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n understands the following flags:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Description\r\nALLOC Section will have space allocated in the process when loaded.\r\n Set for all sections except those containing debug information.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nLOAD Section will be loaded from the file into the child process memory.\r\n Set for pre-initialized code and data, clear for .bss sections.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nRELOC Section needs to be relocated before loading.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nREADONLY Section cannot be modified by the child process.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nCODE Section contains executable code only.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nDATA Section contains data only (no executable code).\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nROM Section will reside in ROM.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nCONSTRUCTOR Section contains data for constructor/destructor lists.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nHAS_CONTENTS Section is not empty.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nNEVER_LOAD An instruction to the linker to not output the section.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nCOFF_SHARED_LIBRARY A notification to the linker that the section contains\r\n COFF shared library information.\r\n COFF is an object file format, similar to ELF.\r\n While ELF is the file format for an executable binary, COFF is the file\r\n format for an object file.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nIS_COMMON Section contains common symbols.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWe can restrict the output to only display sections that contain code with\r\n the command:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nmaint info sections\r\n\\color blue\r\n CODE\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe output:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\nExec file:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n    `/tmp/hello', file type elf64-x86-64.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [10]     0x004003c8->0x004003e2 at 0x000003c8: .init ALLOC LOAD READONLY\r\n \r\n\\color red\r\nCODE\r\n\\color inherit\r\n HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [11]     0x004003f0->0x00400420 at 0x000003f0: .plt ALLOC LOAD READONLY\r\n \r\n\\color red\r\nCODE\r\n\\color inherit\r\n HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [12]     0x00400420->0x00400428 at 0x00000420: .plt.got ALLOC LOAD READONLY\r\n \r\n\\color red\r\nCODE\r\n\\color inherit\r\n HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [13]     0x00400430->0x004005c2 at 0x00000430: .text ALLOC LOAD READONLY\r\n \r\n\\color red\r\nCODE\r\n\\color inherit\r\n HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\size footnotesize\r\n [14]     0x004005c4->0x004005cd at 0x000005c4: .fini ALLOC LOAD READONLY\r\n \r\n\\color red\r\nCODE\r\n\\color inherit\r\n HAS_CONTENTS\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand:\r\n\\family typewriter\r\n info functions\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis commands list all function names and their loaded addresses.\r\n The names can be filtered with a regular expression.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nRun the command, we get the following output:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ninfo functions\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nAll defined functions:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nFile hello.c:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\nint main(int, char **);\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nNon-debugging symbols:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x00000000004003c8  \r\n\\color blue\r\n_init\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000400400  \r\n\\color blue\r\nputs@plt\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000400410  \r\n\\color blue\r\n__libc_start_main@plt\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000400430  \r\n\\color blue\r\n_start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000400460  \r\n\\color blue\r\nderegister_tm_clones\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x00000000004004a0  \r\n\\color blue\r\nregister_tm_clones\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x00000000004004e0 \r\n\\color blue\r\n __do_global_dtors_aux\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000400500  \r\n\\color blue\r\nframe_dummy\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000400550  \r\n\\color blue\r\n__libc_csu_init\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x00000000004005c0  \r\n\\color blue\r\n__libc_csu_fini\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x00000000004005c4  \r\n\\color blue\r\n_fini\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\ninfo variables\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command lists all global and static variable names, or filtered with\r\n a regular expression.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nIf we add a global variable \r\n\\family typewriter\r\nint\r\n\\family default\r\n \r\n\\family typewriter\r\ni\r\n\\family default\r\n into the sample source program and recompile then run the command, we get\r\n the following output:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ninfo variables\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nAll defined variables:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nFile hello.c:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\nint i;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nNon-debugging symbols:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x00000000004005d0  \r\n\\color blue\r\n_IO_stdin_used\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x00000000004005e4  \r\n\\color blue\r\n__GNU_EH_FRAME_HDR\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000400708  \r\n\\color blue\r\n__FRAME_END__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000600e10  \r\n\\color blue\r\n__frame_dummy_init_array_entry\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000600e10  \r\n\\color blue\r\n__init_array_start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000600e18  \r\n\\color blue\r\n__do_global_dtors_aux_fini_array_entry\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000600e18  \r\n\\color blue\r\n__init_array_end\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000600e20  \r\n\\color blue\r\n__JCR_END__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000600e20  \r\n\\color blue\r\n__JCR_LIST__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000600e28  \r\n\\color blue\r\n_DYNAMIC\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000601000  \r\n\\color blue\r\n_GLOBAL_OFFSET_TABLE_\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000601028  \r\n\\color blue\r\n__data_start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000601028  \r\n\\color blue\r\ndata_start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000601030  \r\n\\color blue\r\n__dso_handle\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x000000000060103c  \r\n\\color blue\r\n__bss_start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x000000000060103c  \r\n\\color blue\r\n_edata\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x000000000060103c  \r\n\\color blue\r\ncompleted\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000601040  \r\n\\color blue\r\n__TMC_END__\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0000000000601040  \r\n\\color blue\r\n_end\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\ndisassemble/disas\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command displays the assembly code of the executable file.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\ngdb\r\n\\family default\r\n can display the assembly code of a function:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ndisassemble\r\n\\color inherit\r\n \r\n\\color blue\r\nmain\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDump of assembler code for function \r\n\\color blue\r\nmain\r\n\\color inherit\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840b <+0>: \tlea    ecx,[esp+0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840f <+4>: \tand    esp,0xfffffff0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048412 <+7>: \tpush   DWORD PTR [ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048415 <+10>:\tpush   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048416 <+11>:\tmov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048418 <+13>:\tpush   ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048419 <+14>:\tsub    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804841c <+17>:\tsub    esp,0xc\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804841f <+20>:\tpush   0x80484c0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048424 <+25>:\tcall   0x80482e0 <puts@plt>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048429 <+30>:\tadd    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804842c <+33>:\tmov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048431 <+38>:\tmov    ecx,DWORD PTR [ebp-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048434 <+41>:\tleave  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048435 <+42>:\tlea    esp,[ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048438 <+45>:\tret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEnd of assembler dump.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nIt would be more useful if source is included:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ndisassemble\r\n\\color inherit\r\n \r\n\\color blue\r\n/s main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDump of assembler code for function \r\n\\color blue\r\nmain\r\n\\color inherit\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nhello.c:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color green\r\n4\t{\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840b <+0>:\tlea     ecx,[esp+0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840f <+4>:\tand     esp,0xfffffff0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048412 <+7>:\tpush    DWORD PTR [ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048415 <+10>:\tpush   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048416 <+11>:\tmov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048418 <+13>:\tpush   ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048419 <+14>:\tsub    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color green\r\n5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804841c <+17>:\tsub    esp,0xc\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804841f <+20>:\tpush   0x80484c0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048424 <+25>:\tcall   0x80482e0 <puts@plt>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048429 <+30>:\tadd    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color green\r\n6\t    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804842c <+33>:\tmov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color green\r\n7\t}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048431 <+38>:\tmov    ecx,DWORD PTR [ebp-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048434 <+41>:\tleave  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048435 <+42>:\tlea    esp,[ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048438 <+45>:\tret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEnd of assembler dump.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow the high level source (in \r\n\\color green\r\ngreen\r\n\\color inherit\r\n text) is included as part of the assembly dump.\r\n Each line is backed by the corresponding assembly code below it.\r\n \r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nIf the option \r\n\\family typewriter\r\n/r\r\n\\family default\r\n is added, raw instructions in hex are included, just like how \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n displays assembly code by default:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ndisassemble\r\n\\color inherit\r\n \r\n\\color blue\r\n/rs main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDump of assembler code for function main:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nhello.c:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n4\t{\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840b <+0>:\t\r\n\\color green\r\n 8d 4c 24 04\r\n\\color inherit\r\n\t   lea    ecx,[esp+0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840f <+4>:\t\r\n\\color green\r\n 83 e4 f0\r\n\\color inherit\r\n\t      and    esp,0xfffffff0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048412 <+7>:\t\r\n\\color green\r\n ff 71 fc\r\n\\color inherit\r\n\t      push   DWORD PTR [ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048415 <+10>:\t\r\n\\color green\r\n55\r\n\\color inherit\r\n\t  push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048416 <+11>:\t\r\n\\color green\r\n89 e5\r\n\\color inherit\r\n   mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048418 <+13>:\t\r\n\\color green\r\n51\r\n\\color inherit\r\n\t  push   ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048419 <+14>:\t\r\n\\color green\r\n83 ec 04\r\n\\color inherit\r\n\t      sub    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804841c <+17>:\t\r\n\\color green\r\n83 ec 0c\r\n\\color inherit\r\n\t      sub    esp,0xc\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804841f <+20>:\t\r\n\\color green\r\n68 c0 84 04 08\r\n\\color inherit\r\n\tpush   0x80484c0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048424 <+25>:\t\r\n\\color green\r\ne8 b7 fe ff ff\r\n\\color inherit\r\n\tcall   0x80482e0 <puts@plt>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048429 <+30>:\t\r\n\\color green\r\n83 c4 10\r\n\\color inherit\r\n\t      add    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n6\t    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804842c <+33>:\t\r\n\\color green\r\nb8 00 00 00 00\r\n\\color inherit\r\n\tmov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n7\t}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048431 <+38>:\t\r\n\\color green\r\n8b 4d fc\r\n\\color inherit\r\n\t      mov    ecx,DWORD PTR [ebp-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048434 <+41>:\t\r\n\\color green\r\nc9\r\n\\color inherit\r\n\tleave  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048435 <+42>:\t\r\n\\color green\r\n8d 61 fc\r\n\\color inherit\r\n\t      lea    esp,[ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048438 <+45>:\t\r\n\\color green\r\nc3\r\n\\color inherit\r\n\tret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEnd of assembler dump.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nA function in a specific file can also be specified:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ndisassemble\r\n\\color inherit\r\n \r\n\\color blue\r\n/sr 'hello.c'::main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDump of assembler code for function \r\n\\color blue\r\nmain\r\n\\color inherit\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\nhello.c\r\n\\color inherit\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n4\t{\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840b <+0>:\t 8d 4c 24 04\t  lea    ecx,[esp+0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840f <+4>:\t 83 e4 f0\t     and    esp,0xfffffff0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048412 <+7>:\t ff 71 fc\t     push   DWORD PTR [ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048415 <+10>:\t55\t   push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048416 <+11>:\t89 e5\tmov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048418 <+13>:\t51\t   push   ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048419 <+14>:\t83 ec 04\t      sub    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804841c <+17>:\t83 ec 0c\t      sub    esp,0xc\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804841f <+20>:\t68 c0 84 04 08\tpush   0x80484c0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048424 <+25>:\te8 b7 fe ff ff\tcall   0x80482e0 <puts@plt>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048429 <+30>:\t83 c4 10\t      add    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n6\t    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804842c <+33>:\tb8 00 00 00 00\tmov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n7\t}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048431 <+38>:\t8b 4d fc\t      mov    ecx,DWORD PTR [ebp-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048434 <+41>:\tc9\tleave  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048435 <+42>:\t8d 61 fc\t      lea    esp,[ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048438 <+45>:\tc3\tret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEnd of assembler dump.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe filename must be included in a single quote, and the function must be\r\n prefixed by double colons e.g.\r\n \r\n\\family typewriter\r\n'hello.c'::main\r\n\\family default\r\n to specify disassembling of the function \r\n\\family typewriter\r\nmain\r\n\\family default\r\n in the file \r\n\\family typewriter\r\nhello.c\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nx\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command examines the content of a given memory range.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWe can examine the raw content in \r\n\\family typewriter\r\nmain\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nx\r\n\\color inherit\r\n main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x804840b <main>:\t0x04244c8d\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBy default, without any argument, the command only prints the content of\r\n a single memory address.\r\n In this case, that is the starting memory address in \r\n\\family typewriter\r\nmain\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWith format arguments, the command can print a range of memory in a specific\r\n format.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nx\r\n\\color inherit\r\n/\r\n\\color blue\r\n20\r\n\\color green\r\nb\r\n\\color inherit\r\n main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x804840b <main>:\t   0x8d\t0x4c\t0x24\t0x04\t0x83\t0xe40xf0\t0xff\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x8048413 <main+8>:\t 0x71\t0xfc\t0x55\t0x89\t0xe5\t0x510x83\t0xec\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x804841b <main+16>:\t0x04\t0x83\t0xec\t0x0c\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n/\r\n\\color red\r\n20\r\n\\color green\r\nb\r\n\\color inherit\r\n main\r\n\\family default\r\n argument means that the command prints 20 bytes, where \r\n\\family typewriter\r\nmain\r\n\\family default\r\n starts in memory.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Example\r\nThe general form for format argument is: \r\n\\family typewriter\r\n/\r\n\\color blue\r\n<repeated count>\r\n\\color green\r\n<format letter>\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nIf the repeated count is not supplied, by default \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n supplies the count as 1.\r\n The format letter is one the following values:\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"11\" columns=\"2\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nLetter\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\no\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPrint the memory content in \r\n\\emph on\r\noctal\r\n\\emph default\r\n format.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nx\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPrint the memory content in hex format.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nd\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPrint the memory content in decimal format.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nu\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPrint the memory content in \r\n\\emph on\r\nunsigned decimal\r\n\\emph default\r\n format.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nt\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPrint the memory content in \r\n\\emph on\r\nbinary\r\n\\emph default\r\n format.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nf\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPrint the memory content in \r\n\\emph on\r\nfloat\r\n\\emph default\r\n format.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\na\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPrint the memory content as \r\n\\emph on\r\nmemory addresses\r\n\\emph default\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\ni\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPrint the memory content as a series of assembly instructions, similar to\r\n \r\n\\family typewriter\r\ndisassemble\r\n\\family default\r\n command.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nc\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPrint the memory content as an array of ASCII characters.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\ns\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nPrint the memory content as a string\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nDepends on the circumstance, certain format is advantageous than the others.\r\n For example, if a memory region contains floating-point numbers, then it\r\n is better to use the format \r\n\\family typewriter\r\nf\r\n\\family default\r\n than viewing the number as separated 1-byte hex numbers.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nprint/p\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nExamining raw memory is useful but usually it is better to have a more human-rea\r\ndable output.\r\n This command does precisely the task: it pretty-prints an expression.\r\n An expression can be a global variable, a local variable in current stack\r\n frame, a function, a register, a number, etc.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nRuntime inspection of a program\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe main use of a debugger is to examine the state of a program, when it\r\n is running.\r\n \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n provides a set of useful commands for retrieving useful runtime information.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nrun\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command starts running the program.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nRun the hello program:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nr\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/hello \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nHello World!\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[Inferior 1 (process 1002) exited normally]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe program runs successfully and printed the message \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nHello World\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n.\r\n However, it would not be useful if all \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n can do is run a program.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nbreak/b\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command sets a breakpoint at a location in the high-level source code.\r\n When \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n runs to a specific location marked by a breakpoint, it stops executing\r\n for a programmer to inspect the current state of a program.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nA breakpoint can be set on a line as displayed by an editor.\r\n Suppose we want to set a breakpoint at line 3 of the program, which is\r\n the start of \r\n\\family typewriter\r\nmain\r\n\\family default\r\n function:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language=C,numbers=left,showstringspaces=false,tabsize=4,title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n@|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries int main(int argc, char *argv[])|@\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen running a program, instead of running from start to finish, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stopped at line 3:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb)\r\n\\color red\r\n b\r\n\\color inherit\r\n \r\n\\color blue\r\n3\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1 at 0x400535: file hello.c, line 3.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) r\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/hello \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1, main (argc=1, argv=0x7fffffffdfb8) at hello.c:5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe breakpoint is at line 3, but \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stopped line 5.\r\n The reason is that line 3 does not contain code, but a function signature;\r\n \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n only stops where it can execute code.\r\n The code in the function starts at line 5, the call to \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n, so \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stops there.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nLine of code is not always the reliable way to specify a breakpoint, as\r\n the source code can be changed.\r\n What if \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n should always stop at \r\n\\family typewriter\r\nmain\r\n\\family default\r\n function? In this case, a better method is to use the function name directly:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color red\r\nb\r\n\\color inherit\r\n \r\n\\color blue\r\nmain\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, regardless of how the source code changes, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n always stops at the \r\n\\family typewriter\r\nmain\r\n\\family default\r\n function.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nSometimes, the debugging program does not contain debug info, or \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n is debugging assembly code.\r\n In that case, a memory address can be specified as a stop point.\r\n To get the function address, \r\n\\family typewriter\r\nprint\r\n\\family default\r\n command can be used:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) print main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n$3 = {int (int, char **)} 0x400526 <main>\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nKnowing the address of main, we can easily set a breakpoint with a memory\r\n address:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\nb\r\n\\color inherit\r\n \r\n\\color blue\r\n*0x400526\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\ngdb\r\n\\family default\r\n can also set breakpoint in any source file.\r\n Suppose that \r\n\\family typewriter\r\nhello\r\n\\family default\r\n program is composed not just one file but many files e.g.\r\n \r\n\\family typewriter\r\nhello1.c\r\n\\family default\r\n, \r\n\\family typewriter\r\nhello2.c\r\n\\family default\r\n, \r\n\\family typewriter\r\nhello3.c\r\n\\family default\r\n...\r\n In that case, simply add the filename before either a line number:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color red\r\nb\r\n\\color inherit\r\n \r\n\\color blue\r\nhello.c:3\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nA function name in a specific file can also be set:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color red\r\nb\r\n\\color inherit\r\n \r\n\\color blue\r\nhello.c:main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nnext/n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command executes the current line and stops at the next line.\r\n When the current line is a function call, steps over it.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nAfter setting a breakpoint at \r\n\\family typewriter\r\nmain\r\n\\family default\r\n, run a program and stop at the first \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nr\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/hello \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1, main (argc=1, argv=0x7fffffffdfb8) at hello.c:5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, to proceed to the next statement, we use the \r\n\\family typewriter\r\nnext\r\n\\family default\r\n command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nn\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nHello World!\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n6\t    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the output, the first line shows the output produced after executing\r\n line 5; then, the next line shows where \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stops currently, which is line 6.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nstep/s\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command executes the current line and stops at the next line.\r\n When the current line is a function call, steps into it to the first next\r\n line in the called function.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nSuppose we have a new function \r\n\\family typewriter\r\nadd\r\n\\family default\r\n\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nWhy should we add a new function and function call instead of using the\r\n existing \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n call? Stepping into shared library functions is tricky because to make\r\n debugging works, the debug info must be installed and loaded.\r\n It is not worth the trouble for demonstrating this simple command.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint add(int a, int b) {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\treturn a + b;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tadd(1, 2);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf \r\n\\family typewriter\r\nstep\r\n\\family default\r\n command is used instead of \r\n\\family typewriter\r\nnext\r\n\\family default\r\n on the function call \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n, gdb steps inside the function:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nr\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/hello \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1, main (argc=1, argv=0xffffd154) at hello.c:11\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n11\t    add(1, 2);\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ns\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nadd (a=1, b=2) at hello.c:6\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n6\t    return a + b;\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter executing the command \r\n\\family typewriter\r\n\\color red\r\ns\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stepped into the \r\n\\family typewriter\r\nadd\r\n\\family default\r\n function where the first statement is a \r\n\\family typewriter\r\nreturn\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nni\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAt the core, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n operates on assembly instruction.\r\n Source line by line debugging is simply an enhancement to make it friendlier\r\n for programmers.\r\n Each statement in C translates to one or more assembly instruction, as\r\n shown with \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n and \r\n\\family typewriter\r\ndisassemble\r\n\\family default\r\n command.\r\n With the debug info available, \r\n\\family typewriter\r\ngdb \r\n\\family default\r\nknows how many instructions belong to one line of high-level code; line\r\n by line debugging is just a execution of assembly instructions of a line\r\n when moving from the current line to the next.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command executes the \r\n\\emph on\r\none\r\n\\emph default\r\n assembly instruction belongs to the current line.\r\n Until all assembly instructions of the current line are executed, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n will not move to the next line.\r\n If the current instruction is a \r\n\\family typewriter\r\ncall\r\n\\family default\r\n, step over it to the next instruction.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWhen breakpoint is on the \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n call and \r\n\\family typewriter\r\nni\r\n\\family default\r\n is used, it steps through each assembly instruction:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ndisassemble\r\n\\color inherit\r\n \r\n\\color blue\r\n/s main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDump of assembler code for function main:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nhello.c:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n4\t{\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840b <+0>:\t lea    ecx,[esp+0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840f <+4>:\t and    esp,0xfffffff0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048412 <+7>:\t push   DWORD PTR [ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048415 <+10>:\tpush   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048416 <+11>:\tmov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048418 <+13>:\tpush   ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048419 <+14>:\tsub    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   \r\n\\color blue\r\n0x0804841c\r\n\\color inherit\r\n <+17>:\tsub    esp,0xc\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   \r\n\\color blue\r\n0x0804841f\r\n\\color inherit\r\n <+20>:\tpush   0x80484c0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   \r\n\\color blue\r\n0x08048424\r\n\\color inherit\r\n <+25>:\tcall   0x80482e0 <puts@plt>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   \r\n\\color blue\r\n0x08048429\r\n\\color inherit\r\n <+30>:\tadd    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n6\t    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n=> 0x0804842c <+33>:\tmov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n7\t}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048431 <+38>:\tmov    ecx,DWORD PTR [ebp-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048434 <+41>:\tleave  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048435 <+42>:\tlea    esp,[ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048438 <+45>:\tret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEnd of assembler dump.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) r\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/hello \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1, main (argc=1, argv=0xffffd154) at hello.c:5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nni\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n0x0804841f\r\n\\color inherit\r\n\t5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nni\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n0x08048424\r\n\\color inherit\r\n\t5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nni\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nHello World!\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n0x08048429\r\n\\color inherit\r\n\t5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb)\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n6\t    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nUpon entering \r\n\\family typewriter\r\nni\r\n\\family default\r\n, \r\n\\family typewriter\r\ngdb \r\n\\family default\r\nexecutes current instruction and display the \r\n\\emph on\r\nnext\r\n\\emph default\r\n instruction.\r\n That's why from the output, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n only displays 3 addresses: \r\n\\family typewriter\r\n\\color blue\r\n0x0804841f\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color blue\r\n0x08048424\r\n\\family default\r\n\\color inherit\r\n and \r\n\\family typewriter\r\n\\color blue\r\n0x08048429\r\n\\family default\r\n\\color inherit\r\n.\r\n The instruction at \r\n\\family typewriter\r\n\\color blue\r\n0x0804841c\r\n\\family default\r\n\\color inherit\r\n, which is the first instruction of \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n, is not displayed because it is the first instruction that \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stopped at.\r\n Assume that \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stopped at the first instruction of \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n at \r\n\\family typewriter\r\n\\color blue\r\n0x0804841c\r\n\\family default\r\n\\color inherit\r\n, the current instruction can be displayed using \r\n\\family typewriter\r\nx\r\n\\family default\r\n command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) x/i $eip\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n=> \r\n\\color blue\r\n0x804841c\r\n\\color inherit\r\n <main+17>: sub    esp,0xc\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nsi\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to \r\n\\family typewriter\r\nni\r\n\\family default\r\n, this command executes the current assembly instruction belongs to the\r\n current line.\r\n But if the current instruction is a \r\n\\family typewriter\r\ncall\r\n\\family default\r\n, step into it to the first next instruction in the called function.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nRecall that the assembly code generated from \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n contains a \r\n\\family typewriter\r\ncall\r\n\\family default\r\n instruction:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) disassemble /s main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDump of assembler code for function main:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nhello.c:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n4\t{\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840b <+0>:\tlea    ecx,[esp+0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804840f <+4>:\tand    esp,0xfffffff0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048412 <+7>:\tpush   DWORD PTR [ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048415 <+10>:\tpush   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048416 <+11>:\tmov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048418 <+13>:\tpush   ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048419 <+14>:\tsub    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\t    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804841c <+17>:\tsub    esp,0xc\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x0804841f <+20>:\tpush   0x80484c0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   \r\n\\color blue\r\n0x08048424\r\n\\color inherit\r\n <+25>:\t\r\n\\color blue\r\ncall\r\n\\color inherit\r\n   0x80482e0 <puts@plt>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048429 <+30>:\tadd    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n6\t    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n=> 0x0804842c <+33>:\tmov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n7\t}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048431 <+38>:\tmov    ecx,DWORD PTR [ebp-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048434 <+41>:\tleave  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048435 <+42>:\tlea    esp,[ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0x08048438 <+45>:\tret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEnd of assembler dump.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe try instruction by instruction stepping again, but this time by running\r\n \r\n\\family typewriter\r\nsi\r\n\\family default\r\n at \r\n\\family typewriter\r\n\\color blue\r\n0x08048424\r\n\\family default\r\n\\color inherit\r\n, where \r\n\\family typewriter\r\ncall\r\n\\family default\r\n resides:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nsi\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x0804841f\t5\t        printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nsi\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x08048424\t5\t        printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) x/i $eip\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n=> \r\n\\color blue\r\n0x8048424\r\n\\color inherit\r\n <main+25>:\t\r\n\\color blue\r\ncall\r\n\\color inherit\r\n   \r\n\\color green\r\n0x80482e0\r\n\\color inherit\r\n <puts@plt>\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nsi\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color green\r\n0x080482e0\r\n\\color inherit\r\n in \r\n\\color green\r\nputs\r\n\\color inherit\r\n@plt ()\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe next instruction right after \r\n\\family typewriter\r\n\\color blue\r\n0x8048424\r\n\\family default\r\n\\color inherit\r\n is the first instruction at \r\n\\family typewriter\r\n\\color green\r\n0x080482e0\r\n\\family default\r\n\\color inherit\r\n in \r\n\\family typewriter\r\nputs\r\n\\family default\r\n function.\r\n In other words, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stepped into \r\n\\color green\r\nputs\r\n\\color inherit\r\n instead of stepping over it.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nuntil\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command executes until the next line is greater than the current line.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nSuppose we have a function that execute a long loop:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint add1000() {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    int total = 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    for (int i = 0; i < 1000; ++i){\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        total += i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"Done adding!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return total;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    add1000(1, 2);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"Hello World!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nUsing \r\n\\family typewriter\r\nnext\r\n\\family default\r\n command, we need to press 1000 times for finishing the loop.\r\n Instead, a faster way is to use \r\n\\family typewriter\r\nuntil\r\n\\family default\r\n: \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) b add1000\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1 at 0x8048411: file hello.c, line 4.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) r\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/hello \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1, add1000 () at hello.c:4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n4\t    int total = 0;\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nuntil\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\t    for (int i = 0; i < 1000; ++i){\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nuntil\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n6\t        total += i;\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nuntil\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\t    for (int i = 0; i < 1000; ++i){\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nuntil\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n8\t    printf(\"Done adding!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nExecuting the first \r\n\\family typewriter\r\nuntil\r\n\\family default\r\n, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stopped at line 5 since line 5 is greater than line 4.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nExecuting the second \r\n\\family typewriter\r\nuntil\r\n\\family default\r\n, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stopped at line 6 since line 6 is greater than line 5.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nExecuting the third \r\n\\family typewriter\r\nuntil\r\n\\family default\r\n, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stopped at line 5 since the loop still continues.\r\n Because line 5 is less than line 6, with the fourth \r\n\\family typewriter\r\nuntil\r\n\\family default\r\n, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n kept executing until it does not go back to line 5 anymore and stopped\r\n at line 8.\r\n This is a great way to skip over loop in the middle, instead of setting\r\n unneeded breakpoint.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\n\r\n\\family typewriter\r\nuntil\r\n\\family default\r\n can be supplied with an argument to explicitly execute to a specific line:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) r\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/hello \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1, add1000 () at hello.c:4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n4\t    int total = 0;\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nuntil\r\n\\color inherit\r\n \r\n\\color blue\r\n8\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nadd1000 () at hello.c:\r\n\\color blue\r\n8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n8\r\n\\color inherit\r\n\t    printf(\"Done adding!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nfinish\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command executes until the end of a function and displays the return\r\n value.\r\n \r\n\\family typewriter\r\nfinish\r\n\\family default\r\n is actually just a more convenient version of \r\n\\family typewriter\r\nuntil\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nUsing the \r\n\\family typewriter\r\nadd1000\r\n\\family default\r\n function from the previous example and use \r\n\\family typewriter\r\nfinish\r\n\\family default\r\n instead of \r\n\\family typewriter\r\nuntil\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) r\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/hello \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1, add1000 () at hello.c:4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n4\t    int total = 0;\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nfinish\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\nRun till exit from #0  add1000 () at hello.c:4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDone adding!\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x08048466 in main (argc=1, argv=0xffffd154) at hello.c:15\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n15\t    add1000(1, 2);\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\nValue returned is $1 = 499500\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nbt\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command prints the \r\n\\emph on\r\nbacktrace\r\n\\emph default\r\n of all stack frames.\r\n A \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nbacktrace\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nbacktrace\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nbacktrace\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a list of currently active functions:\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nSuppose we have a chain of function calls:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid d(int d) { };\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid c(int c) { d(0); }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid b(int b) { c(1); }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid a(int a) { b(2); }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    a(3);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nbt\r\n\\family default\r\n can visualize such a chain in action:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) b a\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1 at 0x8048404: file hello.c, line 9.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) r\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/hello \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1, a (a=3) at hello.c:9\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n9\tvoid a(int a) { b(2); }\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) s\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nb (b=2) at hello.c:7\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n7\tvoid b(int b) { c(1); }\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) s\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nc (c=1) at hello.c:5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\tvoid c(int c) { d(0); }\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) s\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nd (d=0) at hello.c:3\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n3\tvoid d(int d) { };\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nbt\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n#0  d (d=0) at hello.c:3\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n#1  0x080483eb in c (c=1) at hello.c:5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n#2  0x080483fb in b (b=2) at hello.c:7\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n#3  0x0804840b in a (a=3) at hello.c:9\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n#4  0x0804841b in main (argc=1, argv=0xffffd154) at hello.c:13\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nMost-recent calls are placed on top and least-recent calls are near the\r\n bottom.\r\n In this case, \r\n\\family typewriter\r\nd\r\n\\family default\r\n is the most current active function, so it has the index 0.\r\n Next is \r\n\\family typewriter\r\nc\r\n\\family default\r\n, the 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n active function, has the index 1 and so on with function \r\n\\family typewriter\r\nb\r\n\\family default\r\n, function \r\n\\family typewriter\r\na\r\n\\family default\r\n, and finally function \r\n\\family typewriter\r\nmain\r\n\\family default\r\n at the bottom, the least-recent function.\r\n That is how we read a backtrace.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\nup\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command goes up one frame earlier the current frame.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nInstead of staying in \r\n\\family typewriter\r\nd\r\n\\family default\r\n function, we can go up to \r\n\\family typewriter\r\nc\r\n\\family default\r\n function and look at its state:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) bt\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#0  d (d=0) at hello.c:3\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#1  0x080483eb in c (c=1) at hello.c:5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#2  0x080483fb in b (b=2) at hello.c:7\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#3  0x0804840b in a (a=3) at hello.c:9\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#4  0x0804841b in main (argc=1, argv=0xffffd154) at hello.c:13\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nup\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#1  0x080483eb in \r\n\\color blue\r\nc\r\n\\color inherit\r\n (c=1) at hello.c:3\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n3\tvoid b(int b) { c(1); }\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe output displays the current frame is moved to \r\n\\family typewriter\r\nc\r\n\\family default\r\n and where the call to \r\n\\family typewriter\r\nc\r\n\\family default\r\n is made, which is in function \r\n\\family typewriter\r\nb\r\n\\family default\r\n at line 3.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\ndown\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSimilar to \r\n\\family typewriter\r\nup\r\n\\family default\r\n, this command goes down one frame later then the current frame.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nAfter inspecting \r\n\\family typewriter\r\nc\r\n\\family default\r\n function, we can go back to \r\n\\family typewriter\r\nd\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) bt\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#0  d (d=0) at hello.c:3\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#1  0x080483eb in c (c=1) at hello.c:5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#2  0x080483fb in b (b=2) at hello.c:7\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#3  0x0804840b in a (a=3) at hello.c:9\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#4  0x0804841b in main (argc=1, argv=0xffffd154) at hello.c:13\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) up\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#1  0x080483eb in c (c=1) at hello.c:3\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n3\tvoid b(int b) { c(1); }\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\ndown\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n#0  \r\n\\color blue\r\nd\r\n\\color inherit\r\n (d=0) at hello.c:1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n1\tvoid d(int d) { };\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nCommand: \r\n\\family typewriter\r\ninfo registers\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis command lists the current values in commonly used registers.\r\n This command is useful when debugging assembly and operating system code,\r\n as we can inspect the current state of the machine.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nExecuting the command, we can see the commonly used registers:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) \r\n\\color red\r\ninfo registers \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\neax            0xf7faddbc\t-134554180\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\necx            0xffffd0c0\t-12096\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nedx            0xffffd0e4\t-12060\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nebx            0x0\t0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nesp            0xffffd0a0\t0xffffd0a0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nebp            0xffffd0a8\t0xffffd0a8\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nesi            0xf7fac000\t-134561792\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nedi            0xf7fac000\t-134561792\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\neip            0x804841c\t0x804841c <main+17>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\neflags         0x286\t[ PF SF IF ]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\ncs             0x23\t35\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nss             0x2b\t43\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nds             0x2b\t43\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nes             0x2b\t43\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nfs             0x0\t0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\ngs             0x63\t99\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe above registers suffice for writing our operating system in later part.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Section\r\nHow debuggers work: A brief introduction\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nHow breakpoints work\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen a programmer places a breakpoint somewhere in his code, what actually\r\n happens is that the \r\n\\emph on\r\nfirst\r\n\\emph default\r\n opcode of the \r\n\\emph on\r\nfirst\r\n\\emph default\r\n instruction of a statement is replaced with another instruction, \r\n\\family typewriter\r\nint 3\r\n\\family default\r\n with opcode \r\n\\family typewriter\r\nCCh\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nOpcode replacement, with \r\n\\family typewriter\r\nint 3\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"2\" columns=\"7\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color red\r\n83\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nec\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0c\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\rightarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color red\r\ncc\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nec\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0c\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nsub esp,0x4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nint 3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nint 3\r\n\\family default\r\n only costs a single byte, making it efficient for debugging.\r\n When \r\n\\family typewriter\r\nint 3\r\n\\family default\r\n instruction is executed, the operating system calls its breakpoint interrupt\r\n handler.\r\n The handler then checks what process reaches a breakpoint, pauses it and\r\n notifies the debugger it has paused a debugged process.\r\n The debugged process is only paused and that means a debugger is free to\r\n inspect its internal state, like a surgeon operates on an anesthetic patient.\r\n Then, the debugger replaces the \r\n\\family typewriter\r\nint 3\r\n\\family default\r\n opcode with the original opcode and executes the original instruction normally.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nRestore\r\n\\family typewriter\r\n \r\n\\family default\r\nthe original opcode, after \r\n\\family typewriter\r\nint 3\r\n\\family default\r\n was executed\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"2\" columns=\"7\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"0.8cm\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color blue\r\ncc\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nec\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0c\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Formula $\\rightarrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color blue\r\n83\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nec\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n0c\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nint 3\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nsub esp,0x4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nIt is simple to see \r\n\\family typewriter\r\nint 3\r\n\\family default\r\n in action.\r\n First, we add an \r\n\\family typewriter\r\nint 3\r\n\\family default\r\n instruction where we need \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n to stop:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    asm(\"int 3\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"Hello World\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nint 3\r\n\\family default\r\n precedes \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n, so \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n is expected to stop at \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n.\r\n Next, we compile with debug enable and with Intel syntax:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -masm=intel -m32 -g hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, start \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gdb hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nRunning without setting any breakpoint, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stops at \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n call, as expected:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color red\r\nr\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/hello \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram received signal SIGTRAP, \r\n\\color blue\r\nTrace/breakpoint\r\n\\color inherit\r\n trap.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nmain (argc=1, argv=0xffffd154) at hello.c:6\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n6\t    \r\n\\color blue\r\nprintf(\"Hello World\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe \r\n\\color blue\r\nblue\r\n\\color inherit\r\n text indicates that \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n encountered a breakpoint, and indeed it stopped at the right place: the\r\n \r\n\\family typewriter\r\nprintf\r\n\\family default\r\n call, where \r\n\\family typewriter\r\nint 3\r\n\\family default\r\n preceded it.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Subsection\r\nSingle stepping\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen breakpoint is implemented, it is easy to implement single stepping:\r\n a debugger simply places another \r\n\\family typewriter\r\nint 3\r\n\\family default\r\n opcode in the next instruction.\r\n So, when a programmer sets a breakpoint at an instruction, the next instruction\r\n is automatically set by the debugger, thus enable instruction by instruction\r\n debugging.\r\n Similarly, source line by line debugging is just the placements of the\r\n very first opcodes in the two statements with two \r\n\\family typewriter\r\nint 3\r\n\\family default\r\n opcodes.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nHow a debugger understands high level source code\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nDWARF is a debugging file format used by many compilers and debuggers to\r\n support source level debugging.\r\n DWARF contains information that maps between entities in the executable\r\n binary with the source files.\r\n A program entity can either be data or code.\r\n A \r\n\\series bold\r\nDIE\r\n\\series default\r\n, or \r\n\\series bold\r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nDebugging Information Entry\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nD\r\n\\series default\r\nebugging \r\n\\series bold\r\nI\r\n\\series default\r\nnformation \r\n\\series bold\r\nE\r\n\\series default\r\nntry\r\n\\emph default\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\emph on\r\nDebugging Information Entry\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, is a description of a program entity.\r\n A DIE consists of a tag, which specifies the entity that the DIE describes, and\r\n a list of  attributes that describes the entity.\r\n Of all the attributes, these two attributes enables source-level debugging:\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nWhere the entity appears in the source files\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n: which file and which line the entity appears.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nWhere the entity appears in the executable binary\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n: in which memory address the entity is loaded at runtime.\r\n With the precise address, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n can retrieve correct value for a data entity, or place a correct breakpoint\r\n and stop accordingly for a code entity.\r\n Without the information of these addresses, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n would not know where the entities are to inspect them.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{Source-binary mapping with DIE}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"7\" columns=\"4\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"2.25cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"6cm\">\r\n<column alignment=\"center\" valignment=\"top\" width=\"1.25cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"4.5cm\">\r\n<row>\r\n<cell multicolumn=\"1\" alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\" width=\"5cm\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nhello.c\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDIE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nLine 1\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nLine 2\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color red\r\n\\begin_inset Formula $\\Rightarrow$\r\n\\end_inset\r\n\r\n Line 3\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nLine 5\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nLine 6\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\color red\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n..........\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n..........\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Formula $\\rightarrow$\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n....\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n....\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nmain\r\n\\family default\r\n in \r\n\\family typewriter\r\nhello.c \r\n\\family default\r\nis at \r\n\\family typewriter\r\n0x804840b\r\n\\family default\r\n in \r\n\\family typewriter\r\nhello\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n....\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n....\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Formula $\\downarrow$\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Formula $\\uparrow$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n\\series bold\r\nhello \r\n\\family default\r\n\\series default\r\n(at\r\n\\family typewriter\r\n\\series bold\r\n \r\n\\series default\r\n0x804840b\r\n\\family default\r\n)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n...8d 4c 24 04 83 e4 f0 ff 71 fc ....\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nhfill\r\n\\backslash\r\nbreak\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn addition to DIEs, another binary-to-source mapping is the \r\n\\emph on\r\nline number table\r\n\\emph default\r\n.\r\n The line number table maps between a line in the source code and at which\r\n memory address is the start of the line in the executable binary.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn sum, to successfully enable source-level debugging, a debugger needs\r\n to know the precise location of the source files and the load addresses\r\n at runtime.\r\n Address matching, between the image layout of the ELF binary and the address\r\n where it is loaded, is extremely important since debug information relies\r\n on correct loading address at runtime.\r\n That is, it assumes the addresses as recorded in the binary image at compile-ti\r\nme the same as at runtime e.g.\r\n if the load address for \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section is recorded in the executable binary at \r\n\\family typewriter\r\n0x800000\r\n\\family default\r\n, then when the binary actually runs, \r\n\\family typewriter\r\n.text\r\n\\family default\r\n should really be loaded at \r\n\\family typewriter\r\n0x800000\r\n\\family default\r\n for gdb to be able to correctly match running instructions with high-level\r\n code statement.\r\n Address mismatching makes debug information useless, as actual code at\r\n one address is displayed as code at another address.\r\n Without this knowledge, we will not be able to build an operating system\r\n that can be debugged with \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWhen an executable binary contains debug info,\r\n\\family typewriter\r\n readelf\r\n\\family default\r\n can display such information in a readable format.\r\n Using the good old hello world program:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\r\n\\emph on\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"Hello World\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nand compile with debug info:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 -g hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWith the binary ready, we can look at the line number table with the command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readlelf -wL hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\r\n\\family typewriter\r\n-w\r\n\\family default\r\n option prints all the debug information.\r\n In combination with its sub-option, only specific information is displayed.\r\n For example, with \r\n\\family typewriter\r\n-L,\r\n\\family default\r\n only the line number table is displayed:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDecoded dump of debug contents of section .debug_line:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nCU: hello.c:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nFile name                            Line number    Starting address\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nhello.c                                        6           0x804840b\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nhello.c                                        7           0x804841c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nhello.c                                        9           0x804842c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nhello.c                                       10           0x8048431\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFrom the above output:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nCU shorts for \r\n\\series bold\r\n\\emph on\r\nC\r\n\\series default\r\nompilation \r\n\\series bold\r\nU\r\n\\series default\r\nnit\r\n\\emph default\r\n, a separately compiled source file.\r\n In the example, we only have one file, \r\n\\family typewriter\r\nhello.c\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nFile\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nname displays the filename of the current compilation unit.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nLine\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nnumber is the line number in the source file of which the line is not an\r\n empty line.\r\n In the example, line 8 is an empty line, so it does not appear.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nStarting\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\naddress is the memory address where the line actually starts in the executable\r\n binary.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWith such crystal clear information, this is how \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n is able to set a breakpoint on a line easily.\r\n For placing breakpoints on variables and functions, it is time to look\r\n at the DIEs.\r\n To get the DIEs information from an executable binary, run the command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readlelf -wi hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n-wi\r\n\\family default\r\n option lists all the DIE entries.\r\n This is one typical DIE entry:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n<0>\r\n\\color blue\r\n<b>\r\n\\color inherit\r\n: \r\n\\color magenta\r\nAbbrev Number: 1\r\n\\color inherit\r\n (DW_TAG_compile_unit)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    \r\n\\color blue\r\n<c> \r\n\\color inherit\r\n  \r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nproducer    : (indirect string, offset: 0xe): GNU C11 5.4.0 20160609 -masm=intel\r\n -m32 -mtune=generic -march=i686 -g -fstack-protector-strong\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    \r\n\\color blue\r\n<10>\r\n\\color inherit\r\n   \r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nlanguage    : 12\t(ANSI C99)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    \r\n\\color blue\r\n<11> \r\n\\color inherit\r\n  \r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nname        : (indirect string, offset: 0xbe): \r\n\\series bold\r\nhello.c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   \r\n\\color blue\r\n <15> \r\n\\color inherit\r\n  \r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\ncomp_dir    : (indirect string, offset: 0x97): \r\n\\series bold\r\n/tmp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    \r\n\\color blue\r\n<19>\r\n\\color inherit\r\n   \r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nlow_pc      : \r\n\\series bold\r\n0x804840b\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    \r\n\\color blue\r\n<1d>\r\n\\color inherit\r\n   \r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nhigh_pc     : \r\n\\series bold\r\n0x2e\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    \r\n\\color blue\r\n<21>\r\n\\color inherit\r\n   \r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nstmt_list   : 0x0\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\color red\r\nRed \r\n\\family default\r\n\\color inherit\r\nThis left-most number indicates the current nesting level of a DIE entry.\r\n \r\n\\family typewriter\r\n0\r\n\\family default\r\n is the outer-most level DIE with its entity is the compilation unit.\r\n This means subsequent DIE entries with higher nesting level are all the\r\n children of this tag, the compilation unit.\r\n It makes sense, as all the entities must originate from a source file.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\color blue\r\nBlue\r\n\\family default\r\n\\color inherit\r\n These numbers in hex format indicate the offsets into \r\n\\family typewriter\r\n.debug_info\r\n\\family default\r\n section.\r\n Each meaningful information is displayed along with its offset.\r\n When an attribute references to another attribute, the offset is used to\r\n precisely identify the referenced attribute.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\color green\r\nGreen\r\n\\color inherit\r\n These names with \r\n\\family typewriter\r\n\\color green\r\nDW_AT_\r\n\\family default\r\n\\color inherit\r\n prefix are the attributes attached to a DIE that describe an entity.\r\n Notable attributes:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nname\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\ncomp_dir\r\n\\family default\r\n The filename of the compilation unit and the directory where compilation\r\n occurred.\r\n Without the filename and the path, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n would not be able to display the high-level source, despite the availability\r\n of the debug info.\r\n Debug info only contains the mapping between source and binary, not the\r\n source code itself.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nlow_pc\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\r\n\\family typewriter\r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nhigh_pc \r\n\\family default\r\nThe start and end of the current entity, which is the compilation unit,\r\n in the executable binary.\r\n The value in \r\n\\family typewriter\r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nlow_pc \r\n\\family default\r\nis the starting address.\r\n \r\n\\family typewriter\r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nhigh_pc\r\n\\family default\r\n is the size of the compilation unit, when adding up to \r\n\\family typewriter\r\n\\color green\r\nDW_AT_\r\n\\color inherit\r\nlow_pc \r\n\\family default\r\nresults in the end address of the entity.\r\n In this example, code compiled from \r\n\\family typewriter\r\nhello.c\r\n\\family default\r\n starts at \r\n\\family typewriter\r\n0x804840b \r\n\\family default\r\nand end at \r\n\\family typewriter\r\n\r\n\\begin_inset Formula $\\mathtt{0x804840b+0x2e=0x8048439}$\r\n\\end_inset\r\n\r\n.\r\n\r\n\\family default\r\n To really make sure, we verify with \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n804840b\r\n\\color inherit\r\n:       8d 4c 24 04             lea    ecx,[esp+0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804840f:       83 e4 f0                and    esp,0xfffffff0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048412:       ff 71 fc                push   DWORD PTR [ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048415:       55                      push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048416:       89 e5                   mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048418:       51                      push   ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048419:       83 ec 04                sub    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    printf(\"Hello World\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804841c:       83 ec 0c                sub    esp,0xc\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804841f:       68 c0 84 04 08          push   0x80484c0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048424:       e8 b7 fe ff ff          call   80482e0 <puts@plt>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048429:       83 c4 10                add    esp,0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804842c:       b8 00 00 00 00          mov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048431:       8b 4d fc                mov    ecx,DWORD PTR [ebp-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048434:       c9                      leave  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048435:       8d 61 fc                lea    esp,[ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048438:       c3                      ret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n8048439\r\n\\color inherit\r\n:       66 90                   xchg   ax,ax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804843b:       66 90                   xchg   ax,ax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804843d:       66 90                   xchg   ax,ax\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804843f:       90                      nop\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIt is true: \r\n\\family typewriter\r\nmain\r\n\\family default\r\n starts at \r\n\\family typewriter\r\n\\color red\r\n804840b\r\n\\family default\r\n\\color inherit\r\n and end at \r\n\\family typewriter\r\n\\color red\r\n8048439\r\n\\family default\r\n\\color inherit\r\n, right after the \r\n\\family typewriter\r\nret\r\n\\family default\r\n instruction at \r\n\\family typewriter\r\n8048438\r\n\\family default\r\n.\r\n The instructions after \r\n\\family typewriter\r\n\\color red\r\n8048439\r\n\\family default\r\n\\color inherit\r\n are just padding bytes inserted by \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n for alignment, which do not belong to \r\n\\family typewriter\r\nmain\r\n\\family default\r\n.\r\n Note that the output from \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n shows much more code past \r\n\\family typewriter\r\nmain\r\n\\family default\r\n.\r\n It is not counted, as the code is outside of \r\n\\family typewriter\r\nhello.c\r\n\\family default\r\n, added by \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n for the operating system.\r\n \r\n\\family typewriter\r\nhello.c\r\n\\family default\r\n contains only one function: \r\n\\family typewriter\r\nmain\r\n\\family default\r\n and this is why \r\n\\family typewriter\r\nhello.c\r\n\\family default\r\n also starts and ends the same as \r\n\\family typewriter\r\nmain\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\n\r\n\\color magenta\r\nPink\r\n\\color inherit\r\n This number displays the abbreviation form of a tag.\r\n An abbreviation is the form of a DIE.\r\n When debug info is displayed with \r\n\\family typewriter\r\n-wi\r\n\\family default\r\n, the DIEs are displayed with their values.\r\n \r\n\\family typewriter\r\n-wa\r\n\\family default\r\n option shows abbreviations in the \r\n\\family typewriter\r\n.debug_abbrev\r\n\\family default\r\n section:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nContents of the .debug_abbrev section:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Number TAG (0x0)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   1      DW_TAG_compile_unit    [has children]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    DW_AT_producer     DW_FORM_strp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    DW_AT_language     DW_FORM_data1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    DW_AT_name         DW_FORM_strp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    DW_AT_comp_dir     DW_FORM_strp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    DW_AT_low_pc       DW_FORM_addr\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    DW_AT_high_pc      DW_FORM_data4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    DW_AT_stmt_list    DW_FORM_sec_offset\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    DW_AT value: 0     DW_FORM value: 0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\series bold\r\n....\r\n more abbreviations ....\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe output is similar to a DIE output, with only attribute names and without\r\n any value.\r\n We can also say an abbreviation is a \r\n\\emph on\r\ntype\r\n\\emph default\r\n of a DIE, as an abbreviation represents the structure of a particular DIE.\r\n Many DIEs share the same abbreviation, or structure, thus they are of the\r\n same type.\r\n An abbreviation number specifies which type a DIE is in the abbreviation\r\n table above.\r\n Abbreviations improve encoding efficiency (reduce binary size) because\r\n each DIE needs not to carry their structure information as pairs of attribute-v\r\nalue\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nFor example, data format such as YAML or JSON encodes its attribute names\r\n along with its values.\r\n This simplifies encoding, but with overhead.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, but simply refers to an abbreviation for correct decoding.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nHere are all the DIEs of hello represented as a tree:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{figure*}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\ncaption{DIE entries visualized as a tree}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Graphics\r\n\tfilename images/06/dwarf_tree.svg\r\n\tscale 60\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"dwarf_tree\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{figure*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nIn the figure \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"dwarf_tree\"\r\n\r\n\\end_inset\r\n\r\n, \r\n\\family typewriter\r\nDW_TAG_subprogram\r\n\\family default\r\n represents a function such as \r\n\\family typewriter\r\nmain\r\n\\family default\r\n.\r\n Its children are the DIEs of \r\n\\family typewriter\r\nargc\r\n\\family default\r\n and \r\n\\family typewriter\r\nargv\r\n\\family default\r\n.\r\n With such precise information, matching source to binary is an easy job\r\n for \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf more than one compilation units exist in an executable binary, the DIE\r\n entries are sorted according to the compilation order from \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n.\r\n For example, suppose we have another \r\n\\family typewriter\r\ntest.c\r\n\\family default\r\n source file\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nIt can contain anything.\r\n Just a sample file.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n and compile it together with hello:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -masm=intel -m32 -g test.c hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, the all DIE entries in \r\n\\family typewriter\r\ntest.c\r\n\\family default\r\n are displayed before the DIE entries in \r\n\\family typewriter\r\nhello.c:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n<0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n    <c>   DW_AT_producer    : (indirect string, offset: 0x0): GNU C11 5.4.0\r\n 20160609 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n-masm=intel -m32 -mtune=generic -march=i686 -g -fstack-protector-strong\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <10>   DW_AT_language    : 12       (ANSI C99)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <11>   DW_AT_name        : (indirect string, offset: 0x64): \r\n\\color red\r\ntest.c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <15>   DW_AT_comp_dir    : (indirect string, offset: 0x5f): /tmp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <19>   DW_AT_low_pc      : 0x804840b\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <1d>   DW_AT_high_pc     : 0x6\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <21>   DW_AT_stmt_list   : 0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n <1><25>: Abbrev Number: 2 (DW_TAG_subprogram)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <26>   DW_AT_external    : 1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <26>   DW_AT_name        : bar\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <2a>   DW_AT_decl_file   : 1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <2b>   DW_AT_decl_line   : 1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <2c>   DW_AT_low_pc      : 0x804840b\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <30>   DW_AT_high_pc     : 0x6\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <34>   DW_AT_frame_base  : 1 byte block: 9c         (DW_OP_call_frame_cfa)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <36>   DW_AT_GNU_all_call_sites: 1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\series bold\r\n....after all DIEs in \r\n\\color red\r\ntest.c\r\n\\color inherit\r\n listed....\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n<0><42>: Abbrev Number: 1 (DW_TAG_compile_unit)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <43>   DW_AT_producer    : (indirect string, offset: 0x0): GNU C11 5.4.0\r\n 20160609 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n-masm=intel -m32 -mtune=generic -march=i686 -g -fstack-protector-strong\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <47>   DW_AT_language    : 12       (ANSI C99)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <48>   DW_AT_name        : (indirect string, offset: 0xc5): \r\n\\color blue\r\nhello.c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <4c>   DW_AT_comp_dir    : (indirect string, offset: 0x5f): /tmp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <50>   DW_AT_low_pc      : 0x8048411\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <54>   DW_AT_high_pc     : 0x2e\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    <58>   DW_AT_stmt_list   : 0x35\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\series bold\r\n....then all DIEs in \r\n\\color blue\r\nhello.c\r\n\\color inherit\r\n are listed....\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Part\r\nGroundwork \r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nBootloader\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"chap:Bootloader\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\nbootloader\r\n\\emph default\r\n loads an OS, or an application \r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nMany embedded devices don't use an OS.\r\n In embedded systems, the bootloader is simply included in boot firmware\r\n and no bootloader is needed.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n that runs and communicate directly with hardware.\r\n To run an OS, the first thing to write is a bootloader.\r\n In this chapter, we are going to write a rudimentary bootloader, as our\r\n main focus is writing an operating system, not a bootloader.\r\n More interestingly, this chapter will present related tools and techniques\r\n that are applicable for writing a bootloader as well as an operating system.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nx86 Boot Process\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter the POST process finished, the CPU's program counter is set to the\r\n address \r\n\\family typewriter\r\nFFFF:0000h\r\n\\family default\r\n for executing BIOS code.\r\n \r\n\\emph on\r\nBIOS - \r\n\\series bold\r\nB\r\n\\series default\r\nasic \r\n\\series bold\r\nI\r\n\\series default\r\nnput/\r\n\\series bold\r\nO\r\n\\series default\r\nutput \r\n\\series bold\r\nS\r\n\\series default\r\nystem\r\n\\emph default\r\n is a firmware that performs hardware initialization and provides a set\r\n of generic subroutines to control input/output devices.\r\n The BIOS checks all available storage devices (floppy disks and hard disks)\r\n if any device is bootable, by examining the last two bytes of the first\r\n sector whether it has the boot record signature of \r\n\\family typewriter\r\n0x55\r\n\\family default\r\n, \r\n\\family typewriter\r\n0xAA\r\n\\family default\r\n.\r\n If so, the BIOS loads the first sector to the address \r\n\\family typewriter\r\n7C00h\r\n\\family default\r\n, set the program counter to that address and let the CPU executing code\r\n from there.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe first sector is called \r\n\\series bold\r\n\\emph on\r\nM\r\n\\series default\r\naster \r\n\\series bold\r\nB\r\n\\series default\r\noot \r\n\\series bold\r\nR\r\n\\series default\r\necord, \r\n\\emph default\r\nor\r\n\\emph on\r\n MBR\r\n\\emph default\r\n.\r\n The program in the first sector is called \r\n\\emph on\r\nMBR Bootloader\r\n\\emph default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nUsing BIOS services\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBIOS provides many basic services for controlling the hardware at the boot\r\n stage.\r\n A service is a group of routines that controls a particular hardware device,\r\n or returns information of current system.\r\n Each service is given an interrupt number.\r\n To call a BIOS routine, an \r\n\\family typewriter\r\nint\r\n\\family default\r\n instruction must be used with an interrupt number.\r\n Each BIOS service defines its own numbers for its routines; to call a routine,\r\n a specific number must be written to a register required by each service.\r\n The list of all BIOS interrupts is available with Ralf Brown's Interrupt\r\n List at: \r\n\\begin_inset Flex URL\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttp://www.cs.cmu.edu/~ralf/files.html\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nThe boot process.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/07/simple_boot_process.pdf\r\n\tscale 80\r\n\tclip\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nExample: Interrupt call \r\n\\family typewriter\r\n13h\r\n\\family default\r\n (diskette service) requires number of sectors to read, track number, sector\r\n number, head number and drive number to read from a storage device.\r\n The content of the sector is stored in memory at the address defined by\r\n the pair of registers \r\n\\family typewriter\r\nES:BX\r\n\\family default\r\n.\r\n The parameters are stored in registers like this:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language={[x86masm]Assembler},numbers=left,showstringspaces=false,tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n; Store sector content in the buffer 10FF:0000\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmov     dx, 10FFh\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmov     es, dx\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nxor     bx, bx\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmov     al, 2    ; read 2 sector\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmov     ch, 0    ; read track 0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmov     cl, 2    ; 2nd sector is read\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmov     dh, 0    ; head number\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmov     dl, 0    ; drive number.\r\n Drive 0 is floppy drive.\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmov     ah, 0x02 ; read floppy sector function\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint     0x13     ; call BIOS - Read the sector\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nThe BIOS is only available in real mode.\r\n However, when switching to protected mode, then BIOS will not be usable\r\n anymore and the operating system code is responsible for controlling hardware\r\n devices.\r\n This is when the operating system stands on its own: it must provide its\r\n own kernel drivers for talking to hardware.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nBoot process\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nBIOS transfers control to MBR bootloader by jumping to \r\n\\family typewriter\r\n0000:7c00h\r\n\\family default\r\n, where bootloader is assumed to exist already.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nSetup machine environment for booting by properly initialize segment registers\r\n to enable flat memory model.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nLoad the kernel:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Enumerate\r\nRead kernel from disk.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nSave it somewhere in the main memory.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nJump to the starting code address of the kernel and execute.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Enumerate\r\nIf error occurs, print a message to notify users something went wrong and\r\n halt.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nExample Bootloader\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHere is a simple bootloader that does nothing, except not crashing the machine\r\n but halt it gracefully.\r\n If the virtual machine does not halt but text repeatedly flashing, it means\r\n the bootloader does not load properly and the machine crashed.\r\n The machine crashed because it keeps executing until the near end of physical\r\n memory (1\r\n\\begin_inset space \\thinspace{}\r\n\\end_inset\r\n\r\nMB in real mode), which is \r\n\\family typewriter\r\nFFFF:0000h\r\n\\family default\r\n, which starts the whole BIOS boot process all over again.\r\n This is effectively a reset, but not fully, since machine environment from\r\n previous run is still reserved.\r\n For that reason, it is called a \r\n\\emph on\r\nwarm reboot\r\n\\emph default\r\n.\r\n The opposite of warm reboot is \r\n\\emph on\r\ncold reboot\r\n\\emph default\r\n, in which the machine environment is reset to initial settings when the\r\n computer starts from a powerless state.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[x86masm]Assembler},numbers=left,basicstyle={\\ttfamily},showstringspaces=false,tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox,title={\\texttt{bootloader.asm}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;******************************************\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n; bootloader.asm\t\t\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n; A Simple Bootloader\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;******************************************\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\norg 0x7c00\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nbits 16\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstart: jmp boot\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;; constant and variable definitions\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmsg\tdb\t\"Welcome to My Operating System!\", 0ah, 0dh, 0h\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nboot:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  cli\t; no interrupts \t\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  cld\t; all that we need to init\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  hlt\t; halt the system\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n; We have to be 512 bytes.\r\n Clear the rest of the bytes with 0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\ntimes 510 - ($-$$) db 0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\ndw 0xAA55\t\t\t\t  ; Boot Signiture\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nCompile and load\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe compile the code with \r\n\\family typewriter\r\nnasm \r\n\\family default\r\nand write it to a disk image:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ nasm -f bin bootloader.asm -o bootloader\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, we create a 1.4 MB floppy disk and:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ dd if=/dev/zero of=disk.img bs=512 count=2880\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n2880+0 records in\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n2880+0 records out\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n1474560 bytes (1.5 MB, 1.4 MiB) copied, 0.00625622 s, 236 MB/s\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, we write the bootloader to the 1\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nst\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nsector:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ dd conv=notrunc if=bootloader of=disk.img bs=512 count=1 seek=0\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n1+0 records in\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n1+0 records out\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n512 bytes copied, 0.000102708 s, 5.0 MB/s\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe option \r\n\\family typewriter\r\nconv=notrunc\r\n\\family default\r\n preserves the original size of the floppy disk.\r\n Without this option, the 1.4 MB disk image will be completely replaced by\r\n the new \r\n\\family typewriter\r\ndisk.img\r\n\\family default\r\n with only 512 bytes, and we do not want that happens.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the past, developing an operating system is complicated because a programmer\r\n needs to understand specific hardware he is using.\r\n Even though x86 was ubiquitous, the minute differences between models made\r\n some code written for a machine not run on another.\r\n Further, if you use the same physical computer you write your operating\r\n system take very long between runs, and also difficult to debug.\r\n Fortunately, today we can uniformly produce a virtual machine with a particular\r\n specification and avoid the incompatibility issue altogether, thus making\r\n an OS easier to write and test since everyone can reproduce the same machine\r\n environment.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe will be using \r\n\\emph on\r\nQEMU\r\n\\emph default\r\n, a generic and open source machine emulator and virtualizer.\r\n QEMU can emulate various types of machine, not limited to x86_64 only.\r\n Debug is easy since you can connect GDB to a virtual machine to debug code\r\n that runs on it, through QEMU's built-in GDB server.\r\n QEMU can use \r\n\\family typewriter\r\ndisk.img\r\n\\family default\r\n as a boot device e.g.\r\n a floppy disk:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$\r\n\\family default\r\n \r\n\\family typewriter\r\nqemu-system-i386 -machine q35 -fda disk.img -gdb tcp::26000 -S\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nWith option \r\n\\family typewriter\r\n-machine q35\r\n\\family default\r\n, QEMU emulates a \r\n\\family typewriter\r\nq35\r\n\\family default\r\n machine model from Intel.\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe following command lists all supported emulated machines from QEMU:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nqemu-system-i386 -machine help\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nWith option \r\n\\family typewriter\r\n-fda disk.img\r\n\\family default\r\n, QEMU uses \r\n\\family typewriter\r\ndisk.img\r\n\\family default\r\n as a floppy disk image.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nWith option \r\n\\family typewriter\r\n-gdb tcp::26000\r\n\\family default\r\n, QEMU allows \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n to connect to the virtual machine for remote debugging through a tcp socket\r\n with port 26000.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nWith option \r\n\\family typewriter\r\n-S\r\n\\family default\r\n, QEMU waits for \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n to connect before it starts running.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter the command is executed, a new console window that displays the screen\r\n output of the virtual machine.\r\n Open another terminal, run \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n and set the current architecture to \r\n\\family typewriter\r\ni8086\r\n\\family default\r\n, since we are running in 16-bit mode:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) set architecture i8086\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nwarning: A handler for the OS ABI \"GNU/Linux\" is not built into this configurati\r\non\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nof GDB.\r\n Attempting to continue with the default i8086 settings.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nThe target architecture is assumed to be i8086\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, connect \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n to the waiting virtual machine with this command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) target remote localhost:26000\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nRemote debugging using localhost:26000\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n0x0000fff0 in ?? ()\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, place a breakpoint at \r\n\\family typewriter\r\n0x7c00\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) b *0x7c00\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nBreakpoint 1 at 0x7c00\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNote the asterick before the memory address.\r\n Without the asterisk, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n treats the address as a symbol in a program rather than an address.\r\n Then, for convenience, we use a split layout for viewing the assembly code\r\n and registers together:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) layout asm\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) layout reg\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, run the program:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) c\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf the virtual machine successfully runs the bootloader, this is what the\r\n QEMU screen should look like:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nBoot succeeded.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/07/boot_succeeded.png\r\n\tscale 25\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nDebugging\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf, for some reason, the sample bootloader cannot get to such screen and\r\n \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n does not stop at \r\n\\family typewriter\r\n0x7c00\r\n\\family default\r\n, then the following scenarios are likely:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nThe bootloader is invalid\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n: the message \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nBoot failed: not a bootable disk\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n appears for floppy disk booting.\r\n Make sure the boot signature is at the last 2 bytes of the 512-byte first\r\n sector.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nThe machine cannot find a boot disk\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n: the message \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nBoot failed: not a bootable disk\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n appears for floppy disk booting.\r\n Make sure the bootloader is correctly written to the first sector.\r\n It can be verify by check the disk with \r\n\\family typewriter\r\nhd\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ hd disk.img | less\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf the first 512 bytes are all zeroes, then it is likely that the bootloader\r\n is incorrectly written to another sector.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nThe machine crashes\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n: When such scenario happens, it reset back to the beginning at \r\n\\family typewriter\r\nFFFF:0000h\r\n\\family default\r\n.\r\n If the QEMU machine starts without waiting for \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n, then the console output window keeps flashing as the machine is repeatedly\r\n reset.\r\n It is likely some instruction in the bootloader code causing the fault.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"-4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Exercise\r\nPrint a welcome message\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\nWe loaded the bootloader successfully.\r\n But, it needs to do something useful other than halting our machine.\r\n The easiest thing to do is printing something on screen, like how an introducti\r\non to all programming language starts with \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nHello World\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n.\r\n Our bootloader prints \r\n\\begin_inset Quotes eld\r\n\\end_inset\r\n\r\nWelcome to my operating system\r\n\\begin_inset Quotes erd\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nOr whatever message you want.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n In this part, we will build a simple I/O library that allows us to set\r\n a cursor anywhere on the screen and print text there.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFirst, create a file \r\n\\series bold\r\nio.asm\r\n\\series default\r\n for I/O related routines.\r\n Then, write the following routines:\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\size large\r\nMovCursor\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\r\n\\series bold\r\nPurpose:\r\n\\series default\r\n Move a cursor to a specific location on screen and remember this location.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\series bold\r\nParameters:\r\n\\series default\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\emph on\r\nbh\r\n\\emph default\r\n = Y coordinate\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\emph on\r\nbl\r\n\\emph default\r\n = X coordinate.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\series bold\r\nReturn:\r\n\\series default\r\n None\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Enumerate\r\n\r\n\\size large\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\size large\r\nPutChar\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\size default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\r\n\\series bold\r\nPurpose:\r\n\\series default\r\n Print a character on screen, at the cursor position previously set by \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nMovCursor\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n .\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\series bold\r\nParameters:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nal\r\n\\family default\r\n = Character to print\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nbl\r\n\\family default\r\n = text color\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\ncx\r\n\\family default\r\n = number of times the character is repeated\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nReturn: None\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Enumerate\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\size large\r\nPrint\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\r\n\\series bold\r\nPurpose:\r\n\\series default\r\n Print a string.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\series bold\r\nParameters:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nds:si\r\n\\family default\r\n = Zero terminated string\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nReturn: None\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nTest the routines by putting each in the bootloader source, compile and\r\n run.\r\n To debug, run GDB and set a breakpoint at a specific routine.\r\n The end result is that \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPrint\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n should display a welcome message on screen.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nLoading a program from bootloader\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow that we get the feel of how to use the BIOS services, it is time for\r\n something more complicated.\r\n We will place our kernel on 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector onward, and our bootloader reads 30 sectors starting from 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector.\r\n Why 30 sectors? Our kernel will grow gradually, so we will preserve 30\r\n sectors and save us time for modifying the bootloader each time the kernel\r\n size expands another sector.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe primary responsibility of a bootloader is to read an operating system\r\n from some storage device e.g.\r\n hard disk, then loads it into main memory and transfer the control to the\r\n loaded operating system, similar to how the BIOS reads and loads a bootloader.\r\n At the moment, our bootloader does nothing more than just an assembly program\r\n loaded by the BIOS.\r\n To make our bootloader a real one, it must perform well the above two tasks:\r\n \r\n\\emph on\r\nread \r\n\\emph default\r\nand \r\n\\emph on\r\nload\r\n\\emph default\r\n an operating system.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nFloppy Disk Anatomy\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo read from a storage device, we must understand how the device works,\r\n and the provided interface for controlling it.\r\n First of all, a floppy disk is a storage device, similar to RAM, but can\r\n store information even when a computer is turned off, thus is called \r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\npersistent storage device\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\npersistent storage device\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph on\r\npersistent storage device\r\n\\emph default\r\n.\r\n A floppy disk also a persistent storage device, thus it provides a storage\r\n space up to 1.4 MB, or 1,474,560 bytes.\r\n When reading from a floppy disk, the smallest unit that can be read is\r\n a \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nsector\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nsector\r\n\\emph default\r\n, a group of 512 contiguous bytes.\r\n A group of 18 sectors is a \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\ntrack\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\ntrack\r\n\\emph default\r\n.\r\n Each side of a floppy disk consists of 80 tracks.\r\n A floppy drive is required to read a floppy disk.\r\n Inside a floppy drive contains an arm with 2 \r\n\\emph on\r\nheads\r\n\\emph default\r\n, each head reads a side of a floppy drive; head 0 writes the upper side\r\n and head 1 writes the lower side of a floppy disk.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nSector and Track.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/07/Disk-structure.pdf\r\n\tscale 38\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen a floppy drive writes data to a brand new floppy disk, track 0 on the\r\n upper side is written first, by head 0.\r\n When the upper track 0 is full, the lower track 0 is used by head 1.\r\n When both the upper and lower side of a track 0 are full, it goes back\r\n to head 0 for writing data again, but this time the upper side of track\r\n 1 and so on, until no space left on the device.\r\n The same procedure is also applied for reading data from floppy disk.\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nFloppy disk platter with 2 sides.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/07/Disk_heads.pdf\r\n\tscale 60\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nRead and load sectors from a floppy disk\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"subsec:Read-and-load\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFirst, we need to a sample program for writing into the 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector, so we can experiment with floppy disk reading:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[x86masm]Assembler},numbers=left,basicstyle={\\ttfamily},showstringspaces=false,tabsize=4,title={\\texttt{sample.asm}},commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;******************************************\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n; sample.asm\t\t\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n; A Sample Program\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;******************************************\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmov eax, 1\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nadd eax, 1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSuch a program is good enough.\r\n To simplify and for the purpose of demonstration, we will use the same\r\n floppy disk that holds the bootloader to hold our operating system.\r\n The operating system image starts from the 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector, as the 1\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nst\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector is already in use by the bootloader.\r\n We compile and write it to the 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector with \r\n\\family typewriter\r\ndd\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ nasm -f bin sample.asm -o sample\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ dd if=sample of=disk.img bs=512 count=1 seek=1\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nThe bootloader and the sample program on floppy disk.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"2\" columns=\"5\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<column alignment=\"center\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n1\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nst\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n.....\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n30\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nth\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nbootloader\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nsample\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"1\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n....\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell multicolumn=\"2\" alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n(empty)\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNext, we need to fix the bootloader for reading from the floppy disk and\r\n load a number of arbitrary sectors.\r\n Before doing so, a basic understanding of floppy disk is required.\r\n To read data from disk, interrupt \r\n\\family typewriter\r\n13\r\n\\family default\r\n with \r\n\\family typewriter\r\nAH = 02\r\n\\family default\r\n is a routine for reading sectors from disk into memory:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nAH = 02\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nAL = number of sectors to read (1-128 dec.)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nCH = track/cylinder number (0-1023 dec., see below)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nCL = sector number (1-17 dec.)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDH = head number (0-15 dec.)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDL = drive number (0=A:, 1=2nd floppy, 80h=drive 0, 81h=drive 1)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nES:BX = pointer to buffer\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nReturn:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   AH = status (see INT 13,STATUS)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   AL = number of sectors read\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   CF = 0 if successful\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n      = 1 if error\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nApply the above routine, the bootloader can read the 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[x86masm]Assembler},numbers=left,basicstyle={\\ttfamily},showstringspaces=false,tabsize=4,title={\\texttt{bootloader.asm}},commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;******************************************\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n; Bootloader.asm\t\t\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n; A Simple Bootloader\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;******************************************\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\norg 0x7c00\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nbits 16\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstart: jmp boot\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;; constant and variable definitions\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  msg\tdb\t\"Welcome to My Operating System!\", 0ah, 0dh, 0h\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nboot:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  cli\t; no interrupts \t\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  cld\t; all that we need to init\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tax, 0x50\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  ;; set the buffer\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tes, ax\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  xor\tbx, bx\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tal, 2\t\t\t\t\t; read 2 sector\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tch, 0\t\t\t\t\t; track 0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tcl, 2\t\t\t\t\t; sector to read (The second sector)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tdh, 0\t\t\t\t\t; head number\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tdl, 0\t\t\t\t\t; drive number\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tah, 0x02\t\t\t     ; read sectors from disk\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  int\t0x13\t\t\t\t\t ; call the BIOS routine\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  jmp\t0x50:0x0\t\t\t\t; jump and execute the sector!\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  hlt\t; halt the system\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  ; We have to be 512 bytes.\r\n Clear the rest of the bytes with 0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\ntimes 510 - ($-$$) db 0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\ndw 0xAA55\t\t\t\t  ; Boot Signiture\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe above code jumps to the address \r\n\\family typewriter\r\n0x50:00\r\n\\family default\r\n (which is \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n).\r\n To test the code, load it on a QEMU virtual machine and connect through\r\n \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n, then place a breakpoint at \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n.\r\n If \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stops at the address, with the assembly listing is the same code as in\r\n \r\n\\family typewriter\r\nsample.asm\r\n\\family default\r\n, then the bootloader successfully loaded the program.\r\n This is an important milestone, as we ensure that our operating system\r\n are loaded and ran properly.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nImprove productivity with scripts\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nAutomate build with GNU Make\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nUp to this point, the whole development process felt repetitive: whenever\r\n a change is made, the same commands are entered again.\r\n The commands are also complex.\r\n\r\n\\family typewriter\r\n Ctrl+r\r\n\\family default\r\n helps, but it still feels tedious.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nGNU Make\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is a program that controls and automates the process of building a complex\r\n software.\r\n For a small program, like a single C source file, invoking \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\emph on\r\ngcc\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is quick and easy.\r\n However, soon your software will be more complex, with multiples spanning\r\n multiple directories, it is a chore to manually build and link files.\r\n To solve such problem, a tool was created to automate away this problem\r\n and is called a \r\n\\emph on\r\nbuild system\r\n\\emph default\r\n.\r\n GNU Make is one such of tools.\r\n There are various build systems out there, but GNU Make is the most popular\r\n in Linux world, as it is used for building the Linux kernel.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFor a comprehensive introduction to make, please refer to the official Introduct\r\nion to Make: \r\n\\begin_inset Flex URL\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttps://www.gnu.org/software/make/manual/html_node/Introduction.html#Introduction\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n And that's enough for our project.\r\n You can also download the manual in different formats e.g.\r\n PDF from the official manual page: \r\n\\begin_inset Flex URL\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nhttps://www.gnu.org/software/make/manual/\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n .\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWith Makefile, we can build simpler commands and save time:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},numbers=left,tabsize=4,title={\\texttt{Makefile}},commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nall: bootloader bootdisk\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nbootloader:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tnasm -f bin bootloader.asm -o bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nkernel:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tnasm -f bin sample.asm -o sample.o\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nbootdisk: bootloader.o kernel.o\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd if=/dev/zero of=disk.img bs=512 count=2880\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd conv=notrunc if=bootloader.o of=disk.img bs=512 count=1 seek=0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd conv=notrunc if=sample.o of=disk.img bs=512 count=1 seek=1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow, with a single command, we can build from start to finish a disk image\r\n with a bootloader at 1\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nst\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nsector and the sample program at 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nsector:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ make bootdisk\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nnasm -f bin bootloader.asm -o bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nnasm -f bin sample.asm -o bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ndd if=/dev/zero of=disk.img bs=512 count=2880\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n2880+0 records in\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n2880+0 records out\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n1474560 bytes (1.5 MB, 1.4 MiB) copied, 0.00482188 s, 306 MB/s\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ndd conv=notrunc if=bootloader.o of=disk.img bs=512 count=1 seek=0\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n0+1 records in\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n0+1 records out\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n10 bytes copied, 7.0316e-05 s, 142 kB/s\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ndd conv=notrunc if=sample.o of=disk.img bs=512 count=1 seek=1\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n0+1 records in\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n0+1 records out\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n10 bytes copied, 0.000208375 s, 48.0 kB/s\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nLooking at the Makefile, we can see a few problems: \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFirst, the name \r\n\\family typewriter\r\ndisk.img\r\n\\family default\r\n are all over the place.\r\n When we want to change the disk image name e.g.\r\n \r\n\\family typewriter\r\nfloppy_disk.img\r\n\\family default\r\n, all the places with the name \r\n\\family typewriter\r\ndisk.img\r\n\\family default\r\n must be changed manually.\r\n To solve this problem, we use a variable, and every appearance of \r\n\\family typewriter\r\ndisk.img\r\n\\family default\r\n is replaced with the reference to the variable.\r\n This way, only one place that is changed - the variable definition - all\r\n other places are updated automatically.\r\n The following variables are added:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBOOTLOADER=bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nOS=sample.o\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nDISK_IMG=disk.img.o\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe second problem is, the name \r\n\\family typewriter\r\nbootloader\r\n\\family default\r\n and \r\n\\family typewriter\r\nsample\r\n\\family default\r\n appears as part of the filenames of the source files e.g.\r\n \r\n\\family typewriter\r\nbootloader.asm \r\n\\family default\r\nand \r\n\\family typewriter\r\nsample.asm\r\n\\family default\r\n, as well as the filenames of the binary files e.g.\r\n \r\n\\family typewriter\r\nbootloader\r\n\\family default\r\n and \r\n\\family typewriter\r\nsample\r\n\\family default\r\n.\r\n Similar to \r\n\\family typewriter\r\ndisk.img\r\n\\family default\r\n, when a name changed, every reference of that name must also be changed\r\n manually for both the names of the source files and the names of the binary\r\n files e.g.\r\n if we change \r\n\\family typewriter\r\nbootloader.asm\r\n\\family default\r\n to \r\n\\family typewriter\r\nloader.asm\r\n\\family default\r\n, then the object file \r\n\\family typewriter\r\nbootloader.o\r\n\\family default\r\n needs changing to \r\n\\family typewriter\r\nloader.o\r\n\\family default\r\n.\r\n To solve this problem, instead of changing filenames manually, we create\r\n a rule that automatically generate the filenames of one extension to another.\r\n In this case, we want any source file that starts with \r\n\\family typewriter\r\n.asm \r\n\\family default\r\nto have its equivalent binary files, without any extension e.g.\r\n \r\n\\family typewriter\r\nbootloader.asm\r\n\\family default\r\n \r\n\\begin_inset Formula $\\rightarrow$\r\n\\end_inset\r\n\r\n \r\n\\family typewriter\r\nbootloader.o\r\n\\family default\r\n.\r\n Such transformation is common, so GNU Make provides built-in functions:\r\n \r\n\\family typewriter\r\nwildcard\r\n\\family default\r\n and \r\n\\family typewriter\r\npatsubst\r\n\\family default\r\n for solving such problems:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBOOTLOADER_SRCS := $(wildcard *.asm)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBOOTLOADER_OBJS := $(patsubst %.asm, %.o, $(BOOTLOADER_SRCS))\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nwildcard\r\n\\family default\r\n matches any \r\n\\family typewriter\r\n.asm\r\n\\family default\r\n file in the current directory, then assigned the list of matched files\r\n into the variable \r\n\\family typewriter\r\nBOOTLOADER_SRCS\r\n\\family default\r\n.\r\n In this case, \r\n\\family typewriter\r\nBOOTLOADER_SRCS \r\n\\family default\r\nis assigned the value: \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nbootloader.asm sample.asm\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\npatsubst\r\n\\family default\r\n substitutes any filename starts with \r\n\\family typewriter\r\n.asm\r\n\\family default\r\n into a filename \r\n\\family typewriter\r\n.o\r\n\\family default\r\n e.g.\r\n \r\n\\family typewriter\r\nbootloader.asm\r\n\\family default\r\n \r\n\\begin_inset Formula $\\rightarrow$\r\n\\end_inset\r\n\r\n \r\n\\family typewriter\r\nbootloader.o\r\n\\family default\r\n.\r\n After \r\n\\family typewriter\r\npatsubsts\r\n\\family default\r\n runs, we get a list of object files in \r\n\\family typewriter\r\nBOOTLOADER_OBJS\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nbootloader.o sample.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, a recipe for building from \r\n\\family typewriter\r\n.asm\r\n\\family default\r\n to \r\n\\family typewriter\r\n.o\r\n\\family default\r\n are needed:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n%.o: %.asm\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tnasm -f bin $< -o $@\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n$<\r\n\\family default\r\n is a special variable that refers to the input of the recipe: \r\n\\family typewriter\r\n%.asm\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n$@\r\n\\family default\r\n is a special variable that refers to the output of the recipe: \r\n\\family typewriter\r\n%.o\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen the recipe is executed, the variables are replaced with the actual\r\n values.\r\n For example, if a transformation is \r\n\\family typewriter\r\nbootloader.asm \r\n\\family default\r\n\r\n\\begin_inset Formula $\\rightarrow$\r\n\\end_inset\r\n\r\n \r\n\\family typewriter\r\nbootloader.o\r\n\\family default\r\n, then the actual command executed when replace the placeholders in the\r\n recipe is: \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nnasm -f bin bootloader.asm -o bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWith the recipe, all the \r\n\\family typewriter\r\n.asm\r\n\\family default\r\n files are built automatically with the \r\n\\family typewriter\r\nnasm\r\n\\family default\r\n command into \r\n\\family typewriter\r\n.o\r\n\\family default\r\n files and we no longer need a separate recipe for each object files.\r\n Putting it all together with the new variables, we get a better Makefile:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},numbers=left,tabsize=4,title={\\texttt{Makefile}},commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBOOTLOADER=bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nOS=sample.o\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nDISK_IMG=disk.img\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBOOTLOADER_SRCS := $(wildcard *.asm)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBOOTLOADER_OBJS := $(patsubst %.asm, %.o, $(BOOTLOADER_SRCS))\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nall: bootdisk\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n%.o: %.asm\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tnasm -f bin $< -o $@\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nbootdisk:  $(BOOTLOADER_OBJS)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd if=/dev/zero of=$(DISK_IMG) bs=512 count=2880\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd conv=notrunc if=$(BOOTLOADER) of=$(DISK_IMG) bs=512 count=1 seek=0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd conv=notrunc if=$(OS) of=$(DISK_IMG) bs=512 count=1 seek=1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFrom here on, any \r\n\\family typewriter\r\n.asm\r\n\\family default\r\n file is compiled automatically, without an explicit recipe for each file.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe object files are in the same directory as the source files, making it\r\n more difficult when working with the source tree.\r\n Ideally, object files and source files should live in different directories.\r\n We want a better organized directory layout like Figure \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"project-layout\"\r\n\r\n\\end_inset\r\n\r\n.\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nA better project layout\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"project-layout\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── \r\n\\color blue\r\nbootloader\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── bootloader.asm\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── Makefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── \r\n\\color blue\r\nbuild\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   ├── \r\n\\color blue\r\nbootloader\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   │   └── bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   ├── disk.img\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── \r\n\\color blue\r\nos\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│       └── sample.o\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── Makefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n└── \r\n\\color blue\r\nos\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    ├── Makefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    └── sample.asm\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\nThe layout can be displayed with \r\n\\family typewriter\r\ntree\r\n\\family default\r\n command:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n$ tree\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n\\color blue\r\nbootloader/\r\n\\family default\r\n\\color inherit\r\n directory holds bootloader source files; \r\n\\family typewriter\r\n\\color blue\r\nos/\r\n\\family default\r\n\\color inherit\r\n holds operating system source files that we are going to write later; \r\n\\family typewriter\r\n\\color blue\r\nbuild/\r\n\\family default\r\n\\color inherit\r\n holds the object files for both the bootloader, the os and the final disk\r\n image \r\n\\family typewriter\r\ndisk.img\r\n\\family default\r\n.\r\n Notice that \r\n\\family typewriter\r\n\\color blue\r\nbootloader/\r\n\\family default\r\n\\color inherit\r\n directory also has its own Makefile.\r\n This Makefile will be responsible for building everything in \r\n\\family typewriter\r\n\\color blue\r\nbootloader/\r\n\\family default\r\n\\color inherit\r\n directory, while the top-level Makefile is released from the burden of\r\n building the bootloader, but only the disk image.\r\n The content of the Makefile in \r\n\\family typewriter\r\n\\color blue\r\nbootloader/\r\n\\family default\r\n\\color inherit\r\n directory should be:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},numbers=left,tabsize=4,title={\\texttt{bootloader/Makefile}},commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBUILD_DIR=@|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries ../build/|@bootloader\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBOOTLOADER_SRCS := $(wildcard *.asm)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBOOTLOADER_OBJS := $(patsubst %.asm, $(BUILD_DIR)/%.o, $(BOOTLOADER_SRCS))\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nall: $(BOOTLOADER_OBJS)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n$(BUILD_DIR)/%.o: %.asm\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tnasm -f bin $< -o $@\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nMakefile in \r\n\\family typewriter\r\n\\color blue\r\nbootloader/\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── \r\n\\color blue\r\nbootloader\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── bootloader.asm\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── \r\n\\color red\r\nMakefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── \r\n\\color blue\r\nbuild\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   ├── \r\n\\color blue\r\nbootloader\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   │   └── bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   ├── disk.img\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── \r\n\\color blue\r\nos\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│       └── sample.o\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── Makefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n└── \r\n\\color blue\r\nos\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    ├── Makefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    └── sample.asm\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBasically everything related to the bootloader in the top-level Makefile\r\n are extracted into this Makefile.\r\n When \r\n\\family typewriter\r\nmake\r\n\\family default\r\n runs this Makefile,\r\n\\family typewriter\r\n bootloader.o\r\n\\family default\r\n should be built and put into\r\n\\color blue\r\n \r\n\\family typewriter\r\n../build\r\n\\family default\r\n/\r\n\\color inherit\r\n directory.\r\n As a good practice, all references to \r\n\\family typewriter\r\n../build/\r\n\\family default\r\n go through \r\n\\family typewriter\r\nBUILD_DIR\r\n\\family default\r\n variable.\r\n The recipe for transforming from \r\n\\family typewriter\r\n.asm \r\n\\family default\r\n\r\n\\begin_inset Formula $\\rightarrow$\r\n\\end_inset\r\n\r\n \r\n\\family typewriter\r\n.o\r\n\\family default\r\n is also updated with proper paths, else it will not work.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n%.asm\r\n\\family default\r\n refers to the assembly source files in the current directory.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n$(BUILD_DIR)/%.o\r\n\\family default\r\n refers to the output object files in the build directory in the path \r\n\\family typewriter\r\n\\color blue\r\n../build/\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe entire recipe implements the transformation from \r\n\\family typewriter\r\n<source_file.asm>\r\n\\family default\r\n \r\n\\begin_inset Formula $\\rightarrow$\r\n\\end_inset\r\n\r\n \r\n\\family typewriter\r\n../build/<object_file.o>\r\n\\family default\r\n.\r\n Note that all paths must be correct.\r\n If we try to build object files in a different directory e.g.\r\n current directory, it will not work since there is no such recipe exists\r\n to build objects at such a path.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe also create a similar Makefile for \r\n\\family typewriter\r\n\\color blue\r\nos/\r\n\\family default\r\n\\color inherit\r\n directory:\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nMakefile in \r\n\\family typewriter\r\n\\color blue\r\nos/\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── \r\n\\color blue\r\nbootloader\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── bootloader.asm\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── Makefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── \r\n\\color blue\r\nbuild\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   ├── \r\n\\color blue\r\nbootloader\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   │   └── bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   ├── disk.img\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── \r\n\\color blue\r\nos\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│       └── sample.o\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── Makefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n└── \r\n\\color blue\r\nos\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    ├── \r\n\\color red\r\nMakefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    └── sample.asm\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},numbers=left,tabsize=4,title={\\texttt{os/Makefile}},commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBUILD_DIR=../build/os\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nOS_SRCS := $(wildcard *.asm)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nOS_OBJS := $(patsubst %.asm, $(BUILD_DIR)/%.o, $(OS_SRCS))\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nall: $(OS_OBJS)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n$(BUILD_DIR)/%.o: %.asm\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tnasm -f bin $< -o $@\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFor now, it looks almost identical to the Makefile for bootloader.\r\n In the next chapter, we will update it for C code.\r\n Then, we update the top-level Makefile:\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nTop-level Makefile\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── \r\n\\color blue\r\nbootloader\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── bootloader.asm\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── Makefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── \r\n\\color blue\r\nbuild\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   ├── \r\n\\color blue\r\nbootloader\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   │   └── bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   ├── disk.img\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│   └── \r\n\\color blue\r\nos\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n│       └── sample.o\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n├── \r\n\\color red\r\nMakefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n└── \r\n\\color blue\r\nos\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    ├── Makefile\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    └── sample.asm\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},numbers=left,tabsize=4,title={\\texttt{Makefile}},commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBUILD_DIR=build\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBOOTLOADER=$(BUILD_DIR)/bootloader/bootloader.o\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nOS=$(BUILD_DIR)/os/sample.o\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nDISK_IMG=disk.img\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nall: bootdisk\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n.PHONY: bootdisk bootloader os\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nbootloader:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tmake -C bootloader\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nos:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tmake -C os\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nbootdisk: bootloader os\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd if=/dev/zero of=$(DISK_IMG) bs=512 count=2880\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd conv=notrunc if=$(BOOTLOADER) of=$(DISK_IMG) bs=512 count=1 seek=0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd conv=notrunc if=$(OS) of=$(DISK_IMG) bs=512 count=1 seek=1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe build process is now truly modularized:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nbootloader\r\n\\family default\r\n and \r\n\\family typewriter\r\nos \r\n\\family default\r\nbuilds are now delegated to child Makefile of respective components.\r\n \r\n\\family typewriter\r\n-C\r\n\\family default\r\n option tells \r\n\\family typewriter\r\nmake\r\n\\family default\r\n to execute with a Makefile in a supplied directory.\r\n In this case, the directories are\r\n\\color blue\r\n \r\n\\family typewriter\r\nbootloader/\r\n\\family default\r\n\\color inherit\r\n and \r\n\\family typewriter\r\n\\color blue\r\nos/\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe target \r\n\\family typewriter\r\nall\r\n\\family default\r\n of the top-level Makefile is only responsible for \r\n\\family typewriter\r\nbootdisk\r\n\\family default\r\n target, which is the primary target of this Makefile.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn many cases, a target is not always a filename, but is just a name for\r\n a recipe to be always executed when requested.\r\n If a filename is of the same name as a target and the file is up-to-date,\r\n \r\n\\family typewriter\r\nmake\r\n\\family default\r\n does not execute the target.\r\n To solve this problem, \r\n\\family typewriter\r\n.PHONY\r\n\\family default\r\n specifies that some targets are not files.\r\n All phony targets will then run when requested, regardless of files of\r\n the same names.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo save time entering the command for starting up a QEMU virtual machine,\r\n we also add a target to the top-level Makefile:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nqemu:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tqemu-system-i386 -machine q35 -fda $(DISK_IMG) -gdb tcp::26000 -S\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOne last problem is project cleaning.\r\n At the moment, object files need removing manually and this is a repetitive\r\n process.\r\n Instead, let the Makefile of each component takes care of cleaning its\r\n object files, then top-level Makefile performs project cleaning by calling\r\n the component Makefile to do the jobs.\r\n Each Makefile is added with a \r\n\\family typewriter\r\nclean\r\n\\family default\r\n target at the end:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nBootloader Makefile:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nclean:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\trm $(BUILD_DIR)/*\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nOS Makefile:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nclean:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\trm $(BUILD_DIR)/*\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nTop-level Makefile:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4,commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nclean:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tmake -C bootloader clean\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tmake -C os clean\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nSimply invoking \r\n\\family typewriter\r\nmake clean\r\n\\family default\r\n at the project root, all object files the are removed.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nGNU Make Syntax summary\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nGNU Make, at its core, is a domain-specific language for build automation.\r\n As any programming language, it needs a way to define data and code.\r\n In a Makefile, variables carry data.\r\n A variable value is either hard coded or evaluated from invoking a shell\r\n such as Bash.\r\n All variable values in Make has the same type: a string of text.\r\n Number 3 is not a number, but textual representation of the symbol 3.\r\n Here are common ways how to define data in a Makefile:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"6\" columns=\"2\">\r\n<features islongtable=\"true\" longtabularalignment=\"center\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"7cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"8cm\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nSyntax\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nA = 1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nB = 2\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nC = $$(expr $(A) + $(B))\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n⇒ \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nA\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nB\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n2\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nC\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n3\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nDeclare a variable and assign a textual value to it.\r\n the double dollar sign \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n$$\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n means the enclosing expression evaluating by a shell, defined by \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n/bin/sh\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n In this case, the enclosing expression is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n(expr $(A) + $(B))\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n and is evaluated by Bash.\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPATH = /bin\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPATH := $PATH:/usr/bin\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n⇒ \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPATH\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n/bin/:/usr/bin\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nDeclare a variable and assign to it.\r\n However, the difference is that the \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n=\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n syntax does not allow refer to a variable to use itself as a value in the\r\n right hand side, while this syntax does.\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPATH = /bin\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPATH += /usr/bin\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n⇒ \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPATH\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n/bin/:/usr/bin\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nAppend a new value at the end of a variable.\r\n Equivalent to: \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPATH := $PATH:/usr/bin\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nCFLAGS ?= -o\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n⇒ \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nCFLAGS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is assigned the value \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n-o\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n if it was not defined.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThis syntax is called \r\n\\series bold\r\nconditional reference\r\n\\series default\r\n.\r\n Set a variable to a value if it is undefined.\r\n This is useful if a user wants to supply different value for a variable\r\n from the command line e.g.\r\n add debugging option to \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nCFLAGS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n Otherwise, Make uses the default defined by \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n?=\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSRCS = lib1.c lib2.c main.c\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nOBJS := $(SRC:.o=.c)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n⇒ \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nOBJS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n has the value \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nlib1.o lib2.o main.o\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nThis syntax is called \r\n\\series bold\r\nsubstitution reference\r\n\\series default\r\n.\r\n A part of referenced variable is replaced with something else.\r\n In this case, all the \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n.c\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n extension is replaced by \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n.o\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n extension, thus creating a list of object files for \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nOBJS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n variable from the list of source files from \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nSRCS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n variable.\r\n\\begin_inset Newline newline\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nCode in GNU Make is a collection of recipes that it can run.\r\n Each recipe is analogous to a function in a programming language, and can\r\n be called like a regular function.\r\n Each recipe carries a series of shell commands to be executed by a shell\r\n e.g.\r\n Bash.\r\n A recipe has the following format:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\ntarget: prerequisites\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tcommand\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEach \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\ntarget\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is analogous to a function name.\r\n Each \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nprerequisite\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is a call another target.\r\n Each command is one of Make's built-in commands or a command that is executable\r\n by a shell.\r\n All prerequisites must be satisfied before entering main body of \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\ntarget\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n; that is, each prerequisite must not return any error.\r\n If any error is returned, Make terminates the whole build process and prints\r\n an error on the command line.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEach time \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nmake\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n runs, by default if no target is supplied, it starts with \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nall\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n target, go through every prerequisites and finally the body of \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nall\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nall\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is analogous to \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nmain\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n in other programming languages.\r\n However, if \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nmake\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is given a target, it will start from that target instead of \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nmain\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n This feature is useful to automate multiple aspects in a project.\r\n For example, one target is for building the project, one target is for\r\n generating the documents e.g.\r\n test reports, another target for running the whole test suite and \r\n\\begin_inset Flex Code\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nall\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n runs every main targets.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Note Note\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n[illustration of makefile tree]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nAutomate debugging steps with GDB script\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"subsec:Automate-debugging-steps\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFor the convenience, we save GDB configuration to \r\n\\family typewriter\r\n.gdbinit\r\n\\family default\r\n file at the project root directory.\r\n This configuration is just a collection of GDB commands and a few extra\r\n commands.\r\n When \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n runs, it first loads the \r\n\\family typewriter\r\n.gdbinit\r\n\\family default\r\n file at home directory, then the \r\n\\family typewriter\r\n.gdbinit\r\n\\family default\r\n file at the current directory.\r\n Why shouldn't we put commands in \r\n\\family typewriter\r\n~/.gdbinit\r\n\\family default\r\n? Because these commands are specific to only this project e.g.\r\n not all programs are required a remote connection.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOur first configuration:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language=bash,numbers=left,showstringspaces=false,tabsize=4,title={\\texttt{.gdbinit}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\ndefine hook-stop\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    # Translate the segment:offset into a physical address\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf \"[%4x:%4x] \", $cs, $eip\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    x/i $cs*16+$eip\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nend\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe above script displays the memory address in \r\n\\family typewriter\r\n[segment:offset]\r\n\\family default\r\n format, which is necessary for debugging our bootloader and operating system\r\n code.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIt is better to use Intel syntax:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nset disassembly-flavor intel\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe following commands set a more convenient layout for debugging assembly\r\n code:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nlayout asm\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nlayout reg\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe are currently debugging bootloader code, so it is a good idea to first\r\n set it to 16-bit:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nset architecture i8086\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEvery time the QEMU virtual machine starts, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n must always connect to port \r\n\\family typewriter\r\n26000\r\n\\family default\r\n.\r\n To avoid the trouble of manually connecting to the virtual machine, add\r\n the command:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\ntarget remote localhost:26000\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nDebugging the bootloader needs a breakpoint at 0x7c00, where our bootloader\r\n code starts:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nb *0x7c00\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow, whenever \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n starts, it automatically set correct architecture based on code, automatically\r\n connects to the virtual machine\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nThe QEMU virtual machine should have already been started before starting\r\n \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, displays output in a convenient layout and set a necessary breakpoint.\r\n All that need to do is run the program.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nLinking and loading on bare metal\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"chap:Linking-and-loading\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nRelocation\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nRelocation\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nRelocation\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is the process of replacing symbol references with its actual symbolic\r\n definitions in an object file.\r\n A symbol reference is the memory address of a symbol.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf the definition is hard to understand, consider a similar analogy: house\r\n relocation.\r\n Suppose that a programmer bought a new house and the new house is empty.\r\n He must buy furnitures and appliances to fulfill daily needs and thus,\r\n he made a list of items to buy, and where to place them.\r\n To visualize the placements of new items, he draws a blueprint of the house\r\n and the respective places of all items.\r\n He then travels to the shops to buy goods.\r\n Whenever he visit a shop and sees matched items, he tells the shop owner\r\n to note them down.\r\n After done selecting, he tells the shop owner to pick up a brand new item\r\n instead of the objects on display, then give the address for delivering\r\n the goods to his new house.\r\n Finally, when the goods arrive, he places the items where he planned at\r\n the beginning.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Note Note\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n[illustration of the house relocation process]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow that house relocation is clear, object relocation is similar:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe list of items represents the relocation table, where the memory location\r\n for each symbol (item) is predetermined.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nEach item represents a pair of \r\n\\emph on\r\nsymbol definition\r\n\\emph default\r\n and its \r\n\\emph on\r\nsymbol address\r\n\\emph default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nEach shop represents a compiled object file.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nEach item on display represents a symbol definition and references in the\r\n object file.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThe new address, where all the goods are delivered, represents the final\r\n executable binary or the final object file.\r\n Since the items on display are not for sale, the shop owner delivers brand\r\n new goods instead.\r\n Similarly, the object files are not merged together, but copied all over\r\n a new file, the object/executable file.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nFinally, the goods are placed in the positions according to the shopping\r\n list made from the beginning.\r\n Similarly, the symbol definitions are placed appropriately in its respective\r\n section and the symbol references of the final object/executable file are\r\n replaced with the actual memory addresses of the symbol definitions.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Note Note\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n[illustration: relocation process]\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Note Note\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nTODO: Convert to table format for clarity.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nUnderstand relocations with readelf\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"sec:Understand-relocations-with-readelf\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEarlier, when we explore object sections, there exists sections that begins\r\n with .\r\n\\family typewriter\r\nrel\r\n\\family default\r\n.\r\n These sections are relocation tables that maps between a symbol and its\r\n location in the final object file or the final executable binary\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nA \r\n\\family typewriter\r\n.rel\r\n\\family default\r\n section is equivalent to a list of items in the house analogy.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSuppose that a function \r\n\\family typewriter\r\nfoo\r\n\\family default\r\n is defined in another object file, so \r\n\\family typewriter\r\nmain.c\r\n\\family default\r\n declares it as \r\n\\family typewriter\r\nextern\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"showstringspaces=false,tabsize=4,title={\\texttt{main.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint i;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid foo();\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    i = 5;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    foo();\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid foo() {}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen we compile \r\n\\family typewriter\r\nmain.c\r\n\\family default\r\n as object file with this command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 -masm=intel -c main.c\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, we can inspect the relocation tables with this command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -r main.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe output:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nRelocation section '.rel.text' at offset 0x1cc contains 2 entries:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Offset     Info    Type            Sym.Value  Sym.\r\n Name\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n000000\r\n\\color blue\r\n13\r\n\\color inherit\r\n  0000\r\n\\color magenta\r\n08\r\n\\color green\r\n01\r\n\\color inherit\r\n \r\n\\color green\r\nR_386_32  \r\n\\color inherit\r\n        000000\r\n\\color cyan\r\n04\r\n\\color inherit\r\n   \r\n\\color red\r\ni\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n000000\r\n\\color blue\r\n1c\r\n\\color inherit\r\n  0000\r\n\\color magenta\r\n0a\r\n\\color green\r\n02\r\n\\color inherit\r\n \r\n\\color green\r\nR_386_PC32\r\n\\color inherit\r\n        000000\r\n\\color cyan\r\n2e\r\n\\color inherit\r\n   \r\n\\color red\r\nfoo\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nRelocation section '.rel.eh_frame' at offset 0x1dc contains 2 entries:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Offset     Info    Type            Sym.Value  Sym.\r\n Name\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000020  00000202 R_386_PC32        00000000   .text\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000004c  00000202 R_386_PC32        00000000   .text\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nOffset \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAn \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\noffset\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\noffset\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\noffset\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is the location into a section of a binary file, where the actual memory\r\n address of a symbol definition is replaced.\r\n The section with .\r\n\\family typewriter\r\nrel\r\n\\family default\r\n prefix determines which section to offset into.\r\n For example, \r\n\\family typewriter\r\n.rel.text\r\n\\family default\r\n is the relocation \r\n\\emph on\r\ntable\r\n\\emph default\r\n of symbols whose address needs correcting in \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section, at a specific offset into \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section.\r\n In the example output:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n000000\r\n\\color blue\r\n1c\r\n\\color inherit\r\n  00000a02 R_386_PC32        0000002e   foo\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe blue number indicates there exists a reference of symbol \r\n\\family typewriter\r\nfoo\r\n\\family default\r\n that is \r\n\\family typewriter\r\n1c\r\n\\family default\r\n bytes into \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section.\r\n To see it clearer, we recompile main.c with option -g into the file \r\n\\family typewriter\r\nmain_debug.o\r\n\\family default\r\n, then run \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n on it and got:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDisassembly of section .text:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000000 <main>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint i;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nvoid foo();\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0:   8d 4c 24 04             lea    ecx,[esp+0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   4:   83 e4 f0                and    esp,0xfffffff0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   7:   ff 71 fc                push   DWORD PTR [ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   a:   55                      push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   b:   89 e5                   mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   d:   51                      push   ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   e:   83 ec 04                sub    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    i = 5;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  11:   c7 05 00 00 00 00 05    mov    DWORD PTR ds:0x0,0x5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  18:   00 00 00 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    foo();\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  1b:   e8 \r\n\\color blue\r\nfc ff ff ff\r\n\\color inherit\r\n          call   \r\n\\color blue\r\n1c\r\n\\color inherit\r\n <main+0x1c>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  20:   b8 00 00 00 00          mov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  25:   83 c4 04                add    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  28:   59                      pop    ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  29:   5d                      pop    ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  2a:   8d 61 fc                lea    esp,[ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  2d:   c3                      ret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n....irrelevant content omitted....\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe byte at \r\n\\family typewriter\r\n1b\r\n\\family default\r\n\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is the opcode \r\n\\family typewriter\r\ne8\r\n\\family default\r\n, the \r\n\\family typewriter\r\ncall\r\n\\family default\r\n instruction; byte at \r\n\\family typewriter\r\n1c\r\n\\family default\r\n is the value \r\n\\family typewriter\r\nfc\r\n\\family default\r\n.\r\n Why is the operand value for \r\n\\family typewriter\r\ne8\r\n\\family default\r\n is \r\n\\family typewriter\r\n0xfffffffc\r\n\\family default\r\n, which is equivalent to -4, but the translated instruction \r\n\\family typewriter\r\ncall 1c\r\n\\family default\r\n? It will be explained after a few more sections, but you should pause and\r\n think a bit about the reason why.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nInfo \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nInfo specifies index of a symbol in the symbol table and the type of relocation\r\n to perform.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000001c  0000\r\n\\color magenta\r\n0a\r\n\\color green\r\n02\r\n\\color inherit\r\n R_386_PC32        0000002e   foo\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe pink number is the index of symbol \r\n\\family typewriter\r\nfoo\r\n\\family default\r\n in the symbol table, and the green number is the relocation type.\r\n The numbers are written in hex format.\r\n In the example, \r\n\\family typewriter\r\n0a\r\n\\family default\r\n means \r\n\\family typewriter\r\n10\r\n\\family default\r\n in decimal, and symbol \r\n\\family typewriter\r\nfoo\r\n\\family default\r\n is indeed at index \r\n\\family typewriter\r\n10\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color magenta\r\n10\r\n\\color inherit\r\n: 0000002e     6 FUNC    GLOBAL DEFAULT    1 foo\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nType \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nType represents the type value in textual form.\r\n Looking at the type of foo:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000001c  00000a\r\n\\color green\r\n02\r\n\\color inherit\r\n \r\n\\color green\r\nR_386_PC32\r\n\\color inherit\r\n        0000002e   foo\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe green number is type in its numeric form, and \r\n\\family typewriter\r\nR_386_PC32\r\n\\family default\r\n is the name assigned to that value.\r\n Each value represents a relocation method of calculation.\r\n For example, with the type \r\n\\family typewriter\r\n\\color green\r\nR_386_PC32\r\n\\family default\r\n\\color inherit\r\n, the following formula is applied for relocation (Inteli386 psABI): \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Formula \r\n\\[\r\nRelocated\\,Offset=S+A-P\r\n\\]\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo understand the formula, it is necessary to understand symbol values.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nSym.Value \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis field shows the \r\n\\emph on\r\nsymbol value\r\n\\emph default\r\n.\r\n A symbol value is a value assigned to a symbol, whose meaning depends on\r\n the \r\n\\family typewriter\r\nNdx\r\n\\family default\r\n field:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nA\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nsymbol\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nwhose\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nsection\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nindex\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nis\r\n\\family typewriter\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nCOMMON\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n,\r\n\\family typewriter\r\n \r\n\\family default\r\nits symbol value holds alignment constraints.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nIn the symbol table, the variable \r\n\\color red\r\ni\r\n\\color inherit\r\n is identified as \r\n\\family typewriter\r\n\\color blue\r\nCOM\r\n\\family default\r\n\\color inherit\r\n (uninitialized variable):\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe command for listing symbol table is (assume the object file is \r\n\\family typewriter\r\nhello.o\r\n\\family default\r\n):\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nreadelf -s hello.o\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSymbol table '.symtab' contains 16 entries:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   Num:    Value  Size Type    Bind   Vis      Ndx Name\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS hello2.c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     2: 00000000     0 SECTION LOCAL  DEFAULT    1 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     3: 00000000     0 SECTION LOCAL  DEFAULT    3 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     4: 00000000     0 SECTION LOCAL  DEFAULT    4 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     5: 00000000     0 SECTION LOCAL  DEFAULT    5 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     6: 00000000     0 SECTION LOCAL  DEFAULT    7 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     7: 00000000     0 SECTION LOCAL  DEFAULT    8 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     8: 00000000     0 SECTION LOCAL  DEFAULT   10 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     9: 00000000     0 SECTION LOCAL  DEFAULT   12 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    10: 00000000     0 SECTION LOCAL  DEFAULT   14 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    11: 00000000     0 SECTION LOCAL  DEFAULT   15 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    12: 00000000     0 SECTION LOCAL  DEFAULT   13 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    13: \r\n\\color green\r\n00000004\r\n\\color inherit\r\n     4 OBJECT  GLOBAL DEFAULT  \r\n\\color blue\r\nCOM\r\n\\color inherit\r\n \r\n\\color red\r\ni\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    14: 00000000    46 FUNC    GLOBAL DEFAULT    1 main\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    15: 0000002e     6 FUNC    GLOBAL DEFAULT    1 foo\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nso its symbol value is a memory alignment for assigning a proper memory\r\n address that conforms to the alignment in the final memory address.\r\n In the case of \r\n\\family typewriter\r\n\\color red\r\ni\r\n\\family default\r\n\\color inherit\r\n, the value is \r\n\\family typewriter\r\n\\color green\r\n4\r\n\\family default\r\n\\color inherit\r\n, so the starting memory address of \r\n\\family typewriter\r\n\\color red\r\ni\r\n\\family default\r\n\\color inherit\r\n in the final binary file will be a multiple of 4.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nA\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nsymbol\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nwhose\r\n\\family typewriter\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nNdx\r\n\\family default\r\n\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nidentifies\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\na\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nspecific\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nsection\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, its symbol value holds a section offset.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nIn the symbol table, \r\n\\family typewriter\r\nmain\r\n\\family default\r\n and \r\n\\family typewriter\r\nfoo\r\n\\family default\r\n belong to section 1:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n14: 00000000    46 FUNC    GLOBAL DEFAULT    \r\n\\color blue\r\n1\r\n\\color inherit\r\n \r\n\\color red\r\nmain\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n15: 0000002e     6 FUNC    GLOBAL DEFAULT    \r\n\\color blue\r\n1\r\n\\color inherit\r\n \r\n\\color red\r\nfoo\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nwhich is \r\n\\family typewriter\r\n\\color blue\r\n.text\r\n\\color inherit\r\n\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.text\r\n\\family default\r\n holds program code and read-only data.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n section\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe command for listing sections is (assume the object file is \r\n\\family typewriter\r\nhello.o\r\n\\family default\r\n):\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nreadelf -S hello.o\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 20 section headers, starting at offset 0x558:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSection Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk\r\n Inf Al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 0]                   NULL            00000000 000000 000000 00     \r\n 0   0  0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color blue\r\n[ 1]\r\n\\color inherit\r\n \r\n\\color blue\r\n.text\r\n\\color inherit\r\n             PROGBITS        00000000 000034 000034 00  AX  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 2] .rel.text         REL             00000000 000414 000010 08   I 18\r\n   1  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 3] .data             PROGBITS        00000000 000068 000000 00  WA  0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 4] .bss              NOBITS          00000000 000068 000000 00  WA  0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 5] .debug_info       PROGBITS        00000000 000068 000096 00      0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.....\r\n remaining output omitted for clarity....\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Description\r\n\\begin_inset Flex Noun\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nIn\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nthe\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nfinal\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nexecutable\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nand\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nshared\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nobject\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nfiles\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, instead of the above values, a symbol value holds a memory address.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Example\r\nAfter compiling hello.o into the final executable hello, the symbol table\r\n now contains the memory address for each symbol\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe command to compile the object file \r\n\\family typewriter\r\nhello.o\r\n\\family default\r\n into the executable \r\n\\family typewriter\r\nhello\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\ngcc -g -m32 -masm=intel hello.o -o hello\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSymbol table '.symtab' contains 75 entries:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   Num:    Value  Size Type    Bind   Vis      Ndx Name\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     1: 08048154     0 SECTION LOCAL  DEFAULT    1 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     2: 08048168     0 SECTION LOCAL  DEFAULT    2 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n     3: 08048188     0 SECTION LOCAL  DEFAULT    3 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n....output omitted...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    64: \r\n\\color blue\r\n08048409\r\n\\color inherit\r\n     6 FUNC    GLOBAL DEFAULT   14 \r\n\\color red\r\nfoo\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    65: 0804a020     0 NOTYPE  GLOBAL DEFAULT   26 _end\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    66: 080482e0     0 FUNC    GLOBAL DEFAULT   14 _start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    67: 08048488     4 OBJECT  GLOBAL DEFAULT   16 _fp_hw\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    68: \r\n\\color blue\r\n0804a01c\r\n\\color inherit\r\n     4 OBJECT  GLOBAL DEFAULT   26 \r\n\\color red\r\ni\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    69: 0804a018     0 NOTYPE  GLOBAL DEFAULT   26 __bss_start\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    70: \r\n\\color blue\r\n080483db\r\n\\color inherit\r\n    46 FUNC    GLOBAL DEFAULT   14 \r\n\\color red\r\nmain\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n...ouput omitted...\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nUnlike the values of the symbols \r\n\\family typewriter\r\n\\color red\r\nfoo\r\n\\family default\r\n\\color inherit\r\n, \r\n\\family typewriter\r\n\\color red\r\ni\r\n\\family default\r\n\\color inherit\r\n and \r\n\\family typewriter\r\n\\color red\r\nmain\r\n\\family default\r\n\\color inherit\r\n as in the hello.o object file, the complete memory addresses are in place.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\end_deeper\r\n\\begin_layout Standard\r\nNow it suffices to understand relocation types.\r\n Previously, we mentioned the type \r\n\\family typewriter\r\n\\color green\r\nR_386_PC32\r\n\\family default\r\n\\color inherit\r\n.\r\n The following formula is applied for relocation (Inteli386 psABI): \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align center\r\n\\begin_inset Formula $Relocated\\,Offset=S+A-P$\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nwhere\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n represents the value of the symbol.\r\n In the final executable binary, it is the address of the symbol.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nA\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n represents the addend, an extra value added to the value of a symbol.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nP\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n Represents the memory address to be fixed.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nRelocate\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nOffset\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is the distance between a relocating location\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nwhere the referenced memory address is to be fixed.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n and the actual memory location of a symbol definition, or a memory address.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBut why do we waste time in calculating a distance instead of replacing\r\n with a direct memory address? The reason is that x86 architecture does\r\n not use employ any addressing mode that uses an absolute memory address,\r\n as listed in table \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"mod-rm-32\"\r\n\r\n\\end_inset\r\n\r\n.\r\n All addressing modes in x86 are relative.\r\n In some assembly language, an absolute address can be used simply because\r\n it is a syntactic sugar that is later transformed into one of the relative\r\n addressing mode provided by the x86 hardware by the assembler.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"-4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nFor the \r\n\\family typewriter\r\nfoo\r\n\\family default\r\n symbol:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n000000\r\n\\color blue\r\n1c\r\n\\color inherit\r\n  00000a02 R_386_PC32        000000\r\n\\color cyan\r\n2e\r\n\\color inherit\r\n   foo\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe distance between the usage of \r\n\\family typewriter\r\nfoo\r\n\\family default\r\n in \r\n\\family typewriter\r\nmain.o\r\n\\family default\r\n and its definition, applying the formula \r\n\\begin_inset Formula $S+A-P$\r\n\\end_inset\r\n\r\n is: \r\n\\begin_inset Formula $\\mathtt{2e+0-1c=12}$\r\n\\end_inset\r\n\r\n.\r\n That is, the place where memory fixing starts is \r\n\\family typewriter\r\n0x12\r\n\\family default\r\n or 18 bytes away from \r\n\\emph on\r\nthe definition\r\n\\emph default\r\n of the symbol \r\n\\family typewriter\r\nfoo\r\n\\family default\r\n.\r\n However, to make an instruction works properly, we must also subtract 4\r\n from \r\n\\family typewriter\r\n0x12\r\n\\family default\r\n and results in \r\n\\family typewriter\r\n0xe\r\n\\family default\r\n.\r\n Why the extra \r\n\\family typewriter\r\n-4\r\n\\family default\r\n? Because the relative address starts at \r\n\\emph on\r\nthe end\r\n\\emph default\r\n of an instruction, \r\n\\emph on\r\nnot the address where memory fixing starts\r\n\\emph default\r\n.\r\n For that reason, we must also exclude the 4 bytes of the overwritten address.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIndeed, looking at the \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n output of the object file \r\n\\family typewriter\r\nhello.o\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDisassembly of section .text:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000000 <main>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   0:   8d 4c 24 04             lea    ecx,[esp+0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   4:   83 e4 f0                and    esp,0xfffffff0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   7:   ff 71 fc                push   DWORD PTR [ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   a:   55                      push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   b:   89 e5                   mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   d:   51                      push   ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   e:   83 ec 04                sub    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  11:   c7 05 00 00 00 00 05    mov    DWORD PTR ds:0x0,0x5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  18:   00 00 00 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color red\r\n1b\r\n\\color inherit\r\n:   e8 \r\n\\color blue\r\nfc ff ff ff\r\n\\color inherit\r\n          call   \r\n\\color blue\r\n1c\r\n\\color inherit\r\n <main+0x1c>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  20:   b8 00 00 00 00          mov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  25:   83 c4 04                add    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  28:   59                      pop    ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  29:   5d                      pop    ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  2a:   8d 61 fc                lea    esp,[ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  2d:   c3                      ret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0000002e <foo>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color red\r\n2e\r\n\\color inherit\r\n:   55                      push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  2f:   89 e5                   mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  31:   90                      nop\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  32:   5d                      pop    ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  33:   c3                      ret    \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe place where memory fixing starts is after the opcode \r\n\\family typewriter\r\ne8\r\n\\family default\r\n, with the mock value \r\n\\family typewriter\r\n\\color blue\r\nfc ff ff ff\r\n\\family default\r\n\\color inherit\r\n, which is \r\n\\family typewriter\r\n-4\r\n\\family default\r\n in decimal.\r\n However, the assembly code, the value is displayed as \r\n\\family typewriter\r\n1c\r\n\\family default\r\n.\r\n The memory address right after \r\n\\family typewriter\r\ne8\r\n\\family default\r\n.\r\n The reason is that the instruction \r\n\\family typewriter\r\ne8\r\n\\family default\r\n starts at \r\n\\family typewriter\r\n1b\r\n\\family default\r\n and ends at \r\n\\family typewriter\r\n20\r\n\\family default\r\n\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe end of an instruction is the memory address right after its last operand.\r\n The whole instruction e8 spans from the address \r\n\\family typewriter\r\n1b\r\n\\family default\r\n to the address \r\n\\family typewriter\r\n1f\r\n\\family default\r\n.\r\n \r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n \r\n\\family typewriter\r\n-4\r\n\\family default\r\n means 4 bytes backward from the end of instruction, that is: \r\n\\begin_inset Formula $\\mathtt{20-4=1c}$\r\n\\end_inset\r\n\r\n.\r\n After linking, the output of the final executable file is displayed with\r\n the actual memory fixing:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n080483db <main>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483db:       8d 4c 24 04             lea    ecx,[esp+0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483df:       83 e4 f0                and    esp,0xfffffff0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e2:       ff 71 fc                push   DWORD PTR [ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e5:       55                      push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e6:       89 e5                   mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e8:       51                      push   ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483e9:       83 ec 04                sub    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483ec:       c7 05 1c a0 04 08 05    mov    DWORD PTR ds:0x804a01c,0x5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483f3:       00 00 00 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n80483f6\r\n\\color inherit\r\n:       e8 \r\n\\color blue\r\n0e\r\n\\color inherit\r\n \r\n\\color blue\r\n00 00 00\r\n\\color inherit\r\n          call   \r\n\\color blue\r\n8048409\r\n\\color inherit\r\n <foo>\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 80483fb:       b8 00 00 00 00          mov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048400:       83 c4 04                add    esp,0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048403:       59                      pop    ecx\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048404:       5d                      pop    ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048405:       8d 61 fc                lea    esp,[ecx-0x4]\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 8048408:       c3                      ret \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n08048409 <foo>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color blue\r\n8048409\r\n\\color inherit\r\n:       55                      push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804840a:       89 e5                   mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804840c:       90                      nop\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804840d:       5d                      pop    ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804840e:       c3                      ret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 804840f:       90                      nop\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the final output, the opcode \r\n\\family typewriter\r\ne8\r\n\\family default\r\n previously at \r\n\\family typewriter\r\n1b\r\n\\family default\r\n now starts at the address \r\n\\family typewriter\r\n\\color red\r\n80483f6\r\n\\family default\r\n\\color inherit\r\n.\r\n The mock value \r\n\\family typewriter\r\n\\color blue\r\nfc ff ff ff\r\n\\family default\r\n\\color inherit\r\n is replaced with the actual value \r\n\\family typewriter\r\n\\color blue\r\n0e\r\n\\color inherit\r\n \r\n\\color blue\r\n00 00 00\r\n\\family default\r\n\\color inherit\r\n using the same calculating method from its object file: opcode e8 is at\r\n \r\n\\family typewriter\r\n\\color red\r\n80483f6\r\n\\family default\r\n\\color inherit\r\n.\r\n The definition of \r\n\\family typewriter\r\nfoo\r\n\\family default\r\n is at \r\n\\family typewriter\r\n\\color blue\r\n8048409\r\n\\family default\r\n\\color inherit\r\n.\r\n The offset from the next address after e8 is \r\n\\family typewriter\r\n\r\n\\begin_inset Formula $\\mathtt{8048409+0-80483f7-4=0e}$\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n.\r\n However, for readability, the assembly is displayed as \r\n\\family typewriter\r\ncall \r\n\\color blue\r\n8048409\r\n\\color inherit\r\n <foo>\r\n\\family default\r\n, since \r\n\\family typewriter\r\nGNU as\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nOr any current assembler in use today.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n \r\n\\family default\r\nassembler allows specifying the actual memory address of a symbol definition.\r\n Such address is later translated into relative addressing mode, saving\r\n the programmer the trouble of calculating offset manually.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nSym.\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nName\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis field displays the name of a symbol to be relocated.\r\n The named symbol is the same as written in a high level language such as\r\n C.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nCrafting ELF binary with linker scripts\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nlinker\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nlinker\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nlinker\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a program that combines separated object files into a final binary file.\r\n When \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n is invoked, it runs \r\n\\family typewriter\r\nld\r\n\\family default\r\n underneath to turn object files into the final executable file..\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nlinker script\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\n\\emph on\r\nlinker script\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nlinker script\r\n\\emph default\r\n is a text file that instructs how a linker should combine object files.\r\n When \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n runs, it uses its default linker script to build the memory layout of a\r\n compiled binary file.\r\n Standardized memory layout is called \r\n\\emph on\r\nobject file format\r\n\\emph default\r\n e.g.\r\n ELF includes program headers, section headers and their attributes.\r\n The default linker script is made for running in the current operating\r\n system environment\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nTo view the default script, use \r\n\\family typewriter\r\n--verbose\r\n\\family default\r\n option:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nld --verbose\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n Running on bare metal, the default script cannot be used as it is not designed\r\n for such environment.\r\n For that reason, a programmer needs to supply his own linker script for\r\n such environments.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEvery linker script consists of a series of commands with the following\r\n format:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nCOMMAND \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n{\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\nsub-command 1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nsub-command 2\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n....\r\n more sub-command....\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout LyX-Code\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEach sub-command is specific to only the top-level command.\r\n The simplest linker script needs only one command: \r\n\\family typewriter\r\nSECTION\r\n\\family default\r\n, that consumes input sections from object files and produces output sections\r\n of the final binary file\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nRecall that sections are chunks of code or data, or both.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nExample linker script\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHere is a minimal example of a linker script:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nSECTIONS                      /* Command */\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n   .\r\n = 0x10000;               /* sub-command 1 */\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n   .text : { *(.text) }       /* sub-command 2 */\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n   .\r\n = 0x8000000;             /* sub-command 3 */\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n   .data : { *(.data) }       /* sub-command 4 */\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n   .bss : { *(.bss) }         /* sub-command 5 */\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n} \r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\series bold\r\nCode Dissection: \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{table*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"7\" columns=\"2\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"5cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"10cm\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nCode\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nSECTION\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nTop-level command that declares a list of custom program sections.\r\n \r\n\\family typewriter\r\nld\r\n\\family default\r\n provides a set of such commands.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.\r\n = 0x10000; \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSet location counter to the address \r\n\\family typewriter\r\n0x10000\r\n\\family default\r\n.\r\n Location counter specifies the base address for subsequent commands.\r\n In this example, subsequent commands will use \r\n\\family typewriter\r\n0x10000\r\n\\family default\r\n onward.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.text : { *(.text) } \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSince location counter is set to \r\n\\family typewriter\r\n0x10000\r\n\\family default\r\n, the output \r\n\\family typewriter\r\n.text\r\n\\family default\r\n in the final binary file will starts at the address \r\n\\family typewriter\r\n0x10000\r\n\\family default\r\n.\r\n This command combines all \r\n\\family typewriter\r\n.text\r\n\\family default\r\n sections from all object files with \r\n\\family typewriter\r\n*(.text)\r\n\\family default\r\n syntax into a final \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section.\r\n The \r\n\\family typewriter\r\n*\r\n\\family default\r\n is the wildcard which matches any file name.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.\r\n = 0x8000000; \r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nAgain, the location counter is set to \r\n\\family typewriter\r\n0x8000000\r\n\\family default\r\n.\r\n Subsequent commands will use this address for working with sections.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.data : { *(.data) }\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nAll \r\n\\family typewriter\r\n.data\r\n\\family default\r\n section are combined into one \r\n\\family typewriter\r\n.data\r\n\\family default\r\n section in the final binary file.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.bss : { *(.bss) }\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nAll \r\n\\family typewriter\r\n.bss\r\n\\family default\r\n section are combined into one \r\n\\family typewriter\r\n.bss\r\n\\family default\r\n section in the final binary file.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{table*}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe addresses \r\n\\family typewriter\r\n0x10000\r\n\\family default\r\n and \r\n\\family typewriter\r\n0x8000000\r\n\\family default\r\n are called \r\n\\emph on\r\nVirtual Memory Address\r\n\\emph default\r\n.\r\n A \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nvirtual memory address\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\emph on\r\nvirtual memory address\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nvirtual memory address\r\n\\emph default\r\n is the address where a section is loaded in memory when a program runs.\r\n To use the linker script, we save it as a file e.g.\r\n \r\n\\family typewriter\r\nmain.lds\r\n\\family default\r\n\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n.lds\r\n\\family default\r\n is the extension for linker script.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n; then, we need a sample program in a file, e.g.\r\n \r\n\\family typewriter\r\nmain.c\r\n\\family default\r\n: \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4,title={\\texttt{main.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid test() {}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, we compile the file and explicitly invoke \r\n\\family typewriter\r\nld\r\n\\family default\r\n with the linker script:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc -m32 -g -c main.c\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the \r\n\\family typewriter\r\nld\r\n\\family default\r\n command, the options are similar to \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"4\" columns=\"2\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"2cm\">\r\n<column alignment=\"left\" valignment=\"top\" width=\"14cm\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nOption\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n-m\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSpecify object file format that \r\n\\family typewriter\r\nld\r\n\\family default\r\n produces.\r\n In the example, elf_i386 means a 32-bit ELF is to be produced.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n-o\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSpecify the name of the final executable binary.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n-T\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nSpecify the linker script to use.\r\n In the example, it is \r\n\\family typewriter\r\nmain.lds\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe remaining input is a list of object files for linking.\r\n After the command \r\n\\family typewriter\r\nld\r\n\\family default\r\n is executed, the final executable binary - \r\n\\family typewriter\r\nmain\r\n\\family default\r\n - is produced.\r\n If we try running it:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ./main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nSegmentation fault \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe reason is that when linking manually, the entry address must be explicitly\r\n set, or else \r\n\\family typewriter\r\nld\r\n\\family default\r\n sets it to the start of \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section by default.\r\n We can verify from the \r\n\\family typewriter\r\nreadelf \r\n\\family default\r\noutput:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -h main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nELF Header:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Class:                             ELF64\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Data:                              2's complement, little endian\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Version:                           1 (current)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  OS/ABI:                            UNIX - System V\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  ABI Version:                       0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type:                              EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Machine:                           Advanced Micro Devices X86-64\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Version:                           0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Entry point address:               \r\n\\color red\r\n0x10000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Start of program headers:          64 (bytes into file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Start of section headers:          2098144 (bytes into file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Flags:                             0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of this header:               64 (bytes)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of program headers:           56 (bytes)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Number of program headers:         3\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of section headers:           64 (bytes)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Number of section headers:         14\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Section header string table index: 11\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe entry point address is set to \r\n\\family typewriter\r\n\\color red\r\n0x10000\r\n\\family default\r\n\\color inherit\r\n, which is the beginning of \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section.\r\n Using objdump to examine the address:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump -z -M intel -S -D prog | less\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nwe see that the address \r\n\\family typewriter\r\n0x10000\r\n\\family default\r\n does not start at \r\n\\family typewriter\r\nmain\r\n\\family default\r\n function when the program runs:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDisassembly of section .text:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color red\r\n00010000 <test>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint a = 5;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint i;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nvoid \r\n\\color red\r\ntest\r\n\\color inherit\r\n(){}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   10000:       55                      push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   10001:       89 e5                   mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   10003:       90                      nop\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   10004:       5d                      pop    ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   10005:       c3                      ret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\color blue\r\n00010006 <main>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nint \r\n\\color blue\r\nmain\r\n\\color inherit\r\n(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   10006:       55                      push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   10007:       89 e5                   mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   10009:       b8 00 00 00 00          mov    eax,0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   1000e:       5d                      pop    ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   1000f:       c3                      ret    \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe start of .\r\n\\family typewriter\r\ntext\r\n\\family default\r\n section at \r\n\\family typewriter\r\n\\color red\r\n0x10000\r\n\\family default\r\n\\color inherit\r\n is the function \r\n\\family typewriter\r\n\\color red\r\ntest\r\n\\family default\r\n\\color inherit\r\n, not \r\n\\family typewriter\r\n\\color blue\r\nmain\r\n\\family default\r\n\\color inherit\r\n! To enable the program to run at \r\n\\family typewriter\r\n\\color blue\r\nmain\r\n\\family default\r\n\\color inherit\r\n properly, we need to set the entry point in the linker script with the\r\n following line at the beginning of the file:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nENTRY(main)\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nRecompile the executable binary file \r\n\\family typewriter\r\n\\color blue\r\nmain\r\n\\family default\r\n\\color inherit\r\n again.\r\n This time, the output from \r\n\\family typewriter\r\nreadelf\r\n\\family default\r\n is different:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nELF Header:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Class:                             ELF32\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Data:                              2's complement, little endian\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Version:                           1 (current)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  OS/ABI:                            UNIX - System V\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  ABI Version:                       0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type:                              EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Machine:                           Intel 80386\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Version:                           0x1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Entry point address:               \r\n\\color blue\r\n0x10006\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Start of program headers:          52 (bytes into file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Start of section headers:          9168 (bytes into file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Flags:                             0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of this header:               52 (bytes)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of program headers:           32 (bytes)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Number of program headers:         3\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of section headers:           40 (bytes)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Number of section headers:         14\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Section header string table index: 11\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe program now executes code at the address \r\n\\family typewriter\r\n\\color red\r\n0x10006\r\n\\family default\r\n\\color inherit\r\n when it starts.\r\n \r\n\\family typewriter\r\n\\color blue\r\n0x10006\r\n\\family default\r\n\\color inherit\r\n is where \r\n\\family typewriter\r\n\\color blue\r\nmain\r\n\\family default\r\n\\color inherit\r\n starts! To make sure we really starts at \r\n\\family typewriter\r\nmain\r\n\\family default\r\n, we run the program with \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n, set two breakpoints at \r\n\\family typewriter\r\nmain\r\n\\family default\r\n and \r\n\\family typewriter\r\ntest\r\n\\family default\r\n functions:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gdb ./main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n....\r\n output omitted ....\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nReading symbols from ./main...done.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) \r\n\\color green\r\nb\r\n\\color inherit\r\n \r\n\\color red\r\ntest\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1 at 0x10003: file main.c, line 1.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) b \r\n\\color blue\r\nmain\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 2 at 0x10009: file main.c, line 5.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) r\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nStarting program: /tmp/main \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 2, \r\n\\color blue\r\nmain\r\n\\color inherit\r\n (argc=-11493, argv=0x0) at main.c:5\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n5\t    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAs displayed in the output, \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n stopped at the 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n breakpoint first.\r\n Now, we run the program normally, without \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n: \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ./main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nSegmentation fault \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe still get a segmentation fault.\r\n It is to be expected, as we ran a custom binary without C runtime support\r\n from the operating system.\r\n The last statement in the \r\n\\family typewriter\r\nmain\r\n\\family default\r\n function: \r\n\\family typewriter\r\nreturn 0\r\n\\family default\r\n, simply returns to a random place\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nReturn address is above the current \r\n\\family typewriter\r\nebp\r\n\\family default\r\n.\r\n However, when we enter \r\n\\family typewriter\r\nmain\r\n\\family default\r\n, no return value is pushed on the stack.\r\n So, when return is executed, it simply retrieves any value above \r\n\\family typewriter\r\nebp\r\n\\family default\r\n and use as a return address.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n The C runtime ensures that the program exit properly.\r\n In Linux, the \r\n\\family typewriter\r\n_exit()\r\n\\family default\r\n function is implicitly called when \r\n\\family typewriter\r\nmain\r\n\\family default\r\n returns.\r\n To fix this problem, we simply change the program to exit properly:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=C,numbers=left,showstringspaces=false,tabsize=4,title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid test() {}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    asm(\"mov eax, 0x1\r\n\\backslash\r\nn\"\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        \"mov ebx, 0x0\r\n\\backslash\r\nn\"\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        \"int 0x80\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nInline assembly is required because interrupt \r\n\\family typewriter\r\n0x80\r\n\\family default\r\n is defined for system calls in Linux.\r\n Since the program uses no library, there is no other way to call system\r\n functions, aside from using assembly.\r\n However, when writing our operating system, we will not need such code,\r\n as there is no environment for exiting properly yet.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow that we can precisely control where the program runs initially, it is\r\n easy to bootstrap the kernel from the bootloader.\r\n Before we move on to the next section, note how \r\n\\family typewriter\r\nreadelf\r\n\\family default\r\n and \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n can be applied to debug a program even before it runs.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nUnderstand the custom ELF structure\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the example, we manage to create a runnable ELF executable binary from\r\n a custom linker script, as opposed to the default one provided by \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n.\r\n To make it convenient to look into its structure:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -e main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n-e\r\n\\family default\r\n option is the combination of 3 options \r\n\\family typewriter\r\n-h -l -S\r\n\\family default\r\n: \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.......\r\n ELF header output omitted .......\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nSection Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk\r\n Inf Al\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 0]                   NULL            00000000 000000 000000 00     \r\n 0   0  0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n \r\n\\color red\r\n \r\n\\color inherit\r\n[ 1] \r\n\\color red\r\n.text \r\n\\color inherit\r\n            PROGBITS        \r\n\\color blue\r\n00010000\r\n\\color inherit\r\n 001000 \r\n\\color magenta\r\n000010\r\n\\color inherit\r\n 00  AX  0   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 2] .eh_frame         PROGBITS        \r\n\\color blue\r\n00010010\r\n\\color inherit\r\n 001010 \r\n\\color magenta\r\n000058\r\n\\color inherit\r\n 00   A  0   0  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 3] .debug_info       PROGBITS        00000000 001068 000087 00      0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 4] .debug_abbrev     PROGBITS        00000000 0010ef 000074 00      0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 5] .debug_aranges    PROGBITS        00000000 001163 000020 00      0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 6] .debug_line       PROGBITS        00000000 001183 000038 00      0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 7] .debug_str        PROGBITS        00000000 0011bb 000078 01  MS  0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 8] .comment          PROGBITS        00000000 001233 000034 01  MS  0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [ 9] .shstrtab         STRTAB          00000000 00133a 000074 00      0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [10] .symtab           SYMTAB          00000000 001268 0000c0 10     11\r\n  10  4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  [11] .strtab           STRTAB          00000000 001328 000012 00      0\r\n   0  1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nKey to Flags:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  W (write), A (alloc), X (execute), M (merge), S (strings)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  O (extra OS processing required) o (OS specific), p (processor specific)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x001000 \r\n\\color blue\r\n0x00010000\r\n\\color inherit\r\n \r\n\\color blue\r\n0x00010000\r\n\\color inherit\r\n \r\n\\color magenta\r\n0x00068\r\n\\color inherit\r\n \r\n\\color magenta\r\n0x00068\r\n\\color inherit\r\n R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     .text .eh_frame \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe structure is incredibly simple.\r\n Both the segment and section listings can be contained within one screen.\r\n This is not the case with default ELF executable binary.\r\n From the output, there are only 11 sections, and only two are loaded at\r\n runtime: \r\n\\family typewriter\r\n.text\r\n\\family default\r\n and \r\n\\family typewriter\r\n.eh_frame \r\n\\family default\r\nbecause both section are assigned with an actual memroy addresses, \r\n\\family typewriter\r\n\\color blue\r\n0x10000\r\n\\family default\r\n \r\n\\color inherit\r\nand \r\n\\family typewriter\r\n\\color blue\r\n0x10010\r\n\\family default\r\n \r\n\\color inherit\r\nrespectively.\r\n The remaining sections are assigned with \r\n\\family typewriter\r\n0\r\n\\family default\r\n \r\n\\emph on\r\nin the final executable binary\r\n\\emph default\r\n\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nAs opposed to the object files, where memory addresses are always 0 and\r\n only assigned with actual values in the linking process.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, which mean they are not loaded at runtime.\r\n It makes sense, as those sections are related to versioning\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nIt is the .comment section.\r\n It can be viewed with the comment \r\n\\family typewriter\r\nreadelf -p .comment main\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n, debugging\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe ones starts with \r\n\\family typewriter\r\n.debug\r\n\\family default\r\n prefix.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n and linking\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe symbol tables and string table.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe program segment header table is even simpler.\r\n It only contains 2 segments: \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n and \r\n\\family typewriter\r\nGNU_STACK\r\n\\family default\r\n.\r\n By default, if the linker script does not supply the instructions for building\r\n program segments, \r\n\\family typewriter\r\nld\r\n\\family default\r\n provides reasonable default segments.\r\n As in this case, \r\n\\family typewriter\r\n.text\r\n\\family default\r\n should be in the \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment.\r\n \r\n\\family typewriter\r\nGNU_STACK\r\n\\family default\r\n segment is a GNU extension used by the Linux kernel to control the state\r\n of the program stack.\r\n We will not need this segment, along with \r\n\\family typewriter\r\n.eh_frame\r\n\\family default\r\n, which is for exception handling, as we write our own operating system\r\n from scratch.\r\n To achieve these goals, we will need to create our own program headers\r\n instead of letting \r\n\\family typewriter\r\nld\r\n\\family default\r\n handles the task, and instruct ld to remove \r\n\\family typewriter\r\n.eh_frame\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nManipulate the program segments\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFirst, we need to craft our own program header table by using the following\r\n syntax:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,showstringspaces=false,tabsize=4\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nPHDRS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  <name> <type> [ FILEHDR ] [ PHDRS ] [ AT ( address ) ]\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n        [ FLAGS ( flags ) ] ;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nPHDRS\r\n\\family default\r\n command, similar to \r\n\\family typewriter\r\nSECTION\r\n\\family default\r\n command, but for declaring a list of custom program segments with a predefined\r\n syntax.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nname\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is the header name for later referenced by a section declared in \r\n\\family typewriter\r\nSECTION\r\n\\family default\r\n command.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\ntype\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is the ELF segment type, as described in section \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"sec:Program-header-table\"\r\n\r\n\\end_inset\r\n\r\n, with added prefix \r\n\\family typewriter\r\nPT_\r\n\\family default\r\n.\r\n For example, instead of \r\n\\family typewriter\r\nNULL\r\n\\family default\r\n or \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n as displayed by \r\n\\family typewriter\r\nreadelf\r\n\\family default\r\n, it is \r\n\\family typewriter\r\nPT_NULL\r\n\\family default\r\n or \r\n\\family typewriter\r\nPT_LOAD\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"-4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWith only \r\n\\family typewriter\r\nname\r\n\\family default\r\n and \r\n\\family typewriter\r\ntype\r\n\\family default\r\n, we can create any number of program segments.\r\n For example, we can add the \r\n\\family typewriter\r\nNULL\r\n\\family default\r\n program segment and remove the \r\n\\family typewriter\r\nGNU_STACK\r\n\\family default\r\n segment:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,numbers=left,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nPHDRS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    null PT_NULL;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    code PT_LOAD;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nSECTIONS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .\r\n = 0x10000;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .text : { *(.text) } :code\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .\r\n = 0x8000000;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data : { *(.data) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .bss : { *(.bss) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe content of \r\n\\family typewriter\r\nPHDRS\r\n\\family default\r\n command tells that the final executable binary contains 2 program segments:\r\n \r\n\\family typewriter\r\nNULL\r\n\\family default\r\n and \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n.\r\n The \r\n\\family typewriter\r\nNULL\r\n\\family default\r\n segment is given the name \r\n\\family typewriter\r\nnull \r\n\\family default\r\nand \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment given the name code to signify this \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment contains program code.\r\n Then, to put a section into a segment, we use the syntax \r\n\\family typewriter\r\n:<phdr>\r\n\\family default\r\n, where \r\n\\family typewriter\r\nphdr\r\n\\family default\r\n is the name given to a segment earlier.\r\n In this example, \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section is put into \r\n\\family typewriter\r\ncode\r\n\\family default\r\n segment.\r\n We compile and see the result (assuming \r\n\\family typewriter\r\nmain.o\r\n\\family default\r\n compiled earlier remains):\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x10000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000     0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x001000 0x00010000 0x00010000 0x00010 0x00010 R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text .eh_frame\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThose 2 segments are now \r\n\\family typewriter\r\nNULL\r\n\\family default\r\n and \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n instead of \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n and \r\n\\family typewriter\r\nGNU_STACK\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWe can add as many segments of the same type, as long as they are given\r\n different names:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,numbers=left,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nPHDRS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    null1 PT_NULL;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    null2 PT_NULL;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    code1 PT_LOAD;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    code2 PT_LOAD;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nSECTIONS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .\r\n = 0x10000;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .text : { *(.text) } :code1\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .eh_frame : { *(.eh_frame) } :code2\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .\r\n = 0x8000000;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data : { *(.data) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .bss : { *(.bss) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter amending the \r\n\\family typewriter\r\nPHDRS\r\n\\family default\r\n content earlier with this new segment listing, we put \r\n\\family typewriter\r\n.text\r\n\\family default\r\n into \r\n\\family typewriter\r\ncode1\r\n\\family default\r\n segment and \r\n\\family typewriter\r\n.eh_frame\r\n\\family default\r\n into \r\n\\family typewriter\r\ncode2\r\n\\family default\r\n segment, we compile and see the new segments:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x10000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 4 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000     0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000     0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x001000 0x00010000 0x00010000 0x00010 0x00010 R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x001010 0x00010010 0x00010010 0x00058 0x00058 R   0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   02     .text \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   03     .eh_frame \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow \r\n\\family typewriter\r\n.text\r\n\\family default\r\n and \r\n\\family typewriter\r\n.eh_frame\r\n\\family default\r\n are in different segments.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nFILEHDR\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is an optional keyword, when added specifies that a program segment includes\r\n the ELF file header of the executable binary.\r\n However, this attribute should only added for the first program segment,\r\n as it drastically alters the size and starting address of a segment because\r\n the ELF header is always at the beginning of a binary file, recall that\r\n a segment starts at the address of its first content, which is in most\r\n of the cases (except for this case, which is the file header), the first\r\n section.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"-4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nAdding the \r\n\\family typewriter\r\nFILEHDR\r\n\\family default\r\n keyword changes the size of \r\n\\family typewriter\r\nNULL\r\n\\family default\r\n segment:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nPHDRS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    null PT_NULL FILEHDR;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    code PT_LOAD;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n.....\r\n content is the same .....\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe link it again and see the result:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x10000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  NULL           0x000000 0x00000000 0x00000000 \r\n\\color red\r\n0x00034\r\n\\color inherit\r\n \r\n\\color red\r\n0x00034\r\n\\color inherit\r\n R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x001000 0x00010000 0x00010000 0x00068 0x00068 R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text .eh_frame \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn previous examples, the file size and memory size of the \r\n\\family typewriter\r\nNULL\r\n\\family default\r\n section are always 0, now they are both \r\n\\family typewriter\r\n34\r\n\\family default\r\n bytes, which is the size of an ELF header.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Separator parbreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nIf we assign \r\n\\family typewriter\r\nFILEHDR\r\n\\family default\r\n to a non-starting segment, its size and starting address changes significantly:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nPHDRS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    null PT_NULL;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    code PT_LOAD FILEHDR;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n.....\r\n content is the same .....\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x10000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000     0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x000000 \r\n\\color red\r\n0x0000f000\r\n\\color inherit\r\n \r\n\\color red\r\n0x0000f000\r\n\\color inherit\r\n \r\n\\color blue\r\n0x01068\r\n\\color inherit\r\n \r\n\\color blue\r\n0x01068\r\n\\color inherit\r\n R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text .eh_frame \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe size of the \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment in the previous example is only \r\n\\family typewriter\r\n0x68\r\n\\family default\r\n, the same size as the total sizes of \r\n\\family typewriter\r\n.text\r\n\\family default\r\n and \r\n\\family typewriter\r\n.eh_frame\r\n\\family default\r\n sections in it.\r\n But now, it is \r\n\\family typewriter\r\n\\color blue\r\n0x01068\r\n\\family default\r\n\\color inherit\r\n, got \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n bytes larger.\r\n What is the reason for these extra bytes? A simple answer: segment alignment.\r\n From the output, the alignment of this segment is \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n; it means that regardless of which address is the start of this segment,\r\n it must be divisible by \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n.\r\n For that reason, the starting address of \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n is \r\n\\family typewriter\r\n\\color red\r\n0xf000\r\n\\family default\r\n\\color inherit\r\n because it is divisible by \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAnother question arises: why is the starting address \r\n\\family typewriter\r\n\\color red\r\n0xf000\r\n\\family default\r\n\\color inherit\r\n instead of \r\n\\family typewriter\r\n0x10000\r\n\\family default\r\n? \r\n\\family typewriter\r\n.text\r\n\\family default\r\n is the first section, which starts at \r\n\\family typewriter\r\n0x10000\r\n\\family default\r\n, so the segment should start at \r\n\\family typewriter\r\n0x10000\r\n\\family default\r\n.\r\n The reason is that we include \r\n\\family typewriter\r\nFILEHDR\r\n\\family default\r\n as part of the segment, it must expand to include the ELF file header,\r\n which is at the very start of an ELF executable binary.\r\n To satisfy this constraint and the alignment constraint, \r\n\\family typewriter\r\n\\color red\r\n0xf000\r\n\\family default\r\n\\color inherit\r\n is the closest address.\r\n Note that the virtual and physical memory addresses are the addresses at\r\n runtime, not the locations of the segment in the file on disk.\r\n As the \r\n\\family typewriter\r\nFileSiz\r\n\\family default\r\n field shows, the segment only consumes \r\n\\family typewriter\r\n\\color blue\r\n0x1068\r\n\\family default\r\n\\color inherit\r\n bytes on disk.\r\n Figure \r\n\\begin_inset CommandInset ref\r\nLatexCommand formatted\r\nreference \"LOAD_segment\"\r\n\r\n\\end_inset\r\n\r\n illustrates the difference between the memory layouts with and without\r\n \r\n\\family typewriter\r\nFILEHDR\r\n\\family default\r\n keyword.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment on disk and in memory.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"LOAD_segment\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nWithout \r\n\\family typewriter\r\n\\series bold\r\nFILEHDR\r\n\\family default\r\n\\series default\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/08/no_FILEHDR.pdf\r\n\tscale 120\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nWith \r\n\\family typewriter\r\n\\series bold\r\nFILEHDR\r\n\\family default\r\n\\series default\r\n.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Graphics\r\n\tfilename images/08/with_FILEHDR.pdf\r\n\tscale 120\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset VSpace vfill\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Newpage pagebreak\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nPHDRS\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n is an optional keyword, when added specifies that a program segment is\r\n a program segment header table.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"0ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nThe first segment of the default executable binary generated by \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n is a \r\n\\family typewriter\r\nPHDR\r\n\\family default\r\n since the program segment header table appears right after the ELF header.\r\n It is also a convenient segment to put the ELF header into using the \r\n\\family typewriter\r\nFILEHDR\r\n\\family default\r\n keyword.\r\n We replace the unused \r\n\\family typewriter\r\nNULL\r\n\\family default\r\n segment earlier with a \r\n\\family typewriter\r\nPHDR\r\n\\family default\r\n segment:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nPHDRS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    headers PT_PHDR FILEHDR PHDRS;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    code PT_LOAD FILEHDR;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n.....\r\n content is the same .....\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x10000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  \r\n\\color red\r\nPHDR\r\n\\color inherit\r\n           0x000000 0x00000000 0x00000000 \r\n\\color blue\r\n0x00074\r\n\\color inherit\r\n \r\n\\color blue\r\n0x00074\r\n\\color inherit\r\n R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x001000 0x00010000 0x00010000 0x00068 0x00068 R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text .eh_frame \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAs shown in the output, the first segment is of type \r\n\\family typewriter\r\n\\color red\r\nPHDR\r\n\\family default\r\n\\color inherit\r\n.\r\n Its size is \r\n\\family typewriter\r\n\\color blue\r\n0x74\r\n\\family default\r\n\\color inherit\r\n, which includes:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n0x34\r\n\\family default\r\n bytes for ELF header.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\n0x40\r\n\\family default\r\n bytes for the program segment header table, with 2 entries, each is \r\n\\family typewriter\r\n0x20\r\n\\family default\r\n bytes (32 bytes) in length.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe above number is consistent with ELF header output:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nELF Header:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Class:                             ELF32\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.......\r\n output omitted ......\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of this header:               \r\n\\color red\r\n52\r\n\\color inherit\r\n (bytes)   \r\n\\series bold\r\n\\size large\r\n--> 0x34 bytes\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of program headers:           \r\n\\color red\r\n32\r\n\\color inherit\r\n (bytes)   \r\n\\series bold\r\n\\size large\r\n-->\r\n\\size default\r\n \r\n\\size large\r\n0x20 bytes each program header\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Number of program headers:         \r\n\\color red\r\n2\r\n\\color inherit\r\n            \r\n\\series bold\r\n\\size large\r\n-->\r\n\\size default\r\n \r\n\\size large\r\n0x40\r\n\\series default\r\n \r\n\\series bold\r\nbytes in total\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Size of section headers:           40 (bytes)  \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Number of section headers:         12\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Section header string table index: 9\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nAT\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n(\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\naddress\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n specifies the load memory address where the segment is placed.\r\n Every segment or section has a \r\n\\emph on\r\nvirtual memory address\r\n\\emph default\r\n and a \r\n\\emph on\r\nload memory address\r\n\\emph default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Itemize\r\nA \r\n\\emph on\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\emph on\r\nvirtual memory address\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nvirtual memory address\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nvirtual memory address\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\emph default\r\n is a starting address of a segment or a section when a program is in memory\r\n and running.\r\n The memory address is called virtual because it does not map to the actual\r\n memory cell that corresponds to the address number, but any random memory\r\n cell, which depends on how the underlying operating system translates the\r\n address.\r\n For example, the virtual memory address \r\n\\family typewriter\r\n0x1\r\n\\family default\r\n might map to the memory cell with the physical address \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nA \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nload memory address\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Marginal\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\emph on\r\nload memory address\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nload memory address\r\n\\emph default\r\n is the physical memory address, where a program is loaded but not yet running.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe load memory address is specified by \r\n\\family typewriter\r\nAT\r\n\\family default\r\n syntax.\r\n Normally both types of addresses are the same, and the physical address\r\n can be ignored.\r\n They differ when loading and running are purposely divided into two distinct\r\n phases that require different address regions.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFor example, a program can be designed to load into a ROM\r\n\\begin_inset Foot\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nRead-Only Memory\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n at a fixed address.\r\n But when loading into RAM for a bare-metal application or an operating\r\n system to use, the program needs a load address that accommodates the addressin\r\ng scheme of the target application or operating system.\r\n \r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"0ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWe can specify a load memory address for the segment \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n with AT syntax:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nPHDRS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    headers PT_PHDR FILEHDR PHDRS AT(0x500);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    code PT_LOAD;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n.....\r\n content is the same .....\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x4000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 \r\n\\color red\r\n0x00000500\r\n\\color inherit\r\n 0x00074 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x001000 0x00004000 0x00002000 0x00068 0x00068 R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text .eh_frame \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIt depends on an operating system whether to use the address or not.\r\n For our operating system, the virtual memory address and load are the same,\r\n so an explicit load address is none of our concern.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\n\\begin_inset Flex Strong\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nFLAGS\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\n(flags)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n assigns permissions to a segment.\r\n Each flag is an integer that represents a permission and can be combined\r\n with OR operations.\r\n Possible values:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset Tabular\r\n<lyxtabular version=\"3\" rows=\"4\" columns=\"3\">\r\n<features tabularvalignment=\"middle\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<column alignment=\"left\" valignment=\"top\">\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nPermission\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nValue\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\series bold\r\nDescription\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nR\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n1\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nReadable\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nW\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n2\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nWritable\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n<row>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nE\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"center\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n4\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n<cell alignment=\"left\" valignment=\"top\" topline=\"true\" bottomline=\"true\" leftline=\"true\" rightline=\"true\" usebox=\"none\">\r\n\\begin_inset Text\r\n\r\n\\begin_layout Plain Layout\r\nExecutable\r\n\\end_layout\r\n\r\n\\end_inset\r\n</cell>\r\n</row>\r\n</lyxtabular>\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"0ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWe can create a LOAD segment with Read, Write and Execute permissions enabled:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nPHDRS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    headers PT_PHDR FILEHDR PHDRS AT(0x500);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    code PT_LOAD FILEHDR FLAGS(0x1 | 0x2 | 0x4);\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n.....\r\n content is the same .....\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 0x00000500 0x00074 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x001000 0x00000000 0x00000000 0x00010 0x00010 \r\n\\color red\r\nRWE\r\n\\color inherit\r\n 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text .eh_frame\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment now gets all the \r\n\\family typewriter\r\n\\color red\r\nRWE\r\n\\family default\r\n\\color inherit\r\n permissions, as shown above.\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset CommandInset line\r\nLatexCommand rule\r\noffset \"4ex\"\r\nwidth \"100col%\"\r\nheight \"1.5pt\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, we want to remove the \r\n\\family typewriter\r\n.eh_frame\r\n\\family default\r\n or any unwanted section, we add a special section called \r\n\\family typewriter\r\n/DISCARD/\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n...\r\n program segment header table remains the same ...\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nSECTIONS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    /* .\r\n = 0x10000; */\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .text : { *(.text) } :code\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .\r\n = 0x8000000;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .data : { *(.data) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    .bss : { *(.bss) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    /DISCARD/ : { *(.eh_frame) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAny section putting in \r\n\\family typewriter\r\n/DISCARD/\r\n\\family default\r\n disappears in the final executable binary:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 0x00000500 0x00074 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x001000 0x00000000 0x00000000 0x00010 0x00010 R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAs can be seen, \r\n\\family typewriter\r\n.eh_frame\r\n\\family default\r\n is nowhere to be found.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nC Runtime: Hosted vs Freestanding\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"sec:C-Runtime\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe purpose of .\r\n\\family typewriter\r\ninit\r\n\\family default\r\n, \r\n\\family typewriter\r\n.init_array\r\n\\family default\r\n, \r\n\\family typewriter\r\n.fini_array\r\n\\family default\r\n and \r\n\\family typewriter\r\n.preinit_array\r\n\\family default\r\n section is to initialize a C Runtime environment that supports the C standard\r\n libraries.\r\n Why does C need a runtime environment, when it is supposed to be a compiled\r\n language? The reason is that many of the standard functions depend on the\r\n underlying operating system, which is of itself a big runtime environment.\r\n For example, I/O related functions such as reading from keyboard with \r\n\\family typewriter\r\ngets()\r\n\\family default\r\n, reading from file with \r\n\\family typewriter\r\nopen()\r\n\\family default\r\n, printing on screen with \r\n\\family typewriter\r\nprintf()\r\n\\family default\r\n, managing system memory with \r\n\\family typewriter\r\nmalloc()\r\n\\family default\r\n, \r\n\\family typewriter\r\nfree()\r\n\\family default\r\n, etc.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA C implementation cannot provide such routines without a running operating\r\n system, which is a\r\n\\emph on\r\n hosted environment\r\n\\emph default\r\n.\r\n A \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nhosted environment\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nhosted environment\r\n\\emph default\r\n is a runtime environment that:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nprovides a default implementation of C libraries that includes system-dependent\r\n data and routines.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nperform resource allocations to prepare an environment for a program to\r\n run.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThis process is similar to the hardware initialization process:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nWhen first powered up, a desktop computer loads its basic system routines\r\n from a read-only memory stored on the motherboard.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nThen, it starts initializing an environment, such as setting default values\r\n for various registers in CPU and devices, before executing the any code.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn contrast, a \r\n\\emph on\r\n\r\n\\begin_inset Index idx\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\nfreestanding environment\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\nfreestanding environment\r\n\\emph default\r\n is an environment that does not provide system-dependent data and routines.\r\n As a consequence, almost no C library exists and the environment can run\r\n code compiled written from pure C syntax.\r\n For a free standing environment to become a host environment, it must implement\r\n standard C system routines.\r\n But for a \r\n\\emph on\r\nconforming\r\n\\emph default\r\n freestanding environment, it only needs these header files available: \r\n\\family typewriter\r\n<float.h>\r\n\\family default\r\n, \r\n\\family typewriter\r\n<limits.h>\r\n\\family default\r\n, \r\n\\family typewriter\r\n<stadarg.h>\r\n\\family default\r\n and \r\n\\family typewriter\r\n<stddef.h>\r\n\\family default\r\n (according to GCC manual).\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFor a typical desktop x86 program, C runtime environment is initialized\r\n by a compiler so a program runs normal.\r\n However, for an embedded platform where a program runs directly on it,\r\n this is not the case.\r\n The typical C runtime environment used in desktop operating systems cannot\r\n be used on the embedded platforms, because architectural differences and\r\n resource constraints.\r\n As such, the software writer must implement a custom C runtime environment\r\n suitable for the targeted platform.\r\n For the embedded platform, \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn writing our operating system, the first step is to create a freestanding\r\n environment before creating a hosted one.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nDebuggable bootloader on bare metal\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nCurrently, the bootloader is compiled as a flat binary file.\r\n Although \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n can display the assembly code, it is not always the same as the source\r\n code.\r\n In the assembly source code, there exists variable names and labels.\r\n These symbols are lost when compiled as a flat binary file, making debugging\r\n more difficult.\r\n Another issue is the mismatch between the written assembly source code\r\n and the displayed assembly source code.\r\n The written code might contain higher level syntax that is assembler-specific\r\n and is generated into lower-level assembly code as displayed by \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n.\r\n Finally, with debug information available, the command \r\n\\family typewriter\r\nnext/n\r\n\\family default\r\n and \r\n\\family typewriter\r\nprev/p\r\n\\family default\r\n can be used instead of \r\n\\family typewriter\r\nni\r\n\\family default\r\n and \r\n\\family typewriter\r\nsi\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo enable debug information, we modify the bootloader Makefile:\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nThe bootloader must be compiled as a ELF binary.\r\n Open the Makefile in bootloader/ directory and change this line under \r\n\\family typewriter\r\n$(BUILD_DIR)/%.o: %.asm\r\n\\family default\r\n recipe:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nnasm -f bin $< -o $@\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nto this line:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nnasm -f elf $< -F dwarf -g -o $@\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn the updated recipe, \r\n\\family typewriter\r\nbin\r\n\\family default\r\n format is replaced with \r\n\\family typewriter\r\nelf\r\n\\family default\r\n format to enable debugging information to be properly produced.\r\n\\family typewriter\r\n-F\r\n\\family default\r\n option specifies the debug information format, which is \r\n\\family typewriter\r\ndwarf\r\n\\family default\r\n in this case.\r\n Finally, \r\n\\family typewriter\r\n-g\r\n\\family default\r\n option causes \r\n\\family typewriter\r\nnasm\r\n\\family default\r\n to actually generate debug information in selected format.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Enumerate\r\nThen, \r\n\\family typewriter\r\nld\r\n\\family default\r\n consumes the ELF bootloader binary and produces another ELF bootloader\r\n binary, with proper\r\n\\family typewriter\r\n \r\n\\family default\r\nstarting memory address of \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section that match the actual address of the bootloader at runtime, when\r\n QEMU virtual machine loads it at \r\n\\family typewriter\r\n0x7c00\r\n\\family default\r\n.\r\n We need \r\n\\family typewriter\r\nld\r\n\\family default\r\n because when compiled by \r\n\\family typewriter\r\nnasm\r\n\\family default\r\n, the starting address is assumed to be \r\n\\family typewriter\r\n0\r\n\\family default\r\n, not \r\n\\family typewriter\r\n0x7c00\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nFinally, we use \r\n\\family typewriter\r\nobjcopy\r\n\\family default\r\n to separate extract only the flat binary content as the original bootloader\r\n by adding this line to \r\n\\family typewriter\r\n$(BUILD_DIR)/%.o: %.asm\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nobjcopy -O binary $(BUILD_DIR)/bootloader.o.elf $@\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nobjcopy\r\n\\family default\r\n, as its name implies, is a program that copies and translates object files.\r\n Here, we copy the original ELF bootloader and translate it into a flat\r\n binary file.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nThe updated recipe should look like:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n$(BUILD_DIR)/%.o: %.asm\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tnasm -f elf $< -F dwarf -g -o $@\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tld -m elf_i386 -T bootloader.lds $@ -o $@.elf\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tobjcopy -O binary  $(BUILD_DIR)/bootloader.o.elf $@\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow we test the bootloader with debug information available:\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nStart the QEMU machine:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ make qemu\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Enumerate\r\nStart \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n with the debug information stored in \r\n\\family typewriter\r\nbootloader.o.elf\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gdb build/bootloader/bootloader.o.elf\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter getting into \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n, press the \r\n\\family typewriter\r\nEnter\r\n\\family default\r\n key and if the sample \r\n\\family typewriter\r\n.gdbinit\r\n\\family default\r\n section \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"subsec:Automate-debugging-steps\"\r\n\r\n\\end_inset\r\n\r\n is used, the output should look like:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n---Type <return> to continue, or q <return> to quit---\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[f000:fff0] 0x0000fff0 in ?? ()\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1 at \r\n\\color red\r\n0x7c00\r\n\\color inherit\r\n: \r\n\\color blue\r\nfile bootloader.asm, line 6.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb)\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ngdb\r\n\\family default\r\n now understand where the instruction at address \r\n\\family typewriter\r\n\\color red\r\n0x7c00\r\n\\family default\r\n\\color inherit\r\n is in the assembly source file, thanks to the debug information.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Section\r\nDebuggable program on bare metal\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe process of building a debug-ready executable binary is similar to that\r\n of a bootloader, except more involved.\r\n Recall that for a debugger to work properly, its debugging information\r\n must contain correct address mappings between memory addresses and the\r\n source code.\r\n \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n stores such mapping information in DIE entries, in which it tells \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n at which code address corresponds to a line in a source file, so that breakpoin\r\nts work properly.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBut first, we need a sample C source file, a very simple one:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"title={\\texttt{os.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nvoid main() {}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBecause this is a free standing environment, standard libraries that involve\r\n system functions such as \r\n\\family typewriter\r\nprintf()\r\n\\family default\r\n would not work, because a C runtime does not exist.\r\n At this stage, the goal is to correctly jump to main with source code displayed\r\n properly in \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n, so no fancy C code is needed yet.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe next step is updating \r\n\\family typewriter\r\nos/Makefile\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\nBUILD_DIR=../build\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nOS=$(BUILD_DIR)/os\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n@|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries CFLAGS+=-ffreestanding -nostdlib -gdwarf-4 -m32 -ggdb3|@\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nOS_SRCS := $(wildcard *.c)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nOS_OBJS := $(patsubst %.c, $(BUILD_DIR)/%.o, $(OS_SRCS))\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nall: $(OS)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n$(BUILD_DIR)/%.o: %.c\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tgcc $(CFLAGS) -c  $< -o $@\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n$(OS): $(OS_OBJS)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tld -m elf_i386 -T@|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries os.lds|@ $(OS_OBJS) -o $@\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nclean:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\trm $(OS_OBJS)\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe updated the Makefile with the following changes:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nAdd a \r\n\\family typewriter\r\nCFLAGS\r\n\\family default\r\n variable for passing options to \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nInstead of the rule to build assembly source code earlier, it is replaced\r\n with a C version with a recipe to build C source files.\r\n The \r\n\\family typewriter\r\nCFLAGS\r\n\\family default\r\n variable makes the \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n command in the recipe looks cleaner regardless how many options are added.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nAdd a linking command for building the final executable binary of the operating\r\n system with a custom linker script \r\n\\family typewriter\r\n\\color blue\r\nos.lds\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEverything looks good, except for the linker script part.\r\n Why is it needed? The linker script is required for controlling at which\r\n physical memory address the operating system binary appears in the memory,\r\n so the linker can jump to the operating system code and execute it.\r\n To complete this requirement, the default linker script used by \r\n\\family typewriter\r\ngcc\r\n\\family default\r\n would not work as it assumes the compiled executable runs inside an existing\r\n operating system, while we are writing an operating system itself.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe next question is, what will be the content in the linker script? To\r\n answer this question, we must understand what goals to achieve with the\r\n linker script:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nFor the bootloader to correctly jump to and execute the operating system\r\n code.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nFor \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n to debug correctly with the operating system source code.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo achieve the goals, we must devise a design of a suitable memory layout\r\n for the operating system.\r\n Recall that the bootloader developed in chapter \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"chap:Bootloader\"\r\n\r\n\\end_inset\r\n\r\n can already load a simple binary compiled from the sample Assembly program\r\n \r\n\\family typewriter\r\nsample.asm\r\n\\family default\r\n.\r\n To load the operating system, we can simply throw binary compiled from\r\n \r\n\\family typewriter\r\nsample.asm\r\n\\family default\r\n with the binary compiled from \r\n\\family typewriter\r\nos.c\r\n\\family default\r\n above.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf only it is that simple.\r\n The idea is correctly, but not enough.\r\n The goals implies the following constraints:\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nThe operating system code is written in C and compiled as an ELF executable\r\n binary.\r\n It means, the bootloader needs to retrieve correct entry address from the\r\n ELF header.\r\n\\end_layout\r\n\r\n\\begin_layout Enumerate\r\nTo debug properly with \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n, the debug info must contain correct mappings between instruction addresses\r\n and source code.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThanks to the understanding of ELF and DWARF acquire in the earlier chapters,\r\n we can certainly modify the bootloader and create an executable binary\r\n that satisfy the above constraint.\r\n We will solve these problems one by one.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nLoading an ELF binary from a bootloader\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEarlier we examined that an ELF header contains a entry address of a program.\r\n That information is 0x18 bytes away from the beginning of an ELF header,\r\n according to \r\n\\family typewriter\r\nman elf\r\n\\family default\r\n :\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language=C,showstringspaces=false,tabsize=4\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\ntypedef struct {\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               unsigned char e_ident[EI_NIDENT];\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               uint16_t      e_type;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               uint16_t      e_machine;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               uint32_t      e_version;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               ElfN_Addr     e_entry;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               ElfN_Off      e_phoff;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               ElfN_Off      e_shoff;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               uint32_t      e_flags;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               uint16_t      e_ehsize;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               uint16_t      e_phentsize;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               uint16_t      e_phnum;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               uint16_t      e_shentsize;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               uint16_t      e_shnum;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n               uint16_t      e_shstrndx;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n           } ElfN_Ehdr;\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe offset from the start of the struct to the start of \r\n\\family typewriter\r\ne_entry\r\n\\family default\r\n is:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n16 bytes of \r\n\\family typewriter\r\ne_ident[EI_NIDENT]:\r\n\\family default\r\n\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout LyX-Code\r\n#define EI_NIDENT 16\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\n2 bytes of e_type\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n2 bytes of e_machine\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n4 bytes of e_version\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align center\r\n\\begin_inset Formula \r\n\\[\r\n\\mathtt{Offset=16+2+2+4=24=0x18}\r\n\\]\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\ne_entry\r\n\\family default\r\n is of type \r\n\\family typewriter\r\nElfN_Addr\r\n\\family default\r\n, in which \r\n\\family typewriter\r\nN\r\n\\family default\r\n is either \r\n\\family typewriter\r\n32\r\n\\family default\r\n or \r\n\\family typewriter\r\n64\r\n\\family default\r\n.\r\n We are writing 32-bit operating system, in this case \r\n\\begin_inset Formula $N=32$\r\n\\end_inset\r\n\r\n and so \r\n\\family typewriter\r\nElfN_Addr\r\n\\family default\r\n is \r\n\\family typewriter\r\nElf32_Addr\r\n\\family default\r\n, which is 4 bytes long.\r\n\\end_layout\r\n\r\n\\begin_layout Example\r\nWith any program, such as this simple one:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"title={\\texttt{hello.c}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n#include <stdio.h>\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nint main(int argc, char *argv[])\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    printf(\"hello world!\r\n\\backslash\r\nn\");\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n    return 0;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe can retrieve the entry address with a human-readable presentation using\r\n \r\n\\family typewriter\r\nreadelf\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gcc hello.c -o hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -h hello\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nELF Header:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  ....\r\n output omitted ....\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Entry point address:               \r\n\\color red\r\n0x400430\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  ....\r\n output omitted ....\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOr in raw binary with \r\n\\family typewriter\r\nhd\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ hd hello | less\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000010  02 00 3e 00 01 00 00 00  \r\n\\color red\r\n30 04 40 00\r\n\\color inherit\r\n 00 00 00 00  |..>.....0.@.....|\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n.........\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe offset \r\n\\family typewriter\r\n0x18\r\n\\family default\r\n is the start of the least-significant byte of \r\n\\family typewriter\r\ne_entry\r\n\\family default\r\n, which is \r\n\\family typewriter\r\n\\color red\r\n0x30\r\n\\family default\r\n\\color inherit\r\n, followed by\r\n\\color red\r\n 04 40 00\r\n\\color inherit\r\n, together in reverse makes the address \r\n\\family typewriter\r\n\\color red\r\n0x00400430\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Standard\r\nNow that we know where the position of the entry address in the ELF header,\r\n it is easy to modify the bootloader made in section \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"subsec:Read-and-load\"\r\n\r\n\\end_inset\r\n\r\n to retrieve and jump to the address:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[x86masm]Assembler},basicstyle={\\ttfamily},showstringspaces=false,tabsize=4,title={\\texttt{bootloader.asm}},commentstyle={\\color{mygray}\\itshape},frame=shadowbox\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;******************************************\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n; Bootloader.asm\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n; A Simple Bootloader\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;******************************************\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nbits 16\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nstart: jmp boot\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n;; constant and variable definitions\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nmsg\tdb\t\"Welcome to My Operating System!\", 0ah, 0dh, 0h\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nboot:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  cli\t; no interrupts\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  cld\t; all that we need to init\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\t\tax, 50h\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  ;; set the buffer\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tes, ax\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  xor\tbx, bx\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tal, 2\t\t\t\t\t      ; read 2 sector\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tch, 0                          ; we are reading the second sector\r\n past us,\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n                                        ; so its still on track 0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tcl, 2\t\t\t\t\t      ; sector to read (The second sector)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tdh, 0\t\t\t\t\t      ; head number\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tdl, 0\t\t\t\t\t      ; drive number.\r\n Remember Drive 0 is floppy drive.\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  mov\tah, 0x02\t\t\t           ; read floppy sector function\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  int\t0x13\t\t\t\t\t       ; call BIOS - Read the sector\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  jmp\t@|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries [500h + 18h]|@\t\t\t\t  ; jump and execute the sector!\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  hlt\t; halt the system\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  ; We have to be 512 bytes.\r\n Clear the rest of the bytes with 0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  times 510 - ($-$$) db 0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  dw 0xAA55\t\t\t\t  ; Boot Signiture\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIt is as simple as that! First, we load the operating system binary at \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n, then we retrieve the entry address at the offset \r\n\\family typewriter\r\n0x18\r\n\\family default\r\n from \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n, by first calculating the expression \r\n\\begin_inset Formula $500h+18h=518h$\r\n\\end_inset\r\n\r\n to get the actual in-memory address, then retrieve the content by dereference\r\n it.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe first part is done.\r\n For the next part, we need to build an ELF operating system image for the\r\n bootloader to load.\r\n The first step is to create a linker script:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n@|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries ENTRY(main);|@\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nPHDRS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  headers PT_PHDR FILEHDR PHDRS;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  code PT_LOAD;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nSECTIONS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  .text @|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries 0x500|@: { *(.text)  } :code\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  .data :  { *(.data)  }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  .bss :  { *(.bss) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  /DISCARD/ : { *(.eh_frame) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe script is straight-forward and remains almost the same as before.\r\n The only differences are:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n\r\n\\family typewriter\r\nmain\r\n\\family default\r\n are explicitly specified as the entry point by specifying \r\n\\family typewriter\r\n\\color red\r\nENTRY(main)\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\n.\r\n\\family typewriter\r\ntext\r\n\\family default\r\n is explicitly specified with \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n as its \r\n\\emph on\r\nvirtual memory address\r\n\\emph default\r\n since we load the operating system image at \r\n\\family typewriter\r\n\\color blue\r\n0x500\r\n\\family default\r\n\\color inherit\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter putting the script, we compile with \r\n\\family typewriter\r\nmake\r\n\\family default\r\n and it should work smoothly:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ make clean; make\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l  build/os/os \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point \r\n\\color red\r\n0x500\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           0x000500 \r\n\\color red\r\n0x00000500\r\n\\color inherit\r\n \r\n\\color red\r\n0x00000500\r\n\\color inherit\r\n 0x00040 0x00040 R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAll looks good, until we run it.\r\n We begin by starting the QEMU virtual machine:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ make qemu\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, start \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n and load the debug info (which is also in the same binary file) and set\r\n a breakpoint at \r\n\\family typewriter\r\nmain\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) symbol-file build/os/os\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nReading symbols from build/os/os...done.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) b main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nBreakpoint 2 at 0x500\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen we start the program:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) symbol-file build/os/os\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nReading symbols from build/os/os...done.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) b main\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nBreakpoint 2 at 0x500\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nKeep the programming running until it stops at \r\n\\family typewriter\r\nmain\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) c\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nContinuing.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n[ 0:7c00] \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nBreakpoint 1, 0x00007c00 in ?? ()\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) c\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nContinuing.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n[ 0: 500] \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nBreakpoint 2, main () at main.c:1\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAt this point, we switch the layout to the C source code instead of the\r\n registers:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) layout split\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nlayout split\r\n\\family default\r\n creates a layout that consists of 3 smaller windows: \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nSource window at the top.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nAssembly window in the middle.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nCommand window at the bottom.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter the command, the layout should look like this:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   ┌──main.c───────────────────────────────────────────────────────┐\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nB+>│1       void main(){}                                          │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │2                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │3                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │4                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │5                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │6                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │7                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │8                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │9                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │10                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │11                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │12                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │13                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │14                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │15                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │16                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   └───────────────────────────────────────────────────────────────┘\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nB+>│\r\n\\color red\r\n0x500 <main>    jg     0x547\r\n\\color inherit\r\n                                   │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │\r\n\\color red\r\n0x502 <main+2>  dec    sp    \r\n\\color inherit\r\n                                  │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │\r\n\\color red\r\n0x503 <main+3>  inc    si   \r\n\\color inherit\r\n                                   │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │\r\n\\color red\r\n0x504 <main+4>  add    WORD PTR [bx+di],ax  \r\n\\color inherit\r\n                   │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x506           add    WORD PTR [bx+si],ax                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x508           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x50a           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x50c           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x50e           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x510           add    al,BYTE PTR [bx+si]                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x512           add    ax,WORD PTR [bx+si]                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x514           add    WORD PTR [bx+si],ax                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x516           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x518           add    BYTE PTR [di],al                        │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x51a           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x51c           xor    al,0x0                                  │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x51e           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   └───────────────────────────────────────────────────────────────┘\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nremote Thread 1 In: main                            L1    PC: 0x500 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[f000:fff0] 0x0000fff0 in ?? ()\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1 at 0x7c00\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) symbol-file build/os/os\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nReading symbols from build/os/os...done.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) b main\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 2 at 0x500: file main.c, line 1.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nContinuing.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[   0:7c00] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1, 0x00007c00 in ?? ()\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nContinuing.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[   0: 500] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 2, main () at main.c:1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) layout split\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nSomething wrong is going on here.\r\n It is not the generated assembly code for function call as it is known\r\n in section \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"subsec:Function-Call-and\"\r\n\r\n\\end_inset\r\n\r\n.\r\n It is definitely wrong, verified with \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump -D build/os/os | less\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n/home/tuhdo/workspace/os/build/os/os:     file format elf32-i386\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDisassembly of section .text:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000500 <main>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 500:   55                      push   %ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 501:   89 e5                   mov    %esp,%ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 503:   90                      nop\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 504:   5d                      pop    %ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 505:   c3                      ret\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n....\r\n remaining output omitted ....\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe assembly code of \r\n\\family typewriter\r\nmain\r\n\\family default\r\n is completely different.\r\n This is why understanding assembly code and its relation to high-level\r\n languages are important.\r\n Without the knowledge, we would have used \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n as a simple source-level debugger without bother looking at the assembly\r\n code from the split layout.\r\n As a consequence, the true cause of the non-working code could never been\r\n discovered.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nDebugging the memory layout\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhat is the reason for the incorrect Assembly code in \r\n\\family typewriter\r\nmain\r\n\\family default\r\n displayed by \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n? There can only be one cause: the bootloader jumped to the wrong addresses.\r\n But why was the address wrong? We made the \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section at address \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n, in which \r\n\\family typewriter\r\nmain\r\n\\family default\r\n code is in the first byte for executing, and instructed the bootloader\r\n to retrieve the address at the offset \r\n\\family typewriter\r\n0x18\r\n\\family default\r\n, then jump to the entry address.\r\n\\begin_inset Float marginfigure\r\nwide false\r\nsideways false\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nMemory state after loading 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"mem-state-bootloader\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/08/mem_after_load_os.pdf\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, it might be possible for the bootloader to load the operating system\r\n address at the wrong address.\r\n But then, we explicitly set the load address to \r\n\\family typewriter\r\n50h:00\r\n\\family default\r\n, which is \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n, and so the correct address was used.\r\n After the bootloader loas the 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector, the in-memory state should look like the figure \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"mem-state-bootloader\"\r\n\r\n\\end_inset\r\n\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHere is the problem: \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n is the start of the ELF header.\r\n The bootloader actually loads the 2\r\n\\begin_inset script superscript\r\n\r\n\\begin_layout Plain Layout\r\nnd\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n sector, which stores the executable as a whole, to \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n.\r\n Clearly, .\r\n\\family typewriter\r\ntext\r\n\\family default\r\n section, where \r\n\\family typewriter\r\nmain\r\n\\family default\r\n resides, is far from \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n.\r\n Since the in-memory entry address of the executable binary is \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n, \r\n\\family typewriter\r\n.text\r\n\\family default\r\n should be at \r\n\\family typewriter\r\n\r\n\\begin_inset Formula $\\mathtt{0x500+0x500=0xa00}$\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n.\r\n However, the entry address recorded in the ELF header remains \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n and as a result, the bootloader jumped there instead of \r\n\\family typewriter\r\n0xa00\r\n\\family default\r\n.\r\n This is one of the issues that must be fixed.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe other issue is the mapping between debug info and the memory address.\r\n Because the debug info is compiled with the assumed offset \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n that is the start of \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section, but due to actual loading, the offset is pushed another \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n bytes, making the address actually is at \r\n\\family typewriter\r\n0xa00\r\n\\family default\r\n.\r\n This memory mismatch renders the debug info useless.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nWrong symbol-memory mappings in debug info.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/08/debug_info_bad_position.pdf\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn summary, we have 2 problems to overcome:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nFix the entry address to account for the extra offset when loading into\r\n memory.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nFix the debug info to account for the extra offset when loading into memory.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFirst, we need to know the actual layout of the compiled executable binary:l\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ readelf -l build/os/os\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point \r\n\\color blue\r\n0x500\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           \r\n\\color red\r\n0x000500\r\n\\color inherit\r\n \r\n\\color blue\r\n0x00000500\r\n\\color inherit\r\n 0x00000500 0x00040 0x00040 R E 0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNotice the \r\n\\family typewriter\r\nOffset\r\n\\family default\r\n and the \r\n\\family typewriter\r\nVirtAddress\r\n\\family default\r\n fields: both have the same value.\r\n This is problematic, as the entry address and the memory addresses in the\r\n debug info depend on \r\n\\family typewriter\r\nVirtAddr\r\n\\family default\r\n field, but the \r\n\\family typewriter\r\nOffset\r\n\\family default\r\n having the same value destroys the validity of \r\n\\family typewriter\r\nVirtAddr\r\n\\family default\r\n\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nThe offset is the distance in bytes between the beginning of the file, the\r\n address 0, to the beginning address of a segment or a section.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n because it means that the real in-memory address will always be greater\r\n than the \r\n\\family typewriter\r\nVirtAddr\r\n\\family default\r\n.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf we try to adjust the virtual memory address of the \r\n\\family typewriter\r\n.text\r\n\\family default\r\n section in the linker script \r\n\\family typewriter\r\nos.lds\r\n\\family default\r\n, whatever value we set also sets the \r\n\\family typewriter\r\nOffset\r\n\\family default\r\n to the same value, until we set it to some value equal or greater than\r\n \r\n\\family typewriter\r\n0x1074\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x1074\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           \r\n\\color red\r\n0x000074\r\n\\color inherit\r\n \r\n\\color blue\r\n0x00001074\r\n\\color inherit\r\n 0x00001074 0x00006 0x00006 R E \r\n\\color magenta\r\n0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf we adjust the virtual address to \r\n\\family typewriter\r\n0x1073\r\n\\family default\r\n, both the \r\n\\family typewriter\r\nOffset\r\n\\family default\r\n and \r\n\\family typewriter\r\nVirtAddr\r\n\\family default\r\n still share the same value:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x1073\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           \r\n\\color red\r\n0x001073\r\n\\color inherit\r\n \r\n\\color blue\r\n0x00001073\r\n\\color inherit\r\n 0x00001073 0x00006 0x00006 R E \r\n\\color magenta\r\n0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe key to answer such phenonemon is in the \r\n\\family typewriter\r\nAlign\r\n\\family default\r\n field.\r\n The value \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n indicates that the offset address of the segment should be divisible by\r\n \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n, or if the distance between segment is divisible by \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n, the linker removes such distance to save the binary size.\r\n We can do some experiments to verify this claim\r\n\\begin_inset Foot\r\nstatus collapsed\r\n\r\n\\begin_layout Plain Layout\r\nAll the outputs are produced by the command:\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\family typewriter\r\n$ readelf -l build/os/os\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nBy setting the virtual address of \r\n\\family typewriter\r\n.text\r\n\\family default\r\n to \r\n\\family typewriter\r\n0x0\r\n\\family default\r\n to \r\n\\family typewriter\r\n0x73\r\n\\family default\r\n (in \r\n\\family typewriter\r\nos.lds\r\n\\family default\r\n), the offset starts from \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n to \r\n\\family typewriter\r\n0x1073\r\n\\family default\r\n, accordingly.\r\n For example, by setting it to \r\n\\family typewriter\r\n0x0\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x0\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 0x00000000 \r\n\\color blue\r\n0x00074\r\n\\color inherit\r\n 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           \r\n\\color red\r\n0x001000\r\n\\color inherit\r\n 0x00000000 0x00000000 0x00006 0x00006 R E \r\n\\color magenta\r\n0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBy default, if we do not specify any virtual address, the offset stays at\r\n \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n because \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n is the perfect offset to satisfy the alignment constraint.\r\n Any addition from \r\n\\family typewriter\r\n0x1\r\n\\family default\r\n to \r\n\\family typewriter\r\n0x73\r\n\\family default\r\n makes the segment misaligned, but the linker keeps it anyway because it\r\n is told so.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nBy setting the virtual address of \r\n\\family typewriter\r\n.text\r\n\\family default\r\n to \r\n\\family typewriter\r\n0x74\r\n\\family default\r\n (in \r\n\\family typewriter\r\nos.lds\r\n\\family default\r\n):\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point 0x74\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           \r\n\\color red\r\n0x000074\r\n\\color inherit\r\n 0x00000074 0x00000074 0x00006 0x00006 R E \r\n\\color magenta\r\n0x1000\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\nPHDR\r\n\\family default\r\n is \r\n\\family typewriter\r\n0x74\r\n\\family default\r\n bytes in size, so if \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n starts at \r\n\\family typewriter\r\n0x1074\r\n\\family default\r\n, the distance between the \r\n\\family typewriter\r\nPHDR\r\n\\family default\r\n segment and \r\n\\family typewriter\r\nLOAD\r\n\\family default\r\n segment is \r\n\\family typewriter\r\n\r\n\\begin_inset Formula $\\mathtt{0x1074-0x74=0x1000}$\r\n\\end_inset\r\n\r\n\r\n\\family default\r\n bytes.\r\n To save space, it removes that extra \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n bytes.\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Itemize\r\nBy setting the virtual address of \r\n\\family typewriter\r\n.text\r\n\\family default\r\n to any value between \r\n\\family typewriter\r\n0x75\r\n\\family default\r\n and \r\n\\family typewriter\r\n0x1073\r\n\\family default\r\n (in \r\n\\family typewriter\r\nos.lds\r\n\\family default\r\n), the offset takes the exact values specified, as can be seen in the case\r\n of setting to \r\n\\family typewriter\r\n0x1073\r\n\\family default\r\n above.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nBy setting the virtual address of \r\n\\family typewriter\r\n.text\r\n\\family default\r\n to any value equal or greater than \r\n\\family typewriter\r\n0x1074\r\n\\family default\r\n: it starts all over again at \r\n\\family typewriter\r\n0x74\r\n\\family default\r\n, where the distance is equal to \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n bytes.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow we get a hint how to control the values of \r\n\\family typewriter\r\nOffset\r\n\\family default\r\n and \r\n\\family typewriter\r\nVirtAddr\r\n\\family default\r\n to produce a desired binary layout.\r\n What we need is to change the \r\n\\family typewriter\r\nAlign\r\n\\family default\r\n field to a value with smaller value for finer grain control.\r\n It might work out with a binary layout like this:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point \r\n\\color blue\r\n0x600\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           \r\n\\color red\r\n0x000100\r\n\\color inherit\r\n \r\n\\color blue\r\n0x00000600\r\n\\color inherit\r\n 0x00000600 0x00006 0x00006 R E \r\n\\color magenta\r\n0x100\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe binary will look like figure \r\n\\begin_inset CommandInset ref\r\nLatexCommand ref\r\nreference \"fig-good-bin-layout\"\r\n\r\n\\end_inset\r\n\r\n in memory:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset Float figure\r\nwide false\r\nsideways false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset Caption Standard\r\n\r\n\\begin_layout Plain Layout\r\nA good binary layout.\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset CommandInset label\r\nLatexCommand label\r\nname \"fig-good-bin-layout\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\begin_inset Graphics\r\n\tfilename images/08/binary_layout_good.pdf\r\n\r\n\\end_inset\r\n\r\n\r\n\\begin_inset space \\hfill{}\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf we set the \r\n\\family typewriter\r\nOffset\r\n\\family default\r\n field to \r\n\\family typewriter\r\n0x100\r\n\\family default\r\n from the beginning of the file and the \r\n\\family typewriter\r\nVirtAddr\r\n\\family default\r\n to \r\n\\family typewriter\r\n0x600\r\n\\family default\r\n, when loading in memory, the actual memory of .text is \r\n\\begin_inset Formula $\\mathtt{0x500+0x100=0x600}$\r\n\\end_inset\r\n\r\n; \r\n\\family typewriter\r\n0x500\r\n\\family default\r\n is the memory location where the bootloader loads into the physical memory\r\n and \r\n\\family typewriter\r\n0x100\r\n\\family default\r\n is the offset from the end of ELF header to \r\n\\family typewriter\r\n.text\r\n\\family default\r\n.\r\n The entry address and the debug info will then take the value \r\n\\family typewriter\r\n0x600 \r\n\\family default\r\nfrom the \r\n\\family typewriter\r\nVirtAddr\r\n\\family default\r\n field above, which totally matches the actual physical layout.\r\n We can do it by changing \r\n\\family typewriter\r\nos.lds\r\n\\family default\r\n as follow:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset listings\r\nlstparams \"language=bash,showstringspaces=false,tabsize=4,title={\\texttt{main.lds}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n@|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries ENTRY(main);|@\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nPHDRS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  headers PT_PHDR FILEHDR PHDRS;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  code PT_LOAD;\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nSECTIONS\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n{\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  .text @|\r\n\\backslash\r\ncolor{blue}\r\n\\backslash\r\nbfseries 0x600|@: @|\r\n\\backslash\r\ncolor{magenta} ALIGN(0x100)|@ { *(.text)  } :code\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  .data :  { *(.data)  }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  .bss :  { *(.bss) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n  /DISCARD/ : { *(.eh_frame) }\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe \r\n\\family typewriter\r\nALIGN\r\n\\family default\r\n keyword, as it implies, tells the linker to align a section, thus the segment\r\n containing it.\r\n However, to make the \r\n\\family typewriter\r\nALIGN\r\n\\family default\r\n keyword has any effect, automatic alignment must be disabled.\r\n According to \r\n\\family typewriter\r\nman ld\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n-n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n--nmagic\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    Turn off page alignment of sections, and disable linking against shared\r\n    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    libraries.\r\n  If the output format supports Unix style magic numbers, mark the \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n    output as \"NMAGIC\"\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThat is, by default, each section is aligned by an operating system page,\r\n which is \r\n\\family typewriter\r\n4096\r\n\\family default\r\n, or \r\n\\family typewriter\r\n0x1000\r\n\\family default\r\n bytes in size.\r\n The \r\n\\family typewriter\r\n-n\r\n\\family default\r\n or \r\n\\family typewriter\r\n-nmagic\r\n\\family default\r\n option disables this behavior, which is needed.\r\n We amend the \r\n\\family typewriter\r\nld\r\n\\family default\r\n command used in \r\n\\family typewriter\r\nos/Makefile\r\n\\family default\r\n:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4,title={\\texttt{os/Makefile}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n.....\r\n above content omitted ....\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n$(OS): $(OS_OBJS)\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tld -m elf_i386 @|\r\n\\backslash\r\ncolor{red}\r\n\\backslash\r\nbfseries -nmagic|@ -Tos.lds $(OS_OBJS) -o $@\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFinally, we also need to update the top-level Makefile to write more than\r\n one sector into the disk image for the operating system binary, as its\r\n size exceeds one sector:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ ls -l build/os/os\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n-rwxrwxr-x 1 tuhdo tuhdo \r\n\\color red\r\n9060\r\n\\color inherit\r\n Feb 13 21:37 build/os/os\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWe update the rule so that the sectors are automatically calculated:\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\noindent\r\n\\align left\r\n\\begin_inset listings\r\nlstparams \"language={[gnu]make},tabsize=4,title={\\texttt{os/Makefile}}\"\r\ninline false\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n.....\r\n above content omitted ....\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\nbootdisk:  bootloader os\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd if=/dev/zero of=$(DISK_IMG) bs=512 count=2880\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd conv=notrunc if=$(BOOTLOADER) of=$(DISK_IMG) bs=512 count=1 seek=0\r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\tdd conv=notrunc if=$(OS) of=$(DISK_IMG) bs=512 count=$$(($(shell stat --printf=\r\n\"%s\" $(OS))/512)) seek=1\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAfter updating the everything, recompiling the executable binary and we\r\n get the desired offset and virtual memory at \r\n\\family typewriter\r\n0x100\r\n\\family default\r\n and \r\n\\family typewriter\r\n0x600\r\n\\family default\r\n, respectively:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nElf file type is EXEC (Executable file)\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nEntry point \r\n\\color blue\r\n0x600\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nThere are 2 program headers, starting at offset 52\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nProgram Headers:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R   0x4\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  LOAD           \r\n\\color red\r\n0x000100\r\n\\color inherit\r\n \r\n\\color blue\r\n0x00000600\r\n\\color inherit\r\n 0x00000600 0x00006 0x00006 R E \r\n\\color magenta\r\n0x100\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n Section to Segment mapping:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n  Segment Sections...\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   00     \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   01     .text \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nTesting the new binary\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFirst, we start the QEMU machine:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ make qemu\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn another terminal, we start \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n, loading the debug info and set a breakpoint at \r\n\\family typewriter\r\nmain\r\n\\family default\r\n:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ gdb\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe following output should be produced:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n---Type <return> to continue, or q <return> to quit---\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[f000:fff0] 0x0000fff0 in ?? ()\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1 at 0x7c00\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 2 at \r\n\\color blue\r\n0x600\r\n\\color inherit\r\n: file main.c, line 1.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThen, let \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n runs until it hits the \r\n\\family typewriter\r\nmain\r\n\\family default\r\n function, then we change to the split layout between source and assembly:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) layout split\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe final terminal output should look like this:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   ┌──main.c───────────────────────────────────────────────────────┐\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nB+>│1       void main(){}                                          │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │2                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │3                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │4                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │5                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │6                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │7                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │8                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │9                                                              │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │10                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │11                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │12                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │13                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │14                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │15                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │16                                                             │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   └───────────────────────────────────────────────────────────────┘\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nB+>│\r\n\\color red\r\n0x600 <main>    push   bp\r\n\\color inherit\r\n                                      │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │\r\n\\color red\r\n0x601 <main+1>  mov    bp,sp\r\n\\color inherit\r\n                                   │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │\r\n\\color red\r\n0x603 <main+3>  nop    \r\n\\color inherit\r\n                                        │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │\r\n\\color red\r\n0x604 <main+4>  pop    bp\r\n\\color inherit\r\n                                      │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │\r\n\\color red\r\n0x605 <main+5>  ret    \r\n\\color inherit\r\n                                        │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x606           aaa                                            │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x607           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x609           add    BYTE PTR [si],al                        │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x60b           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x60d           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x60f           add    BYTE PTR [si],al                        │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x611           add    ax,bp                                   │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x613           push   ss                                      │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x614           add    BYTE PTR [bx+si],al                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x616           or     al,0x67                                 │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x618           adc    al,BYTE PTR [bx+si]                     │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   │0x61a           add    BYTE PTR [bx+si+0x2],al                 │\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n   └───────────────────────────────────────────────────────────────┘\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nremote Thread 1 In: main                            L1    PC: 0x600 \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nContinuing.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[   0:7c00] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 1, 0x00007c00 in ?? ()\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) c\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nContinuing.\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n[   0: 600] \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nBreakpoint 2, main () at main.c:1\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n(gdb) layout split\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nNow, the displayed assembly is the same as in \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n, except the registers are 16-bit ones.\r\n This is normal, as \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n is operating in 16-bit mode, while \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n displays code in 32-bit mode.\r\n To make sure, we verify the raw opcode by using \r\n\\family typewriter\r\nx\r\n\\family default\r\n command:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n(gdb) x/16xb 0x600\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x600 <main>:   \r\n\\color red\r\n0x55    0x89    0xe5    0x90    0x5d    0xc3 \r\n\\color inherit\r\n   0x37\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n        0x00\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n0x608:  0x00    0x00    0x04    0x00    0x00    0x00    0x00    0x00\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nFrom the assembly window, \r\n\\family typewriter\r\nmain\r\n\\family default\r\n stops at the address \r\n\\family typewriter\r\n0x605\r\n\\family default\r\n.\r\n As such, the corresponding bytes from \r\n\\family typewriter\r\n0x600\r\n\\family default\r\n to \r\n\\family typewriter\r\n0x605\r\n\\family default\r\n are highlighted in red from the output of the command \r\n\\family typewriter\r\nx/16xb 0x600\r\n\\family default\r\n.\r\n Then, the raw opcode from the \r\n\\family typewriter\r\nobjdump\r\n\\family default\r\n output:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\family typewriter\r\n$ objdump -z -M intel -S -D build/os/os | less\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shellcommand}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbegin{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nbuild/os/os:     file format elf32-i386\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDisassembly of section .text:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n00000600 <main>:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nvoid main(){}\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 600:   \r\n\\color red\r\n55\r\n\\color inherit\r\n                      push   ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 601:   \r\n\\color red\r\n89 e5\r\n\\color inherit\r\n                   mov    ebp,esp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 603:   \r\n\\color red\r\n90\r\n\\color inherit\r\n                      nop\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 604:   \r\n\\color red\r\n5d\r\n\\color inherit\r\n                      pop    ebp\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n 605:   \r\n\\color red\r\nc3\r\n\\color inherit\r\n                      ret    \r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\nDisassembly of section .debug_info:\r\n\\end_layout\r\n\r\n\\begin_layout LyX-Code\r\n......\r\n output omitted ......\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nend{shelloutput16.6}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nBoth raw opcode displayed by the two programs are the same.\r\n In this case, it proved that \r\n\\family typewriter\r\ngdb\r\n\\family default\r\n correctly jumped to the address in \r\n\\family typewriter\r\nmain\r\n\\family default\r\n for a proper debugging.\r\n This is an extremely important milestone.\r\n Being able to debug in bare metal will help tremendously in writing an\r\n operating system, as a debugger allows a programmer to inspect the internal\r\n state of a running machine at each step to verify his code, step by step,\r\n to gradually build up a solid understanding.\r\n Some professional programmers do not like debuggers, but it is because\r\n they understand their domain deep enough to not need to rely on a debugger\r\n to verify their code.\r\n When encountering new domains, a debugger is indispensable learning tool\r\n because of its verifiability.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHowever, even with the aid of debugger, writing an operating system is still\r\n not a walk in the park.\r\n The debugger may give the access to the machine at one point in time, but\r\n it does not give the cause.\r\n To find out the root cause, is up to the ability of a programmer.\r\n Later in the book, we will learn how to use other debugging techniques,\r\n such as using QEMU logging facility to debug CPU exceptions.\r\n\\end_layout\r\n\r\n\\begin_layout Part\r\nKernel Programming\r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nx86 Descriptors\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nBasic operating system concepts\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThe first and foremost, OS manages hardware resources.\r\n It's easy to see the core features of an OS based on Von Neumann diagram:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nCPU\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nmanagement: allows programs to share CPU for multitasking.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nMemory\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nmanagement: allocates enough storage for programs to run.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nDevices\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nmanagement: detects and communicates with different devices\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAny OS should be good at the above fundamentals tasks.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAnother important feature of an OS is to provide an software interface layer,\r\n that hides away hardware interfaces, to interface with applications that\r\n run on top of that OS.\r\n The benefits of such a layer:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nreusability: that is, the same software API can be reused across programs,\r\n thus simplifying software development process\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nseparation of concerns: bugs appear either in application programs, or in\r\n the OS; a programmer needs to isolate where the bugs are.\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nsimplify software development process: provides an easier to use software\r\n interface layer with a uniform access to hardware resources across devices,\r\n instead of directly using the hardware interface of a particular device.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nHardware Abstraction Layer\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThere are so many hardware devices out there, so it's best to leave the\r\n hardware engineers how the devices talk to an OS.\r\n To achieve this goal, the OS only provides a set of agreed software interfaces\r\n between itself and the device driver writers and is called \r\n\\emph on\r\nHardware Abstraction\r\n\\emph default\r\n \r\n\\emph on\r\nLayer\r\n\\emph default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn C, this software interface is implemented through a structure function\r\n pointers.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n[illustrate with Linux example]\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nSystem programming interface \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\nSystem programming interfaces\r\n\\emph default\r\n are standard interfaces that an OS provides application programs to use\r\n its services.\r\n For example, if a program wishes to read a file on disk, then it must call\r\n a function like \r\n\\emph on\r\nopen()\r\n\\emph default\r\n and let the OS handle the details of talking to the hard disk for retrieving\r\n the file.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nThe need for an Operating System \r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn a way, OS is an overhead, but a necessary one, for a user to tell a computer\r\n what to do.\r\n When resources in a computer system (CPU, GPU, memory, hard drive...) became\r\n big and more complicated, it's tedious to manually manage all the resources.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nImagine we have to manually load programs on a computer with 3\r\n\\begin_inset space \\thinspace{}\r\n\\end_inset\r\n\r\nGB of RAM.\r\n We would have to load programs at various fix addresses, and for each program\r\n a size must be manually calculated to avoid wasting memory resource, and\r\n enough for programs to not overriding each other.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOr, when we want to give computer input through the keyboard, without an\r\n OS, an application also has to carry code to facilitate the communication\r\n with keyboard hardware; each application then handles such keyboard communicati\r\non on its own.\r\n Why should there be such duplications across applications for such standard\r\n feature? If you write an accounting software, why should a programmer concern\r\n writing a keyboard driver, totally irrelevant to the problem domain?\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nThat's why a crucial job of an OS is to hide the complexity of hardware\r\n devices, so a program is freed from the burden of maintaining its own code\r\n for hardware communication by having a standardized set of interfaces and\r\n thus, reduce potential bugs along with faster development time.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo write an OS effectively, a programmer need to understand well the underlying\r\n computer architecture that programmer are writing an OS for.\r\n The first reason is, many OS concepts are supported by the architecture\r\n e.g.\r\n the concepts of virtual memory are well supported by x86 architecture.\r\n If the underlying computer architecture is not well-understood, OS developers\r\n are doomed to reinvent it in your OS, and such software-implemented solutions\r\n run slower than the hardware version.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nDrivers\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nDrivers are programs that enable an OS to communicate and use features of\r\n hardware devices.\r\n For example, a keyboard driver enables an OS to get input from keyboard;\r\n or a network driver allows a network card to send and receive data packets\r\n to and from the Internet.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIf you only write application programs, you may wonder how can software\r\n control hardware devices? As mentioned in Chapter 2, through the hardware-softw\r\nare interface: by writing to a device's registers or to write to ports of\r\n a device, through the use of CPU's instructions.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nUserspace and kernel space\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\nKernel space\r\n\\emph default\r\n refers to the working environment of an OS that only the kernel can access.\r\n Kernel space includes the direct communication with hardware, or manipulate\r\n privileged memory regions (such as kernel code and data).\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nIn contrast, \r\n\\emph on\r\nuserspace\r\n\\emph default\r\n refers to less privileged processes that run above the OS, and is supervised\r\n by the OS.\r\n To access the kernel facility, user program must go through the standardized\r\n system programming interfaces provided by the OS.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nMemory Segment\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nSegment Descriptor\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nTypes of Segment Descriptors\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nCode and Data descriptors\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nTask Descriptor\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nInterrupt Descriptor\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nDescriptor Scope\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nGlobal Descriptor\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nLocal Descriptor\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nSegment Selector\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nEnhancement: Bootloader with descriptors\r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nProcess\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nConcepts\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nProcess\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nTask\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\ntask\r\n\\emph default\r\n is a unit of work that an OS needs to do, similar to how human have tasks\r\n to do daily.\r\n From a user point of view, a task for a computer to do can be web browsing,\r\n document editing, gaming, sending and receiving emails, etc.\r\n Since a CPU can only execute sequentially, one instruction after another\r\n (fetching from main memory), there must be some way to do many meaningful\r\n tasks at once.\r\n For that reason, the computer must share the resources e.g.\r\n registers, stack, memory, etc, between tasks, since we have many tasks\r\n but single and limited resources.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nProcess\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\nProcess\r\n\\emph default\r\n is a data structure that keeps track of the execution state of a task.\r\n Task is a general concept, and process is the implementation of a task.\r\n In a general-purpose OS, a task is usually a program.\r\n For example, when you run Firefox, a process structure is created to keep\r\n track of where the stack and the heap allocated for firefox are, where\r\n Firefox's code area is and which instruction EIP is holding to execute\r\n next, etc.\r\n The typical process structure looks like this:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n[insert process image]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nProcess is a virtual computer, but much more primitive than the virtual\r\n machine in virtualization software like Virtual Box, and that's a good\r\n thing.\r\n Imagine having to run a full-fledged virtual machine for every task; how\r\n wasteful of machine resources that would be..\r\n In the view of a running process, its code executes as if it runs directly\r\n on hardware.\r\n Each process has its own set of register values, which are kept tracked\r\n by the OS, and its own contiguous virtual memory space (which is discontiguous\r\n in actual physical memory).\r\n The code in a process is given virtual memory addresses to read and write\r\n from.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n[ illustrate: - a process looks like a mini Von Neumann - with contiguous\r\n memory, each with a color; each cell of a process mapped to distant memory\r\n cell in physical memory ]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA process can run so much until the OS tells it to temporary stop for other\r\n tasks to use the hardware resources.\r\n The suspended process can then wait until further notice from the OS.\r\n This whole switching process is so fast that a computer user think his\r\n computer actually runs tasks in parallel.\r\n The program that does the switching between tasks is called a *scheduler*.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nScheduler\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAn OS needs to perform a wide range of different functionalities, e.g.\r\n web browsing, document editing, gaming, etc.\r\n A \r\n\\emph on\r\nscheduler\r\n\\emph default\r\n decides which tasks got to run before the others and, for how long, in\r\n an efficient manner.\r\n Scheduler enables your computer to become a \r\n\\emph on\r\ntime sharing system\r\n\\emph default\r\n, because tasks share CPU execution time and no one process can monopolize\r\n the CPU (in practice, it still happens regularly).\r\n Without a scheduler, only a single task can be performed at a time.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nContext switch\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhen a process is prepared to be switched out for another process to take\r\n its place, certain hardware resources i.e.\r\n current open files, current register values, etc.\r\n must be backed up to later resume that process's execution.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nPriority\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\nPriority\r\n\\emph default\r\n is an important metric for OS to decide which task is scheduled to run\r\n before the others to allocate appropriate CPU execution time for each task.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nPreemptive vs Non-preemptive\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\npreemptive\r\n\\emph default\r\n OS can interrupt an executing process and switch to another process.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nA \r\n\\emph on\r\nnon-preemtive\r\n\\emph default\r\n OS, a task runs until its completion.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nProcess states\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\nState\r\n\\emph default\r\n is a particular condition of a process, triggered by an action from the\r\n scheduler.\r\n A process goes through various states during its life cycle.\r\n A process typically has these states:\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nRun indicating CPU is executing code in this process.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nSleep (or Suspended): indicating CPU is executing some process else.\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nDestroyed: process is done and waiting to be destroyed completely.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nprocfs\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nThreads\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\nThreads\r\n\\emph default\r\n are units of work inside a process that shares the execution environment.\r\n A process creates a whole new execution environment with code of its own:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n[illustration between process and thread, with each process is a big rectangle\r\n box and threads nested boxes point to different code region]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nInstead of creating a completely new process structure in memory, OS simply\r\n let the thread uses some of the resources of the parent process that created\r\n it.\r\n A thread has its own registers, program counter, stack pointer, and its\r\n own call stack.\r\n Everything else is shared between the threads, such as an address space,\r\n heap, static data, and code segments, and file descriptors.\r\n Because thread simply reuses existing resources and involve no context\r\n switching, it is much faster to create and switch between processes.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nHowever, note that the above scheme is just an implementation of thread\r\n concept.\r\n You can completely treat thread the same as process (hence you can call\r\n all processes threads and vice versa).\r\n Or you can just back up some resources, whlie leaving some resources shared.\r\n It's up to the OS designer to distinguish between threads and processes.\r\n Threads are usually implemented as a component of a process.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOn Linux, a thread is simply a process that shares resources with its parent\r\n process; for that reason, a Linux thread is also called *light-weight process*.\r\n Or put it another way, a thread in Linux is merely an implementation of\r\n a single-threaded process that execute its main program code.\r\n A multi-threaded program in Linux is just a process with shared with its\r\n single-threaded children processes, each points to different code region\r\n of its parent process.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n[TODO: turn the above table into a diagram]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nOn Windows, threads and processes are two separated entities, so the above\r\n description for Linux does not apply.\r\n However, the general idea: a thread shares the execution environment, holds.\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nTask: x86 concept of a process\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nTask Data Structure\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nTask State Segment\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nTask Descriptor\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nProcess Implementation\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nRequirements\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nMajor Plan\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nStage 1: Switch to a task from bootloader\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nStage 2: Switch to a task with one function from kernel\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nStage 3: Switch to a task with many functions from kernel\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nTo implement the concept of a process, a kernel needs to be able to save\r\n and restore its machine states for different tasks.\r\n \r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nDescription\r\n\\begin_inset Separator latexpar\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_deeper\r\n\\begin_layout Standard\r\n[Describe task switching mechanism involving LDT and GDT]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nqasdfasdf asd\r\n\\end_layout\r\n\r\n\\end_deeper\r\n\\begin_layout Description\r\nConstraints\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nDesign\r\n\\end_layout\r\n\r\n\\begin_layout Description\r\nImplementation\r\n\\begin_inset space ~\r\n\\end_inset\r\n\r\nplan\r\n\\end_layout\r\n\r\n\\begin_layout Section\r\nMilestone: Code Refactor\r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nInterrupt\r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nMemory management\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nAddress Space\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\nAddress space\r\n\\emph default\r\n is the set of all addressable memory locations.\r\n There are 2 types of address spaces in physical memory address:\r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nOne for memory: \r\n\\end_layout\r\n\r\n\\begin_layout Itemize\r\nOne for I/O:\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nEach process has its own address space to do whatever it wants, as long\r\n as the physical memory is not exhausted.\r\n This address space is called \r\n\\emph on\r\nvirtual memory\r\n\\emph default\r\n.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nVirtual Memory\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nPhysical memory is a contagious memory locations that has a simple mapping\r\n between a physical memory address and its corresponding location in memory,\r\n decoded by memory controller.\r\n On the other hand, *virtual memory* does not have direct mapping between\r\n a memory address and the corresponding physical memory location, even though\r\n it appears contagious from the view of an userspace program.\r\n Instead, virtual memory address is translated by OS into an actual physical\r\n memory address.\r\n For that reason, even addresses appear next to each other in virtual memory\r\n space, they are scattered through out the physical memory.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nWhy virtual memory is needed? Because virtual memory reduces the complexity\r\n of programming, by giving each program an illusion that it has its own\r\n separate \"physical\" memory to work with.\r\n Without virtual memory, programs must know and agree with each other their\r\n own memory regions to not accidentally destroy each other.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n[illustration a world without virtual memory]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nVirtual memory also enables a more secured OS, as application programs cannot\r\n manipulate main memory directly, so malicious programs won't cause havocs\r\n by destroying main memory and possibly hardware devices, by gaining access\r\n to hardware I/O ports.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\nAnother benefit is that virtual memory can extend beyond physical memory,\r\n by storing its data to hard disk.\r\n By swapping some of unused memory (i.e.\r\n inactive memory of a sleeping process), the system gains some free memory\r\n to continue running, so no data is destroyed.\r\n Otherwise, the OS is forced to kill a random user process to free up some\r\n memory, and you may lose unsaved work that belongs to the killed process.\r\n However, this process can significantly slow down the whole system because\r\n of Von Neumann bottleneck.\r\n In the old days, when memory was scarce, it was useful.\r\n\\end_layout\r\n\r\n\\begin_layout Chapter\r\nFile System\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\nFile system\r\n\\emph default\r\n is a mechanism on how raw bytes in a storage device can be meaningfully\r\n managed.\r\n That is, a group of bytes at specific locations in a storage device can\r\n be allocated for a purpose e.g.\r\n storing raw ASCII document, and later the exact chunks of bytes can be\r\n retrieved correctly.\r\n File system manages many such groups of bytes.\r\n It's helpful to think a file system as a database that maps between high\r\n level information and specific locations in a hard disk, similar to how\r\n business information is mapped to a specific row in a table.\r\n The high level information that is relevant to a file system is organized\r\n as *files* and *directories*.\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n[illustration between a file system and a database table to see how they\r\n are similar]\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\r\n\\emph on\r\nFile\r\n\\emph default\r\n is an entity that includes two components: metadata and the actual raw\r\n data.\r\n \r\n\\emph on\r\nMetadata\r\n\\emph default\r\n is the information describes the properties of the raw data associated\r\n with the file; raw data are real content of a file.\r\n \r\n\\emph on\r\nDirectory\r\n\\emph default\r\n is a file that holds a group of files and also child directories.\r\n Together, they create a file hierarchy system as commonly seen in Windows\r\n or Linux.\r\n\\end_layout\r\n\r\n\\begin_layout Subsection\r\nExample: Ex2 filesystem\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\nbookmarksetup{startatroot} % this is it \r\n\\end_layout\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\naddtocontents{toc}{\r\n\\backslash\r\nbigskip}% perhaps as well\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset CommandInset index_print\r\nLatexCommand printindex\r\ntype \"idx\"\r\nname \"Index\"\r\nliteral \"true\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Chapter*\r\nBibliography\r\n\\emph on\r\n\r\n\\begin_inset ERT\r\nstatus open\r\n\r\n\\begin_layout Plain Layout\r\n\r\n\r\n\\backslash\r\naddcontentsline{toc}{chapter}{Biblography}\r\n\\end_layout\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\begin_layout Standard\r\n\\begin_inset CommandInset bibtex\r\nLatexCommand bibtex\r\nbtprint \"btPrintCited\"\r\nbibfiles \"references\"\r\noptions \"plainnat\"\r\n\r\n\\end_inset\r\n\r\n\r\n\\end_layout\r\n\r\n\\end_body\r\n\\end_document\r\n"
  },
  {
    "path": "book_src/Operating Systems From 0 to 1.txt",
    "content": "﻿Operating Systems:\r\nFrom 0 to 1\r\n\r\nTu, Do Hoang\r\n\r\n\r\n\r\n\r\n\r\n\r\nTable of Contents\r\n\r\n    Preface\r\n        Why another book on Operating Systems?\r\n        Prerequisites\r\n        What you will learn in this book \r\n        What this book is not about\r\n        The organization of the book\r\n        Acknowledgments\r\nPart I Preliminary\r\n    Domain documents\r\n        Problem domains\r\n        Documents for implementing a problem dom\r\n            Software Requirement Document\r\n            Software Specification\r\n        Documents for writing an x86 Operating S\r\n        The physical implementation of a bit\r\n            MOSFET transistors\r\n        Beyond transistors: digital logic gates\r\n            The theory behind logic gates\r\n            Logic Gate implementation: CMOS circuit\r\n        Beyond Logic Gates: Machine Language\r\n            Machine language\r\n            Assembly Language\r\n            Programming Languages\r\n        Abstraction\r\n            Why abstraction works\r\n            Why abstraction reduces complexity\r\n    Computer Architecture\r\n        What is a computer?\r\n            Server\r\n            Desktop Computer\r\n            Mobile Computer\r\n            Game Consoles\r\n            Embedded Computer\r\n            Field Gate Programmable Array\r\n            Application-Specific Integrated Circuit\r\n        Computer Architecture\r\n            Instruction Set Architecture\r\n            Computer organization\r\n            Hardware\r\n        x86 architecture\r\n        Intel Q35 Chipset\r\n        x86 Execution Environment\r\n    x86 Assembly and C\r\n        objdump\r\n        Reading the output\r\n        Intel manuals\r\n        Experiment with assembly code \r\n        Anatomy of an Assembly Instruction\r\n        Understand an instruction in detail\r\n        Example: jmp instruction\r\n        Examine compiled data\r\n            Fundamental data types\r\n            Pointer Data Types\r\n            Bit Field Data Type\r\n            String Data Types\r\n        Examine compiled code\r\n            Data Transfer\r\n            Expressions\r\n            Stack\r\n            Automatic variables\r\n            Function Call and Return\r\n            Loop\r\n            Conditional\r\n    The Anatomy of a Program\r\n        Reference documents: \r\n        ELF header\r\n        Section header table\r\n        Understand Section in-depth\r\n        Program header table\r\n        Segments vs sections\r\n    Runtime inspection and debug\r\n        A sample program\r\n        Static inspection of a program\r\n            Command: info target/info file/info file\r\n            Command: maint info sections\r\n            Command: info functions\r\n            Command: info variables\r\n            Command: disassemble/disas\r\n            Command: x\r\n            Command: print/p\r\n        Runtime inspection of a program\r\n            Command: run\r\n            Command: break/b\r\n            Command: next/n\r\n            Command: step/s\r\n            Command: ni\r\n            Command: si\r\n            Command: until\r\n            Command: finish\r\n            Command: bt\r\n            Command: up\r\n            Command: down\r\n            Command: info registers\r\n        How debuggers work: A brief introduction\r\n            How breakpoints work\r\n            Single stepping\r\n            How a debugger understands high level so\r\nPart II Groundwork \r\n    Bootloader\r\n        x86 Boot Process\r\n        Using BIOS services\r\n        Boot process\r\n        Example Bootloader\r\n        Compile and load\r\n            Debugging\r\n        Loading a program from bootloader\r\n            Floppy Disk Anatomy\r\n            Read and load sectors from a floppy disk\r\n        Improve productivity with scripts\r\n            Automate build with GNU Make\r\n            GNU Make Syntax summary\r\n            Automate debugging steps with GDB script\r\n    Linking and loading on bare metal\r\n        Understand relocations with readelf\r\n            Offset \r\n            Info \r\n            Type \r\n            Sym.Value \r\n            Sym. Name\r\n        Crafting ELF binary with linker scripts\r\n            Example linker script\r\n            Understand the custom ELF structure\r\n            Manipulate the program segments\r\n        C Runtime: Hosted vs Freestanding\r\n        Debuggable bootloader on bare metal\r\n        Debuggable program on bare metal\r\n            Loading an ELF binary from a bootloader\r\n            Debugging the memory layout\r\n            Testing the new binary\r\nPart III Kernel Programming\r\n    x86 Descriptors\r\n        Basic operating system concepts\r\n            Hardware Abstraction Layer\r\n            System programming interface \r\n            The need for an Operating System \r\n        Drivers\r\n        Userspace and kernel space\r\n        Memory Segment\r\n        Segment Descriptor\r\n        Types of Segment Descriptors\r\n            Code and Data descriptors\r\n            Task Descriptor\r\n            Interrupt Descriptor\r\n        Descriptor Scope\r\n            Global Descriptor\r\n            Local Descriptor\r\n        Segment Selector\r\n        Enhancement: Bootloader with descriptors\r\n    Process\r\n        Concepts\r\n        Process\r\n            Task\r\n            Process\r\n            Scheduler\r\n            Context switch\r\n            Priority\r\n            Preemptive vs Non-preemptive\r\n            Process states\r\n            procfs\r\n        Threads\r\n        Task: x86 concept of a process\r\n        Task Data Structure\r\n            Task State Segment\r\n            Task Descriptor\r\n        Process Implementation\r\n            Requirements\r\n            Major Plan\r\n            Stage 1: Switch to a task from bootloade\r\n            Stage 2: Switch to a task with one funct\r\n            Stage 3: Switch to a task with many func\r\n        Milestone: Code Refactor\r\n    Interrupt\r\n    Memory management\r\n            Address Space\r\n            Virtual Memory\r\n    File System\r\n            Example: Ex2 filesystem\r\n    Bibliography\r\n\r\n\r\n\r\n\r\nPreface\r\n\r\n\r\n\r\nGreetings!\r\n\r\nYou've probably asked yourself at least once how an operating \r\nsystem is written from the ground up. You might even have years \r\nof programming experience under your belt, yet your understanding \r\nof operating systems may still be a collection of abstract \r\nconcepts not grounded in actual implementation. To those who've \r\nnever built one, an operating system may seem like magic: a \r\nmysterious thing that can control hardware while handling a \r\nprogrammer's requests via the API of their favorite programming \r\nlanguage. Learning how to build an operating system seems \r\nintimidating and difficult; no matter how much you learn, it \r\nnever feels like you know enough. You're probably reading this \r\nbook right now to gain a better understanding of operating \r\nsystems to be a better software engineer.\r\n\r\nIf that is the case, this book is for you. By going through this \r\nbook, you will be able to find the missing pieces that are \r\nessential and enable you to implement your own operating system \r\nfrom scratch! Yes, from scratch, without going through any \r\nexisting operating system layer to prove to yourself that you are \r\nan operating system developer. You may ask,“Isn't it more \r\npractical to learn the internals of Linux?”.\r\n\r\nYes...\r\n\r\nand no.\r\n\r\nLearning Linux can help your workflow at your day job. However, \r\nif you follow that route, you still won't achieve the ultimate \r\ngoal of writing an actual operating system. By writing your own \r\noperating system, you will gain knowledge that you will not be \r\nable to glean just from learning Linux.\r\n\r\nHere's a list of some benefits of writing your own OS:\r\n\r\n• You will learn how a computer works at the hardware level, and \r\n  you will learn to write software to manage that hardware \r\n  directly.\r\n\r\n• You will learn the fundamentals of operating systems, allowing \r\n  you to adapt to any operating system, not just Linux\r\n\r\n• To hack on Linux internals suitably, you'll need to write at \r\n  least one operating system on your own. This is just like \r\n  applications programming: to write a large application, you'll \r\n  need to start with simple ones.\r\n\r\n• You will open pathways to various low-level programming domains \r\n  such as reverse engineering, exploits, building virtual \r\n  machines, game console emulation and more. Assembly language \r\n  will become one of your most indispensable tools for low-level \r\n  analysis. (But that does not mean you have to write your \r\n  operating system in Assembly!)\r\n\r\n• Writing an operating system is fun!\r\n\r\n  Why another book on Operating Systems?\r\n\r\nThere are many books and courses on this topic made by famous \r\nprofessors and experts out there already. Who am I to write a \r\nbook on such an advanced topic? While it's true that many quality \r\nresources exist, I find them lacking. Do any of them show you how \r\nto compile your C code and the C runtime library independent of \r\nan existing operating system? Most books on operating system \r\ndesign and implementation only discuss the software side; how the \r\noperating system communicates with the hardware is skipped. \r\nImportant hardware details are skipped, and it's difficult for a \r\nself-learner to find relevant resources on the Internet. The aim \r\nof this book is to bridge that gap: not only will you learn how \r\nto program hardware directly, but also how to read official \r\ndocuments from hardware vendors to program it. You no longer have \r\nto seek out resources to help yourself interpret hardware manuals \r\nand documentation: you can do it yourself. Lastly, I wrote this \r\nbook from an autodidact's perspective. I made this book as \r\nself-contained as possible so you can spend more time learning \r\nand less time guessing or seeking out information on the \r\nInternet.\r\n\r\nOne of the core focuses of this book is to guide you through the \r\nprocess of reading official documentation from vendors to \r\nimplement your software. Official documents from hardware vendors \r\nlike Intel are critical for implementing an operating system or \r\nany other software that directly controls the hardware. At a \r\nminimum, an operating system developer needs to be able to \r\ncomprehend these documents and implement software based on a set \r\nof hardware requirements. Thus, the first chapter is dedicated to \r\ndiscussing relevant documents and their importance.\r\n\r\nAnother distinct feature of this book is that it is “Hello World” \r\ncentric. Most examples revolve around variants of a “Hello World” \r\nprogram, which will acquaint you with core concepts. These \r\nconcepts must be learned before attempting to write an operating \r\nsystem. Anything beyond a simple “Hello World” example gets in \r\nthe way of teaching the concepts, thus lengthening the time spent \r\non getting started writing an operating system. \r\n\r\nLet's dive in. With this book, I hope to provide enough \r\nfoundational knowledge that will open doors for you to make sense \r\nof other resources. This book will be especially beneficial to \r\nstudents who've just finished their first C/C++ course. Imagine \r\nhow cool it would be to show prospective employers that you've \r\nalready built an operating system.\r\n\r\n  Prerequisites\r\n\r\n• Basic knowledge of circuits\r\n\r\n  – Basic Concepts of Electricity: atoms, electrons, proton, \r\n    neutron, current flow.\r\n\r\n  – Ohm's law\r\n\r\n  If you are unfamiliar with these concepts, you can quickly \r\n  learn them here: http://www.allaboutcircuits.com/textbook/, by \r\n  reading chapter 1 and chapter 2.\r\n\r\n• C programming. In particular:\r\n\r\n  – Variable and function declarations/definitions\r\n\r\n  – While and for loops\r\n\r\n  – Pointers and function pointers\r\n\r\n  – Fundamental algorithms and data structures in C\r\n\r\n• Linux basics: \r\n\r\n  – Know how to navigate directory with the command line\r\n\r\n  – Know how to invoke a command with options\r\n\r\n  – Know how to pipe output to another program\r\n\r\n• Touch typing. Since we are going to use Linux, touch typing \r\n  helps. I know typing speed does not relate to problem-solving, \r\n  but at least your typing speed should be fast enough to not let \r\n  it get in the way and degrade the learning experience.\r\n\r\nIn general, I assume that the reader has basic C programming \r\nknowledge, and can use an IDE to build and run a program.\r\n\r\n  What you will learn in this book \r\n\r\n• How to write an operating system from scratch by reading \r\n  hardware datasheets. In the real world, you will not be able to \r\n  consult Google for a quick answer. \r\n\r\n• Write code independently. It's pointless to copy and paste \r\n  code. Real learning happens when you solve problems on your \r\n  own. Some examples are provided to help kick start your work, \r\n  but most problems are yours to conquer. However, the solutions \r\n  are available online for you after giving a good try.\r\n\r\n• A big picture of how each layer of a computer related to each \r\n  other, from hardware to software.\r\n\r\n• How to use Linux as a development environment and common tools \r\n  for low-level programming.\r\n\r\n• How a program is structured so that an operating system can \r\n  run.\r\n\r\n• How to debug a program running directly on hardware with gdb \r\n  and QEMU.\r\n\r\n• Linking and loading on bare metal x86_64, with pure C. No \r\n  standard library. No runtime overhead.\r\n\r\n  What this book is not about\r\n\r\n• Electrical Engineering: The book discusses some concepts from \r\n  electronics and electrical engineering only to the extent of \r\n  how software operates on bare metal.\r\n\r\n• How to use Linux or any OS types of books: Though Linux is used \r\n  as a development environment and as a medium to demonstrate \r\n  high-level operating system concepts, it is not the focus of \r\n  this book.\r\n\r\n• Linux Kernel development: There are already many high-quality \r\n  books out there on this subject.\r\n\r\n• Operating system books focused on algorithms: This book focuses \r\n  more on actual hardware platform - Intel x86_64 - and how to \r\n  write an OS that utilizes of OS support from the hardware \r\n  platform.\r\n\r\n  The organization of the book\r\n\r\n  Part 1 provides a foundation for learning operating system.\r\n\r\n  • Chapter 1 briefly explains the importance of domain \r\n    documents. Documents are crucial for the learning experience, \r\n    so they deserve a chapter.\r\n\r\n  • Chapter 2 explains the layers of abstractions from hardware \r\n    to software. The idea is to provide insight into how code \r\n    runs physically.\r\n\r\n  • Chapter 3 provides the general architecture of a computer, \r\n    then introduces a sample computer model that you will use to \r\n    write an operating system.\r\n\r\n  • Chapter 4 introduces the x86 assembly language through the \r\n    use of the Intel manuals, along with commonly used \r\n    instructions. This chapter gives detailed examples of how \r\n    high-level syntax corresponds to low-level assembly, enabling \r\n    you to read generated assembly code comfortably. It is \r\n    necessary to read assembly code when debugging an operating \r\n    system.\r\n\r\n  • Chapter 5 dissects ELF in detail. Only by understanding how \r\n    the structure of a program at the binary level, you can build \r\n    one that runs on bare metal.\r\n\r\n  • Chapter 6 introduces gdb debugger with extensive examples for \r\n    commonly used commands. After acquainting the reader with \r\n    gdb, it then provides insight on how a debugger works. This \r\n    knowledge is essential for building a debuggable program on \r\n    the bare metal. \r\n\r\n  Part 2 presents how to write a bootloader to bootstrap a \r\n  kernel. Hence the name “Groundwork”. After mastering this part, \r\n  the reader can continue with the next part, which is a guide \r\n  for writing an operating system. However, if the reader does not \r\n  like the presentation, he or she can look elsewhere, such as \r\n  the OSDev Wiki: http://wiki.osdev.org/. \r\n\r\n  • Chapter 7 introduces what the bootloader is, how to write one \r\n    in assembly, and how to load it on QEMU, a hardware emulator. \r\n    This process involves typing repetitive and long commands, so \r\n    GNU Make is applied to improve productivity by automating the \r\n    repetitive parts and simplifying the interaction with the \r\n    project. This chapter also demonstrates the use of GNU Make \r\n    in context.\r\n\r\n  • Chapter 8 introduces linking by explaining the relocation \r\n    process when combining object files. In addition to a \r\n    bootloader and an operating system written in C, this is the \r\n    last piece of the puzzle required for building debuggable \r\n    programs on bare metal, including the bootloader written in \r\n    Assembly and an operating system written in C.\r\n\r\n  Part 3 provides guidance on how to write an operating system, \r\n  as you should implement an operating system on your own and be \r\n  proud of your creation. The guidance consists of simpler and \r\n  coherent explanations of necessary concepts, from hardware to \r\n  software, to implement the features of an operating system. \r\n  Without such guidance, you will waste time gathering \r\n  information spread through various documents and the Internet. \r\n  It then provides a plan on how to map the concepts to code.\r\n\r\n  Acknowledgments\r\n\r\nThank you, my beloved family. Thank you, the contributors.\r\n\r\n\r\n\r\nPreliminary\r\n\r\nDomain documents\r\n\r\n  Problem domains\r\n\r\nIn the real world, software engineering is not only focused on \r\nsoftware, but also the problem domain it is trying to solve. \r\n\r\nA problem domain[margin:\r\nproblem domain\r\n]problem domain is the part of the world where the computer is to \r\nproduce effects, together with the means available to produce \r\nthem, directly or indirectly. (Kovitz, 1999)\r\n\r\nA problem domainproblem domain is anything outside of programming \r\nthat a software engineer needs to understand to produce correct \r\ncode that can achieve the desired effects. “Directly” means \r\ninclude anything that the software can control to produce the \r\ndesired effects, e.g. keyboards, printers, monitors, other \r\nsoftware... “Indirectly” means anything not part of the software \r\nbut relevant to the problem domain e.g. appropriate people to be \r\ninformed by the software when some event happens, students that \r\nmove to correct classrooms according to the schedule generated by \r\nthe software. To write a finance application, a software engineer \r\nneeds to learn sufficient finance concepts to understand the [margin:\r\nrequirements\r\n]requirementsrequirements of a customer and implement such \r\nrequirements, correctly.\r\n\r\nRequirements are the effects that the machine is to exert in the \r\nproblem domain by virtue of its programming.\r\n\r\nProgramming alone is not too complicated; programming to solve a \r\nproblem domain, is [footnote:\r\nWe refer to the concept of “programming” here as someone able to \r\nwrite code in a language, but not necessary know any or all \r\nsoftware engineering knowledge.\r\n]. Not only a software engineer needs to understand how to \r\nimplement the software, but also the problem domain that it tries \r\nto solve, which might require in-depth expert knowledge. The \r\nsoftware engineer must also select the right programming \r\ntechniques that apply to the problem domain he is trying to \r\nsolve because many techniques that are effective in one domain \r\nmight not be in another. For example, many types of applications \r\ndo not require performant written code, but a short time to \r\nmarket. In this case, interpreted languages are widely popular \r\nbecause it can satisfy such need. However, for writing huge 3D \r\ngames or operating system, compiled languages are dominant \r\nbecause it can generate the most efficient code required for such \r\napplications.\r\n\r\nOften, it is too much for a software engineer to learn \r\nnon-trivial domains (that might require a bachelor degree or \r\nabove to understand the domains). Also, it is easier for a domain expert\r\ndomain expert to learn enough programming to break down the \r\nproblem domain into parts small enough for the software engineers \r\nto implement. Sometimes, domain experts implement the software \r\nthemselves.\r\n\r\n[float Figure:\r\n\r\n\r\n[Figure 0.1:\r\nProblem domains: Software and Non-software.\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/01/domains_general.pdf>\r\n     \r\n]\r\n\r\nOne example of such scenario is the domain that is presented in \r\nthis book: operating system. A certain amount of electrical \r\nengineering (EE) knowledge is required to implement an operating \r\nsystem. If a computer science (CS) curriculum that does not \r\ninclude minimum EE courses, students in the curriculum have \r\nlittle chance to implement a working operating system. Even if \r\nthey can implement one, either they need to invest a significant \r\namount of time to study on their own, or they fill code in a \r\npredefined framework just to understand high-level algorithms. \r\nFor that reason, EE students have an easier time to implement an \r\nOS, as they only need to study a few core CS courses. In fact, \r\nonly “C programming” and “Algorithms and Data Structures” classes \r\nare usually enough to get them started writing code for device \r\ndrivers, and later generalize it into an operating system.\r\n\r\n[float Figure:\r\n\r\n\r\n[Figure 0.2:\r\nOperating System domain.\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/01/domains_os_example.pdf>\r\n     \r\n]\r\n\r\nOne thing to note is that software is its own problem domain. A \r\nproblem domain does not necessarily divide between software and \r\nitself. Compilers, 3D graphics, games, cryptography, artificial \r\nintelligence, etc., are parts of software engineering domains \r\n(actually it is more of a computer science domain than a software \r\nengineering domain). In general, a software-exclusive domain \r\ncreates software to be used by other software. Operating System \r\nis also a domain, but is overlapped with other domains such as \r\nelectrical engineering. To effectively implement an operating \r\nsystem, it is required to learn enough of the external domain. \r\nHow much learning is enough for a software engineer? At the \r\nminimum, a software engineer should be knowledgeable enough to \r\nunderstand the documents prepared by hardware engineers for using \r\n(i.e. programming) their devices.\r\n\r\nLearning a programming language, even C or Assembly, does not \r\nmean a software engineer can automatically be good at hardware \r\nprogramming or any related low-level programming domains. One can \r\nspend 10 years, 20 years or his entire life writing C/C++ code, \r\nand he still cannot write an operating system, simply because of \r\nthe ignorance of relevant domain knowledge. Just like learning \r\nEnglish does not mean a person automatically becomes good at \r\nreading Math books written in English. Much more than that is \r\nneeded. Knowing one or two programming languages is not enough. \r\nIf a programmer writes software for a living, he had better be \r\nspecialized in one or two problem domains outside of software if \r\nhe does not want his job taken by domain experts who learn \r\nprogramming in their spare time.\r\n\r\n  Documents for implementing a problem domain\r\n\r\nDocuments are essential for learning a problem domain (and \r\nactually, anything) since information can be passed down in a \r\nreliable way. It is evident that this written text has been used \r\nfor thousands of years to pass knowledge from generation to \r\ngeneration. Documents are integral parts of non-trivial \r\nprojects. Without the documents:\r\n\r\n• New people will find it much harder to join a project.\r\n\r\n• It is harder to maintain a project because people may forget \r\n  important unresolved bugs or quirks in their system.\r\n\r\n• It is challenging for customers to understand the product they \r\n  are going to use. However, documents do not need to be written \r\n  in book format. It can be anything from HTML format to database \r\n  format to be displayed by a graphical user interface. Important \r\n  information must be stored somewhere safe, readily accessible.\r\n\r\nThere are many types of documents. However, to facilitate the \r\nunderstanding of a problem domain, these two documents need to be \r\nwritten: software requirement document and software \r\nspecification. \r\n\r\n  Software Requirement Document\r\n\r\nSoftware requirement document[margin:\r\nSoftware requirement\r\n]Software requirement document includes both a list of \r\nrequirements and a description of the problem domain (Kovitz, 1999)\r\n.\r\n\r\nA software solves a business problem. But, which problems to \r\nsolve, are requested by a customer. Many of these requests make a \r\nlist of requirements that our software needs to fulfill. However, \r\nan enumerated list of features is seldom useful in delivering \r\nsoftware. As stated in the previous section, the tricky part is \r\nnot programming alone but programming according to a problem \r\ndomain. The bulk of software design and implementation depends \r\nupon the knowledge of the problem domain. The better understood \r\nthe domain, the higher quality software can be. For example, \r\nbuilding a house is practiced over thousands of years and is well \r\nunderstood, and it is easy to build a high-quality house; \r\nsoftware is no different. Code that is difficult to understand \r\nis usually due to the author's ignorance of a problem domain. In\r\nthe context of this book, we seek to understand the low-level \r\nworking of various hardware devices.\r\n\r\nBecause software quality depends upon an understanding of the \r\nproblem domain, a software requirement document should always \r\ninclude a description of the problem domain.\r\n\r\nBe aware that software requirements are not:\r\n\r\n  What vs How\r\n\r\n  “what” and “how” are vague terms. What is the “what”? Is it \r\n  nouns only? If so, what if a customer requires his software to \r\n  perform specific steps of operations, such as purchasing \r\n  procedure for a customer on a website. Does it include “verbs” \r\n  now? However, isn't the “how” supposed to be step by step \r\n  operations? Anything can be the “what” and anything can be the “\r\n  how”. \r\n\r\n  Sketches\r\n\r\n  Software requirement document is all about the problem domain. \r\n  It should not be a high-level description of an implementation. \r\n  Some problems might seem straightforward to map directly from \r\n  its domain description to the structure of an implementation. \r\n  For example:\r\n\r\n  • Users are given a list of books in a drop-down menu to \r\n    choose. \r\n\r\n  • Books are stored in a linked list”. \r\n\r\n  • ...\r\n\r\n  In the future, instead of a drop-down menu, all books are \r\n  listed directly on a page in thumbnails. Books might be \r\n  reimplemented as a graph, and each node is a book for finding \r\n  related books, as a recommender is going to be added in the \r\n  next version. The requirement document needs updating again to \r\n  remove all the outdated implementation details, thus required \r\n  additional efforts to maintain the requirement document, and \r\n  when the effort for syncing with the implementation is too \r\n  much, the developers give up documentation, and everyone starts \r\n  ranting how useless documentation is. \r\n\r\n  More often than not there is no straightforward one-to-one \r\n  mapping. For example, a regular computer user expects an OS to\r\n  be something that runs some program with GUI, or their favorite \r\n  computer games. But for such requirements, an operating system \r\n  is implemented as multiple layers, each hiding the details from \r\n  the upper layers. To implement an operating system, a large \r\n  body of knowledge from multiple fields is required, especially \r\n  if the operating system runs on non-PC devices. \r\n\r\n  It's best to include informat related to the problem domain in \r\n  the requirement document. A good way to test the quality of \r\n  a requirement document is to provide it to a domain expert \r\n  for proofreading, to ensure he can understand the material. \r\n  thoroughly. A requirement document is also useful as a help document later, \r\n  or for writing one much easier.\r\n\r\n  Software Specification\r\n\r\nSoftware specification[margin:\r\nSoftware specification\r\n]Software specification document states rules relating desired \r\nbehavior of the output devices to all possible behavior of the \r\ninput devices, as well as any rules that other parts of the \r\nproblem domain must obey.Kovitz (1999)\r\n\r\nSimply put, software specification is interface design, with \r\nconstraints for the problem domain to follow e.g. the software \r\ncan accept certain types of input such as the software is \r\ndesigned to accept English but no other language. For a hardware \r\ndevice, a specification is always needed, as software depends on \r\nits hardwired behaviors. And in fact, it is mostly the case that \r\nhardware specifications are well-defined, with the tiniest \r\ndetails in it. It needs to be that way because once hardware is \r\nphysically manufactured, there's no going back, and if defects \r\nexist, it's a devastating damage to the company on both finance \r\nand reputation.\r\n\r\nNote that, similar to a requirement document, a specification \r\nonly concerns interface design. If implementation details leak \r\nin, it is a burden to sync between the actual implementation and \r\nthe specification, and soon to be abandoned.\r\n\r\nAnother important remark is that, though a specification document \r\nis important, it does not have to be produced before the \r\nimplementation. It can be prepared in any order: before or after \r\na complete implementation; or at the same time with the \r\nimplementation, when some part is done, and the interface is \r\nready to be recorded in the specification. Regardless of methods, \r\nwhat matter is a complete specification at the end.\r\n\r\n  Documents for writing an x86 Operating System\r\n\r\nWhen problem domain is different from software domain, \r\nrequirement document and specification are usually separated. \r\nHowever, if the problem domain is inside software, specification \r\nmost often includes both, and content of both can be mixed with \r\neach other. As demonstrated by previous sections the importance \r\nof documents, to implement an OS, we will need to collect \r\nrelevant documents to gain sufficient domain knowledge. These \r\ndocuments are as follow:\r\n\r\n• Intel® 64 and IA-32 Architectures Software Developer’s Manual \r\n  (Volume 1, 2, 3)\r\n\r\n• Intel® 3 Series Express Chipset Family Datasheet\r\n\r\n• System V Application Binary Interface\r\n\r\nAside from the Intel's official website, the website of this book \r\nalso hosts the documents for convenience[footnote:\r\nIntel may change the links to the documents as they update their \r\nwebsite, so this book doesn't contain any link to the documents \r\nto avoid confusion for readers.\r\n].\r\n\r\nIntel documents divide the requirement and specification sections \r\nclearly, but call the sections with different names. The \r\ncorresponding to the requirement document is a section called “\r\nFunctional Description”, which consists mostly of domain \r\ndescription; for specification, “Register Description” section \r\ndescribes all programming interfaces. Both documents carry no \r\nunnecessary implementation details[footnote:\r\nAs it should be, those details are trade secret.\r\n]. Intel documents are also great examples of how to write well \r\nrequirements/specifications, as explained in this chapter.\r\n\r\nOther than the Intel documents, other documents will be \r\nintroduced in the relevant chapters.\r\n\r\n\r\n\r\nThis chapter gives an intuition on how hardware and software \r\nconnected together, and how software is represented physically.\r\n\r\n  The physical implementation of a bit\r\n\r\nAll electronic devices, from simple to complex, manipulate this \r\nflow to achieve desired effects in the real world. Computers are \r\nno exception. When we write software, we indirectly manipulate \r\nelectrical current at the physical level, in such a way that the \r\nunderlying machine produces desired effects. To understand the \r\nprocess, we consider a simple light bulb. A light bulb can change \r\ntwo states between on and off with a switch, periodically: an off \r\nmeans number 0, and an on means 1.[float MarginFigure:\r\n[MarginFigure 1:\r\nA lightbulb\r\n]\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/02/bulb.svg>\r\n]\r\n\r\nHowever, one problem is that such a switch requires manual \r\nintervention from a human. What is required is an automatic \r\nswitch based on the voltage level, as described above. To enable \r\nautomatic switching of electrical signals, a device called \r\ntransistor, invented by William Shockley, John Bardeen and Walter \r\nBrattain. This invention started the whole computer industry. \r\n\r\nAt the core, a [margin:\r\ntransistor\r\n]transistortransistor is just a resistor whose values can vary \r\nbased on an input voltage value[float MarginFigure:\r\n[MarginFigure 2:\r\nModern transistor\r\n]\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/02/transistor.svg>\r\n]. With this property, a transistor can be used as a current \r\namplifier (more voltage, less resistance) or switch electrical \r\nsignals off and on (block and unblock an electron flow) based on \r\na voltage level. At 0 v, no current can pass through a \r\ntransistor, thus it acts like a circuit with an open switch \r\n(light bulb off) because the resistor value is enough to block \r\nthe electrical flow. Similarly, at +3.5 v, current can flow \r\nthrough a transistor because the resistor value is lessened, \r\neffectively enables electron flow, thus acts like a circuit with \r\na closed switch.[margin:\r\nIf you want a deeper explanation of transistors e.g. how \r\nelectrons move, you should look at the video “How semiconductors \r\nwork” on Youtube, by Ben Eater.\r\n]\r\n\r\nA bit has two states: 0 and 1, which is the building block of all \r\ndigital systems and software. Similar to a light bulb that can be \r\nturned on and off, bits are made out of this electrical stream \r\nfrom the power source: Bit 0 are represented with 0 v (no \r\nelectron flow), and bit 1 is +3.5 v to +5 v (electron flow). \r\nTransistor implements a bit correctly, as it can regulate the \r\nelectron flow based on voltage level. \r\n\r\n  MOSFET transistors\r\n\r\nThe classic transistors invented open a whole new world of micro \r\ndigital devices. Prior to the invention, vacuum tubes - which are \r\njust fancier light bulbs - were used to present 0 and 1, and \r\nrequired human to turn it on and off. [margin:\r\nMOSFET\r\n]MOSFETMOSFET, or Metal–Oxide–Semiconductor Field-Effect \r\nTransistor, invented in 1959 by Dawon Kahng and Martin M. (John) \r\nAtalla at Bell Labs, is an improved version of classic \r\ntransistors that is more suitable for digital devices, as it \r\nrequires shorter switching time between two states 0 and 1, more \r\nstable, consumes less power and easier to produce.\r\n\r\nThere are also two types of MOSFETs analogous to two types of \r\ntransistors: n-MOSFET and p-MOSFET. n-MOSFET and p-MOSFET are \r\nalso called NMOS and PMOS transistors for short.\r\n\r\n  Beyond transistors: digital logic gates\r\n\r\nAll digital devices are designed with logic gates. A logic gate[margin:\r\nlogic gate\r\n]logic gate is a device that implements a boolean function. Each \r\nlogic gate includes a number of inputs and an output. All \r\ncomputer operations are built from the combinations of logic \r\ngates, which are just combinations of boolean functions. [float MarginFigure:\r\n[MarginFigure 3:\r\nExample: NAND gate\r\n]\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/02/Nand-gate.svg>\r\n\r\n\r\n]\r\n\r\n  The theory behind logic gates\r\n\r\nLogic gates accept only binary inputs[footnote:\r\nInput that is either a 0 or 1.\r\n] and produce binary outputs. In other words, logic gates are \r\nfunctions that transform binary values. Fortunately, a branch of \r\nmath that deals exclusively with binary values already existed, \r\ncalled Boolean Algebra, developed in the 19[superscript:th]century by George Boole. With a sound mathematical theory as a \r\nfoundation logic gates were created. As logic gates implement \r\nBoolean functions, a set of Boolean functions is functionally complete\r\n[margin:\r\nfunctionally complete\r\n]functionally complete, if this set can construct all other \r\nBoolean functions can be constructed from. Later, Charles Sanders \r\nPeirce (during 1880 -- 1881) proved that either Boolean function \r\nof NOR or NAND alone is enough to create all other Boolean logic \r\nfunctions. Thus NOR and NAND gates are functionally complete Peirce (1933)\r\n. Gates are simply the implementations of Boolean logic \r\nfunctions, therefore NAND or NOR gate is enough to implement all \r\nother logic gates. The simplest gates CMOS circuit can implement \r\nare inverters (NOT gates) and from the inverters, comes NAND \r\ngates. With NAND gates, we are confident to implement everything \r\nelse. This is why the inventions of transistors, then CMOS \r\ncircuit revolutionized computer industry.[margin:\r\nIf you want to understand why and how from NAND gate we can \r\ncreate all Boolean functions and a computer, I suggest the course \r\nBuild a Modern Computer from First Principles: From Nand to \r\nTetris available on Coursera: https://www.coursera.org/learn/build-a-computer\r\n. Go even further, after the course, you should take the series \r\nComputational Structures on Edx.\r\n]\r\n\r\nWe should realize and appreciate how powerful boolean functions \r\nare available in all programming languages.\r\n\r\n  Logic Gate implementation: CMOS circuit\r\n\r\nUnderlying every logic gate is a circuit called [margin:\r\nCMOS\r\n]CMOSCMOS - Complementary MOSFET. CMOS consists of two \r\ncomplementary transistors, NMOS and PMOS. The simplest CMOS \r\ncircuit is an inverter or a NOT gate:\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nFrom NOT gate, a NAND gate can be created:\r\n\r\n\r\n\r\nFrom NAND gate, we have all other gates. As demonstrated, such a \r\nsimple circuitry performs the logical operators in day-to-day \r\nprogram languages e.g. NOT operator ~ is executed directly by an \r\ninverter circuit, and operator & is executed by an AND circuit \r\nand so on. Code does not run on a magic black box. In contrast, \r\ncode execution is precise and transparent, often as simple as \r\nrunning some hardwired circuit. When we write software, we simply \r\nmanipulate electrical current at the physical level to run \r\nappropriate circuits to produce desired outcomes. However, this \r\nwhole process somehow does not relate to any thought involving \r\nelectrical current. That is the real magic and will be explained \r\nsoon.\r\n\r\nOne interesting property of CMOS is that a k-input gate uses k \r\nPMOS and k NMOS transistors (Wakerly, 1999). All logic gates are \r\nbuilt by pairs of NMOS and PMOS transistors, and gates are the \r\nbuilding blocks of all digital devices from simple to complex, \r\nincluding any computer. Thanks to this pattern, it is possible to \r\nseparate between the actual physical circuit implementation and \r\nlogical implementation. Digital designs are done by designing \r\nwith logic gates then later be “compiled” into physical circuits. \r\nIn fact, later we will see that logic gates become a language \r\nthat describes how circuits operate. Understanding how CMOS works \r\nis important to understand how a computer is designed, and as a \r\nconsequence, how a computer works[footnote:\r\nAgain, if you want to understand how logic gates make a computer, \r\nconsider the suggested courses on Coursera and Edx earlier. \r\n].\r\n\r\nFinally, an implemented circuit with its wires and transistors is \r\nstored physically in a package called a chip. A chipchip is a \r\nsubstrate that an integrated circuit is etched onto. However, a \r\nchip also refers to a completely packaged integrated circuit in \r\nconsumer market. Depends on the context, it is understood \r\ndifferently.[float MarginFigure:\r\n\r\n\r\n[MarginFigure 4:\r\n74HC00 chip physical view\r\n]\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/02/74hc00_nxp_physical.jpg>\r\n]\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n74HC00 is a chip with four 2-input NAND gates. The chip comes \r\nwith 8 input pins and 4 output pins, 1 pin for connecting to a \r\nvoltage source and 1 pin for connecting to the ground. This \r\ndevice is the physical implementation of NAND gates that we can \r\nphysically touch and use. But instead of just a single gate, the \r\nchip comes with 4 gates that can be combined. Each combination \r\nenables a different logic function, effective creating other \r\nlogic gates. This feature is what make the chip popular. \r\n\r\n  [float Figure:\r\n[Figure 0.3:\r\n74HC00 logic diagrams (Source: 74HC00 datasheet, https://neurophysics.ucsd.edu/courses/physics_120/74HC00_QUAD_NAND.pdf\r\n)\r\n]\r\n\r\n[float Figure:\r\n[Sub-Figure a:\r\nLogic diagram of 74HC00\r\n]\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/02/7400_block_diagram.png>\r\n]     [float Figure:\r\n[Sub-Figure b:\r\nLogic diagram of one NAND gate\r\n]\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/02/7400_logic_diagram.png>\r\n]\r\n]\r\n\r\n  Each of the gates above is just a simple NAND circuit with the \r\n  electron flows, as demonstrated earlier. Yet, many these \r\n  NAND-gates chips combined can build a simple computer. \r\n  Software, at the physical level, is just electron flows.\r\n\r\n  \r\n\r\n  How can the above gates can be created with 74HC00? It is \r\n  simple: as every gate has 2 input pins and 1 output pin, we can \r\n  write the output of 1 NAND gate to an input of another NAND \r\n  gate, thus chaining NAND gates together to produce the diagrams \r\n  as above.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n  Beyond Logic Gates: Machine Language\r\n\r\n  Machine language\r\n\r\nBeing built upon gates, as gates only accept a series of 0 and 1, \r\na hardware device only understands 0 and 1. However, a device \r\nonly takes 0 and 1 in a systematic way. [margin:\r\nMachine language\r\n]Machine languageMachine language is a collection of unique bit \r\npatterns that a device can identify and perform a corresponding \r\naction. A machine instruction is a unique bit pattern that a \r\ndevice can identify. In a computer system, a device with its \r\nlanguage is called CPU - Central Processing Unit, which controls \r\nall activities going inside a computer. For example, in the x86 \r\narchitecture, the pattern 10100000 means telling a CPU to add two \r\nnumbers, or 000000101 to halt a computer. In the early days of \r\ncomputers, people had to write completely in binary.\r\n\r\nWhy does such a bit pattern cause a device to do something? The \r\nreason is that underlying each instruction is a small circuit \r\nthat implements the instruction. Similar to how a \r\nfunction/subroutine in a computer program is called by its name, \r\na bit pattern is a name of a little function inside a CPU that \r\ngot executed when the CPU finds one.\r\n\r\nNote that CPU is not the only device with its language. CPU is \r\njust a name to indicate a hardware device that controls a \r\ncomputer system. A hardware device may not be a CPU but still has \r\nits language. A device with its own machine language is a \r\nprogrammable device, since a user can use the language to command \r\nthe device to perform different actions. For example, a printer \r\nhas its set of commands for instructing it how to print a page.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n<exa:74HC00-chip-can>A user can use 74HC00 chip without knowing \r\nits internal, but only the interface for using the device. First, \r\nwe need to know its layout:\r\n\r\n  [float Figure:\r\n[Figure 0.4:\r\n74HC00 Pin Layout (Source: 74HC00 datasheet, http://www.nxp.com/documents/data_sheet/74HC_HCT00.pdf\r\n)\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/02/7400_pin_configuration.pdf>\r\n     \r\n]\r\n\r\n  \r\n\r\n\r\n\r\n  \r\nThen, the functionality of each pin:\r\n\r\n  [float Table:\r\n[Table 1:\r\nPin Description (Source: 74HC00 datasheet, http://www.nxp.com/documents/data_sheet/74HC_HCT00.pdf\r\n)\r\n]\r\n\r\n\r\n+-----------------------------+---------------+-----------------+\r\n| Symbol                      |  Pin          |  Description    |\r\n+------------------------------+---------------+----------------+\r\n| 1A to 4A                     | 1, 4, 9, 12   | data input     |\r\n+------------------------------+---------------+----------------+\r\n| 1B to 4B                     | 2, 5, 10, 13  | data input     |\r\n+------------------------------+---------------+----------------+\r\n| 1Y to 4Y                     | 3, 6, 8, 11   | data output    |\r\n+------------------------------+---------------+----------------+\r\n| GND                          | 7             | ground (0 V)   |\r\n+------------------------------+---------------+----------------+\r\n| V[subscript:cc][subscript:]  | 14            | supply voltage |\r\n+------------------------------+---------------+----------------+\r\n\r\n]\r\n\r\n  Finally, how to use the pins:\r\n\r\n  [float Table:\r\n[Table 2:\r\nFunctional Description\r\n]\r\n\r\n\r\n+------------+--------+\r\n| Input      | Output |\r\n+-----+------+--------+\r\n| nA  | nB   | nY     |\r\n+-----+------+--------+\r\n| L   | L    | H      |\r\n+-----+------+--------+\r\n| L   | X    | H      |\r\n+-----+------+--------+\r\n| X   | L    | H      |\r\n+-----+------+--------+\r\n| H   | H    | L      |\r\n+-----+------+--------+\r\n\r\n]\r\n\r\n  [margin:\r\n• n is a number, either 1, 2, 3, or 4\r\n\r\n• H = HIGH voltage level; L = LOW voltage level; X = don’t care.\r\n]The functional description provides a truth table with all \r\n  possible pin inputs and outputs, which also describes the usage \r\n  of all pins in the device. A user needs not to know the \r\n  implementation, but on such a table to use the device. We can \r\n  say that the truth table above is the machine language of the \r\n  device. Since the device is digital, its language is a \r\n  collection of binary strings:\r\n\r\n  • The device has 8 input pins, and this means it accepts binary \r\n    strings of 8 bits.\r\n\r\n  • The device has 4 output pins, and this means it produces \r\n    binary strings of 4 bits from the 8-bit inputs.\r\n\r\n  The number of input strings is what the device understand, and \r\n  the number of output strings is what the device can speak. \r\n  Together, they make the language of the device. Even though \r\n  this device is simple, yet the language it can accept contains \r\n  quite many binary strings: 2^{8}+2^{4}=272\r\n. However, the \r\n  number is a tiny fraction of a complex device like a CPU, with \r\n  hundreds of pins.\r\n\r\n  When leaving as is, 74HC00 is simply a NAND device with two \r\n  4-bit inputs[footnote:\r\nOr simply 4-bit NAND gate, as it can only accept 4 bits of input \r\nat the maximum.\r\n]. \r\n\r\n  \r\n+--------+-----------------------------------------------+----------------------+\r\n  |        |                    Input                      |        Output        |\r\n  +--------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----+\r\n  | Pin    | 1A  | 1B  | 2A  | 2B  | 3A  | 3B  | 4A  | 4B  | 1Y  | 2Y  | 3Y  | 4Y |\r\n  +--------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----+\r\n  | Value  | 1   | 1   | 0   | 0   | 1   | 1   | 0   | 0   | 0   | 1   | 0   | 1  |\r\n  +--------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----+\r\n  \r\n\r\n  \r\n\r\n\r\n\r\n  The inputs and outputs as visually presented:\r\n\r\n  [float Figure:\r\n\r\n\r\n[Figure 0.5:\r\nPins when receiving digital signals that correspond to a binary \r\nstring. Green signals are inputs; blue signals are outputs.\r\n]     <Graphics file: C:/Users/Tu Do/os01/book_src/images/02/7400_bin_string1.pdf>\r\n     \r\n]\r\n\r\n  On the other hand, if OR gate is implemented, we can only build \r\n  a 2-input OR gate from 74HC00, as it requires 3 NAND gates: 2 \r\n  input NAND gates and 1 output NAND gate. Each input NAND gate \r\n  represents only a 1-bit input of the OR gate. In the following \r\n  figure, the pins of each input NAND gates are always set to the \r\n  same values (either both inputs are A or both inputs are B) to \r\n  represent a single bit input for the final OR gate:\r\n\r\n  \r\n\r\n  \r\n\r\n[float Table:\r\n[Table 3:\r\nTruth table of OR logic diagram.\r\n]\r\n\r\n     \r\n+----+----+----+----+---+\r\n| A  | B  | C  | D  | Y |\r\n+----+----+----+----+---+\r\n| 0  | 0  | 1  | 1  | 0 |\r\n+----+----+----+----+---+\r\n| 0  | 1  | 1  | 0  | 1 |\r\n+----+----+----+----+---+\r\n| 1  | 0  | 0  | 1  | 1 |\r\n+----+----+----+----+---+\r\n| 1  | 1  | 0  | 0  | 1 |\r\n+----+----+----+----+---+\r\n     \r\n]\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nTo implement a 4-bit OR gate, we need a total of four of 74HC00 \r\nchips configured as OR gates, packaged as a single chip as in \r\nfigure [or-chip-74hc00].\r\n\r\n[float Figure:\r\n[Figure 0.6:\r\n4-bit OR chip made from four 74HC00 devices\r\n]<or-chip-74hc00>\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/02/4-bit-or-gate-layout.pdf>\r\n     \r\n]\r\n\r\n  Assembly Language\r\n\r\nAssembly language is the symbolic representation of binary \r\nmachine code, by giving bit patterns mnemonic names. It was a \r\nvast improvement when programmers had to write 0 and 1. For \r\nexample, instead of writing 000000101, a programmer simply write \r\nhlt to stop a computer. Such an abstraction makes instructions \r\nexecuted by a CPU easier to remember, and thus more instructions \r\ncould be memorized, less time spent looking up CPU manual to find \r\ninstructions in bit forms and as a result, code was written \r\nfaster.\r\n\r\nUnderstand assembly language is crucial for low-level programming \r\ndomains, even to this day. The more instructions a programmer \r\nwant to understand, the deeper understanding of machine \r\narchitecture is required.\r\n\r\nWe can build a device with 2 assembly instructions:\r\n\r\n  or   <op1>, <op2>\r\n\r\n  nand <op1>, <op2>\r\n\r\n  • or accepts two 4-bit operands. This corresponds to a 4-input \r\n    OR gate device built from 4 74HC00 chips.\r\n\r\n  • nand accepts two 4-bit operands. This corresponds to a single \r\n    74HC00 chips, leave as is.\r\n\r\n  Essentially, the gates in the example [exa:74HC00-chip-can] \r\n  implements the instructions. Up to this point, we only specify \r\n  input and output and manually feed it to a device. That is, to \r\n  perform an operation:\r\n\r\n  • Pick a device by hands.\r\n\r\n  • Manually put electrical signals into pins.\r\n\r\n  First, we want to automate the process of device selection. \r\n  That is, we want to simply write assembly instruction and the \r\n  device that implements the instruction is selected correctly. \r\n  Solving this problem is easy:\r\n\r\n  • Give each instruction an index in binary code, called \r\n    operation code or opcode for short, and embed it as part of \r\n    input. The value for each instruction is specified as in \r\n    table [ex-ins-ops].[float MarginTable:\r\n[MarginTable 1:\r\nInstruction-Opcode mapping.\r\n]<ex-ins-ops>\r\n\r\n\r\n+--------------+-------------+\r\n| Instruction  | Binary Code |\r\n+--------------+-------------+\r\n+--------------+-------------+\r\n|    nand      |     00      |\r\n+--------------+-------------+\r\n|     or       |     01      |\r\n+--------------+-------------+\r\n\r\n]\r\n\r\n    Each input now contains additional data at the beginning: an \r\n    opcode. For example, the instruction:\r\n\r\n    nand 1100, 1100\r\n\r\n    corresponds to the binary string: 0011001100. The first two \r\n    bits 00 encodes a nand instruction, as listed in the table \r\n    above.\r\n\r\n  • Add another device to select a device, based on a binary code \r\n    peculiar to an instruction. \r\n\r\n  Such a device is called a decoder, an important component in a \r\n  CPU that decides which circuit to use. In the above example, \r\n  when feeding 0011001100 to the decoder, because the opcode is \r\n  00, data are sent to NAND device for computing.\r\n\r\n  Finally, writing assembly code is just an easier way to write \r\n  binary strings that a device can understand. When we write \r\n  assembly code and save in a text file, a program called an [margin:\r\nassembler\r\n]assemblerassembler translates the text file into binary strings \r\n  that a device can understand. So, how can an assembler exist in \r\n  the first place? Assume this is the first assembler in the \r\n  world, then it is written in binary code. In the next version, \r\n  life is easier: the programmers write the assembler in the \r\n  assembly code, then use the first version to compile itself. \r\n  These binary strings are then stored in another device that \r\n  later can be retrieved and sent to a decoder. A storage device[margin:\r\nstorage device\r\n]storage device is the device that stores machine instructions, \r\n  which is an array of circuits for saving 0 and 1 states.\r\n\r\n  A decoder is built out of logic gates similar to other digital \r\n  devices. However, a storage device can be anything that can \r\n  store 0 and 1 and is retrievable. A storage device can be a \r\n  magnetized device that uses magnetism to store information, or \r\n  it can be made out of electrical circuits using. Regardless of \r\n  the technology used, as long as the device can store data and \r\n  is accessible to retrieve data, it suffices. Indeed, the modern \r\n  devices are so complex that it is impossible and unnecessary to \r\n  understand every implementation detail. Instead, we only need \r\n  to learn the interfaces, e.g. the pins, that the devices \r\n  expose.\r\n\r\n  \r\n\r\n\r\n\r\n  \r\n\r\n\r\n  A computer essentially implements this process: \r\n\r\n  • Fetch an instruction from a storage device.\r\n\r\n  • Decode the instruction.\r\n\r\n  • Execute the instruction.\r\n\r\n  Or in short, a fetch -- decode -- executefetch -- decode -- \r\n  execute cycle. The above device is extremely rudimentary, but \r\n  it already represents a computer with a fetch -- decode -- \r\n  execute cycle. More instructions can be implemented by adding \r\n  more devices and allocating more opcodes for the instructions, \r\n  then update the decoder accordingly. The Apollo Guidance \r\n  Computer, a digital computer produced for the Apollo space \r\n  program from 1961 -- 1972, was built entirely with NOR gates - \r\n  the other choice to NAND gate for creating other logic gates. \r\n  Similarly, if we keep improving our hypothetical device, it \r\n  eventually becomes a full-fledge computer.\r\n\r\n  Programming Languages\r\n\r\nAssembly language is a step up from writing 0 and 1. As time goes \r\nby, people realized that many pieces of assembly code had \r\nrepeating patterns of usages. It would be nice if instead of \r\nwriting all the repeating blocks of code all over again in all \r\nplaces, we simply refer to such blocks of code with easier to use \r\ntext forms. For example, a block of assembly code checks whether \r\none variable is greater than another and if so, execute a block \r\nof code, else execute another block of code; in C, such block of \r\nassembly code is represented by an if statement that is close to \r\nhuman language. \r\n\r\n[float Figure:\r\n[Figure 0.7:\r\nRepeated assembly patterns are generalized into a new language.\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/02/asm_to_proglang.pdf>\r\n     \r\n]\r\n\r\nPeople created text forms to represent common blocks of assembly \r\ncode, such as the if syntax above, then write a program to \r\ntranslate the text forms into assembly code. The program that \r\ntranslates such text forms to machine code is called a [margin:\r\ncompiler\r\n]compilercompiler:\r\n\r\n\r\n\r\nAny software logic a programming language can implement, hardware \r\ncan also implement. The reverse is also true: any hardware logic \r\nthat is implemented in a circuit can be reimplemented in a \r\nprogramming language. The simple reason is that programming \r\nlanguages, or assembly languages, or machine languages, or logic \r\ngates are just languages to express computations. It is \r\nimpossible for software to implement something hardware is \r\nincapable of because programming language is just a simpler way \r\nto use the underlying hardware. At the end of the day, \r\nprogramming languages are translated to machine instructions that \r\nare valid to a CPU. Otherwise, code is not runnable, thus a \r\nuseless software. In reverse, software can do everything hardware \r\n(that run the software) can, as programming languages are just an \r\neasier way to use the hardware.\r\n\r\nIn reality, even though all languages are equivalent in power, \r\nnot all of them are capable of express programs of each other. \r\nProgramming languages vary between two ends of a spectrum: high \r\nlevel and low level. \r\n\r\nThe higher level a programming language is, the more distant it \r\nbecomes from the hardware. In some high-level programming languages, \r\nsuch as Python, a programmer cannot manipulate underlying \r\nhardware, despite being able to deliver the same computations as \r\nlow-level programming languages. The reason is that high-level \r\nlanguages want to hide hardware details to free programmers from \r\ndealing with irrelevant details not related to current problem \r\ndomains. Such convenience, however, is not free: it requires \r\nsoftware to carry an extra code for managing hardware details \r\n(e.g. memory) thus making the code run slower, and it makes \r\nhardware programming difficult or impossible. The more \r\nabstractions a programming language imposes, the more difficult \r\nit is for writing low-level software, such as hardware drivers or \r\nan operating system. This is the reason why C is usually a \r\nlanguage of choice for writing an operating system, since C is \r\njust a thin wrapper of the underlying hardware, making it easy to \r\nunderstand how exactly a hardware device runs when executing a \r\ncertain piece of C code.\r\n\r\nEach programming language represents a way of thinking about \r\nprograms. Higher-level programming languages help to focus on \r\nproblem domains that are not related to hardware at all, and \r\nwhere programmer performance is more important than computer \r\nperformance. Lower-level programming languages help to focus on \r\nthe inner-working of a machine, thus are best suited for problem \r\ndomains that are related to control hardware. That is why so many \r\nlanguages exist. Use the right tools for the right job to achieve \r\nthe best results.\r\n\r\n\r\n\r\n  Abstraction\r\n\r\nAbstractionAbstraction is a technique for hiding complexity that \r\nis irrelevant to the problem in context. For example, writing \r\nprograms without any other layer except the lowest layer: with \r\ncircuits. Not only a person needs an in-depth understanding of \r\nhow circuits work, making it much more obscure to design a \r\ncircuit because the designer must look at the raw circuits but \r\nthink in higher-level such as logic gates. It is a distracting \r\nprocess, as a designer must constantly translate the idea into \r\ncircuits. It is possible for a designer simply thinks his \r\nhigh-level ideas straight, and later translate the ideas into \r\ncircuits. Not only it is more efficient, but it is also more \r\naccurate as a designer can focus all his efforts into verifying \r\nthe design with high-level thinking. When a new designer arrives, \r\nhe can easily understand the high-level designs, thus can \r\ncontinue to develop or maintain existing systems.\r\n\r\n  Why abstraction works\r\n\r\nIn all the layers, abstractions manifest itself:\r\n\r\n• Logic gates abstract away the details of CMOS.\r\n\r\n• Machine language abstracts away the details of logic gates.\r\n\r\n• Assembly language abstracts away the details of machine \r\n  languages.\r\n\r\n• Programming language abstracts away the details of assembly \r\n  languages.\r\n\r\nWe see repeating patterns of how lower-layers build upper-layers:\r\n\r\n• A lower layer has a recurring pattern. Then, this recurring \r\n  pattern is taken out and built a language on top of it.\r\n\r\n• A higher layer strips away layer-specific (non-recurring) \r\n  details to focus on the recurring details.\r\n\r\n• The recurring details are given a new and simpler language than \r\n  the languages of the lower layers. \r\n\r\nWhat to realize is that every layer is just a more convenient \r\nlanguage to describe the lower layer. Only after a description is \r\nfully created with the language of the higher layer, it is then \r\nbe implemented with the language of the lower layer.\r\n\r\n• CMOS layer has a recurring pattern that makes sure logic gates \r\n  are reliably translated to CMOS circuits: a k-input gate uses k \r\n  PMOS and k NMOS transistors (Wakerly, 1999). Since digital \r\n  devices use CMOS exclusively, a language arose to describe \r\n  higher level ideas while hiding CMOS circuits: Logic Gates.\r\n\r\n• Logic Gates hides the language of circuits and focuses on how \r\n  to implement primitive Boolean functions and combine them to \r\n  create new functions. All logic gates receive input and \r\n  generate output as binary numbers. Thanks to this recurring \r\n  patterns, logic gates are hidden away for the new language: \r\n  Assembly, which is a set of predefined binary patterns that \r\n  cause the underlying gates to perform an action.\r\n\r\n• Soon, people realized that many recurring patterns arisen from \r\n  within Assembly language. Repeated blocks of Assembly code \r\n  appear in Assembly source files that express the same or \r\n  similar idea. There were many such ideas that can be reliably \r\n  translated into Assembly code. Thus, the ideas were extracted \r\n  for building into the high level programming languages that \r\n  everyone programmer learns today.\r\n\r\nRecurring patterns are the key to abstraction. Recurring patterns \r\nare why abstraction works. Without them, no language can be \r\nbuilt, and thus no abstraction. Fortunately, human already \r\ndeveloped a systematic discipline for studying patterns: \r\nMathematics. As quoted from the British mathematician G. H. Hardy \r\n(2005):\r\n\r\nA mathematician, like a painter or a poet, is a maker of \r\npatterns. If his patterns are more permanent than theirs, it is \r\nbecause they are made with ideas.\r\n\r\nIsn't that a mathematical formula a representation of a pattern? \r\nA variable represents values with the same properties given by \r\nconstraints? Mathematics provides a formal system to identify and \r\ndescribe existing patterns in nature. For that reason, this \r\nsystem can certainly be applied in the digital world, which is \r\njust a subset of the real world. Mathematics can be used as a \r\ncommon language to help translation between layers easier, and \r\nhelp with the understanding of layers.\r\n\r\n\r\n\r\n  Why abstraction reduces complexity\r\n\r\nAbstraction by building language certainly leverages productivity \r\nby stripping irrelevant details to a problem. Imagine writing \r\nprograms without any other layout except the lowest layer: with \r\ncircuits. This is how complexity emerges: when high-level ideas \r\nare expressed with lower-level language, as the example above \r\ndemonstrated. Unfortunately, this is the case with software as \r\nprogramming languages at the moment are more emphasized on \r\nsoftware rather than the problem domains. That is, without prior \r\nknowledge, code written in a language is unable to express itself \r\nthe knowledge of its target domain. In other words, a language is \r\nexpressive if its syntax is designed to express the problem \r\ndomain it is trying to solve. Consider this example: That is, the \r\nwhat it will do rather the how it will do.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nGraphviz (http://www.graphviz.org/) is a visualization software \r\nthat provides a language, called dot, for describing graph:\r\n\r\n  \r\n\r\n  As can be seen, the code perfectly expresses itself how the \r\n  graph is connected. Even a non-programmer can understand and \r\n  use such language easily. An implementation in C\r\n  would be more troublesome, and that's assuming that the \r\n  functions for drawing graphs are already available. To draw a \r\n  line, in C we might write something like:\r\n\r\n  draw_line(a, b);\r\n\r\n  However, it is still verbose compared with:\r\n\r\n  a -> b;\r\n\r\n  Also, a and b must be defined in C, compared to the implicit \r\n  nodes in the dot language. However, if we do not factor in the \r\n  verbosity, then C still has a limitation: it cannot change its \r\n  syntax to suit the problem domain. A domain-specific language \r\n  might even be more verbose, but it makes a domain more \r\n  understandable. If a problem domain must be expressed in C, \r\n  then it is constraint by the syntax of C. Since C is not a \r\n  specialized language for a problem domain that, but is a \r\n  general-purpose programming language, the domain knowledge is \r\n  buried within the implementation details. As a result, a C \r\n  programmer is needed to decipher and extract the domain \r\n  knowledge out. If the domain knowledge cannot be extracted, \r\n  then the software cannot be further developed.\r\n\r\n\r\n\r\nLinux is full of applications controlled by many domain-specific \r\nlanguages and are placed in /etc directory, such as a web server. \r\nInstead of reprogramming the software, a domain-agnostic language \r\nis made for it.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nIn general, code that can express a problem domain must be \r\nunderstandable by a domain expert. Even within the software \r\ndomain, building a language out of repeated programming patterns \r\nis useful. It helps people aware the existence of such patterns \r\nin code and thus making software easier to maintain, as software \r\nstructure is visible as a language. Only a programming language \r\nthat is capable of morphing itself to suit a problem domain can \r\nachieve that goal. Such language is called a programmable \r\nprogramming language. Unfortunately, this approach of turning \r\nsoftware structure visible is not favored among programmers, as a \r\nnew language must be made out of it along with new toolchain to \r\nsupport it. Thus, software structure and domain knowledge are \r\nburied within code written in the syntax of a general-purpose \r\nlanguage, and if a programmer is not familiar or even aware of \r\nthe existence of a code pattern, then it is hopeless to \r\nunderstand the code. A prime example is reading C code that \r\ncontrols hardware, e.g. an operating system: if a programmer \r\nknows absolutely nothing about hardware, then it is impossible to \r\nread and write operating system code in C, even if he could have \r\n20 years of writing application C code.\r\n\r\nWith abstraction, a software engineer can also understand the \r\ninner-working of a device without specialized knowledge of \r\nphysical circuit design, enables the software engineer to write \r\ncode that controls a device. The separation between logical and \r\nphysical implementation also entails that gate designs can be \r\nreused even when the underlying technologies changed. For \r\nexample, in some distant future biological computer could be a \r\nreality, and gates might not be implemented as CMOS but some kind \r\nof biological cells e.g. as living cells; in either technology: \r\nelectrical or biological, as long as logic gates are physically \r\nrealized, the same computer design could be implemented.\r\n\r\nComputer Architecture\r\n\r\nTo write lower level code, a programmer must understand the \r\narchitecture of a computer. It is similar to when one writes \r\nprograms in a software framework, he must know what kinds of \r\nproblems the framework solves, and how to use the framework by \r\nits provided software interfaces. But before getting to the \r\ndefinition of what computer architecture is, we must understand \r\nwhat exactly is a computer, as many people still think that a \r\ncomputer is a regular computer we put on a desk, or at best, a \r\nserver. Computers come in various shapes and sizes and are \r\ndevices that people never imagine they are computers, and that \r\ncode can run on such devices. \r\n\r\n  What is a computer?\r\n\r\nA [margin:\r\ncomputer\r\n]computercomputer is a hardware device that consists of at least \r\na processor (CPU), a memory device and input/output interfaces. \r\nAll the computers can be grouped into two types:\r\n\r\n  Single-purpose computer is a computer built at the hardware \r\n  level for specific tasks. For example, dedicated application \r\n  encoders/decoders , timer, image/video/sound processors.\r\n\r\n  General-purpose computer is a computer that can be programmed \r\n  (without modifying its hardware) to emulate various features of \r\n  single-purpose computers.\r\n\r\n  Server\r\n\r\nA server[margin:\r\nserver\r\n]server is a general-purpose high-performance computer with huge \r\nresources to provide large-scale services for a broad audience. \r\nThe audience are people with their personal computer connected to \r\na server.\r\n\r\n[float Figure:\r\n[Figure 0.8:\r\nBlade servers. Each blade server is a computer with a modular \r\ndesign optimize for the use of physical space and energy. The \r\nenclosure of blade servers is called a chassis.(Source: [https://commons.wikimedia.org/wiki/File:Wikimedia_Foundation_Servers-8055_35.jpg||Wikimedia]\r\n, author: Victorgrigas)\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/03/Wikimedia_Foundation_Servers-8055_35.jpg>\r\n     \r\n]\r\n\r\n  Desktop Computer\r\n\r\nA [margin:\r\ndesktop computer\r\n]desktop computerdesktop computer is a general-purpose computer \r\nwith an input and output system designed for a human user, with \r\nmoderate resources enough for regular use. The input system \r\nusually includes a mouse and a keyboard, while the output system \r\nusually consists of a monitor that can display a large mount of \r\npixels. The computer is enclosed in a chassis large enough for \r\nputting various computer components such as a processor, a \r\nmotherboard, a power supply, a hard drive, etc.\r\n\r\n[float Figure:\r\n[Figure 0.9:\r\nA typical desktop computer.\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/03/computer-158675.svg>\r\n     \r\n]\r\n\r\n  Mobile Computer\r\n\r\nA mobile computer[margin:\r\nmobile computer\r\n]mobile computer is similar to a desktop computer with fewer \r\nresources but can be carried around.\r\n\r\n\r\n\r\n  Game Consoles\r\n\r\nGame consoles are similar to desktop computers but are optimized \r\nfor gaming. Instead of a keyboard and a mouse, the input system \r\nof a game console are game controllers, which is a device with a \r\nfew buttons for controlling on-screen objects; the output system \r\nis a television. The chassis is similar to a desktop computer but \r\nis smaller. Game consoles use custom processors and graphic \r\nprocessors but are similar to ones in desktop computers. For \r\nexample, the first Xbox uses a custom Intel Pentium III \r\nprocessor.\r\n\r\n\r\n\r\nHandheld game consoles are similar to game consoles, but \r\nincorporate both the input and output systems along with the \r\ncomputer in a single package.\r\n\r\n\r\n\r\n  Embedded Computer\r\n\r\nAn [margin:\r\nembedded computer\r\n]embedded computerembedded computer is a single-board or \r\nsingle-chip computer with limited resources designed for \r\nintegrating into larger hardware devices. [float MarginFigure:\r\n[MarginFigure 5:\r\nAn Intel 82815 Graphics and Memory Controller Hub embedded on a \r\nPC motherboard. (Source: [https://commons.wikimedia.org/wiki/File:Intel_82815_GMCH.jpg||Wikimedia]\r\n, author: Qurren)\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/03/Intel_82815_GMCH.jpg>\r\n     \r\n][float MarginFigure:\r\n[MarginFigure 6:\r\nA PIC microcontroller. (Soure: [http://www.microchip.com/wwwproducts/en/PIC18F4620||Microchip]\r\n)\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/03/medium-PIC18F4620-PDIP-40.png>\r\n     \r\n]\r\n\r\nA [margin:\r\nmicrocontroller\r\n]microcontrollerMicrocontroller is an embedded computer designed \r\nfor controlling other hardware devices. A microcontroller is \r\nmounted on a chip. Microcontrollers are general-purpose \r\ncomputers, but with limited resources so that it is only able to \r\nperform one or a few specialized tasks. These computers are used \r\nfor a single purpose, but they are still general-purpose since it \r\nis possible to program them to perform different tasks, depends \r\non the requirements, without changing the underlying hardware.\r\n\r\nAnother type of embedded computer is system-on-chip. A \r\nsystem-on-chipsystem-on-chip is a full computer on a single chip. \r\nThough a microcontroller is housed on a chip, its purpose is \r\ndifferent: to control some hardware. A microcontroller is usually \r\nsimpler and more limited in hardware resources as it specializes \r\nonly in one purpose when running, whereas a system-on-chip is a \r\ngeneral-purpose computer that can serve multiple purposes. A \r\nsystem-on-chip can run like a regular desktop computer that is \r\ncapable of loading an operating system and run various \r\napplications. A system-on-chip typically presents in a \r\nsmartphone, such as Apple A5 SoC used in Ipad2 and iPhone 4S, or \r\nQualcomm Snapdragon used in many Android phones.[float MarginFigure:\r\n\r\n\r\n[MarginFigure 7:\r\nApple A5 SoC\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/03/128px-Apple_A5_Chip.jpg>\r\n     \r\n]\r\n\r\nBe it a microcontroller or a system-on-chip, there must be an \r\nenvironment where these devices can connect to other devices. \r\nThis environment is a circuit board called a PCBPCB -- Printed Circuit Board\r\nPrinted Circuit Board. A printed circuit boardPrinted Circuit Board\r\n is a physical board that contains lines and pads to enable \r\nelectron flows between electrical and electronics components. \r\nWithout a PCB, devices cannot be combined to create a larger \r\ndevice. As long as these devices are hidden inside a larger \r\ndevice and contribute to a larger device that operates at a \r\nhigher level layer for a higher level purpose, they are embedded \r\ndevices. Writing a program for an embedded device is therefore \r\ncalled embedded programmingembedded programming. Embedded \r\ncomputers are used in automatically controlled devices including \r\npower tools, toys, implantable medical devices, office machines, \r\nengine control systems, appliances, remote controls and other \r\ntypes of embedded systems.\r\n\r\n\r\n\r\nThe line between a microcontroller and a system-on-chip is \r\nblurry. If hardware keeps evolving more powerful, then a \r\nmicrocontroller can get enough resources to run a minimal \r\noperating system on it for multiple specialized purposes. In \r\ncontrast, a system-on-chip is powerful enough to handle the job \r\nof a microcontroller. However, using a system-on-chip as a \r\nmicrocontroller would not be a wise choice as price will rise \r\nsignificantly, but we also waste hardware resources since the \r\nsoftware written for a microcontroller requires little computing \r\nresources.\r\n\r\n  Field Gate Programmable Array\r\n\r\n[margin:\r\nField Programmable Gate Array\r\n]Field Programmable Gate ArrayField Gate Programmable Array (FPGA\r\nFPGA) is a hardware an array of reconfigurable gates that makes \r\ncircuit structure programmable after it is shipped away from the \r\nfactory[footnote:\r\nThis is why it is called Field Gate Programmable Array. It is \r\nchangeable “in the field” where it is applied.\r\n]. Recall that in the previous chapter, each 74HC00 chip can be \r\nconfigured as a gate, and a more sophisticated device can be \r\nbuilt by combining multiple 74HC00 chips. In a similar manner, \r\neach FPGA device contains thousands of chips called logic blocks, \r\nwhich is a more complicated chip than a 74HC00 chip that can be \r\nconfigured to implement a Boolean logic function. These logic \r\nblocks can be chained together to create a high-level hardware \r\nfeature. This high-level feature is usually a dedicated algorithm \r\nthat needs high-speed processing.\r\n\r\n[float Figure:\r\n[Figure 0.10:\r\nFPGA Architecture (Source: [http://www.ni.com/tutorial/6097/en/||National Instruments]\r\n)\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/03/fpga_400x212.jpg>\r\n     \r\n]\r\n\r\nDigital devices can be designed by combining logic gates, without \r\nregarding actual circuit components, since the physical circuits \r\nare just multiples of CMOS circuits. Digital hardware, including \r\nvarious components in a computer, is designed by writing code, \r\nlike a regular programmer, by using a language to describe how \r\ngates are wired together. This language is called a Hardware \r\nDescription LanguageHardware Description Language. Later the \r\nhardware description is compiled to a description of connected \r\nelectronic components called a netlistnetlist, which is a more \r\ndetailed description of how gates are connected.\r\n\r\nThe difference between FPGA and other embedded computers is that \r\nprograms in FPGA are implemented at the digital logic level, \r\nwhile programs in embedded computers like microcontrollers or \r\nsystem-on-chip devices are implemented at assembly code level. An \r\nalgorithm written for a FPGA device is a description of the \r\nalgorithm in logic gates, which the FPGA device then follows the \r\ndescription to configure itself to run the algorithm. An \r\nalgorithm written for a microcontroller is in assembly \r\ninstructions that a processor can understand and act accordingly. \r\n\r\nFPGA is applied in the cases where the specialized operations are \r\nunsuitable and costly to run on a regular computer such as \r\nreal-time medical image processing, cruise control system, \r\ncircuit prototyping, video encoding/decoding, etc. These \r\napplications require high-speed processing that is not achievable \r\nwith a regular processor because a processor wastes a significant \r\namount of time in executing many non-specialized instructions - \r\nwhich might add up to thousands of instructions or more - to \r\nimplement a specialized operation, thus more circuits at physical \r\nlevel to carry the same operation. A FPGA device carries no such \r\noverhead; instead, it runs a single specialized operation \r\nimplemented in hardware directly.\r\n\r\n  Application-Specific Integrated Circuit\r\n\r\nAn Application-Specific Integrated CircuitApplication-Specific \r\nIntegrated Circuit (or ASICASIC) is a chip designed for a \r\nparticular purpose rather than for general-purpose use. ASIC does \r\nnot contain a generic array of logic blocks that can be \r\nreconfigured to adapt to any operation like an FPGA; instead, \r\nevery logic block in an ASIC is made and optimized for the \r\ncircuit itself. FPGA can be considered as the prototyping stage \r\nof an ASIC, and ASIC as the final stage of circuit production. \r\nASIC is even more specialized than FPGA, so it can achieve even \r\nhigher performance. However, ASICs are very costly to manufacture \r\nand once the circuits are made, if design errors happen, \r\neverything is thrown away, unlike the FPGA devices which can \r\nsimply be reprogrammed because of the generic gate array.\r\n\r\n  Computer Architecture\r\n\r\nThe previous section examined various classes of computers. \r\nRegardless of shapes and sizes, every computer is designed for an \r\narchitect from high level to low level.\r\n\r\nComputer\\,Architecture=Instruction\\,Set\\,Architecture+Computer\\,Organization+Hardware\r\n\r\n\r\nAt the highest-level is the Instruction Set Architecture.\r\n\r\nAt the middle-level is the Computer Organization.\r\n\r\nAt the lowest-level is the Hardware.\r\n\r\n  Instruction Set Architecture\r\n\r\nAn instruction setinstruction set is the basic set of commands \r\nand instructions that a microprocessor understands and can carry \r\nout. \r\n\r\nAn Instruction Set ArchitectureInstruction Set Architecture, or ISA\r\nISA, is the design of an environment that implements an \r\ninstruction set. Essentially, a runtime environment similar to \r\nthose interpreters of high-level languages. The design includes \r\nall the instructions, registers, interrupts, memory models (how \r\nmemory are arranged to be used by programs), addressing modes, \r\nI/O... of a CPU. The more features (e.g. more instructions) a CPU \r\nhas, the more circuits are required to implement it. \r\n\r\n  Computer organization\r\n\r\n[margin:\r\nComputer organization\r\n]Computer organizationComputer organization is the functional \r\nview of the design of a computer. In this view, hardware \r\ncomponents of a computer are presented as boxes with input and \r\noutput that connects to each other and form the design of a \r\ncomputer. Two computers may have the same ISA, but different \r\norganizations. For example, both AMD and Intel processors \r\nimplement x86 ISA, but the hardware components of each processor \r\nthat make up the environments for the ISA are not the same.\r\n\r\nComputer organizations may vary depend on a manufacturer's \r\ndesign, but they are all originated from the Von Neumann \r\narchitecture[footnote:\r\nJohn von Neumann was a mathematician and physicist who invented a \r\ncomputer architecture.\r\n]:\r\n\r\n[float Figure:\r\n[Figure 0.11:\r\nVon-Neumann Architecture\r\n]\r\n\r\n <Graphics file: C:/Users/Tu Do/os01/book_src/images/03/von_neumann_architecture.pdf>\r\n \r\n]\r\n\r\n  CPUCPU fetches instructions continuously from main memory and \r\n  execute.\r\n\r\n  MemoryMemory stores program code and data.\r\n\r\n  BusBus are electrical wires for sending raw bits between the \r\n  above components.\r\n\r\n  I/O DevicesI/O Devices are devices that give input to a \r\n  computer i.e. keyboard, mouse, sensor... and takes the output \r\n  from a computer i.e. monitor takes information sent from CPU to \r\n  display it, LED turns on/off according to a pattern computed by \r\n  CPU...\r\n\r\nThe Von-Neumann computer operates by storing its instructions in \r\nmain memory, and CPU repeatedly fetches those instructions into \r\nits internal storage for executing, one after another. Data are \r\ntransferred through a data bus between CPU, memory and I/O \r\ndevices, and where to store in the devices is transferred through \r\nthe address bus by the CPU. This architecture completely \r\nimplements the fetch -- decode -- executefetch -- decode -- \r\nexecute cycle.\r\n\r\nThe earlier computers were just the exact implementations of the \r\nVon Neumann architecture, with CPU and memory and I/O devices \r\ncommunicate through the same bus. Today, a computer has more \r\nbuses, each is specialized in a type of traffic. However, at the \r\ncore, they are still Von Neumann architecture. To write an OS for \r\na Von Neumann computer, a programmer needs to be able to \r\nunderstand and write code that controls the cores components: \r\nCPU, memory, I/O devices, and bus.\r\n\r\nCPUCPU, or Central Processing UnitCentral Processing Unit, is the \r\nheart and brain of any computer system. Understand a CPU is \r\nessential to writing an OS from scratch:\r\n\r\n• To use these devices, a programmer needs to controls the CPU to \r\n  use the programming interfaces of other devices. CPU is the \r\n  only way, as CPU is the only direct device a programmer can use \r\n  and the only device that understand code written by a \r\n  programmer.\r\n\r\n• In a CPU, many OS concepts are already implemented directly in \r\n  hardware, e.g. task switching, paging. A kernel programmer \r\n  needs to know how to use the hardware features, to avoid \r\n  duplicating such concept in software, thus wasting computer \r\n  resources.\r\n\r\n• CPU built-in OS features boost both OS performance and \r\n  developer productivity because those features are actual \r\n  hardware, the lowest possible level, and developers are free to \r\n  implement such features.\r\n\r\n• To effectively use the CPU, a programmer needs to understand \r\n  the documentation provided from CPU manufacturer. For example, [[http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html||Intel® 64 and IA-32 Architectures Software Developer Manuals]\r\n  .\r\n\r\n• After understanding one CPU architecture well, it is easier to \r\n  learn other CPU architectures.\r\n\r\nA CPU is an implementation of an ISA, effectively the \r\nimplementation of an assembly language (and depending on the CPU \r\narchitecture, the language may vary). Assembly language is one of \r\nthe interfaces that are provided for software engineers to \r\ncontrol a CPU, thus control a computer. But how can every \r\ncomputer device be controlled with only the access to the CPU? \r\nThe simple answer is that a CPU can communicate with other \r\ndevices through these two interfaces, thus commanding them:\r\n\r\n  Registers Registers[margin:\r\nRegisters\r\n]are a hardware component for high-speed data access and \r\n  communication with other hardware devices. Registers allow \r\n  software to control hardware directly by writing to registers \r\n  of a device, or receive information from hardware device when \r\n  reading from registers of a device.\r\n\r\n  Not all registers are used for communication with other \r\n  devices. In a CPU, most registers are used as high-speed \r\n  storage for temporary data. Other devices that a CPU can \r\n  communicate always have a set of registers for interfacing with \r\n  the CPU. \r\n\r\n  Port Port[margin:\r\nPort\r\n]is a specialized register in a hardware device used for \r\n  communication with other devices. When data is written to a \r\n  port, it causes a hardware device to perform some operation \r\n  according to the value written to a port. The difference between \r\n  a port and a register is that a port does not store data, but \r\n  delegates data to some other circuit.\r\n\r\nThese two interfaces are extremely important, as they are the \r\nonly interfaces for controlling hardware with software. Writing \r\ndevice drivers is essentially learning the functionality of each \r\nregister and how to use them properly to control the device.\r\n\r\n[margin:\r\nMemory\r\n]MemoryMemory is a storage device that stores information. Memory \r\nconsists of many cells. Each cell is a byte with its address \r\nnumber, so a CPU can use such address number to access an exact \r\nlocation in memory. Memory is where software instructions (in the \r\nform of machine language) are stored and retrieved to be executed \r\nby the CPU; memory also stores data needed by some software. Memory \r\nin a Von Neumann machine does not distinguish between which bytes \r\nare data and which bytes are software instructions. It's up to \r\nthe software to decide, and if somehow data bytes are fetched and \r\nexecuted as instructions, CPU still does it if such bytes \r\nrepresent valid instructions, but will produce undesirable \r\nresults. To a CPU, there's no code and data; both are merely \r\ndifferent types of instructions for it to act on: one tells it how to do \r\nsomething in a specific manner, and one is necessary materials \r\nfor it to carry out such an action.\r\n\r\nRAM is controlled by a device called a memory controllermemory controller\r\n. Currently, most processors have this device embedded, so the \r\nCPU has a dedicated memory bus connecting the processor to the \r\nRAM. On older CPUs[footnote:\r\nPrior to the CPU's produced in 2009\r\n], however, this device was located in a chip also known as MCH \r\nor Memory Controller HubMemory Controller Hub. In this case, the \r\nCPU does not communicate directly with the RAM, but with the MCH \r\nchip, and this chip then accesses the memory to read or write \r\ndata. The first option provides better performance since there is \r\nno middleman in the communications between the CPU and the \r\nmemory.\r\n\r\n\r\n\r\nAt the physical level, RAM is implemented as a grid of cells that \r\neach contain a transistor and an electrical device called a [margin:\r\ncapacitor\r\n]capacitorcapacitor, which stores charge for short periods of \r\ntime. The transistor controls access to the capacitor; when \r\nswitched on, it allows a small charge to be read from or written \r\nto the capacitor. The charge on the capacitor slowly dissipates, \r\nrequiring the inclusion of a refresh circuit to periodically read \r\nvalues from the cells and write them back after amplification \r\nfrom an external power source.\r\n\r\nBus[margin:\r\nBus\r\n]Bus is a subsystem that transfers data between computer \r\ncomponents or between computers. Physically, buses are just \r\nelectrical wires that connect all components together with each \r\nwire transfering a single big chunk of data. The total number of wires is \r\ncalled bus width[margin:\r\nbus width\r\n]bus width, and is dependent on how many wires a CPU can support. \r\nIf a CPU can only accept 16 bits at a time, then the bus has 16 \r\nwires connecting from a component to the CPU, which means the CPU \r\ncan only retrieve 16 bits of data a time.\r\n\r\n  Hardware\r\n\r\nHardware is a specific implementation of a computer. A line of \r\nprocessors implement the same instruction set architecture and \r\nuse nearly identical organizations but differ in hardware \r\nimplementation. For example, the Core i7 family provides a model \r\nfor desktop computers that is more powerful but consumes more \r\nenergy, while another model for laptops is less performant but \r\nmore energy efficient. To write software for a hardware device, \r\nseldom we need to understand a hardware implementation if \r\ndocuments are available. Computer organization and especially the \r\ninstruction set architecture are more relevant to an operating \r\nsystem programmer. For that reason, the next chapter is devoted \r\nto study the x86 instruction set architecture in depth.\r\n\r\n  x86 architecture\r\n\r\nA chipsetchipset is a chip with multiple functions. Historically, \r\na chipset is actually a set of individual chips, and each is \r\nresponsible for a function, e.g. memory controller, graphic \r\ncontrollers, network controller, power controller, etc. As \r\nhardware progressed, the set of chips were incorporated into a \r\nsingle chip, thus more space, energy, and cost efficient. In a \r\ndesktop computer, various hardware devices are connected to each \r\nother through a PCB called a motherboardmotherboard. Each CPU \r\nneeds a compatible motherboard that can host it. Each motherboard \r\nis defined by its chipset model that determines the environment \r\nthat a CPU can control. This environment typically consists of \r\n\r\n• a slot or more for CPU\r\n\r\n• a chipset of two chips which are the Northbridge and \r\n  Southbridge chips\r\n\r\n  – Northbridge chip is responsible for the high-performance \r\n    communication between CPU, main memory and the graphic card. \r\n\r\n  – Southbridge chip is responsible for the communication with \r\n    I/O devices and other devices that are not performance \r\n    sensitive.\r\n\r\n• slots for memory sticks\r\n\r\n• a slot or more for graphic cards. \r\n\r\n• generic slots for other devices, e.g. network card, sound card.\r\n\r\n• ports for I/O devices, e.g. keyboard, mouse, USB.\r\n\r\n[float Figure:\r\n[Figure 0.12:\r\nMotherboard organization.\r\n]<mobo-organization>\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/03/Motherboard_diagram.svg>\r\n]\r\n\r\nTo write a complete operating system, a programmer needs to \r\nunderstand how to program these devices. After all, an operating \r\nsystem manages hardware automatically to free application \r\nprograms doing so. However, of all the components, learning to \r\nprogram the CPU is the most important, as it is the component \r\npresent in any computer, regardless of what type a computer is. \r\nFor this reason, the primary focus of this book will be on how to \r\nprogram an x86 CPU. Even solely focused on this device, a \r\nreasonably good minimal operating system can be written. The \r\nreason is that not all computers include all the devices as in a \r\nnormal desktop computer. For example, an embedded computer might \r\nonly have a CPU and limited internal memory, with pins for \r\ngetting input and producing an output; yet, operating systems \r\nwere written for such devices. \r\n\r\nHowever, learning how to program an x86 CPU is a daunting task, \r\nwith 3 primary manuals written for it: almost 500 pages for \r\nvolume 1, over 2000 pages for volume 2 and over 1000 pages for \r\nvolume 3. It is an impressive feat for a programmer to master \r\nevery aspect of x86 CPU programming.\r\n\r\n  Intel Q35 Chipset\r\n\r\nQ35 is an Intel chipset released September 2007. Q35 is used as \r\nan example of a high-level computer organization because later we \r\nwill use QEMU to emulate a Q35 system, which is latest Intel \r\nsystem that QEMU can emulate. Though released in 2007, Q35 is \r\nrelatively modern to the current hardware, and the knowledge can \r\nstill be reused for current chipset model. With a Q35 chipset, \r\nthe emulated CPU is also relatively up-to-date with features \r\npresented in current day CPUs so we can use the latest software \r\nmanuals from Intel.\r\n\r\nFigure [mobo-organization] is a typical current-day motherboard \r\norganization, in which Q35 shares similar organization.\r\n\r\n  x86 Execution Environment\r\n\r\nAn execution environmentexecution environment is an environment \r\nthat provides the facility to make code executable. The execution \r\nenvironment needs to address the following questions:\r\n\r\n• Supported operations? data transfer, arithmetic, control, \r\n  floating-point...\r\n\r\n• Where are operands stored? registers, memory, stack, \r\n  accumulator\r\n\r\n• How many explicit operands are there for each instruction? 0, \r\n  1, 2, or 3\r\n\r\n• How is the operand location specified? register, immediate, \r\n  indirect, . . .\r\n\r\n• What type and size of operands are supported? byte, int, float, \r\n  double, string, vector...\r\n\r\n• etc.\r\n\r\nFor the remain of this chapter, please carry on the reading to \r\nchapter 3 in Intel Manual Volume 1, “Basic Execution Environment” \r\n.\r\n\r\nx86 Assembly and C\r\n\r\nIn this chapter, we will explore assembly language, and how it \r\nconnects to C. But why should we do so? Isn't it better to trust \r\nthe compiler, plus no one writes assembly anymore? \r\n\r\nNot quite. Surely, the compiler at its current state of the art \r\nis trustworthy, and we do not need to write code in assembly, \r\nmost of the time. A compiler can generate code, but as mentioned \r\npreviously, a high-level language is a collection of patterns of \r\na lower-level language. It does not cover everything that a \r\nhardware platform provides. As a consequence, not every assembly \r\ninstruction can be generated by a compiler, so we still need to \r\nwrite assembly code for these circumstances to access \r\nhardware-specific features. Since hardware-specific features \r\nrequire writing assembly code, debugging requires reading it. We \r\nmight spend even more time reading than writing. Working with \r\nlow-level code that interacts directly with hardware, assembly \r\ncode is unavoidable. Also, understand how a compiler generates \r\nassembly code could improve a programmer's productivity. For \r\nexample, if a job or school assignment requires us to write \r\nassembly code, we can simply write it in C, then let gcc does the \r\nhard working of writing the assembly code for us. We merely \r\ncollect the generated assembly code, modify as needed and be done \r\nwith the assignment.\r\n\r\nWe will learn objdump extensively, along with how to use Intel \r\ndocuments to aid in understanding x86 assembly code.\r\n\r\n  objdump\r\n\r\nobjdumpobjdump is a program that displays information about \r\nobject files. It will be handy later to debug incorrect layout \r\nfrom manual linking. Now, we use objdump to examine how high \r\nlevel source code maps to assembly code. For now, we ignore the \r\noutput and learn how to use the command first. It is simple to \r\nuse objdump :\r\n\r\n\r\n\r\n$ objdump -d hello\r\n\r\n\r\n\r\n-d option only displays assembled contents of executable \r\nsections. A sectionsection is a block of memory that contains \r\neither program code or data. A code section is executable by the \r\nCPU, while a data section is not executable. Non-executable \r\nsections, such as .data and .bss (for storing program data), \r\ndebug sections... are not displayed. We will learn more about \r\nsection when studying ELF binary file format in chapter [chap:The-Anatomy-of-a-program]\r\n . On the other hand:\r\n\r\n\r\n\r\n$ objdump -D hello\r\n\r\n\r\n\r\nwhere -D option displays assembly contents of all sections. If -D\r\n, -d is implicitly assumed. objdump is mostly used for inspecting \r\nassembly code, so -d is the most useful and thus is set by \r\ndefault.\r\n\r\nThe output overruns the terminal screen. To make it easy for \r\nreading, send all the output to less:\r\n\r\n\r\n\r\n$ objdump -d hello | less\r\n\r\n\r\n\r\nTo intermix source code and assembly, the binary must be compiled \r\nwith -g option to include source code in it, then add -S option:\r\n\r\n\r\n\r\n$ objdump -S hello | less\r\n\r\n\r\n\r\nThe default syntax used by objdump is AT&T syntax. To change it \r\nto the familiar Intel syntax:\r\n\r\n\r\n\r\n$ objdump -M intel -D hello | less\r\n\r\n\r\n\r\nWhen using -M option, option -D or -d must be explicitly \r\nsupplied. Next, we will use objdump to examine how compiled C \r\ndata and code are represented in machine code.\r\n\r\nFinally, we will write a 32-bit kernel, therefore we will need to \r\ncompile a 32-bit binary and examine it in 32-bit mode:\r\n\r\n\r\n\r\n$ objdump -M i386,intel -D hello | less\r\n\r\n\r\n\r\n-M i386 tells objdump to display assembly content using 32-bit \r\nlayout. Knowing the difference between 32-bit and 64-bit is \r\ncrucial for writing kernel code. We will examine this matter \r\nlater on when writing our kernel.\r\n\r\n  Reading the output\r\n\r\nAt the start of the output displays the file format of the object \r\nfile:\r\n\r\nhello: file format elf64-x86-64\r\n\r\nAfter the line is a series of disassembled sections:\r\n\r\nDisassembly of section .interp:\r\n\r\n...\r\n\r\nDisassembly of section .note.ABI-tag:\r\n\r\n...\r\n\r\nDisassembly of section .note.gnu.build-id:\r\n\r\n...\r\n\r\n...\r\n\r\netc\r\n\r\nFinally, each disassembled section displays its actual content - \r\nwhich is a sequence of assembly instructions - with the following \r\nformat:\r\n\r\n4004d6:       55                      push   rbp\r\n\r\n• The first column is the address of an assembly instruction. In \r\n  the above example, the address is 0x4004d6.\r\n\r\n• The second column is assembly instruction in raw hex values. In \r\n  the above example, the address is 0x55.\r\n\r\n• The third column is the assembly instruction. Depends on the \r\n  section, the assembly instruction might be meaningful or \r\n  meaningless. For example, if the assembly instructions are in a \r\n  .text section, then the assembly instructions are actual \r\n  program code. On the other hand, if the assembly instructions \r\n  are displayed in a .data section, then we can safely ignore the \r\n  displayed instructions. The reason is that objdump doesn't know \r\n  which hex values are code and which are data, so it blindly \r\n  translates every hex values into assembly instructions. In the \r\n  above example, the assembly instruction is push %rbp. \r\n\r\n• The optional fourth column is a comment - appears when there is \r\n  a reference to an address - to inform where the address \r\n  originates. For example, the comment in blue:\r\n\r\n      lea    r12,[rip+0x2008ee]        # 600e10 \r\n  <__frame_dummy_init_array_entry>\r\n\r\n  is to inform that the referenced address from [rip+0x2008ee] is \r\n  0x600e10, where the variable __frame_dummy_init_array_entry \r\n  resides.\r\n\r\nIn a disassembled section, it may also contain labels. A label is \r\na name given to an assembly instruction. The label denotes the \r\npurpose of an assembly block to a human reader, to make it easier \r\nto understand. For example, .text section carries many of such \r\nlabels to denote where code in a program start; .text section \r\nbelow carries two functions: _start and deregister_tm_clones. The \r\n_start function starts at address 4003e0, is annotated to the \r\nleft of the function name. Right below _start label is also the \r\ninstruction at address 4003e0. This whole thing means that a \r\nlabel is simply a name of a memory address. The function \r\nderegister_tm_clones also shares the same format as every \r\nfunction in the section.\r\n\r\n00000000004003e0 <_start>:\r\n\r\n  4003e0:       31 ed                   xor    ebp,ebp\r\n\r\n  4003e2:       49 89 d1                mov    r9,rdx\r\n\r\n  4003e5:       5e                      pop    rsi\r\n\r\n...more assembly code....\r\n\r\n\r\n\r\n0000000000400410 <deregister_tm_clones>:\r\n\r\n  400410:       b8 3f 10 60 00          mov    eax,0x60103f\r\n\r\n  400415:       55                      push   rbp\r\n\r\n  400416:       48 2d 38 10 60 00       sub    rax,0x601038\r\n\r\n...more assembly code....\r\n\r\n  Intel manuals\r\n\r\nThe best way to understand and use assembly language properly is \r\nto understand precisely the underlying computer architecture and \r\nwhat each machine instruction does. To do so, the most reliable \r\nsource is to refer to documents provided by vendors. After all, \r\nhardware vendors are the one who made their machines. To \r\nunderstand Intel's instruction set, we need the document “Intel \r\n64 and IA-32 architectures software developer's manual combined \r\nvolumes 2A, 2B, 2C, and 2D: Instruction set reference, A-Z”. The \r\ndocument can be retrieved here: https://software.intel.com/en-us/articles/intel-sdm\r\n.\r\n\r\n• Chapter 1 provides brief information about the manual, and the \r\n  comment notations used in the book.\r\n\r\n• Chapter 2 provides an in-depth explanation of the anatomy of an \r\n  assembly instruction, which we will investigate in the next \r\n  section.\r\n\r\n• Chapter 3 - 5 provide the details of every instruction of the \r\n  x86_64 architecture.\r\n\r\n• Chapter 6 provides information about safer mode extensions. We \r\n  won't need to use this chapter.\r\n\r\nThe first volume “Intel® 64 and IA-32 Architectures Software \r\nDeveloper’s Manual Volume 1: Basic Architecture” describes the \r\nbasic architecture and programming environment of Intel \r\nprocessors. In the book, Chapter 5 gives the summary of all Intel \r\ninstructions, by listing instructions into different categories. \r\nWe only need to learn general-purpose instructions listed chapter \r\n5.1 for our OS. Chapter 7 describes the purpose of each category. \r\nGradually, we will learn all of these instructions.\r\n\r\nRead section 1.3 in volume 2, exclude sections 1.3.5 and 1.3.7.\r\n\r\n  Experiment with assembly code \r\n\r\nThe subsequent sections examine the anatomy of an assembly \r\ninstruction. To fully understand, it is necessary to write code \r\nand see the code in its actual form displayed as hex numbers. For \r\nthis purpose, we use nasm assembler to write a few line of \r\nassembly code and see the generated code.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nSuppose we want to see the machine code generated for this \r\ninstruction:\r\n\r\n  jmp eax\r\n\r\n  Then, we use an editor e.g. Emacs, then create a new file, \r\n  write the code and save it in a file, e.g. test.asm. Then, in \r\n  the terminal, run the command: \r\n\r\n  \r\n\r\n  $ nasm -f bin test.asm -o test\r\n\r\n  \r\n\r\n  -f option specifies the file format, e.g. ELF, of the final \r\n  output file. But in this case, the format is bin, which means \r\n  this file is just a flat binary output without any extra \r\n  information. That is, the written assembly code is translated \r\n  to machine code as is, without the overhead of the metadata \r\n  from file format like ELF. Indeed, after compiling, we can \r\n  examine the output using this command:\r\n\r\n  \r\n\r\n  $ hd test\r\n\r\n  \r\n\r\n  hd (short for hexdump) is a program that displays the content \r\n  of a file in hex format[margin:\r\nThough its name is short for hexdump, hd can display in different \r\nbase, e.g. binary, other than hex. \r\n]. And get the following output:\r\n\r\n  00000000  66 ff e0                          |f..|\r\n\r\n  00000003\r\n\r\n  The file only consists of 3 bytes: 66 ff e0, which is \r\n  equivalent to the instruction jmp eax. \r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n\r\n\r\nIf we were to use elf as file format:\r\n\r\n  \r\n\r\n  $ nasm -f elf test.asm -o test\r\n\r\n  \r\n\r\n  It would be more challenging to learn and understand assembly \r\n  instructions with all the added noise[footnote:\r\nThe output from hd.\r\n]:\r\n\r\n  00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  \r\n  |.ELF............|\r\n\r\n  00000010  01 00 03 00 01 00 00 00  00 00 00 00 00 00 00 00  \r\n  |................|\r\n\r\n  00000020  40 00 00 00 00 00 00 00  34 00 00 00 00 00 28 00  \r\n  |@.......4.....(.|\r\n\r\n  00000030  05 00 02 00 00 00 00 00  00 00 00 00 00 00 00 00  \r\n  |................|\r\n\r\n  00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  \r\n  |................|\r\n\r\n  *\r\n\r\n  00000060  00 00 00 00 00 00 00 00  01 00 00 00 01 00 00 00  \r\n  |................|\r\n\r\n  00000070  06 00 00 00 00 00 00 00  10 01 00 00 02 00 00 00  \r\n  |................|\r\n\r\n  00000080  00 00 00 00 00 00 00 00  10 00 00 00 00 00 00 00  \r\n  |................|\r\n\r\n  00000090  07 00 00 00 03 00 00 00  00 00 00 00 00 00 00 00  \r\n  |................|\r\n\r\n  000000a0  20 01 00 00 21 00 00 00  00 00 00 00 00 00 00 00  | \r\n  ...!...........|\r\n\r\n  000000b0  01 00 00 00 00 00 00 00  11 00 00 00 02 00 00 00  \r\n  |................|\r\n\r\n  000000c0  00 00 00 00 00 00 00 00  50 01 00 00 30 00 00 00  \r\n  |........P...0...|\r\n\r\n  000000d0  04 00 00 00 03 00 00 00  04 00 00 00 10 00 00 00  \r\n  |................|\r\n\r\n  000000e0  19 00 00 00 03 00 00 00  00 00 00 00 00 00 00 00  \r\n  |................|\r\n\r\n  000000f0  80 01 00 00 0d 00 00 00  00 00 00 00 00 00 00 00  \r\n  |................|\r\n\r\n  00000100  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  \r\n  |................|\r\n\r\n  00000110  ff e0 00 00 00 00 00 00  00 00 00 00 00 00 00 00  \r\n  |................|\r\n\r\n  00000120  00 2e 74 65 78 74 00 2e  73 68 73 74 72 74 61 62  \r\n  |..text..shstrtab|\r\n\r\n  00000130  00 2e 73 79 6d 74 61 62  00 2e 73 74 72 74 61 62  \r\n  |..symtab..strtab|\r\n\r\n  00000140  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  \r\n  |................|\r\n\r\n  *\r\n\r\n  00000160  01 00 00 00 00 00 00 00  00 00 00 00 04 00 f1 ff  \r\n  |................|\r\n\r\n  00000170  00 00 00 00 00 00 00 00  00 00 00 00 03 00 01 00  \r\n  |................|\r\n\r\n  00000180  00 74 65 73 74 2e 61 73  6d 00 00 00 00 00 00 00  \r\n  |.disp8-5.asm....|\r\n\r\n  00000190\r\n\r\n  Thus, it is better just to use flat binary format in this case, \r\n  to experiment instruction by instruction.\r\n\r\nWith such a simple workflow, we are ready to investigate the \r\nstructure of every assembly instruction.\r\n\r\nNote: Using the bin format puts nasm by default into 16-bit mode. \r\nTo enable 32-bit code to be generated, we must add this line at \r\nthe beginning of an nasm source file:\r\n\r\nbits 32\r\n\r\n  Anatomy of an Assembly Instruction\r\n\r\nChapter 2 of the instruction reference manual provides an \r\nin-depth of view of instruction format. But, the information is \r\ntoo much that it can overwhelm beginners. This section provides \r\nan easier instruction before reading the actual chapter in the \r\nmanual.\r\n\r\n\r\n\r\n\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/04/x86_instruction_format.pdf>\r\n\r\n\r\n\r\nRecall that an assembly instruction is simply a fixed-size series \r\nof bits. The length of an instruction varies and depends on how \r\ncomplicated an instruction is. What every instruction shares is a \r\ncommon format described in the figure above that divides the bits \r\nof an instruction into smaller parts that encode different types \r\nof information. These parts are:\r\n\r\n  Instruction Prefixes appears at the beginning of an \r\n  instruction. Prefixes are optional. A programmer can choose to \r\n  use a prefix or not because in practice, a so-called prefix is \r\n  just another assembly instruction to be inserted before another \r\n  assembly instruction that such prefix is applicable. \r\n  Instructions with 2 or 3-bytes opcodes include the prefixes by \r\n  default.\r\n\r\n  Opcode is a unique number that identifies an instruction. Each \r\n  opcode is given an mnemonic name that is human readable, e.g. \r\n  one of the opcodes for instruction add is 04. When a CPU sees \r\n  the number 04 in its instruction cache, it sees instruction add \r\n  and execute accordingly. Opcode can be 1,2 or 3 bytes long and \r\n  includes an additional 3-bit field in the ModR/M byte when \r\n  needed.\r\n\r\n  This instruction:\r\n\r\n  jmp [0x1234]\r\n\r\n  generates the machine code:\r\n\r\n  ff 26 34 12\r\n\r\n  The very first byte, 0xff is the opcode, which is unique to jmp \r\n  instruction.\r\n\r\n  ModR/M specifies operands of an instruction. Operand can either \r\n  be a register, a memory location or an immediate value. This \r\n  component of an instruction consists of 3 smaller parts:\r\n\r\n  • mod field, or modifier field, is combined with r/m field for \r\n    a total of 5 bits of information to encode 32 possible \r\n    values: 8 registers and 24 addressing modes.\r\n\r\n  • reg/opcode field encodes either a register operand, or \r\n    extends the Opcode field with 3 more bits.\r\n\r\n  • r/m field encodes either a register operand or can be \r\n    combined with mod field to encode an addressing mode.\r\n\r\n  The tables [mod-rm-16] and [mod-rm-32] list all possible 256 \r\n  values of ModR/M byte and how each value maps to an addressing \r\n  mode and a register, in 16-bit and 32-bit modes.\r\n\r\n\r\n\r\n\r\n\r\n\r\n+---------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| r8(/r)                                      | AL    | CL    | DL    | BL    | AH    | CH    | DH    | BH                                                                                                                                                                                                                                                                                                                                      |\r\n| r16(/r)                                     | AX    | CX    | DX    | BX    | SP    | BP¹   | SI    | DI                                                                                                                                                                                                                                                                                                                                      |\r\n| r32(/r)                                     | EAX   | ECX   | EDX   | EBX   | ESP   | EBP   | ESI   | EDI                                                                                                                                                                                                                                                                                                                                     |\r\n| mm(/r)                                      | MM0   | MM1   | MM2   | MM3   | MM4   | MM5   | MM6   | MM7                                                                                                                                                                                                                                                                                                                                     |\r\n| xmm(/r)                                     | XMM0  | XMM1  | XMM2  | XMM3  | XMM4  | XMM5  | XMM6  | XMM7                                                                                                                                                                                                                                                                                                                                    |\r\n| (In decimal) /digit (Opcode)                | 0     | 1     | 2     | 3     | 4     | 5     | 6     | 7                                                                                                                                                                                                                                                                                                                                       |\r\n| (In binary) REG =                           | 000   | 001   | 010   | 011   | 100   | 101   | 110   | 111                                                                                                                                                                                                                                                                                                                                     |\r\n+---------------------------+--------+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n|        Effective Address  |   Mod  |   R/M  |                                                                                                                                                                             Values of ModR/M Byte (In Hexadecimal)                                                                                                                                                                              |\r\n+---------------------------+--------+--------+-------+-------+-------+-------+-------+-------+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| [BX + SI]                 | 00     | 000    | 00    | 08    | 10    | 18    | 20    | 28    | 30    | 38                                                                                                                                                                                                                                                                                                                                      |\r\n| [BX + DI]                 |        | 001    | 01    | 09    | 11    | 19    | 21    | 29    | 31    | 39                                                                                                                                                                                                                                                                                                                                      |\r\n| [BP + SI]                 |        | 010    | 02    | 0A    | 12    | 1A    | 22    | 2A    | 32    | 3A                                                                                                                                                                                                                                                                                                                                      |\r\n| [BP + DI]                 |        | 011    | 03    | 0B    | 13    | 1B    | 23    | 2B    | 33    | 3B                                                                                                                                                                                                                                                                                                                                      |\r\n| [SI]                      |        | 100    | 04    | 0C    | 14    | 1C    | 24    | 2C    | 34    | 3C                                                                                                                                                                                                                                                                                                                                      |\r\n| [DI]                      |        | 101    | 05    | 0D    | 15    | 1D    | 25    | 2D    | 35    | 3D                                                                                                                                                                                                                                                                                                                                      |\r\n| disp16²                   |        | 110    | 06    | 0E    | 16    | 1E    | 26    | 2E    | 36    | 3E                                                                                                                                                                                                                                                                                                                                      |\r\n| [BX]                      |        | 111    | 07    | 0F    | 17    | 1F    | 27    | 2F    | 37    | 3F                                                                                                                                                                                                                                                                                                                                      |\r\n+---------------------------+--------+--------+-------+-------+-------+-------+-------+-------+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| [BX + SI] + disp8³        | 01     | 000    | 40    | 48    | 50    | 58    | 60    | 68    | 70    | 78                                                                                                                                                                                                                                                                                                                                      |\r\n| [BX + DI] + disp8         |        | 001    | 41    | 49    | 51    | 59    | 61    | 69    | 71    | 79                                                                                                                                                                                                                                                                                                                                      |\r\n| [BP + SI] + disp8         |        | 010    | 42    | 4A    | 52    | 5A    | 62    | 6A    | 72    | 7A                                                                                                                                                                                                                                                                                                                                      |\r\n| [BP + DI] + disp8         |        | 011    | 43    | 4B    | 53    | 5B    | 63    | 6B    | 73    | 7B                                                                                                                                                                                                                                                                                                                                      |\r\n| [SI] + disp8              |        | 100    | 44    | 4C    | 54    | 5C    | 64    | 6C    | 74    | 7C                                                                                                                                                                                                                                                                                                                                      |\r\n| [DI] + disp8              |        | 101    | 45    | 4D    | 55    | 5D    | 65    | 6D    | 75    | 7D                                                                                                                                                                                                                                                                                                                                      |\r\n| [BP] + disp8              |        | 110    | 46    | 4E    | 56    | 5E    | 66    | 6E    | 76    | 7E                                                                                                                                                                                                                                                                                                                                      |\r\n| [BX] + disp8              |        | 111    | 47    | 4F    | 57    | 5F    | 67    | 6F    | 77    | 7F                                                                                                                                                                                                                                                                                                                                      |\r\n+---------------------------+--------+--------+-------+-------+-------+-------+-------+-------+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| [BX + SI] + disp16        | 10     | 000    | 80    | 88    | 90    | 98    | A0    | A8    | B0    | B8                                                                                                                                                                                                                                                                                                                                      |\r\n| [BX + DI] + disp16        |        | 001    | 81    | 89    | 91    | 99    | A1    | A9    | B1    | B9                                                                                                                                                                                                                                                                                                                                      |\r\n| [BP + SI] + disp16        |        | 010    | 82    | 8A    | 92    | 9A    | A2    | AA    | B2    | BA                                                                                                                                                                                                                                                                                                                                      |\r\n| [BP + DI] + disp16        |        | 011    | 83    | 8B    | 93    | 9B    | A3    | AB    | B3    | BB                                                                                                                                                                                                                                                                                                                                      |\r\n| [SI] + disp16             |        | 100    | 84    | 8C    | 94    | 9C    | A4    | AC    | B4    | BC                                                                                                                                                                                                                                                                                                                                      |\r\n| [DI] + disp16             |        | 101    | 85    | 8D    | 95    | 9D    | A5    | AD    | B5    | BD                                                                                                                                                                                                                                                                                                                                      |\r\n| [BP] + disp16             |        | 110    | 86    | 8E    | 96    | 9E    | A6    | AE    | B6    | BE                                                                                                                                                                                                                                                                                                                                      |\r\n| [BX] + disp16             |        | 111    | 87    | 8F    | 97    | 9F    | A7    | AF    | B7    | BF                                                                                                                                                                                                                                                                                                                                      |\r\n+---------------------------+--------+--------+-------+-------+-------+-------+-------+-------+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| EAX/AX/AL/MM0/XMM0        | 11     | 000    | C0    | C8    | D0    | D8    | E0    | E8    | F0    | F8                                                                                                                                                                                                                                                                                                                                      |\r\n| ECX/CX/CL/MM1/XMM1        |        | 001    | C1    | C9    | D1    | D9    | E1    | E9    | F1    | F9                                                                                                                                                                                                                                                                                                                                      |\r\n| EDX/DX/DL/MM2/XMM2        |        | 010    | C2    | CA    | D2    | DA    | E2    | EA    | F2    | FA                                                                                                                                                                                                                                                                                                                                      |\r\n| EBX/BX/BL/MM3/XMM3        |        | 011    | C3    | CB    | D3    | DB    | E3    | EB    | F3    | FB                                                                                                                                                                                                                                                                                                                                      |\r\n| ESP/SP/AHMM4/XMM4         |        | 100    | C4    | CC    | D4    | DC    | E4    | EC    | F4    | FC                                                                                                                                                                                                                                                                                                                                      |\r\n| EBP/BP/CH/MM5/XMM5        |        | 101    | C5    | CD    | D5    | DD    | E5    | ED    | F5    | FD                                                                                                                                                                                                                                                                                                                                      |\r\n| ESI/SI/DH/MM6/XMM6        |        | 110    | C6    | CE    | D6    | DE    | E6    | EE    | F6    | FE                                                                                                                                                                                                                                                                                                                                      |\r\n| EDI/DI/BH/MM7/XMM7        |        | 111    | C7    | CF    | D7    | DF    | E7    | EF    | F7    | FF                                                                                                                                                                                                                                                                                                                                      |\r\n+---------------------------+--------+--------+-------+-------+-------+-------+-------+-------+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n  1. The default segment register is SS for the effective addresses \r\n  containing a BP index, DS for other effective addresses.\r\n\r\n2. The disp16 nomenclature denotes a 16-bit displacement that \r\n  follows the ModR/M byte and that is added to the index.\r\n\r\n3. The disp8 nomenclature denotes an 8-bit displacement that \r\n  follows the ModR/M byte and that is sign-extended and added to \r\n  the index.                                          \r\n<mod-rm-16>\r\n\r\n\r\n\r\n\r\n\r\n\r\n+---------------------------------------------+-------+-------+-------+-------+-------+-------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| r8(/r)                                      | AL    | CL    | DL    | BL    | AH    | CH    | DH    | BH                                                                                                                                                                                                                                                                                                                                                   |\r\n| r16(/r)                                     | AX    | CX    | DX    | BX    | SP    | BP    | SI    | DI                                                                                                                                                                                                                                                                                                                                                   |\r\n| r32(/r)                                     | EAX   | ECX   | EDX   | EBX   | ESP   | EBP   | ESI   | EDI                                                                                                                                                                                                                                                                                                                                                  |\r\n| mm(/r)                                      | MM0   | MM1   | MM2   | MM3   | MM4   | MM5   | MM6   | MM7                                                                                                                                                                                                                                                                                                                                                  |\r\n| xmm(/r)                                     | XMM0  | XMM1  | XMM2  | XMM3  | XMM4  | XMM5  | XMM6  | XMM7                                                                                                                                                                                                                                                                                                                                                 |\r\n| (In decimal) /digit (Opcode)                | 0     | 1     | 2     | 3     | 4     | 5     | 6     | 7                                                                                                                                                                                                                                                                                                                                                    |\r\n| (In binary) REG =                           | 000   | 001   | 010   | 011   | 100   | 101   | 110   | 111                                                                                                                                                                                                                                                                                                                                                  |\r\n+---------------------------+--------+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n|        Effective Address  |   Mod  |   R/M  |                                                                                                                                                                                    Values of ModR/M Byte (In Hexadecimal)                                                                                                                                                                                    |\r\n+---------------------------+--------+--------+-------+-------+-------+-------+-------+-------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| [EAX]                     | 00     | 000    | 00    | 08    | 10    | 18    | 20    | 28    | 30    | 38                                                                                                                                                                                                                                                                                                                                                   |\r\n| [ECX]                     |        | 001    | 01    | 09    | 11    | 19    | 21    | 29    | 31    | 39                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EDX]                     |        | 010    | 02    | 0A    | 12    | 1A    | 22    | 2A    | 32    | 3A                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EBX]                     |        | 011    | 03    | 0B    | 13    | 1B    | 23    | 2B    | 33    | 3B                                                                                                                                                                                                                                                                                                                                                   |\r\n| [-][-]¹                   |        | 100    | 04    | 0C    | 14    | 1C    | 24    | 2C    | 34    | 3C                                                                                                                                                                                                                                                                                                                                                   |\r\n| disp32²                   |        | 101    | 05    | 0D    | 15    | 1D    | 25    | 2D    | 35    | 3D                                                                                                                                                                                                                                                                                                                                                   |\r\n| [ESI]                     |        | 110    | 06    | 0E    | 16    | 1E    | 26    | 2E    | 36    | 3E                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EDI]                     |        | 111    | 07    | 0F    | 17    | 1F    | 27    | 2F    | 37    | 3F                                                                                                                                                                                                                                                                                                                                                   |\r\n+---------------------------+--------+--------+-------+-------+-------+-------+-------+-------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| [EAX] + disp8³            | 01     | 000    | 40    | 48    | 50    | 58    | 60    | 68    | 70    | 78                                                                                                                                                                                                                                                                                                                                                   |\r\n| [ECX] + disp8             |        | 001    | 41    | 49    | 51    | 59    | 61    | 69    | 71    | 79                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EDX] + disp8             |        | 010    | 42    | 4A    | 52    | 5A    | 62    | 6A    | 72    | 7A                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EBX] + disp8             |        | 011    | 43    | 4B    | 53    | 5B    | 63    | 6B    | 73    | 7B                                                                                                                                                                                                                                                                                                                                                   |\r\n| [-][-] + disp8            |        | 100    | 44    | 4C    | 54    | 5C    | 64    | 6C    | 74    | 7C                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EBP] + disp8             |        | 101    | 45    | 4D    | 55    | 5D    | 65    | 6D    | 75    | 7D                                                                                                                                                                                                                                                                                                                                                   |\r\n| [ESI] + disp8             |        | 110    | 46    | 4E    | 56    | 5E    | 66    | 6E    | 76    | 7E                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EDI] + disp8             |        | 111    | 47    | 4F    | 57    | 5F    | 67    | 6F    | 77    | 7F                                                                                                                                                                                                                                                                                                                                                   |\r\n+---------------------------+--------+--------+-------+-------+-------+-------+-------+-------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| [EAX] + disp32            | 10     | 000    | 80    | 88    | 90    | 98    | A0    | A8    | B0    | B8                                                                                                                                                                                                                                                                                                                                                   |\r\n| [ECX] + disp32            |        | 001    | 81    | 89    | 91    | 99    | A1    | A9    | B1    | B9                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EDX] + disp32            |        | 010    | 82    | 8A    | 92    | 9A    | A2    | AA    | B2    | BA                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EBX] + disp32            |        | 011    | 83    | 8B    | 93    | 9B    | A3    | AB    | B3    | BB                                                                                                                                                                                                                                                                                                                                                   |\r\n| [-][-] + disp32           |        | 100    | 84    | 8C    | 94    | 9C    | A4    | AC    | B4    | BC                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EBP] + disp32            |        | 101    | 85    | 8D    | 95    | 9D    | A5    | AD    | B5    | BD                                                                                                                                                                                                                                                                                                                                                   |\r\n| [ESI] + disp32            |        | 110    | 86    | 8E    | 96    | 9E    | A6    | AE    | B6    | BE                                                                                                                                                                                                                                                                                                                                                   |\r\n| [EDI] + disp32            |        | 111    | 87    | 8F    | 97    | 9F    | A7    | AF    | B7    | BF                                                                                                                                                                                                                                                                                                                                                   |\r\n+---------------------------+--------+--------+-------+-------+-------+-------+-------+-------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| EAX/AX/AL/MM0/XMM0        | 11     | 000    | C0    | C8    | D0    | D8    | E0    | E8    | F0    | F8                                                                                                                                                                                                                                                                                                                                                   |\r\n| ECX/CX/CL/MM/XMM1         |        | 001    | C1    | C9    | D1    | D9    | E1    | E9    | F1    | F9                                                                                                                                                                                                                                                                                                                                                   |\r\n| EDX/DX/DL/MM2/XMM2        |        | 010    | C2    | CA    | D2    | DA    | E2    | EA    | F2    | FA                                                                                                                                                                                                                                                                                                                                                   |\r\n| EBX/BX/BL/MM3/XMM3        |        | 011    | C3    | CB    | D3    | DB    | E3    | EB    | F3    | FB                                                                                                                                                                                                                                                                                                                                                   |\r\n| ESP/SP/AH/MM4/XMM4        |        | 100    | C4    | CC    | D4    | DC    | E4    | EC    | F4    | FC                                                                                                                                                                                                                                                                                                                                                   |\r\n| EBP/BP/CH/MM5/XMM5        |        | 101    | C5    | CD    | D5    | DD    | E5    | ED    | F5    | FD                                                                                                                                                                                                                                                                                                                                                   |\r\n| ESI/SI/DH/MM6/XMM6        |        | 110    | C6    | CE    | D6    | DE    | E6    | EE    | F6    | FE                                                                                                                                                                                                                                                                                                                                                   |\r\n| EDI/DI/BH/MM7/XMM7        |        | 111    | C7    | CF    | D7    | DF    | E7    | EF    | F7    | FF                                                                                                                                                                                                                                                                                                                                                   |\r\n+---------------------------+--------+--------+-------+-------+-------+-------+-------+-------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n  1. The [-][-] nomenclature means a SIB follows the ModR/M byte.\r\n\r\n2. The disp32 nomenclature denotes a 32-bit displacement that \r\n  follows the ModR/M byte (or the SIB byte if one is present) and \r\n  that is added to the index.\r\n\r\n3. The disp8 nomenclature denotes an 8-bit displacement that \r\n  follows the ModR/M byte (or the SIB byte if one is present) and \r\n  that is sign-extended and added to the index.                                          \r\n<mod-rm-32>\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nHow to read the table:\r\n\r\nIn an instruction, next to the opcode is a ModR/M byte. Then, \r\nlook up the byte value in this table to get the corresponding \r\noperands in the row and column.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nAn instruction uses this addressing mode:\r\n\r\n  jmp [0x1234]\r\n\r\n  Then, the machine code is:\r\n\r\n  ff 26 34 12\r\n\r\n  0xff is the opcode. Next to it, 0x26 is the ModR/M byte. Look \r\n  up in the 16-bit table [margin:\r\nRemember, using bin format generates 16-bit code by default\r\n], the first operand is in the row, equivalent to a disp16, which \r\n  means a 16-bit offset. Since the instruction does not have a \r\n  second operand, the column can be ignored.\r\n\r\n  An instruction uses this addressing mode:\r\n\r\n  add eax, ecx\r\n\r\n  Then the machine code is:\r\n\r\n  01 c8\r\n\r\n  0x01 is the opcode. Next to it, c8 is the ModR/M byte. Look up \r\n  in the 16-bit table at c8 value, the row tells the first \r\n  operand is ax [margin:\r\nRemember, using bin format generates 16-bit code by default\r\n], the column tells the second operand is cx; the column can't be \r\n  ignored as the second operand is in the instruction.\r\n\r\n  Why is the first operand in the row and the second in a column? \r\n  Let's break down the ModR/M byte, with an example value c8, \r\n  into bits:\r\n\r\n  \r\n+----------+---------------------+-------------+\r\n  |   mod    |     reg/opcode      |     r/m     |\r\n  +----------+---------------------+-------------+\r\n  +----+-----+----+----+-----------+----+----+---+\r\n  | 1  | 1   | 0  | 0  |    1      | 0  | 0  | 0 |\r\n  +----+-----+----+----+-----------+----+----+---+\r\n  \r\n\r\n  The mod field divides addressing modes into 4 different \r\n  categories. Further combines with the r/m field, exactly one \r\n  addressing mode can be selected from one of the 24 rows. If an \r\n  instruction only requires one operand, then the column can be \r\n  ignored. Then the reg/opcode field finally provides the if an \r\n  instruction requires one.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n  SIB is Scale-Index-Base byte. This byte encodes ways to \r\n  calculate the memory position into an element of an array. SIB \r\n  is the name that is based on this formula for calculating an \r\n  effective address:\r\n\r\n  \\mathtt{Effective\\,address=scale*index+base}\r\n\r\n\r\n  • Index is an offset into an array.\r\n\r\n  • Scale is a factor of Index. Scale is one of the values 1, 2, \r\n    4 or 8; any other value is invalid. To scale with values \r\n    other than 2, 4 or 8, the scale factor must be set to 1, and \r\n    the offset must be calculated manually. For example, if we \r\n    want to get the address of the n[superscript:th] element in an array and each element is 12-bytes long. Because \r\n    each element is 12-bytes long instead of 1, 2, 4 or 8, Scale \r\n    is set to 1 and a compiler needs to calculate the offset:\r\n\r\n    \\mathtt{Effective\\,address=1*(12*n)+base}\r\n\r\n\r\n    Why do we bother with SIB when we can manually calculate the \r\n    offset? The answer is that in the above scenario, an \r\n    additional mul instruction must be executed to get the \r\n    offset, and the mul instruction consumes more than 1 byte, \r\n    while the SIB only consumes 1 byte. More importantly, if the \r\n    element is repeatedly accessed many times in a loop, e.g. \r\n    millions of times, then an extra mul instruction can \r\n    detriment the performance as the CPU must spend time \r\n    executing millions of these additional mul instructions. \r\n\r\n    The values 2, 4 and 8 are not random chosen. They map to \r\n    16-bit (or 2 bytes), 32-bit (or 4 bytes) and 64-bit (or 8 \r\n    bytes) numbers that are often used for intensive numeric \r\n    calculations.\r\n\r\n  • Base is the starting address.\r\n\r\n  Below is the table listing all 256 values of SIB byte, with the \r\n  lookup rule similar to ModR/M tables:\r\n\r\n  \r\n\r\n  \r\n+--------------------------------------------+------+------+-------+-------+------+------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n  | r32(/r)                                    | EAX  | ECX  | EDX   | EBX   | ESP  | EBP  | ESI  | EDI                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |\r\n  | (In decimal) /digit (Opcode)               | 0    | 1    | 2     | 3     | 4    | 5    | 6    | 7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |\r\n  | (In binary) REG =                          | 000  | 001  | 010   | 011   | 100  | 101  | 110  | 111                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |\r\n  +---------------------------+-------+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n  |        Effective Address  |   SS  |   R/M  |                                                                                                                                                                                                                                                                                                                 Values of SIB Byte (In Hexadecimal)                                                                                                                                                                                                                                                                                                                 |\r\n  +---------------------------+-------+--------+------+------+-------+-------+------+------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n  | [EAX]                     | 00    | 000    | 00   | 01   | 02    | 03    | 04   | 05   | 06   | 07                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [ECX]                     |       | 001    | 08   | 09   | 0A    | 0B    | 0C   | 0D   | 0E   | 0F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EDX]                     |       | 010    | 10   | 11   | 12    | 13    | 14   | 15   | 16   | 17                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EBX]                     |       | 011    | 18   | 19   | 1A    | 1B    | 1C   | 1D   | 1E   | 1F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | none                      |       | 100    | 20   | 21   | 22    | 23    | 24   | 25   | 26   | 27                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EBP]                     |       | 101    | 28   | 29   | 2A    | 2B    | 2C   | 2D   | 2E   | 2F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [ESI]                     |       | 110    | 30   | 31   | 32    | 33    | 34   | 35   | 36   | 37                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EDI]                     |       | 111    | 38   | 39   | 3A    | 3B    | 3C   | 3D   | 3E   | 3F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  +---------------------------+-------+--------+------+------+-------+-------+------+------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n  | [EAX*2]                   | 01    | 000    | 40   | 41   | 42    | 43    | 44   | 45   | 46   | 47                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [ECX*2]                   |       | 001    | 48   | 49   | 4A    | 4B    | 4C   | 4D   | 4E   | 4F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EDX*2]                   |       | 010    | 50   | 51   | 52    | 53    | 54   | 55   | 56   | 57                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EBX*2]                   |       | 011    | 58   | 59   | 5A    | 5B    | 5C   | 5D   | 5E   | 5F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | none                      |       | 100    | 60   | 61   | 62    | 63    | 64   | 65   | 66   | 67                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EBP*2]                   |       | 101    | 68   | 69   | 6A    | 6B    | 6C   | 6D   | 6E   | 6F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [ESI*2]                   |       | 110    | 70   | 71   | 72    | 73    | 74   | 75   | 76   | 77                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EDI*2]                   |       | 111    | 78   | 79   | 7A    | 7B    | 7C   | 7D   | 7E   | 7F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  +---------------------------+-------+--------+------+------+-------+-------+------+------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n  | [EAX*4]                   | 10    | 000    | 80   | 81   | 82    | 83    | 84   | 85   | 86   | 87                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [ECX*4]                   |       | 001    | 88   | 89   | 8A    | 8B    | 8C   | 8D   | 8E   | 8F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EDX*4]                   |       | 010    | 90   | 91   | 92    | 93    | 94   | 95   | 96   | 97                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EBX*4]                   |       | 011    | 98   | 99   | 9A    | 9B    | 9C   | 9D   | 9E   | 9F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | none                      |       | 100    | A0   | A1   | A2    | A3    | A4   | A5   | A6   | A7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EBP*4]                   |       | 101    | A8   | A9   | AA    | AB    | AC   | AD   | AE   | AF                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [ESI*4]                   |       | 110    | B0   | B1   | B2    | B3    | B4   | B5   | B6   | B7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EDI*4]                   |       | 111    | B8   | B9   | BA    | BB    | BC   | BD   | BE   | BF                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  +---------------------------+-------+--------+------+------+-------+-------+------+------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n  | [EAX*8]                   | 11    | 000    | C0   | C1   | C2    | C3    | C4   | C5   | C6   | C7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [ECX*8]                   |       | 001    | C8   | C9   | CA    | CB    | CC   | CD   | CE   | CF                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EDX*8]                   |       | 010    | D0   | D1   | D2    | D3    | D4   | D5   | D6   | D7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EBX*8]                   |       | 011    | D8   | D9   | DA    | DB    | DC   | DD   | DE   | DF                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | none                      |       | 100    | E0   | E1   | E2    | E3    | E4   | E5   | E6   | E7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EBP*8]                   |       | 101    | E8   | E9   | EA    | EB    | EC   | ED   | EE   | EF                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [ESI*8]                   |       | 110    | F0   | F1   | F2    | F3    | F4   | F5   | F6   | F7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  | [EDI*8]                   |       | 111    | F8   | F9   | FA    | FB    | FC   | FD   | FE   | FF                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |\r\n  +---------------------------+-------+--------+------+------+-------+-------+------+------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    1. The [*] nomenclature means a disp32 with no base if the MOD is \r\n  00B. Otherwise, [*] means disp8 or disp32 + [EBP]. This \r\n  provides the following address modes:\r\n\r\n\r\n+-----------+---------------------------------+\r\n| MOD bits  | Effective Address               |\r\n+-----------+---------------------------------+\r\n+-----------+---------------------------------+\r\n| 00        | [scaled index] + disp32         |\r\n+-----------+---------------------------------+\r\n| 01        | [scaled index] + disp8 + [EBP]  |\r\n+-----------+---------------------------------+\r\n| 10        | [scaled index] + disp32 + [EBP] |\r\n+-----------+---------------------------------+\r\n                                          \r\n  <sib>\r\n\r\n  \r\n\r\n  This instruction:\r\n\r\n    jmp [eax*2 + ebx]\r\n\r\n    generates the following code:\r\n\r\n    00000000  67 ff 24 43\r\n\r\n    First of all, the first byte, 0x67 is not an opcode but a \r\n    prefix. The number is a predefined prefix for address-size \r\n    override prefix. After the prefix, comes the opcode 0xff and \r\n    the ModR/M byte 0x24. The value from ModR/M suggests that \r\n    there exists a SIB byte that follows. The SIB byte is 0x43. \r\n\r\n    Look up in the SIB table, the row tells that eax is scaled by \r\n    2, and the column tells that the base to be added is in ebx.\r\n\r\n  Displacement is the offset from the start of the base index. \r\n\r\n  This instruction:\r\n\r\n    jmp [0x1234]\r\n\r\n    generates machine code is:\r\n\r\n    ff 26 34 12      \r\n\r\n    0x1234, which is generated as 34 12 in raw machine code, is \r\n    the displacement and stands right next to 0x26, which is the \r\n    ModR/M byte. \r\n\r\n    This instruction:\r\n\r\n      jmp [eax * 4  + 0x1234]\r\n\r\n    generates the machine code:\r\n\r\n    67 ff 24 8d 34 12 00 00\r\n\r\n    • 0x67 is an address-size override prefix. Its meaning is \r\n      that if an instruction runs a default address size e.g. \r\n      16-bit, the use of prefix enables the instruction to use \r\n      non-default address size, e.g. 32-bit or 64-bit. Since the \r\n      binary is supposed to be 16-bit, 0x67 changes the \r\n      instruction to 32-bit mode.\r\n\r\n    • 0xff is the opcode.\r\n\r\n    • 0x24 is the ModR/M byte. The value suggests that a SIB byte \r\n      follows, according to table [mod-rm-32].\r\n\r\n    • 34 12 00 00 is the displacement. As can be seen, the \r\n      displacement is 4 bytes in size, which is equivalent to \r\n      32-bit, due to address-size override prefix.\r\n\r\n  Immediate When an instruction accepts a fixed value, e.g. \r\n  0x1234, as an operand, this optional field holds the value. \r\n  Note that this field is different from displacement: the value \r\n  is not necessary used an offset, but an arbitrary value of \r\n  anything. \r\n\r\n  This instruction:\r\n\r\n    mov eax, 0x1234\r\n\r\n    generates the code:\r\n\r\n    66 b8 34 12 00 00\r\n\r\n    • 0x66 is operand-sized override prefix. Similar to \r\n      address-size override prefix, this prefix enables \r\n      operand-size to be non-default.\r\n\r\n    • 0xb8 is one of the opcodes for mov instruction.\r\n\r\n    • 0x1234 is the value to be stored in register eax. It is \r\n      just a value for storing directly into a register, and \r\n      nothing more. On the other hand, displacement value is an \r\n      offset for some address calculation.\r\n\r\nRead section 2.1 in Volume 2 for even more details. \r\n\r\n  Skim through section 5.1 in volume 1. Read chapter 7 in volume \r\n  1. If there are terminologies that you don't understand e.g. \r\n  segmentation, don't worry as the terms will be explained in \r\n  later chapters or ignored.\r\n\r\n  Understand an instruction in detail\r\n\r\nIn the instruction reference manual (Volume 2), from chapter 3 \r\nonward, every x86 instruction is documented in detail. Whenever \r\nthe precise behavior of an instruction is needed, we always \r\nconsult this document first. However, before using the document, \r\nwe must know the writing conventions first. Every instruction has \r\nthe following common structure for organizing information:\r\n\r\n  Opcode table lists all possible opcodes of an assembly \r\n  instruction.\r\n\r\n  Each table contains the following fields, and can have one or \r\n  more rows:\r\n\r\n  \r\n+---------------------------------------------------------------------------------------+\r\n  | Opcode    Instruction    Op/En    64/32-bit Mode    CPUID\r\nFeature flag    Description |\r\n  +---------------------------------------------------------------------------------------+\r\n  \r\n\r\n  Opcode shows a unique hexadecimal number assigned to an \r\n    instruction. There can be more than one opcode for an \r\n    instruction, each encodes a variant of the instruction. For \r\n    example, one variant requires one operand, but another \r\n    requires two. In this column, there can be other notations \r\n    aside from hexadecimal numbers. For example, /r indicates \r\n    that the ModR/M byte of the instruction contains a reg \r\n    operand and an r/m operand. The detail listing is in section \r\n    3.1.1.1 and 3.1.1.2 in the Intel's manual, volume 2.\r\n\r\n  Instruction gives the syntax of the assembly instruction that a \r\n    programmer can use for writing code. Aside from the mnemonic \r\n    representation of the opcode, e.g. jmp, other symbols \r\n    represent operands with specific properties in the \r\n    instruction. For example, rel8 represents a relative address \r\n    from 128 bytes before the end of the instruction to 127 bytes \r\n    after the end of instruction; similarly rel16/rel32 also \r\n    represents relative addresses, but with the operand size of \r\n    16/32-bit instead of 8-bit like rel8. For a detailed listing, \r\n    please refer to section 3.1.1.3 of volume 2.\r\n\r\n  Op/En is short for Operand/Encoding. An operand encoding \r\n    specifies how a ModR/M byte encodes the operands that an \r\n    instruction requires. If a variant of an instruction requires \r\n    operands, then an additional table named “Instruction Operand \r\n    Encoding” is added for explaining the operand encoding, with \r\n    the following structure:\r\n\r\n    \r\n+--------+------------+------------+------------+-----------+\r\n    | Op/En  | Operand 1  | Operand 2  | Operand 3  | Operand 4 |\r\n    +--------+------------+------------+------------+-----------+\r\n    \r\n\r\n    Most instructions require one to two operands. We make use of \r\n    these instructions for our OS and skip the instructions that \r\n    require three or four operands. The operands can be readable \r\n    or writable or both. The symbol (r) denotes a readable \r\n    operand, and (w) denotes a writable operand. For example, \r\n    when Operand 1 field contains ModRM:r/m (r), it means the \r\n    first operand is encoded in r/m field of ModR/M byte, and is \r\n    only readable. \r\n\r\n  64/32-bit mode indicates whether the opcode sequence is \r\n    supported in a 64-bit mode and possibly 32-bit mode.\r\n\r\n  CPUID Feature Flag indicates a particular CPU feature \r\n    must be available to enable the instruction. An instruction \r\n    is invalid if a CPU does not support the required feature.[margin:\r\nIn Linux, the command:\r\n\r\ncat /proc/cpuinfo\r\n\r\nlists the information of available CPUs and its features in flags \r\nfield.\r\n]\r\n\r\n    Compat/Leg Mode Many instructions do not have this field, but \r\n      instead is replaced with Compat/Leg Mode, which stands for \r\n      Compatibility or Legacy Mode. This mode enables 64-bit \r\n      variants of instructions to run normally in 16 or 32-bit \r\n      mode. [float MarginTable:\r\n[MarginTable 2:\r\nNotations in Compat/Leg Mode\r\n]\r\n\r\n\r\n+-----------+----------------------------------------------------------------------------------+\r\n| Notation  | Description                                                                      |\r\n+-----------+----------------------------------------------------------------------------------+\r\n+-----------+----------------------------------------------------------------------------------+\r\n| Valid     | Supported                                                                        |\r\n+-----------+----------------------------------------------------------------------------------+\r\n| I         | Not supported                                                                    |\r\n+-----------+----------------------------------------------------------------------------------+\r\n| N.E.      | The 64-bit opcode cannot be encoded as it overlaps with existing \r\n32-bit opcode. |\r\n+-----------+----------------------------------------------------------------------------------+\r\n\r\n\r\n\r\n]\r\n\r\n  Description briefly explains the variant of an instruction in \r\n    the current row.\r\n\r\n  Description specifies the purpose of the instructions and how \r\n  an instruction works in detail.\r\n\r\n  Operation is pseudo-code that implements an instruction. If a \r\n  description is vague, this section is the next best source to \r\n  understand an assembly instruction. The syntax is described in \r\n  section 3.1.1.9 in volume 2.\r\n\r\n  Flags affected lists the possible changes to system flags in \r\n  EFLAGS register. \r\n\r\n  Exceptions list the possible errors that can occur when an \r\n  instruction cannot run correctly. This section is valuable for \r\n  OS debugging. Exceptions fall into one of the following \r\n  categories:\r\n\r\n• Protected Mode Exceptions\r\n\r\n• Real-Address Mode Exception\r\n\r\n• Virtual-8086 Mode Exception\r\n\r\n• Floating-Point Exception\r\n\r\n• SIMD Floating-Point Exception\r\n\r\n• Compatibility Mode Exception\r\n\r\n• 64-bit Mode Exception\r\n\r\nFor our OS, we only use Protected Mode Exceptions and \r\nReal-Address Mode Exceptions. The details are in section 3.1.1.13 \r\nand 3.1.1.14, volume 2.\r\n\r\n  Example: jmp instruction\r\n\r\nLet's look at our good old jmp instruction. First, the opcode \r\ntable:\r\n\r\n\r\n\r\n\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| Opcode          | Instruction   | Op/\r\n\r\nEn  | 64-bit Mode  | Compat/Leg Mode  | Description                                                                                    |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| EB cb           | JMP rel8      | D        | Valid        | Valid            | Jump short, RIP = RIP + 8-bit displacement sign extended to \r\n64-bits                           |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| E9 cw           | JMP rel16     | D        | N.S.         | Valid            | Jump near, relative, displacement relative to next instruction. \r\nNot supported in 64-bit mode. |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| E9 cd           | JMP rel32     | D        | Valid        | Valid            | Jump near, relative, RIP = RIP + 32-bit displacement sign \r\nextended to 64-bits                 |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| FF /4           | JMP r/m16     | M        | N.S.         | Valid            | Jump near, absolute indirect, address = zero- extended r/m16. Not \r\nsupported in 64-bit mode    |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| FF /4           | JMP r/m32     | M        | N.S.         | Valid            | Jump near, absolute indirect, address given in r/m32. Not \r\nsupported in 64-bit mode            |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| FF /4           | JMP r/m64     | M        | Valid        | N.E              | Jump near, absolute indirect, RIP = 64-Bit offset from register \r\nor memory                     |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| EA cd           | JMP ptr16:16  | D        | Inv.         | Valid            | Jump far, absolute, address given in operand                                                   |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| EA cp           | JMP ptr16:32  | D        | Inv.         | Valid            | Jump far, absolute, address given in operand                                                   |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| FF /5           | JMP m16:16    | D        | Valid        | Valid            | Jump far, absolute indirect, address given in m16:16                                           |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| FF /5           | JMP m16:32    | D        | Valid        | Valid            | Jump far, absolute indirect, address given in m16:32                                           |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n| REX.W + FF /5   | JMP m16:64    | D        | Valid        | N.E.             | Jump far, absolute indirect, address given in m16:64                                           |\r\n+-----------------+---------------+----------+--------------+------------------+------------------------------------------------------------------------------------------------+\r\n\r\n\r\n<jmp-instruction>\r\n\r\n\r\n\r\nEach row lists a variant of jmp instruction. The first column has \r\nthe opcode EB cb, with an equivalent symbolic form jmp rel8. \r\nHere, rel8 means 128 bytes offset, counting from the end of the \r\ninstruction. The end of an instruction is the next byte after the \r\nlast byte of an instruction. To make it more concrete, consider \r\nthis assembly code:\r\n\r\nmain:\r\n\r\n  jmp main\r\n\r\n  jmp main2\r\n\r\n  jmp main\r\n\r\nmain2:\r\n\r\n  jmp 0x1234\r\n\r\ngenerates the machine code:\r\n\r\n[float Table:\r\n[Table 4:\r\nMemory address of each opcode\r\n]\r\n\r\n\r\n           +-------------------+                  +-------------------------+            \r\n           | main              |                  |          main2          |            \r\n           +-------------------+                  +-------------------------+            \r\n             \\downarrow\r\n                                  \\downarrow\r\n                    \r\n+----------+--------------+-----+-----+-----+-----+-----+--------------+-----+-----+----+\r\n| Address  |     00       | 01  | 02  | 03  | 04  | 05  |     06       | 07  | 08  | 09 |\r\n+----------               +-----+-----+-----+-----+-----+--------------+-----+-----+----+\r\n+----------+--------------+-----+-----+-----+-----+-----+--------------+-----+-----+----+\r\n| Opcode   |     eb       | fe  | eb  | 02  | eb  | fa  |     e9       | 2b  | 12  | 00 |\r\n+----------+--------------+-----+-----+-----+-----+-----+--------------+-----+-----+----+\r\n\r\n]\r\n\r\nThe first jmp main instruction is generated into eb fe and \r\noccupies the addresses 00 and 01; the end of the first jmp main \r\nis at address 02, past the last byte of the first jmp main which \r\nis located at the address 01. The value fe is equivalent to -2, \r\nsince eb opcode uses only a byte (8 bits) for relative \r\naddressing. The offset is -2, and the end address of the first \r\njmp main is 02, adding them together we get 00 which is the \r\ndestination address for jumping to. \r\n\r\nSimilarly, the jmp main2 instruction is generated into eb 02, \r\nwhich means the offset is +2; the end address of jmp main2 is at \r\n04, and adding together with the offset we get the destination \r\naddress is 06, which is the start instruction marked by the label \r\nmain2.\r\n\r\nThe same rule can be applied to rel16 and rel32 encoding. In the \r\nexample code, jmp 0x1234 uses rel16 (which means 2-byte offset) \r\nand is generated into e9 2b 12. As the table [jmp-instruction] \r\nshows, e9 opcode takes a cw operand, which is a 2-byte offset \r\n(section 3.1.1.1, volume 2). Notice one strange issue here: the \r\noffset value is 2b 12, while it is supposed to be 34 12. There is \r\nnothing wrong. Remember, rel8/rel16/rel32 is an offset, not an \r\naddress. A offset is a distance from a point. Since no label is \r\ngiven but a number, the offset is calculated from the start of a \r\nprogram. In this case, the start of the program is the address \r\n00, the end of jmp 0x1234 is the address 09[footnote:\r\nwhich means 9 bytes was consumed, starting from address 0.\r\n], so the offset is calculated as 0x1234 - 0x9 = 0x122b. That \r\nsolved the mystery!\r\n\r\nThe jmp instructions with opcode FF /4 enable jumping to a near, \r\nabsolute address stored in a general-purpose register or a memory \r\nlocation; or in short, as written in the description, absolute \r\nindirect. The symbol /4 is the column with digit 4 in table [mod-rm-16]\r\n[footnote:\r\nThe column with the following fields:\r\n\r\nAH\r\n\r\nSP\r\n\r\nESP\r\n\r\nM45\r\n\r\nXMM4\r\n\r\n4\r\n\r\n100\r\n]. For example:\r\n\r\njmp [0x1234]\r\n\r\nis generated into:\r\n\r\nff 26 34 12\r\n\r\nSince this is 16-bit code, we use table [mod-rm-16]. Looking up \r\nthe table, ModR/M value 26 means disp16, which means a 16-bit \r\noffset from the start of current index[footnote:\r\nLook at the note under the table.\r\n], which is the base address stored in DS register. In this case, \r\njmp [0x1234] is implicitly understood as jmp [ds:0x1234], which \r\nmeans the destination address is 0x1234 bytes away from the start \r\nof a data segment.\r\n\r\nThe jmp instruction with opcode FF /5 enables jumping to a far, \r\nabsolute address stored in a memory location (as opposed to /4, \r\nwhich means stored in a register); in short, a far pointer. To \r\ngenerate such instruction, the keyword far is needed to tell nasm \r\nwe are using a far pointer:\r\n\r\njmp far [eax]\r\n\r\nis generated into:\r\n\r\n67 ff 28\r\n\r\nSince 28 is the value in the 5th column of the table [mod-rm-32][footnote:\r\nRemember the prefix 67 indicates the instruction is used as \r\n32-bit. The prefix only added if the default environment is \r\nassumed as 16-bit when generating code by an assembler.\r\n] that refers to [eax], we successfully generate an instruction \r\nfor a far jump. After CPU runs the instruction, the program \r\ncounter eip and code segment register cs is set to the memory \r\naddress, stored in the memory location that eax points to, and \r\nCPU starts fetching code from the new address in cs and eip. To \r\nmake it more concrete, here is an example:\r\n\r\n\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/04/far_jmp_ex.pdf>\r\n     \r\n\r\n\r\n\r\nThe far address consumes total of 6 bytes in size for a 16-bit \r\nsegment and 32-bit address, which is encoded as m16:32 from the \r\ntable [jmp-instruction]. As can be seen from the figure above, \r\nthe blue part is a segment address, loaded into cs register with \r\nthe value 0x5678; the red part is the memory address within that \r\nsegment, loaded into eip register with the value 0x1234 and start \r\nexecuting from there. \r\n\r\nFinally, the jmp instructions with EA opcode jump to a direct \r\nabsolute address. For example, the instruction:\r\n\r\njmp 0x5678:0x1234\r\n\r\nis generated into:\r\n\r\nea 34 12 78 56\r\n\r\nThe address 0x5678:0x1234 is right next to the opcode, unlike FF \r\n/5 instruction that needs an indirect address in eax register.\r\n\r\nWe skip the jump instruction with REX prefix, as it is a 64-bit \r\ninstruction. \r\n\r\n\r\n\r\n  Examine compiled data\r\n\r\nIn this section, we will examine how data definition in C maps to \r\nits assembly form. The generated code is extracted from .bss \r\nsection. That means, the assembly code displayed has no[footnote:\r\nActually, code is just a type of data, and is often used for \r\nhijacking into a running program to execute such code. However, \r\nwe have no use for it in this book.\r\n], aside from showing that such a value has an equivalent \r\nassembly opcode that represents an instruction.\r\n\r\nThe code-assembly listing is not random, but is based on Chapter \r\n4 of Volume 1, “Data Type”. The chapter lists fundamental data \r\ntypes that x86 hardware operates on, and through learning the \r\ngenerated assembly code, it can be understood how close C maps \r\nits syntax to hardware, and then a programmer can see why C is \r\nappropriate for OS programming. The specific objdump command used \r\nin this section will be:\r\n\r\n\r\n\r\n$ objdump -z -M intel -S -D -j .data -j .bss <object file> | less\r\n\r\n\r\n\r\nNote: zero bytes are hidden with three dot symbols: ... To show \r\nall the zero bytes, we add -z option.\r\n\r\n  Fundamental data types\r\n\r\nThe most basic types that x86 architecture works with are based \r\non sizes, each is twice as large as the previous one: 1 byte (8 \r\nbits), 2 bytes (16 bits), 4 bytes (32 bits), 8 bytes (64 bits) \r\nand 16 bytes (128 bits).\r\n\r\n\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/04/fundamental_data_types.pdf>\r\n\r\n\r\n\r\nThese types are simplest: they are just chunks of memory at \r\ndifferent sizes that enables CPU to access memory efficiently. \r\nFrom the manual, section 4.1.1, volume 1:\r\n\r\nWords, doublewords, and quadwords do not need to be aligned in \r\nmemory on natural boundaries. The natural boundaries for words, \r\ndouble words, and quadwords are even-numbered addresses, \r\naddresses evenly divisible by four, and addresses evenly \r\ndivisible by eight, respectively. However, to improve the \r\nperformance of programs, data structures (especially stacks) \r\nshould be aligned on natural boundaries whenever possible. The \r\nreason for this is that the processor requires two memory \r\naccesses to make an unaligned memory access; aligned accesses \r\nrequire only one memory access. A word or doubleword operand that \r\ncrosses a 4-byte boundary or a quadword operand that crosses an \r\n8-byte boundary is considered unaligned and requires two separate \r\nmemory bus cycles for access.\r\n\r\nSome instructions that operate on double quadwords require memory \r\noperands to be aligned on a natural boundary. These instructions \r\ngenerate a general-protection exception (#GP) if an unaligned \r\noperand is specified. A natural boundary for a double quadword is \r\nany address evenly divisible by 16. Other instructions that \r\noperate on double quadwords permit unaligned access (without \r\ngenerating a general-protection exception). However, additional \r\nmemory bus cycles are required to access unaligned data from \r\nmemory.\r\n\r\nIn C, the following primitive types (must include stdint.h) maps \r\nto the fundamental types:\r\n\r\n  Source\r\n\r\n  #include <stdint.h>\r\n\r\n\r\n\r\nuint8_t @|\\color{red}\\bfseries byte|@ = 0x12;\r\n\r\nuint16_t @|\\color{blue}\\bfseries word|@ = 0x1234;\r\n\r\nuint32_t @|\\color{green}\\bfseries dword|@ = 0x12345678;\r\n\r\nuint64_t @|\\color{magenta}\\bfseries qword|@ = 0x123456789abcdef;\r\n\r\nunsigned __int128 @|\\color{cyan}\\bfseries dqword1|@ =  (__int128) \r\n0x123456789abcdef;\r\n\r\nunsigned __int128 @|\\color{cyan}\\bfseries dqword2|@ =  (__int128) \r\n0x123456789abcdef << 64;\r\n\r\n\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n        return 0;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  0804a018 <byte>:\r\n\r\n   804a018:       12 00                   adc    al,BYTE PTR \r\n  [eax]\r\n\r\n  \r\n\r\n  0804a01a <word>:\r\n\r\n   804a01a:       34 12                   xor    al,0x12\r\n\r\n  \r\n\r\n  0804a01c <dword>:\r\n\r\n   804a01c:       78 56                   js     804a074 \r\n  <_end+0x48>\r\n\r\n   804a01e:       34 12                   xor    al,0x12\r\n\r\n  \r\n\r\n  0804a020 <qword>:\r\n\r\n   804a020:       ef                      out    dx,eax\r\n\r\n   804a021:       cd ab                   int    0xab\r\n\r\n   804a023:       89 67 45                mov    DWORD PTR \r\n  [edi+0x45],esp\r\n\r\n   804a026:       23 01                   and    eax,DWORD PTR \r\n  [ecx]\r\n\r\n  \r\n\r\n  0000000000601040 <dqword1>:\r\n\r\n    601040:       ef                      out    dx,eax\r\n\r\n    601041:       cd ab                   int    0xab\r\n\r\n    601043:       89 67 45                mov    DWORD PTR \r\n  [rdi+0x45],esp\r\n\r\n    601046:       23 01                   and    eax,DWORD PTR \r\n  [rcx]\r\n\r\n    601048:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    60104a:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    60104c:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    60104e:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n  \r\n\r\n  0000000000601050 <dqword2>:\r\n\r\n    601050:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    601052:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    601054:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    601056:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    601058:       ef                      out    dx,eax\r\n\r\n    601059:       cd ab                   int    0xab\r\n\r\n    60105b:       89 67 45                mov    DWORD PTR \r\n  [rdi+0x45],esp\r\n\r\n    60105e:       23 01                   and    eax,DWORD PTR \r\n  [rcx]\r\n\r\n  \r\n\r\ngcc generates the variables byte, word, dword, qword, dqword1, \r\ndword2, written earlier, with their respective values highlighted \r\nin the same colors; variables of the same type are also \r\nhighlighted in the same color. Since this is data section, the \r\nassembly listing carries no meaning. When byte is declared with \r\nuint8_t, gcc guarantees that the size of byte is always 1 byte. \r\nBut, an alert reader might notice the 00 value next to the 12 \r\nvalue in the byte variable. This is normal, as gcc avoid memory \r\nmisalignment by adding extra padding bytespadding bytes. To make \r\nit easier to see, we look at readelf output of .data section:\r\n\r\n\r\n\r\n$ readelf -x .data hello\r\n\r\n\r\n\r\nthe output is (the colors mark which values belong to which \r\nvariables):\r\n\r\nHex dump of section '.data':\r\n\r\n  0x00601020 00000000 00000000 00000000 00000000 ................\r\n\r\n  0x00601030 12003412 78563412 efcdab89 67452301 ..4.xV4.....gE#.\r\n\r\n  0x00601040 efcdab89 67452301 00000000 00000000 ....gE#.........\r\n\r\n  0x00601050 00000000 00000000 efcdab89 67452301 ............gE#.\r\n\r\nAs can be seen in the readelf output, variables are allocated \r\nstorage space according to their types and in the declared order \r\nby the programmer (the colors correspond the the variables). \r\nIntel is a little-endian machine, which means smaller addresses \r\nhold bytes with smaller values, larger addresses hold byte with \r\nlarger values. For example, 0x1234 is displayed as 34 12; that \r\nis, 34 appears first at address 0x601032, then 12 at 0x601033. \r\nThe decimal values within a byte is unchanged, so we see 34 12 \r\ninstead of 43 21. This is quite confusing at first, but you will \r\nget used to it soon.\r\n\r\nAlso, isn't it redundant when char type is always 1 byte already \r\nand why do we bother adding int8_t? The truth is, char type is \r\nnot guaranteed to be 1 byte in size, but only the minimum of 1 \r\nbyte in size. In C, a byte is defined to be the size of a char, \r\nand a char is defined to be smallest addressable unit of the \r\nunderlying hardware platform. There are hardware devices that the \r\nsmallest addressable unit is 16 bit or even bigger, which means \r\nchar is 2 bytes in size and a “byte” in such platforms is \r\nactually 2 units of 8-bit bytes.\r\n\r\nNot all architectures support the double quadword type. Still, \r\ngcc does provide support for 128-bit number and generate code \r\nwhen a CPU supports it (that is, a CPU must be 64-bit). By \r\nspecifying a variable of type __int128 or unsigned __int128, we \r\nget a 128-bit variable. If a CPU does not support 64-bit mode, \r\ngcc throws an error.\r\n\r\nThe data types in C, which represents the fundamental data types, \r\nare also called unsigned numbers. Other than numerical \r\ncalculations, unsigned numbers are used as a tool for structuring \r\ndata in memory; we will see this application later on in the book, \r\nwhen various data structures are organized into bit groups.\r\n\r\nIn all the examples above, when the value of a variable with \r\nsmaller size is assigned to a variable with larger size, the \r\nvalue easily fits in the larger variable. On the contrary, the \r\nvalue of a variable with larger size is assigned to a variable \r\nwith smaller size, two scenarios occur: \r\n\r\n• The value is greater than the maximum value of the variable \r\n  with smaller layout, so it needs truncating to the size of the \r\n  variable and causing incorrect value.\r\n\r\n• The value is smaller than the maximum value of the variable \r\n  with a smaller layout, so it fits the variable.\r\n\r\nHowever, the value might be unknown until runtime and can be \r\nvalue, it is best not to let such implicit conversion handled by \r\nthe compiler, but explicitly controlled by a programmer. \r\nOtherwise it will cause subtle bugs that are hard to catch as the \r\nerroneous values might rarely be used to reproduce the bugs.\r\n\r\n  Pointer Data Types\r\n\r\nPointers are variables that hold memory addresses. x86 works with \r\n2 types of pointers:\r\n\r\n  Near pointer is a 16-bit/32-bit offset within a segment, also \r\n  called effective address.\r\n\r\n  Far pointer is also an offset like a near pointer, but with an \r\n  explicit segment selector.\r\n\r\n\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/04/pointer_data_type.pdf>\r\n     \r\n\r\n\r\n\r\nC only provides support for near pointers, since far pointers are platform \r\ndependent. In application code, you can assume that \r\nthe address of current segment starts at 0, so the offset is \r\nactually any memory address from 0 to the maximum address.\r\n\r\n  Source\r\n\r\n  #include <stdint.h>\r\n\r\n\r\n\r\nint8_t i = 0;\r\n\r\nint8_t @|\\color{red}\\bfseries *p1|@ =  (int8_t *) 0x1234;\r\n\r\nint8_t @|\\color{blue}\\bfseries *p2|@ =  &i;\r\n\r\n\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n        return 0;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  0000000000601030 <p1>:\r\n\r\n    601030:       34 12                   xor    al,0x12\r\n\r\n    601032:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    601034:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    601036:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n  0000000000601038 <p2>:\r\n\r\n    601038:       41 10 60 00             adc    BYTE PTR \r\n  [r8+0x0],spl\r\n\r\n    60103c:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    60103e:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n  \r\n\r\n  Disassembly of section .bss:\r\n\r\n  \r\n\r\n  0000000000601040 <__bss_start>:\r\n\r\n    601040:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n  0000000000601041 <i>:\r\n\r\n    601041:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    601043:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    601045:       00 00                   add    BYTE PTR \r\n  [rax],al\r\n\r\n    601047:       00                      .byte 0x0\r\n\r\nThe pointer p1 holds a direct address with the value 0x1234. The \r\npointer p2 holds the address of the variable i. Note that both \r\nthe pointers are 8 bytes in size (or 4-byte, if 32-bit).\r\n\r\n  Bit Field Data Type\r\n\r\nA bit fieldbit field is a contiguous sequence of bits. Bit fields \r\nallow data structuring at bit level. For example, a 32-bit data \r\ncan hold multiple bit fields that represent multiples different \r\npieces of information, such as bits 0-4 specifies the size of a \r\ndata structure, bit 5-6 specifies permissions and so on. Data \r\nstructures at the bit level are common for low-level programming. \r\n\r\n\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/04/bit_field_data_type.pdf>\r\n     \r\n\r\n\r\n\r\n  Source\r\n\r\n  struct bit_field {\r\n\r\n    int data1:8;\r\n\r\n    int data2:8;\r\n\r\n    int data3:8;\r\n\r\n    int data4:8;\r\n\r\n};\r\n\r\n\r\n\r\nstruct bit_field2 {\r\n\r\n    int data1:8;\r\n\r\n    int data2:8;\r\n\r\n    int data3:8;\r\n\r\n    int data4:8;\r\n\r\n    char data5:4;\r\n\r\n};\r\n\r\n\r\n\r\nstruct normal_struct {\r\n\r\n    int data1;\r\n\r\n    int data2;\r\n\r\n    int data3;\r\n\r\n    int data4;\r\n\r\n};\r\n\r\n\r\n\r\nstruct normal_struct @|\\color{red}\\bfseries ns|@ = {\r\n\r\n    .data1 = @|\\color{red}\\bfseries 0x12345678|@,\r\n\r\n    .data2 = @|\\color{red}\\bfseries 0x9abcdef0|@,\r\n\r\n    .data3 = @|\\color{red}\\bfseries 0x12345678|@,\r\n\r\n    .data4 = @|\\color{red}\\bfseries 0x9abcdef0|@,\r\n\r\n};\r\n\r\n\r\n\r\nint @|\\color{blue}\\bfseries i|@ = 0x12345678;\r\n\r\n\r\n\r\nstruct bit_field @|\\color{magenta}\\bfseries bf|@ = {\r\n\r\n    .data1 = @|\\color{magenta}\\bfseries 0x12|@,\r\n\r\n    .data2 = @|\\color{magenta}\\bfseries 0x34|@,\r\n\r\n    .data3 = @|\\color{magenta}\\bfseries 0x56|@,\r\n\r\n    .data4 = @|\\color{magenta}\\bfseries 0x78|@\r\n\r\n};\r\n\r\n\r\n\r\nstruct bit_field2 @|\\color{green}\\bfseries bf2|@ = {\r\n\r\n    .data1 = @|\\color{green}\\bfseries 0x12|@,\r\n\r\n    .data2 = @|\\color{green}\\bfseries 0x34|@,\r\n\r\n    .data3 = @|\\color{green}\\bfseries 0x56|@,\r\n\r\n    .data4 = @|\\color{green}\\bfseries 0x78|@,\r\n\r\n    .data5 = @|\\color{green}\\bfseries 0xf|@\r\n\r\n};\r\n\r\n\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  Each variable and its value are given a unique color in the \r\n  assembly listing below:\r\n\r\n  0804a018 <ns>:\r\n\r\n   804a018: 78 56                   js     804a070 <_end+0x34>\r\n\r\n   804a01a: 34 12                   xor    al,0x12\r\n\r\n   804a01c: f0 de bc 9a 78 56 34    lock fidivr WORD PTR \r\n  [edx+ebx*4+0x12345678]\r\n\r\n   804a023: 12 \r\n\r\n   804a024: f0 de bc 9a 78 56 34    lock fidivr WORD PTR \r\n  [edx+ebx*4+0x12345678]\r\n\r\n   804a02b: 12 \r\n\r\n  0804a028 <i>:\r\n\r\n   804a028: 78 56                   js     804a080 <_end+0x44>\r\n\r\n   804a02a: 34 12                   xor    al,0x12\r\n\r\n  0804a02c <bf>:\r\n\r\n   804a02c: 12 34 56                adc    dh,BYTE PTR \r\n  [esi+edx*2]\r\n\r\n   804a02f: 78 12                   js     804a043 <_end+0x7>\r\n\r\n  0804a030 <bf2>:\r\n\r\n   804a030: 12 34 56                adc    dh,BYTE PTR \r\n  [esi+edx*2]\r\n\r\n   804a033: 78 0f                   js     804a044 <_end+0x8>\r\n\r\n   804a035: 00 00                   add    BYTE PTR [eax],al\r\n\r\n   804a037: 00                      .byte 0x0\r\n\r\nThe sample code creates 4 variables: ns, i, bf, bf2. The \r\ndefinition of normal_struct and bit_field structs both specify 4 \r\nintegers. bit_field specifies additional information next to its \r\nmember name, separated by a colon, e.g. .data1 : 8. This extra \r\ninformation is the bit width of each bit group. It means, even \r\nthough defined as an int, .data1 only consumes 8 bit of \r\ninformation. If additional data members are specified after \r\n.data1, two scenarios happen:\r\n\r\n• If the new data members fit within the remaining bits after \r\n  .data, which are 24 bits[footnote:\r\nSince .data1 is declared as an int, 32 bits are still allocated, \r\nbut .data1 can only access 8 bits of information.\r\n], then the total size of bit_field struct is still 4 bytes, or \r\n  32 bits.\r\n\r\n• If the new data members don't fit, then the remaining 24 bits \r\n  (3 bytes) are still allocated. However, the new data members \r\n  are allocated brand new storages, without using the previous 24 \r\n  bits.\r\n\r\nIn the example, the 4 data members: .data1, .data2, .data3 and \r\n.data4, each can access 8 bits of information, and together can \r\naccess all of 4 bytes of the integer first declared by .data1. As \r\ncan be seen by the generated assembly code, the values of bf are \r\nfollow natural order as written in the C code: 12 34 56 78, since \r\neach value is a separate members. In contrast, the value of i is \r\na number as a whole, so it is subject to the rule of little \r\nendianess and thus contains the value 78 56 34 12. Note that at \r\n804a02f, is the address of the final byte in bf, but next to it \r\nis a number 12, despite 78 is the last number in it. This extra \r\nnumber 12 does not belong to the value of bf. objdump is just \r\nbeing confused that 78 is an opcode; 78 corresponds to js \r\ninstruction, and it requires an operand. For that reason, objdump \r\ngrabs whatever the next byte after 78 and put it there. objdump \r\nis a tool to display assembly code after all. A better tool to \r\nuse is gdb that we will learn in the next chapter. But for this \r\nchapter, objdump suffices.\r\n\r\nUnlike bf, each data member in ns is allocated fully as an \r\ninteger, 4 bytes each, 16 bytes in total. As we can see, bit \r\nfield and normal struct are different: bit field structure data \r\nat the bit level, while normal struct works at byte level.\r\n\r\nFinally, the struct of bf2[footnote:\r\nbit_field2\r\n] is the same of bf[footnote:\r\nbit_field\r\n], except it contains one more data member: .data5, and is \r\ndefined as an integer. For this reason, another 4 bytes are \r\nallocated just for .data5, even though it can only access 8 bits \r\nof information, and the final value of bf2 is: 12 34 56 78 0f 00 \r\n00 00. The remaining 3 bytes must be accessed by the mean of a \r\npointer, or casting to another data type that can fully access \r\nall 4 bytes..\r\n\r\nWhat happens when the definition of bit_field struct and bf \r\nvariable are changed to:\r\n\r\n  struct bit_field {\r\n\r\n      int data1:8;\r\n\r\n  };\r\n\r\n  struct bit_field bf = {\r\n\r\n      .data1 = 0x1234,\r\n\r\n  };\r\n\r\n  What will be the value of .data1?\r\n\r\n  What happens when the definition of bit_field2 struct is \r\n  changed to:\r\n\r\n  struct bit_field2 {\r\n\r\n      int data1:8;\r\n\r\n      int data5:32;\r\n\r\n  };\r\n\r\n  What is layout of a variable of type bit_field2?\r\n\r\n  String Data Types\r\n\r\nAlthough share the same name, string as defined by x86 is \r\ndifferent than a string in C. x86 defines string as “continuous \r\nsequences of bits, bytes, words, or doublewords”. On the other \r\nhand, C defines a string as an array of 1-byte characters with a \r\nzero as the last element of the array to make a null-terminated \r\nstring. This implies that strings in x86 are arrays, not C \r\nstrings. A programmer can define an array of bytes, words or \r\ndoublewords with char or uint8_t, short or uint16_t and int or \r\nuint32_t, except an array of bits. However, such a feature can be \r\neasily implemented, as an array of bits is essentially any array \r\nof bytes, or words or doublewords, but operates at the bit level.\r\n\r\nThe following code demonstrates how to define array (string) data \r\ntypes:\r\n\r\n  Source\r\n\r\n  #include <stdint.h>\r\n\r\n\r\n\r\nuint8_t @|\\color{red}\\bfseries a8[2]|@ = {0x12, 0x34};\r\n\r\nuint16_t @|\\color{blue}\\bfseries a16[2]|@ = {0x1234, 0x5678};\r\n\r\nuint32_t @|\\color{magenta}\\bfseries a32[2]|@ = {0x12345678, \r\n0x9abcdef0};\r\n\r\nuint64_t @|\\color{green}\\bfseries a64[2]|@ = {0x123456789abcdef0, \r\n0x123456789abcdef0};\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  0804a018 <a8>:\r\n\r\n   804a018: 12 34 00                adc    dh,BYTE PTR \r\n  [eax+eax*1]\r\n\r\n   804a01b: 00 34 12                add    BYTE PTR \r\n  [edx+edx*1],dh\r\n\r\n  0804a01c <a16>:\r\n\r\n   804a01c: 34 12                   xor    al,0x12\r\n\r\n   804a01e: 78 56                   js     804a076 <_end+0x3a>\r\n\r\n  0804a020 <a32>:\r\n\r\n   804a020: 78 56                   js     804a078 <_end+0x3c>\r\n\r\n   804a022: 34 12                   xor    al,0x12\r\n\r\n   804a024: f0 de bc 9a f0 de bc    lock fidivr WORD PTR \r\n  [edx+ebx*4-0x65432110]\r\n\r\n   804a02b: 9a \r\n\r\n  0804a028 <a64>:\r\n\r\n   804a028: f0 de bc 9a 78 56 34    lock fidivr WORD PTR \r\n  [edx+ebx*4+0x12345678]\r\n\r\n   804a02f: 12 \r\n\r\n   804a030: f0 de bc 9a 78 56 34    lock fidivr WORD PTR \r\n  [edx+ebx*4+0x12345678]\r\n\r\n   804a037: 12 \r\n\r\nDespite a8 is an array with 2 elements, each is 1-byte long, but \r\nit is still allocated with 4 bytes. Again, to ensure natural \r\nalignment for best performance, gcc pads extra zero bytes. As \r\nshown in the assembly listing, the actual value of a8 is 12 34 00 \r\n00, with a8[0] equals to 12 and a8[1] equals to 34.\r\n\r\nThen it comes a16 with 2 elements, each is 2-byte long. Since 2 \r\nelements are 4 bytes in total, which is in the natural alignment, \r\ngcc pads no byte. The value of a16 is 34 12 78 56, with a16[0] \r\nequals to 34 12 and a16[1] equals to 78 56. Note that, objdump is \r\nconfused again, as de is the opcode for the instruction fidivr \r\n(short of reverse divide) that requires another operand, so \r\nobjdump grabs whatever the next bytes that makes sense to it for \r\ncreating “an operand”. Only the highlighted values belong to a32. \r\n\r\nNext is a32, with 2 elements, 4 bytes each. Similar to above \r\narrays, the value of a32[0] is 78 56 34 12, the value of a32[1] \r\nis f0 de bc 9a, exactly what is assigned in the C code.\r\n\r\nFinally is a64, also with 2 elements, but 8 bytes each. The total \r\nsize of a64 is 16 bytes, which is in the natural alignment, \r\ntherefore no padding bytes added. The values of both a64[0] and \r\na64[1] are the same: f0 de bc 9a 78 56 34 12, that got \r\nmisinterpreted to fidivr instruction.\r\n\r\n[float Figure:\r\n[Figure 0.13:\r\na8, a16, a32 and a64 memory layouts\r\n]\r\n\r\na8:  \r\n+----------+\r\n| 12 | 34  |\r\n+----------+\r\n\r\n\r\na16: \r\n+--------------------+\r\n| 34 12   | 78 56    |\r\n+--------------------+\r\n\r\n\r\na32: \r\n+----------------------------------------+\r\n| 78 56 34 12       | f0 de bc 9a        |\r\n+----------------------------------------+\r\n\r\n\r\na64: \r\n+---------------------------------------------------------------------------------+\r\n| f0 de bc 9a 78 56 34 12               | f0 de bc 9a 78 56 34 12   \r\n             |\r\n+---------------------------------------------------------------------------------+\r\n\r\n]\r\n\r\nHowever, beyond one-dimensional arrays that map directly to \r\nhardware string type, C provides its own syntax for \r\nmulti-dimensional arrays:\r\n\r\n  Source\r\n\r\n  #include <stdint.h>\r\n\r\n\r\n\r\nuint8_t @|\\color{red}\\bfseries a2[2][2]|@ = {\r\n\r\n    {0x12, 0x34},\r\n\r\n    {0x56, 0x78}\r\n\r\n};\r\n\r\n\r\n\r\nuint8_t @|\\color{blue}\\bfseries a3[2][2][2]|@ = {\r\n\r\n    {{0x12, 0x34},\r\n\r\n     {0x56, 0x78}},\r\n\r\n    {{0x9a, 0xbc},\r\n\r\n     {0xde, 0xff}},\r\n\r\n};\r\n\r\n\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  0804a018 <a2>:\r\n\r\n   804a018: 12 34 56                adc    dh,BYTE PTR \r\n  [esi+edx*2]\r\n\r\n   804a01b: 78 12                   js     804a02f <_end+0x7>\r\n\r\n  0804a01c <a3>:\r\n\r\n   804a01c: 12 34 56                adc    dh,BYTE PTR \r\n  [esi+edx*2]\r\n\r\n   804a01f: 78 9a                   js     8049fbb \r\n  <_DYNAMIC+0xa7>\r\n\r\n   804a021: bc                      .byte 0xbc\r\n\r\n   804a022: de ff                   fdivrp st(7),st\r\n\r\n  \r\n\r\nTechnically, multi-dimensional arrays are like normal arrays: in \r\nthe end, the total size is translated into flat allocated bytes. \r\nA 2 x 2 array is allocated with 4 bytes; a 2\\times2\\times2\r\n array \r\nis allocated with 8 bytes, as can be seen in the assembly listing \r\nof a2[footnote:\r\nAgain, objdump is confused and put the number 12 next to 78 in a3 \r\nlisting.\r\n] and a3. In low-level assembly code, the representation is the \r\nsame between a[4] and a[2][2]. However, in high-level C code, the \r\ndifference is tremendous. The syntax of multi-dimensional array \r\nenables a programmer to think with higher level concepts, instead \r\nof translating manually from high-level concepts to low-level \r\ncode and work with high-level concepts in his head at the same \r\ntime. \r\n\r\nThe following two-dimensional array can hold a list of 2 names \r\nwith the length of 10:\r\n\r\n  char names[2][10] = {\r\n\r\n    \"John Doe\",\r\n\r\n    \"Jane Doe\"\r\n\r\n};\r\n\r\n  To access a name, we simply adjust the column index[footnote:\r\nThe left index is called column index since it changes the index \r\nbased on a column.\r\n] e.g. names[0], names[1]. To access individual character within \r\n  a name, we use the row index[footnote:\r\nSame with column index, the right index is called row index since \r\nit changes the index based on a row.\r\n] e.g. names[0][0] gives the character “J”, names[0][1] gives the \r\n  character “o” and so on.\r\n\r\n  Without such syntax, we need to create a 20-byte array e.g. \r\n  names[20], and whenever we want to access a character e.g. to \r\n  check if the names contains with a number in it, we need to \r\n  calculate the index manually. It would be distracting, since we \r\n  constantly need to switch thinkings between the actual problem \r\n  and the translate problem. \r\n\r\n  Since this is a repeating pattern, C abstracts away this \r\n  problem with the syntax for define and manipulating \r\n  multi-dimensional array. Through this example, we can clearly \r\n  see the power of abstraction through language can give us. It \r\n  would be ideal if a programmer is equipped with such power to \r\n  define whatever syntax suitable for a problem at hands. Not \r\n  many languages provide such capacity. Fortunately, through C \r\n  macro, we can partially achieve that goal .\r\n\r\nIn all cases, an array is guaranteed to generate contiguous bytes \r\nof memory, regardless of the dimensions it has.\r\n\r\nWhat is the difference between a multi-dimensional array and an \r\narray of pointers, or even pointers of pointers?\r\n\r\n  Examine compiled code\r\n\r\nThis section will explore how compiler transform high level code \r\ninto assembly code that CPU can execute, and see how common \r\nassembly patterns help to create higher level syntax. -S option \r\nis added to objdump to better demonstrate the connection between \r\nhigh and low level code.\r\n\r\nIn this section, the option --no-show-raw-insn is added to \r\nobjdump command to omit the opcodes for clarity:\r\n\r\n\r\n\r\n$ objdump --no-show-raw-insn -M intel -S -D <object file> | less \r\n\r\n\r\n\r\n  Data Transfer\r\n\r\nPrevious section explores how various types of data are created, \r\nand how they are laid out in memory. Once memory storages are \r\nallocated for variables, they must be accessible and writable. \r\nData transfer instructions move data (bytes, words, doublewords \r\nor quadwords) between memory and registers, and between \r\nregisters, effectively read from a storage source and write to \r\nanother storage source.\r\n\r\n  Source\r\n\r\n  #include <stdint.h>\r\n\r\n\r\n\r\nint32_t i = 0x12345678;\r\n\r\n\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n        int j = i;\r\n\r\n        int k = 0xabcdef;\r\n\r\n\r\n\r\n        return 0;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  080483db <main>:\r\n\r\n  #include <stdint.h>\r\n\r\n  int32_t i = 0x12345678;\r\n\r\n  int main(int argc, char *argv[]) {\r\n\r\n   80483db:       push   ebp\r\n\r\n   80483dc:       mov    ebp,esp\r\n\r\n   80483de:       sub    esp,0x10\r\n\r\n          int j = i;\r\n\r\n   80483e1:       mov    eax,ds:0x804a018\r\n\r\n   80483e6:       mov    DWORD PTR [ebp-0x8],eax\r\n\r\n          int k = 0xabcdef;\r\n\r\n   80483e9:       mov    DWORD PTR [ebp-0x4],0xabcdef\r\n\r\n          return 0;\r\n\r\n   80483f0:       mov    eax,0x0\r\n\r\n  }\r\n\r\n   80483f5:       leave  \r\n\r\n   80483f6:       ret    \r\n\r\n   80483f7:       xchg   ax,ax\r\n\r\n   80483f9:       xchg   ax,ax\r\n\r\n   80483fb:       xchg   ax,ax\r\n\r\n   80483fd:       xchg   ax,ax\r\n\r\n   80483ff:       nop\r\n\r\nThe general data movement is performed with the mov instruction. \r\nNote that despite the instruction being called mov, it actually \r\ncopies data from one destination to another.\r\n\r\nThe red instruction copies data from the register esp to the \r\nregister ebp. This mov instruction moves data between registers \r\nand is assigned the opcode 89.\r\n\r\nThe blue instructions copies data from one memory location (the i \r\nvariable) to another (the j variable). There exists no data \r\nmovement from memory to memory; it requires two mov instructions, \r\none for copying the data from a memory location to a register, \r\nand one for copying the data from the register to the destination \r\nmemory location.\r\n\r\nThe pink instruction copies an immediate value into memory. \r\nFinally, the green instruction copies immediate data into a \r\nregister.\r\n\r\n  Expressions\r\n\r\n  Source\r\n\r\n  int expr(int i, int j)\r\n\r\n{\r\n\r\n    int add            = i + j;\r\n\r\n    int sub            = i - j;\r\n\r\n    int mul            = i * j;\r\n\r\n    int div            = i / j;\r\n\r\n    int mod            = i % j;\r\n\r\n    int neg            = -i;\r\n\r\n    int and            = i & j;\r\n\r\n    int or             = i | j;\r\n\r\n    int xor            = i ^ j;\r\n\r\n    int not            = ~i;\r\n\r\n    int shl            = i << 8;\r\n\r\n    int shr            = i >> 8;\r\n\r\n    char equal1        = (i == j);\r\n\r\n    int equal2         = (i == j);\r\n\r\n    char greater       = (i > j);\r\n\r\n    char less          = (i < j);\r\n\r\n    char greater_equal = (i >= j);\r\n\r\n    char less_equal    = (i <= j);\r\n\r\n    int logical_and    = i && j;\r\n\r\n    int logical_or     = i || j;\r\n\r\n    ++i;\r\n\r\n    --i;\r\n\r\n    int i1             = i++;\r\n\r\n    int i2             = ++i;\r\n\r\n    int i3             = i--;\r\n\r\n    int i4             = --i;\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  The full assembly listing is really long. For that reason, we \r\n  examine expression by expression.\r\n\r\n  Expression: int add = i + j;\r\n\r\n     80483e1:       mov    edx,DWORD PTR [ebp+0x8]\r\n\r\n     80483e4:       mov    eax,DWORD PTR [ebp+0xc]\r\n\r\n     80483e7:       add    eax,edx\r\n\r\n     80483e9:       mov    DWORD PTR [ebp-0x34],eax\r\n\r\n    The assembly code is straight forward: variable i and j are \r\n    stored in eax and edx respectively, then added together with \r\n    the add instruction, and the final result is stored into eax. \r\n    Then, the result is saved into the local variable add, which \r\n    is at the location [ebp-0x34].\r\n\r\n  Expression: int sub = i - j;\r\n\r\n    80483ec:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    80483ef:       sub    eax,DWORD PTR [ebp+0xc]\r\n\r\n    80483f2:       mov    DWORD PTR [ebp-0x30],eax\r\n\r\n    Similar to add instruction, x86 provides a sub instruction \r\n    for subtraction. Hence, gcc translates a subtraction into sub \r\n    instruction, with eax is reloaded with i, as eax still \r\n    carries the result from previous expression. Then, j is \r\n    subtracted from i. After the subtraction, the value is saved \r\n    into the variable sub, at location [ebp-0x30].\r\n\r\n  Expression: int mul = i * j;\r\n\r\n    80483f5:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    80483f8:       imul   eax,DWORD PTR [ebp+0xc]\r\n\r\n    80483fc:       mov    DWORD PTR [ebp-0x34],eax\r\n\r\n    Similar to sub instruction, only eax is reloaded, since it \r\n    carries the result of previous calculation. imul performs \r\n    signed multiply[footnote:\r\nUnsigned multiply is perform by mul instruction.\r\n]. eax is first loaded with i, then is multiplied with j and \r\n    stored the result back into eax, then stored into the \r\n    variable mul at location [ebp-0x34].\r\n\r\n  Expression: int div = i / j;\r\n\r\n    80483ff:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    8048402:       cdq    \r\n\r\n    8048403:       idiv   DWORD PTR [ebp+0xc]\r\n\r\n    8048406:       mov    DWORD PTR [ebp-0x30],eax\r\n\r\n    Similar to imul, idiv performs sign divide. But, different \r\n    from imul above idiv only takes one operand:\r\n\r\n    1. First, i is reloaded into eax. \r\n\r\n    2. Then, cdq converts the double word value in eax into a \r\n      quadword value stored in the pair of registers edx:eax, by \r\n      copying the signed (bit 31[superscript:th]) of the value in eax into every bit position in edx. The pair \r\n      edx:eax is the dividend, which is the variable i, and the \r\n      operand to idiv is the divisor, which is the variable j. \r\n\r\n    3. After the calculation, the result is stored into the pair \r\n      edx:eax registers, with the quotient in eax and remainder \r\n      in edx. The quotient is stored in the variable div, at \r\n      location [ebp-0x30].\r\n\r\n  Expression: int mod = i % j;\r\n\r\n    8048409:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    804840c:       cdq    \r\n\r\n    804840d:       idiv   DWORD PTR [ebp+0xc]\r\n\r\n    8048410:       mov    DWORD PTR [ebp-0x2c],edx\r\n\r\n    The same idiv instruction also performs the modulo operation, \r\n    since it also calculates a remainder and stores in the \r\n    variable mod, at location [ebp-0x2c].\r\n\r\n  Expression: int neg = -i;\r\n\r\n    8048413:       mov    eax,DWORD PTR [ebp+0x8] \r\n\r\n    8048416:       neg    eax                     \r\n\r\n    8048418:       mov    DWORD PTR [ebp-0x28],eax\r\n\r\n    neg replaces the value of operand (the destination operand) \r\n    with its two's complement (this operation is equivalent to \r\n    subtracting the operand from 0). In this example, the value i \r\n    in eax is replaced replaced with -i using neg instruction. \r\n    Then, the new value is stored in the variable neg at \r\n    [ebp-0x28].\r\n\r\n  Expression: int and = i & j;\r\n\r\n    804841b:        mov    eax,DWORD PTR [ebp+0x8] \r\n\r\n    804841e:        and    eax,DWORD PTR [ebp+0xc] \r\n\r\n    8048421:        mov    DWORD PTR [ebp-0x24],eax\r\n\r\n    and performs a bitwise AND operation on two operands, and \r\n    stores the result in the destination operand, which is the \r\n    variable and at [ebp-0x24].\r\n\r\n  Expression: int or = i | j;\r\n\r\n    8048424:       mov    eax,DWORD PTR [ebp+0x8] \r\n\r\n    8048427:       or     eax,DWORD PTR [ebp+0xc] \r\n\r\n    804842a:       mov    DWORD PTR [ebp-0x20],eax\r\n\r\n    Similar to and instruction, or performs a bitwise OR \r\n    operation on two operands, and stores the result in the \r\n    destination operand, which is the variable or at [ebp-0x20] \r\n    in this case.\r\n\r\n  Expression: int xor = i ^ j;\r\n\r\n    804842d:       mov    eax,DWORD PTR [ebp+0x8] \r\n\r\n    8048430:       xor    eax,DWORD PTR [ebp+0xc] \r\n\r\n    8048433:       mov    DWORD PTR [ebp-0x1c],eax\r\n\r\n    Similar to and/or instruction, xor performs a bitwise XOR \r\n    operation on two operands, and stores the result in the \r\n    destination operand, which is the variable xor at [ebp-0x1c].\r\n\r\n  Expression: int not = ~i;\r\n\r\n    8048436:       mov    eax,DWORD PTR [ebp+0x8] \r\n\r\n    8048439:       not    eax                     \r\n\r\n    804843b:       mov    DWORD PTR [ebp-0x18],eax\r\n\r\n    not performs a bitwise NOT operation (each 1 is set to 0, and \r\n    each 0 is set to 1) on the destination operand and stores the \r\n    result in the destination operand location, which is the \r\n    variable not at [ebp-0x18]. \r\n\r\n  Expression: int shl = i << 8;\r\n\r\n    804843e:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    8048441:\t   shl    eax,0x8\r\n\r\n    8048444:\t   mov    DWORD PTR [ebp-0x14],eax\r\n\r\n    shl (shift logical left) shifts the bits in the destination \r\n    operand to the left by the number of bits specified in the \r\n    source operand. In this case, eax stores i and shl shifts eax \r\n    by 8 bits to the left. A different name for shl is sal (shift \r\n    arithmetic left). Both can be used synonymous. Finally, the \r\n    result is stored in the variable shl at [ebp-0x14].\r\n\r\n    Here is a visual demonstration of shl/sal and shr \r\n    instructions:\r\n\r\n    \r\n\r\n    After shifting to the left, the right most bit is set for \r\n    Carry Flag in EFLAGS register.\r\n\r\n  Expression: int shr = i >> 8;\r\n\r\n    8048447:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    804844a:   \tsar    eax,0x8\r\n\r\n    804844d:   \tmov    DWORD PTR [ebp-0x10],eax\r\n\r\n    sar is similar to shl/sal, but shift bits to the right and \r\n    extends the sign bit. For right shift, shr and sar are two \r\n    different instructions. shr differs to sar is that it does \r\n    not extend the sign bit. Finally, the result is stored in the \r\n    variable shr at [ebp-0x10].\r\n\r\n    In the figure (b), notice that initially, the sign bit is 1, \r\n    but after 1-bit and 10-bit shiftings, the shifted-out bits \r\n    are filled with zeros.\r\n\r\n    [float Figure:\r\n[Figure 0.14:\r\nSAR Instruction Operation (Source: Figure 7-8, Volume 1)\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/04/sar.pdf>\r\n     \r\n]\r\n\r\n    With sar, the sign bit (the most significant bit) is \r\n    preserved. That is, if the sign bit is 0, the new bits always \r\n    get the value 0; if the sign bit is 1, the new bits always \r\n    get the value 1.\r\n\r\n  Expression: char equal1 = (i == j);\r\n\r\n    8048450:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    8048453:       cmp    eax,DWORD PTR [ebp+0xc]\r\n\r\n    8048456:       sete   al                     \r\n\r\n    8048459:       mov    BYTE PTR [ebp-0x41],al\r\n\r\n    cmp and variants of the variants of set instructions make up \r\n    all the logical comparisons. In this expression, cmp compares \r\n    variable i and j; then sete stores the value 1 to al register \r\n    if the comparison from cmp earlier is equal, or stores 0 \r\n    otherwise. The general name for variants of set instruction \r\n    is called SETcc. The suffix cc denotes the condition being \r\n    tested for in EFLAGS register. Appendix B in volume 1, \r\n    “EFLAGS Condition Codes”, lists the conditions it is possible \r\n    to test for with this instruction. Finally, the result is \r\n    stored in the variable equal1 at [ebp-0x41].\r\n\r\n  Expression: int equal2 = (i == j);\r\n\r\n    804845c:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    804845f:       cmp    eax,DWORD PTR [ebp+0xc]\r\n\r\n    8048462:       sete   al\r\n\r\n    8048465:       movzx  eax,al\r\n\r\n    8048468:       mov    DWORD PTR [ebp-0xc],eax\r\n\r\n    Similar to equality comparison, this expression also compares \r\n    for equality, with an exception that the result is stored in \r\n    an int type. For that reason, one more instruction is a \r\n    added: movzx instruction, a variant of mov that copies the \r\n    result into a destination operand and fills the remaining \r\n    bytes with 0. In this case, since eax is 4-byte wide, after \r\n    copying the first byte in al, the remaining bytes of eax are \r\n    filled with 0 to ensure the eax carries the same value as al.\r\n\r\n    [float Figure:\r\n[Figure 0.15:\r\nmovzx instruction\r\n]     [float Figure:\r\n[Sub-Figure a:\r\neax before movzx\r\n]\r\n\r\n\r\n+-----+-----+-----+----+\r\n| 12  | 34  | 56  | 78 |\r\n+-----+-----+-----+----+\r\n\r\n]     [float Figure:\r\n[Sub-Figure b:\r\nafter movzx eax, al\r\n]\r\n\r\n\r\n+-----+-----+-----+----+\r\n| 00  | 00  | 00  | 78 |\r\n+-----+-----+-----+----+\r\n\r\n]     \r\n]\r\n\r\n  Expression: char greater = (i > j);\r\n\r\n    804846b:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    804846e:       cmp    eax,DWORD PTR [ebp+0xc]\r\n\r\n    8048471:       setg   al                     \r\n\r\n    8048474:       mov    BYTE PTR [ebp-0x40],al \r\n\r\n    Similar to equality comparison, but used setg for greater \r\n    comparison instead.\r\n\r\n  Expression: char less = (i < j);\r\n\r\n    8048477:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    804847a:       cmp    eax,DWORD PTR [ebp+0xc]\r\n\r\n    804847d:       setl   al                     \r\n\r\n    8048480:       mov    BYTE PTR [ebp-0x3f],al \r\n\r\n    Applied setl for less comparison.\r\n\r\n  Expression: char greater_equal = (i >= j);\r\n\r\n    8048483:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    8048486:       cmp    eax,DWORD PTR [ebp+0xc]\r\n\r\n    8048489:       setge  al                     \r\n\r\n    804848c:       mov    BYTE PTR [ebp-0x3e],al \r\n\r\n    Applied setge for greater or equal comparison.\r\n\r\n  Expression: char less_equal = (i <= j);\r\n\r\n    804848f:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    8048492:       cmp    eax,DWORD PTR [ebp+0xc]\r\n\r\n    8048495:       setle  al\r\n\r\n    8048498:       mov    BYTE PTR [ebp-0x3d],al\r\n\r\n    Applied setle for less than or equal comparison.\r\n\r\n  Expression: int logical_and = (i && j);\r\n\r\n    804849b:       cmp    DWORD PTR [ebp+0x8],0x0\r\n\r\n    804849f:       je     80484ae <expr+0xd3>    \r\n\r\n    80484a1:       cmp    DWORD PTR [ebp+0xc],0x0\r\n\r\n    80484a5:       je     80484ae <expr+0xd3>    \r\n\r\n    80484a7:       mov    eax,0x1                \r\n\r\n    80484ac:       jmp    80484b3 <expr+0xd8>    \r\n\r\n    80484ae:       mov    eax,0x0                \r\n\r\n    80484b3:       mov    DWORD PTR [ebp-0x8],eax\r\n\r\n    Logical AND operator && is one of the syntaxes that is made \r\n    entirely in software[footnote:\r\nThat is, there is no equivalent assembly instruction implemented \r\nin hardware.\r\n] with simpler instructions. The algorithm from the assembly code \r\n    is simple:\r\n\r\n    1. First, check if i is 0 with the instruction at 0x804849b. \r\n\r\n      (a) If true, jump to 0x80484ae and set eax to 0. \r\n\r\n      (b) Set the variable logical_and to 0, as it is the next \r\n        instruction after 0x80484ae.\r\n\r\n    2. If i is not 0, check if j is 0 with the instruction at \r\n      0x80484a1. \r\n\r\n      (a) If true, jump to 0x80484ae and set eax to 0.\r\n\r\n      (b) Set the variable logical_and to 0, as it is the next \r\n        instruction after 0x80484ae.\r\n\r\n    3. If both i and j are not 0, the result is certainly 1, or \r\n      true.\r\n\r\n      (a) Set it accordingly with the instruction at 0x80484a7. \r\n\r\n      (b) Then jump to the instruction at 0x80484b3 to set the \r\n        variable logical_and at [ebp-0x8] to 1.\r\n\r\n  Expression: int logical_or = (i || j);\r\n\r\n    80484b6:       cmp    DWORD PTR [ebp+0x8],0x0\r\n\r\n    80484ba:       jne    80484c2 <expr+0xe7>    \r\n\r\n    80484bc:       cmp    DWORD PTR [ebp+0xc],0x0\r\n\r\n    80484c0:       je     80484c9 <expr+0xee>    \r\n\r\n    80484c2:       mov    eax,0x1                \r\n\r\n    80484c7:       jmp    80484ce <expr+0xf3>    \r\n\r\n    80484c9:       mov    eax,0x0                \r\n\r\n    80484ce:       mov    DWORD PTR [ebp-0x4],eax\r\n\r\n    Logical OR operator || is similar to logical and above. \r\n    Understand the algorithm is left as an exercise for readers.\r\n\r\n  Expression: ++i; and --i; (or i++ and i--)\r\n\r\n    80484d1:       add    DWORD PTR [ebp+0x8],0x1\r\n\r\n    80484d5:       sub    DWORD PTR [ebp+0x8],0x1\r\n\r\n    The syntax of increment and decrement is similar to logical \r\n    AND and logical OR in that it is made from existing \r\n    instruction, that is add. The difference is that the CPU \r\n    actually does has a built-in instruction, but gcc decided not \r\n    to use the instruction because inc and dec cause a partial \r\n    flag register stall, occurs when an instruction modifies a \r\n    part of the flag register and the following instruction is \r\n    dependent on the outcome of the flags (section 3.5.2.6, Intel Optimization Manual, 2016\r\n    ). The manual even suggests that inc and dec should be \r\n    replaced with add and sub instructions (section 3.5.1.1, Intel Optimization Manual, 2016\r\n    ). \r\n\r\n  Expression: int i1 = i++;\r\n\r\n    80484d9:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    80484dc:       lea    edx,[eax+0x1]\r\n\r\n    80484df:       mov    DWORD PTR [ebp+0x8],edx\r\n\r\n    80484e2:       mov    DWORD PTR [ebp-0x10],eax\r\n\r\n    First, i is copied into eax at 80484d9. Then, the value of \r\n    eax + 0x1 is copied into edx as an effective address at \r\n    80484dc. The lea (load effective address) instruction copies \r\n    a memory address into a register. According to Volume 2, the \r\n    source operand is a memory address specified with one of the \r\n    processors addressing modes. This means, the source operand \r\n    must be specified by the addressing modes defined in \r\n    16-bit/32-bit ModR/M Byte tables, [mod-rm-16] and [mod-rm-32]\r\n    .\r\n\r\n    After loading the incremented value into edx, the value of i \r\n    is increased by 1 at 80484df. Finally, the previous i value \r\n    is stored back to i1 at [ebp-0x8] by the instruction at \r\n    80484e2.\r\n\r\n  Expression: int i2 = ++i;\r\n\r\n    80484e5:       add    DWORD PTR [ebp+0x8],0x1\r\n\r\n    80484e9:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    80484ec:       mov    DWORD PTR [ebp-0xc],eax\r\n\r\n    The primary differences between this increment syntax and the \r\n    previous one are:\r\n\r\n    • add is used instead of lea to increase i directly.\r\n\r\n    • the newly incremented i is stored into i2 instead of the \r\n      old value.\r\n\r\n    • the expression only costs 3 instructions instead of 4.\r\n\r\n    This prefix-increment syntax is faster than the post-fix one \r\n    used previously. It might not matter much which version to \r\n    use if the increment is only used once or a few hundred times \r\n    in a small loop, but it matters when a loop runs millions or \r\n    more times. Also, depends on different circumstances, it is \r\n    more convenient to use one over the other e.g. if i is an \r\n    index for accessing an array, we want to use the old value \r\n    for accessing previous array element and newly incremented i \r\n    for current element.\r\n\r\n  Expression: int i3 = i--;\r\n\r\n    80484ef:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    80484f2:       lea    edx,[eax-0x1]\r\n\r\n    80484f5:       mov    DWORD PTR [ebp+0x8],edx\r\n\r\n    80484f8:       mov    DWORD PTR [ebp-0x8],eax\r\n\r\n    Similar to i++ syntax, and is left as an exercise to readers.\r\n\r\n  Expression: int i4 = --i;\r\n\r\n    80484fb:       sub    DWORD PTR [ebp+0x8],0x1\r\n\r\n    80484ff:       mov    eax,DWORD PTR [ebp+0x8]\r\n\r\n    8048502:       mov    DWORD PTR [ebp-0x4],eax\r\n\r\n    Similar to ++i syntax, and is left as an exercise to readers.\r\n\r\nRead section 3.5.2.4, “Partial Register Stalls” to understand \r\nregister stalls in general.\r\n\r\n  Read the sections from 7.3.1 to 7.3.7 in volume 1.\r\n\r\n  Stack\r\n\r\nA stack is a contiguous array of memory locations that holds a \r\ncollection of discrete data. When a new element is added, a stack \r\ngrows down in memory toward lesser addresses, and shrinks up \r\ntoward greater addresses when an element is removed. x86 uses the \r\nesp register to point to the top of the stack, at the newest \r\nelement. A stack can be originated anywhere in main memory, as \r\nesp can be set to any memory address. x86 provides two operations \r\nfor manipulating stacks: \r\n\r\n• push instruction and its variants add a new element on top of \r\n  the stack\r\n\r\n• pop instructions and its variants remove the top-most element \r\n  from the stack.\r\n\r\n\r\n\r\n\r\n\r\n\r\n+----------+----+                      \r\n| 0x10000  | 00 |                      \r\n+----------+----+                      \r\n| 0x10001  | 00 |                      \r\n+----------+----+                      \r\n| 0x10002  | 00 |                      \r\n+----------+----+                      \r\n| 0x10003  | 00 |                      \r\n+----------+----+               +-----+\r\n| 0x10004  | 12 |  \\leftarrow\r\n  | esp |\r\n+----------+----+               +-----+\r\n\r\n\r\n     \r\n\r\n\r\n+----------+----+                      \r\n| 0x10000  | 00 |                      \r\n+----------+----+                      \r\n| 0x10001  | 00 |                      \r\n+----------+----+               +-----+\r\n| 0x10002  | 78 |  \\leftarrow\r\n  | esp |\r\n                                +-----+\r\n+----------+----+                      \r\n| 0x10003  | 56 |                      \r\n+----------+----+                      \r\n| 0x10004  | 12 |                      \r\n+----------+----+                      \r\n\r\n\r\n     \r\n\r\n\r\n+----------+----+                      \r\n| 0x10000  | 00 |                      \r\n+----------+----+                      \r\n| 0x10001  | 00 |                      \r\n+----------+----+                      \r\n| 0x10002  | 00 |                      \r\n+----------+----+                      \r\n| 0x10003  | 00 |                      \r\n+----------+----+               +-----+\r\n| 0x10004  | 12 |  \\leftarrow\r\n  | esp |\r\n+----------+----+               +-----+\r\n\r\n\r\n\r\n\r\n  Automatic variables\r\n\r\nLocal variables are variables that exist within a scope. A scope \r\nis delimited by a pair of braces: {..}. The most common scope to \r\ndefine local variables is at function scope. However, scope can \r\nbe unnamed, and variables created inside an unnamed scope do not \r\nexist outside of its scope and its inner scope. \r\n\r\nFunction scope:\r\n\r\n  void foo() {\r\n\r\n    int a;\r\n\r\n    int b;\r\n\r\n}\r\n\r\n  a and b are variables local to the function foo.\r\n\r\n\r\n\r\nUnnamed scope:\r\n\r\n  int foo() {\r\n\r\n    int i;\r\n\r\n\r\n\r\n    {\r\n\r\n        int a = 1;\r\n\r\n        int b = 2;\r\n\r\n        {\r\n\r\n            return i = a + b;\r\n\r\n        }\r\n\r\n    }\r\n\r\n}\r\n\r\n  a and b are local to where it is defined and local into its \r\n  inner child scope that return i = a + b. However, they do not \r\n  exist at the function scope that creates i.\r\n\r\nWhen a local variable is created, it is pushed on the stack; when \r\na local variable goes out of scope, it is pop out of the stack, \r\nthus destroyed. When an argument is passed from a caller to a \r\ncallee, it is pushed on the stack; when a callee returns to the \r\ncaller, the arguments are popped out the stack. The local \r\nvariables and arguments are automatically allocated upon enter a \r\nfunction and destroyed after exiting a function, that's why it's \r\ncalled automatic variables.\r\n\r\nA base frame pointer points to the start of the current function \r\nframe, and is kept in ebp register. Whenever a function is \r\ncalled, it is allocated with its own dedicated storage on stack, \r\ncalled stack frame. A stack frame is where all local variables \r\nand arguments of a function are placed on a stack[footnote:\r\nData and only data are exclusively allocated on stack for every \r\nstack frame. No code resides here.\r\n]. \r\n\r\nWhen a function needs a local variable or an argument, it uses \r\nebp to access a variable:\r\n\r\n• All local variables are allocated after the ebp pointer. Thus, \r\n  to access a local variable, a number is subtracted from ebp to \r\n  reach the location of the variable. \r\n\r\n• All arguments are allocated before ebp pointer. To access an \r\n  argument, a number is added to ebp to reach the location of the \r\n  argument. \r\n\r\n• The ebp itself pointer points to the return address of its \r\n  caller.\r\n\r\n\r\n\r\n\r\n+--------------------------------------+---------------------------------------------------------------------------+\r\n|           Previous Frame             |                               Current Frame                               |\r\n+--------------------------------------+-----------------------------+----------+----------------------------------+\r\n|         Function Arguments           |                             |   ebp    |         Local variables          |\r\n+-----+-----+-----+-----------+--------+-----------------------------+----------+-----+-----+-----+-----------+----+\r\n| A1  | A2  | A3  | ........  |  An    |       Return Address        | Old ebp  | L1  | L2  | L3  | ........  | Ln |\r\n+-----+-----+-----+-----------+--------+-----------------------------+----------+-----+-----+-----+-----------+----+\r\n\r\n\r\n\r\n\r\nA = Argument\r\n\r\nL = Local Variable\r\n\r\nHere is an example to make it more concrete:\r\n\r\n  Source\r\n\r\n  int add(int @|\\color{red}\\bfseries a|@, int \r\n@|\\color{green}\\bfseries b|@) {\r\n\r\n    int @|\\color{blue}\\bfseries i|@ = @|\\color{red}\\bfseries a|@ \r\n+ @|\\color{green}\\bfseries b|@;\r\n\r\n\r\n\r\n    return i;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  080483db <add>:\r\n\r\n  #include <stdint.h>\r\n\r\n  int add(int a, int b) {\r\n\r\n   80483db:       push   ebp\r\n\r\n   80483dc:       mov    ebp,esp\r\n\r\n   80483de:       sub    esp,0x10\r\n\r\n      int i = a + b;\r\n\r\n   80483e1:       mov    edx,DWORD PTR [ebp+0x8]\r\n\r\n   80483e4:       mov    eax,DWORD PTR [ebp+0xc]\r\n\r\n   80483e7:       add    eax,edx\r\n\r\n   80483e9:       mov    DWORD PTR [ebp-0x4],eax\r\n\r\n      return i;\r\n\r\n   80483ec:       mov    eax,DWORD PTR [ebp-0x4]\r\n\r\n  }\r\n\r\n   80483ef:       leave  \r\n\r\n   80483f0:       ret    \r\n\r\nIn the assembly listing, [ebp-0x4] is the local variable i, since \r\nit is allocated after ebp, with the length of 4 bytes (an int). \r\nOn the other hand, a and b are arguments and can be accessed with \r\nebp: \r\n\r\n• [ebp+0x8] accesses a.\r\n\r\n• [ebp+0xc] access b.\r\n\r\nFor accessing arguments, the rule is that the closer a variable \r\non stack to ebp, the closer it is to a function name. \r\n\r\n\r\n\r\n\r\n                       +-------------------+            +-------------------+            +-------------------+            +-------------------+\r\n                       |      ebp+0xc      |            |      ebp+0x8      |            |      ebp+0x4      |            |        ebp        |\r\n                       +-------------------+            +-------------------+            +-------------------+            +-------------------+\r\n                                                                                                                                ---------------\r\n                               \\downarrow\r\n                      \\downarrow\r\n                      \\downarrow\r\n                      \\downarrow\r\n  \r\n+----------+-----+-----+-----+--------------+-----+-----+-----+--------------+-----+-----+-----+--------------+-----+-----+-----+-------------+\r\n|          | 00  | 01  | 02  |     03       | 04  | 05  | 06  |     07       | 08  | 09  | 0a  |     0b       | 0c  | 0d  | 0e  |     0f      |\r\n+----------+--------------------------------+--------------------------------+--------------------------------+-------------------------------+\r\n| 0x10000  |               b                |               a                |        Return Address          |            Old ebp            |\r\n+----------+--------------------------------+--------------------------------+--------------------------------+-------------------------------+\r\n                                                                                                                                               \r\n                                                                                         +-------------------+            +-------------------+\r\n                                                                                         |      ebp+0x8      |            |      ebp+0x4      |\r\n                                                                                         +-------------------+            +-------------------+\r\n                                                                                                 \\downarrow\r\n                      \\downarrow\r\n  \r\n+----------+-----+-----+-----+--------------+-----+-----+-----+--------------+-----+-----+-----+--------------+-----+-----+-----+-------------+\r\n|          | 00  | 01  | 02  |     03       | 04  | 05  | 06  |     07       | 08  | 09  | 0a  |     0b       | 0c  | 0d  | 0e  |     0f      |\r\n                                                                             +-----                           +-----            +-------------+\r\n+----------+-----+-----+-----+--------------+-----+-----+-----+--------------+-----+-----+-----+--------------+-------------------------------+\r\n|  0xffe0  |     |     |     |              |     |     |     |              |     |     |     |      N       |               i               |\r\n+----------+-----+-----+-----+--------------+-----+-----+-----+--------------+-----+-----+-----+--------------+-------------------------------+\r\n\r\n\r\n\r\n\r\nN = Next local variable starts here\r\n\r\nFrom the figure, we can see that a and b are laid out in memory \r\nwith the exact order as written in C, relative to the return \r\naddress. \r\n\r\n  Function Call and Return<sub:Function-Call-and>\r\n\r\n  Source \r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\nint add(int a, int b) {\r\n\r\n    int local = 0x12345;\r\n\r\n\r\n\r\n    return a + b;\r\n\r\n}\r\n\r\n\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n    add(1,1);\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  For every function call, gcc pushes arguments on the stack in \r\n  reversed order with the push instructions. That is, the \r\n  arguments pushed on stack are in reserved order as it is \r\n  written in high level C code, to ensure the relative order \r\n  between arguments, as seen in previous section how function \r\n  arguments and local variables are laid out. Then, gcc generates \r\n  a call instruction, which then implicitly pushes a return \r\n  address before transferring the control to add function:\r\n\r\n  080483f2 <main>:\r\n\r\n  int main(int argc, char *argv[]) {\r\n\r\n   80483f2:       push   ebp\r\n\r\n   80483f3:       mov    ebp,esp\r\n\r\n      add(1,2);\r\n\r\n   80483f5:       push   0x2\r\n\r\n   80483f7:       push   0x1\r\n\r\n   80483f9:       call   80483db <add>\r\n\r\n   80483fe:       add    esp,0x8\r\n\r\n      return 0;\r\n\r\n   8048401:       mov    eax,0x0\r\n\r\n  }\r\n\r\n   8048406:       leave  \r\n\r\n   8048407:       ret    \r\n\r\nUpon finishing the call to add function, the stack is restored by \r\nadding 0x8 to stack pointer esp (which is equivalent to 2 pop \r\ninstructions). Finally, a leave instruction is executed and main \r\nreturns with a ret instruction. A ret instruction transfers the \r\nprogram execution back to the caller to the instruction right \r\nafter the call instruction, the add instruction. The reason ret \r\ncan return to such location is that the return address implicitly \r\npushed by the call instruction, which is the address right after \r\nthe call instruction; whenever the CPU executes ret instruction, \r\nit retrieves the return address that sits right after all the \r\narguments on the stack:\r\n\r\nAt the end of a function, gcc places a leave instruction to clean \r\nup all spaces allocated for local variables and restore the frame \r\npointer to frame pointer of the caller.\r\n\r\n080483db <add>:\r\n\r\n#include <stdio.h>\r\n\r\nint add(int a, int b) {\r\n\r\n 80483db:       push   ebp\r\n\r\n 80483dc:       mov    ebp,esp\r\n\r\n 80483de:       sub    esp,0x10\r\n\r\n    int local = 0x12345;\r\n\r\n 80483e1:       DWORD PTR [ebp-0x4],0x12345\r\n\r\n    return a + b;\r\n\r\n 80483e8:       mov    edx,DWORD PTR [ebp+0x8]\r\n\r\n 80483eb:       mov    eax,DWORD PTR [ebp+0xc]\r\n\r\n 80483ee:       add    eax,edx\r\n\r\n}\r\n\r\n 80483f0:       leave  \r\n\r\n 80483f1:       ret \r\n\r\nThe above code that gcc generated for function calling is \r\nactually the standard method x86 defined. Read chapter 6, “\r\nProduce Calls, Interrupts, and Exceptions”, Intel manual volume \r\n1.\r\n\r\n  Loop\r\n\r\nLoop is simply resetting the instruction pointer to an already \r\nexecuted instruction and starting from there all over again. A \r\nloop is just one application of jmp instruction. However, because \r\nlooping is a pervasive pattern, it earned its own syntax in C.\r\n\r\n  Source\r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n    for (int i = 0; i < 10; i++) {\r\n\r\n    }\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  080483db <main>:\r\n\r\n  #include <stdio.h>\r\n\r\n  int main(int argc, char *argv[]) {\r\n\r\n   80483db:       push   ebp\r\n\r\n   80483dc:       mov    ebp,esp\r\n\r\n   80483de:       sub    esp,0x10\r\n\r\n      for (int i = 0; i < 10; i++) {\r\n\r\n   80483e1:       mov    DWORD PTR [ebp-0x4],0x0\r\n\r\n   80483e8:       jmp    80483ee <main+0x13>\r\n\r\n   80483ea:       add    DWORD PTR [ebp-0x4],0x1\r\n\r\n   80483ee:       cmp    DWORD PTR [ebp-0x4],0x9\r\n\r\n   80483f2:       jle    80483ea <main+0xf>\r\n\r\n      }\r\n\r\n      return 0;\r\n\r\n   80483f4: b8 00 00 00 00        mov    eax,0x0\r\n\r\n  }\r\n\r\n   80483f9: c9                    leave\r\n\r\n   80483fa: c3                    ret\r\n\r\n   80483fb: 66 90                 xchg   ax,ax\r\n\r\n   80483fd: 66 90                 xchg   ax,ax\r\n\r\n   80483ff: 90                    nop\r\n\r\n  The colors mark corresponding high level code to assembly code:\r\n\r\n  1. The red instruction initialize i to 0.\r\n\r\n  2. The green instructions compare i to 10 by using jle and \r\n    compare it to 9. If true, jump to 80483ea for another \r\n    iteration.\r\n\r\n  3. The blue instruction increase i by 1, making the loop able \r\n    to terminate once the terminate condition is satisfied.\r\n\r\n  Why does the increment instruction (the blue instruction) \r\n  appears before the compare instructions (the green \r\n  instructions)?\r\n\r\n    What assembly code can be generated for while and do...while?\r\n\r\n  Conditional\r\n\r\nAgain, conditional in C with if...else... construct is just \r\nanother application of jmp instruction under the hood. It is also \r\na pervasive pattern that earned its own syntax in C.\r\n\r\n  Source\r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n    int i = 0;\r\n\r\n\r\n\r\n    if (argc) {\r\n\r\n        i = 1;\r\n\r\n    } else {\r\n\r\n        i = 0;\r\n\r\n    }\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  Assembly\r\n\r\n  int main(int argc, char *argv[]) {\r\n\r\n   80483db:        push   ebp\r\n\r\n   80483dc:        mov    ebp,esp\r\n\r\n   80483de:        sub    esp,0x10\r\n\r\n      int i = 0;\r\n\r\n   80483e1:        mov    DWORD PTR [ebp-0x4],0x0\r\n\r\n      if (argc) {\r\n\r\n   80483e8:        cmp    DWORD PTR [ebp+0x8],0x0\r\n\r\n   80483ec:        je     80483f7 <main+0x1c>\r\n\r\n          i = 1;\r\n\r\n   80483ee:        mov    DWORD PTR [ebp-0x4],0x1\r\n\r\n   80483f5:        jmp    80483fe <main+0x23>\r\n\r\n      } else {\r\n\r\n          i = 0;\r\n\r\n   80483f7:        mov    DWORD PTR [ebp-0x4],0x0\r\n\r\n      }\r\n\r\n      return 0;\r\n\r\n   80483fe:        mov    eax,0x0\r\n\r\n  }\r\n\r\n   8048403:        leave  \r\n\r\n   8048404:        ret    \r\n\r\n  The generated assembly code follows the same order as the \r\n  corresponding high level syntax:\r\n\r\n  • red instructions represents if branch.\r\n\r\n  • blue instructions represents else branch.\r\n\r\n  • green instruction is the exit point for both if and else \r\n    branch.\r\n\r\n  if branch first compares whether argc is false (equal to 0) \r\n  with cmp instruction. If true, it proceeds to else branch at \r\n  80483f7. Otherwise, if branch continues with the code of its \r\n  branch, which is the next instruction at 80483ee for copying 1 \r\n  to i. Finally, it skips over else branch and proceeds to \r\n  80483fe, which is the next instruction pasts the if..else... \r\n  construct.\r\n\r\n  else branch is entered when cmp instruction from if branch is \r\n  true. else branch starts at 80483f7, which is the first \r\n  instruction of else branch. The instruction copies 0 to i, and \r\n  proceeds naturally to the next instruction pasts the \r\n  if...else... construct without any jump.\r\n\r\nThe Anatomy of a Program<chap:The-Anatomy-of-a-program>\r\n\r\nEvery program consists of code and data, and only those two \r\ncomponents made up a program. However, if a program consists \r\npurely code and data of its own, from the perspective of an \r\noperating system (as well as human), it does not know in a \r\nprogram, which block of binary is a program and which is just raw \r\ndata, where in the program to start execution, which region of \r\nmemory should be protected and which is free to modify. For that \r\nreason, each program carries extra metadata to communicate with \r\nthe operating system how to handle the program.\r\n\r\nWhen a source file is compiled, the generated machine code is \r\nstored into an object file[margin:\r\nobject file\r\n]object file, which is just a block of binary. One or more object \r\nfiles can be combined to produce an executable binary[margin:\r\nexecutable binary\r\n]executable binary, which is a complete program runnable in an \r\noperating system.\r\n\r\nreadelf is a program that recognizes and displays the ELF \r\nmetadata of a binary file, be it an object file or an executable \r\nbinary. ELF, or Executable and Linkable Format, is the content at \r\nthe very beginning of an executable to provide an operating \r\nsystem necessary information to load into main memory and run the \r\nexecutable. ELF can be thought of similar to the table of \r\ncontents of a book. In a book, a table of contents list the page \r\nnumbers of the main sections, subsections, sometimes even figures \r\nand tables for easy lookup. Similarly, ELF lists various sections \r\nused for code and data, and the memory addresses of each symbol \r\nalong with other information.\r\n\r\nAn ELF binary is composed of:\r\n\r\n• An ELF header[margin:\r\nELF header\r\n]ELF header: the very first section of an executable that \r\n  describes the file's organization.\r\n\r\n• A Program header table[margin:\r\nprogram header table\r\n]program header table: is an array of fixed-size structures that \r\n  describes segments of an executable.\r\n\r\n• A Section header table[margin:\r\nsection header table\r\n]section header table: is an array of fixed-size structures that \r\n  describes sections of an executable. \r\n\r\n• Segments and section[margin:\r\nSegments and sections\r\n]Segments and sections are the main content of an ELF binary, \r\n  which are the code and data, divided into chunks of different \r\n  purposes.\r\n\r\n  A segmentsegment is a composition of zero or more sections and \r\n  is directly loaded by an operating system at runtime.\r\n\r\n  A sectionsection is a block of binary that is either:\r\n\r\n  – actual program code and data that is available in memory when \r\n    a program runs. \r\n\r\n  – metadata about other sections used only in the linking \r\n    process, and disappear from the final executable.\r\n\r\n  Linker uses sections to build segments.\r\n\r\n[float Figure:\r\n[Figure 0.16:\r\nELF - Linking View vs Executable View (Source: Wikipedia)\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/05/Elf-layout--en.pdf>\r\n     \r\n]\r\n\r\nLater we will compile our kernel as an ELF executable with GCC, \r\nand explicitly specify how segments are created and where they \r\nare loaded in memory through the use a linker script, a text file \r\nto instruct how a linker should generate a binary. For now, we \r\nwill examine the anatomy of an ELF executable in detail.\r\n\r\n  Reference documents: \r\n\r\nThe [margin:\r\nELF specification\r\n]ELF specification is bundled as a man page in Linux:\r\n\r\n\r\n\r\n$ man elf\r\n\r\n\r\n\r\nIt is a useful resource to understand and implement ELF. However, \r\nit will be much easier to use after you finish this chapter, as \r\nthe specification mixes implementation details in it.\r\n\r\nThe default specification is a generic one, in which every ELF \r\nimplementation follows. However, each platform provides extra \r\nfeatures unique to it. The ELF specification for x86 is currently \r\nmaintained on Github by H.J. Lu: https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI\r\n. \r\n\r\nPlatform-dependent details are referred to as “processor specific”\r\n in the generic ELF specification. We will not explore these \r\ndetails, but study the generic details, which are enough for \r\ncrafting an ELF binary image for our operating system.\r\n\r\n  ELF header\r\n\r\nTo see the information of an ELF header:\r\n\r\n\r\n\r\n$ readelf -h hello\r\n\r\n\r\n\r\nThe output:\r\n\r\n\r\n\r\nELF Header:\r\n\r\n  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00\r\n\r\n  Class:                             ELF64   \r\n\r\n  Data:                              2's complement, little \r\nendian\r\n\r\n  Version:                           1 (current)\r\n\r\n  OS/ABI:                            UNIX - System V\r\n\r\n  ABI Version:                       0\r\n\r\n  Type:                              EXEC (Executable file)\r\n\r\n  Machine:                           Advanced Micro Devices \r\nX86-64\r\n\r\n  Version:                           0x1\r\n\r\n  Entry point address:               0x400430\r\n\r\n  Start of program headers:          64 (bytes into file)\r\n\r\n  Start of section headers:          6648 (bytes into file)\r\n\r\n  Flags:                             0x0\r\n\r\n  Size of this header:               64 (bytes)\r\n\r\n  Size of program headers:           56 (bytes)\r\n\r\n  Number of program headers:         9\r\n\r\n  Size of section headers:           64 (bytes)\r\n\r\n  Number of section headers:         31\r\n\r\nSection header string table index: 28\r\n\r\n\r\n\r\nLet's go through each field:\r\n\r\n  Magic\r\n\r\n  Displays the raw bytes that uniquely addresses a file is an ELF \r\n  executable binary. Each byte gives a brief information.\r\n\r\n  In the example, we have the following magic bytes:\r\n\r\n  \r\n\r\n  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00\r\n\r\n  \r\n\r\n  Examine byte by byte:\r\n\r\n   \r\n  Byte                       Description                                                                                                                                                                     \r\n  -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\r\n    7f 45 4c 46                Predefined values. The first byte is always 7F, the remaining 3 \r\nbytes represent the string “ELF”.                                                                              \r\n                                                                                                                                                                                                               \r\n    02                         See Class field below.                                                                                                                                                          \r\n                                                                                                                                                                                                               \r\n    01                         See Data field below.                                                                                                                                                           \r\n                                                                                                                                                                                                               \r\n    01                         See Version field below.                                                                                                                                                        \r\n                                                                                                                                                                                                               \r\n    00                         See OS/ABI field below.                                                                                                                                                         \r\n                                                                                                                                                                                                               \r\n    00 00 00 00 00 00 00 00    Padding bytes. These bytes are unused and are always set to 0. \r\nPadding bytes are added for proper alignment, and is reserved for \r\nfuture use when more information is needed.  \r\n  \r\n\r\n  Class\r\n\r\n  A byte in Magic field. It specifies the class or capacity of a \r\n  file. \r\n\r\n  Possible values:\r\n\r\n   \r\n  Value     Description    \r\n  ---------------------------\r\n      0      Invalid class   \r\n      1      32-bit objects  \r\n      2      64-bit objects  \r\n  \r\n\r\n  Data\r\n\r\n  A byte in Magic field. It specifies the data encoding of the \r\n  processor-specific data in the object file.\r\n\r\n  Possible values:\r\n\r\n   \r\n  Value    Description                    \r\n  ------------------------------------------\r\n      0      Invalid data encoding          \r\n      1      Little endian, 2's complement  \r\n      2      Big endian, 2's complement     \r\n  \r\n\r\n  Version\r\n\r\n  A byte in Magic. It specifies the ELF header version number.\r\n\r\n  Possible values:\r\n\r\n   \r\n  Value    Description      \r\n  ----------------------------\r\n      0      Invalid version  \r\n      1      Current version  \r\n  \r\n\r\n  OS/ABI\r\n\r\n  A byte in Magic field. It specifies the target operating system \r\n  ABI. Originally, it was a padding byte.\r\n\r\n  Possible values: Refer to the latest ABI document, as it is a \r\n  long list of different operating systems.\r\n\r\n  Type\r\n\r\n  Identifies the object file type.\r\n\r\n   \r\n  Value     Description                      \r\n            -----------------------------------\r\n  ---------------------------------------------\r\n      0       No file type                     \r\n      1       Relocatable file                 \r\n      2       Executable file                  \r\n      3       Shared object file               \r\n      4       Core file                        \r\n    0xff00    Processor specific, lower bound  \r\n    0xffff    Processor specific, upper bound  \r\n  \r\n\r\n  The values from 0xff00 to 0xffff are reserved for a processor \r\n  to define additional file types meaningful to it.\r\n\r\n  Machine\r\n\r\n  Specifies the required architecture value for an ELF file e.g. \r\n  x86_64, MIPS, SPARC, etc. In the example, the machine is of x86_64\r\n   architecture.\r\n\r\n  Possible values: Please refer to the latest ABI document, as it \r\n  is a long list of different architectures.\r\n\r\n  Version\r\n\r\n  Specifies the version number of the current object file (not \r\n  the version of the ELF header, as the above Version field \r\n  specified).\r\n\r\n  Entry point address\r\n\r\n  Specifies the memory address where the very first code to be \r\n  executed. The address of main function is the default in a \r\n  normal application program, but it can be any function by \r\n  explicitly specifying the function name to gcc. For the \r\n  operating system we are going to write, this is the single most \r\n  important field that we need to retrieve to bootstrap our \r\n  kernel, and everything else can be ignored.\r\n\r\n  Start of program headers\r\n\r\n  The offset of the program header table, in bytes. In the \r\n  example, this number is 64 bytes, which means the 65th byte, or \r\n  <start address> + 64, is the start address of the program \r\n  header table. That is, if a program is loaded at address 0x10000\r\n   in memory, then the start address is 0x10000 (the very first \r\n  byte of Magic field, where the value 0x7f resides) and the \r\n  start address of program header table is 0x10000 + 0x40 = 0x10040\r\n  .\r\n\r\n  Start of section headers\r\n\r\n  The offset of the section header table in bytes, similar to the \r\n  start of program headers. In the example, it is 6648 bytes into \r\n  file.\r\n\r\n  Flags\r\n\r\n  Hold processor-specific flags associated with the file. When \r\n  the program is loaded, in a x86 machine, EFLAGS register is set \r\n  according to this value. In the example, the value is 0x0, \r\n  which means EFLAGS register is in a clear state.\r\n\r\n  Size of this header\r\n\r\n  Specifies the total size of ELF header's size in bytes. In the \r\n  example, it is 64 bytes, which is equivalent to Start of \r\n  program headers. Note that these two numbers are not necessarily \r\n  equivalent, as program header table might be placed far away \r\n  from the ELF header. The only fixed component in the ELF \r\n  executable binary is the ELF header, which appears at the very \r\n  beginning of the file.\r\n\r\n  Size of program headers\r\n\r\n  Specifies the size of each program header in bytes. In the \r\n  example, it is 64 bytes.\r\n\r\n  Number of program headers\r\n\r\n  Specifies the total number of program headers. In the example, \r\n  the file has a total of 9 program headers.\r\n\r\n  Size of section headers\r\n\r\n  Specifies the size of each section header in bytes. In the \r\n  example, it is 64 bytes.\r\n\r\n  Number of section headers\r\n\r\n  Specifies the total number of section headers. In the example, \r\n  the file has a total of 31 section headers. In a section header \r\n  table, the first entry in the table is always an empty section.\r\n\r\n  Section header string table index\r\n\r\n  Specifies the index of the header in the section header table \r\n  that points to the section that holds all null-terminated \r\n  strings. In the example, the index is 28, which means it's the \r\n  28[superscript:th] entry of the table. \r\n\r\n  Section header table\r\n\r\nAs we know already, code and data compose a program. However, not \r\nall types of code and data have the same purpose. For that \r\nreason, instead of a big chunk of code and data, they are divided \r\ninto smaller chunks, and each chunk must satisfy these conditions \r\n(according to gABI):\r\n\r\n• Every section in an object file has exactly one section header \r\n  describing it. But, section headers may exist that do not have \r\n  a section.\r\n\r\n• Each section occupies one contiguous (possibly empty) sequence \r\n  of bytes within a file. That means, there's no two regions of \r\n  bytes that are the same section.\r\n\r\n• Sections in a file may not overlap. No byte in a file resides \r\n  in more than one section.\r\n\r\n• An object file may have inactive space. The various headers and \r\n  the sections might not “cover” every byte in an object file. \r\n  The contents of the inactive data are unspecified.\r\n\r\nTo get all the headers from an executable binary e.g. hello, use \r\nthe following command:\r\n\r\n\r\n\r\n$ readelf -S hello\r\n\r\n\r\n\r\nHere is a sample output (do not worry if you don't understand the \r\noutput. Just skim to get your eyes familiar with it. We will \r\ndissect it soon enough):\r\n\r\n\r\n\r\nThere are 31 section headers, starting at offset 0x19c8:\r\n\r\n\r\n\r\nSection Headers:\r\n\r\n  [Nr] Name              Type             Address           \r\nOffset\r\n\r\n       Size              EntSize          Flags  Link  Info  \r\nAlign\r\n\r\n  [ 0]                   NULL             0000000000000000  \r\n00000000\r\n\r\n       0000000000000000  0000000000000000           0     0     0\r\n\r\n  [ 1] .interp           PROGBITS         0000000000400238  \r\n00000238\r\n\r\n       000000000000001c  0000000000000000   A       0     0     1\r\n\r\n  [ 2] .note.ABI-tag     NOTE             0000000000400254  \r\n00000254\r\n\r\n       0000000000000020  0000000000000000   A       0     0     4\r\n\r\n  [ 3] .note.gnu.build-i NOTE             0000000000400274  \r\n00000274\r\n\r\n       0000000000000024  0000000000000000   A       0     0     4\r\n\r\n  [ 4] .gnu.hash         GNU_HASH         0000000000400298  \r\n00000298\r\n\r\n       000000000000001c  0000000000000000   A       5     0     8\r\n\r\n  [ 5] .dynsym           DYNSYM           00000000004002b8  \r\n000002b8\r\n\r\n       0000000000000048  0000000000000018   A       6     1     8\r\n\r\n  [ 6] .dynstr           STRTAB           0000000000400300  \r\n00000300\r\n\r\n       0000000000000038  0000000000000000   A       0     0     1\r\n\r\n  [ 7] .gnu.version      VERSYM           0000000000400338  \r\n00000338\r\n\r\n       0000000000000006  0000000000000002   A       5     0     2\r\n\r\n  [ 8] .gnu.version_r    VERNEED          0000000000400340  \r\n00000340\r\n\r\n       0000000000000020  0000000000000000   A       6     1     8\r\n\r\n  [ 9] .rela.dyn         RELA             0000000000400360  \r\n00000360\r\n\r\n       0000000000000018  0000000000000018   A       5     0     8\r\n\r\n  [10] .rela.plt         RELA             0000000000400378  \r\n00000378\r\n\r\n       0000000000000018  0000000000000018  AI       5    24     8\r\n\r\n  [11] .init             PROGBITS         0000000000400390  \r\n00000390\r\n\r\n       000000000000001a  0000000000000000  AX       0     0     4\r\n\r\n  [12] .plt              PROGBITS         00000000004003b0  \r\n000003b0\r\n\r\n       0000000000000020  0000000000000010  AX       0     0     \r\n16\r\n\r\n  [13] .plt.got          PROGBITS         00000000004003d0  \r\n000003d0\r\n\r\n       0000000000000008  0000000000000000  AX       0     0     8\r\n\r\n  [14] .text             PROGBITS         00000000004003e0  \r\n000003e0\r\n\r\n       0000000000000192  0000000000000000  AX       0     0     \r\n16\r\n\r\n  [15] .fini             PROGBITS         0000000000400574  \r\n00000574\r\n\r\n       0000000000000009  0000000000000000  AX       0     0     4\r\n\r\n  [16] .rodata           PROGBITS         0000000000400580  \r\n00000580\r\n\r\n       0000000000000004  0000000000000004  AM       0     0     4\r\n\r\n  [17] .eh_frame_hdr     PROGBITS         0000000000400584  \r\n00000584\r\n\r\n       000000000000003c  0000000000000000   A       0     0     4\r\n\r\n  [18] .eh_frame         PROGBITS         00000000004005c0  \r\n000005c0\r\n\r\n       0000000000000114  0000000000000000   A       0     0     8\r\n\r\n  [19] .init_array       INIT_ARRAY       0000000000600e10  \r\n00000e10\r\n\r\n       0000000000000008  0000000000000000  WA       0     0     8\r\n\r\n  [20] .fini_array       FINI_ARRAY       0000000000600e18  \r\n00000e18\r\n\r\n       0000000000000008  0000000000000000  WA       0     0     8\r\n\r\n  [21] .jcr              PROGBITS         0000000000600e20  \r\n00000e20\r\n\r\n       0000000000000008  0000000000000000  WA       0     0     8\r\n\r\n  [22] .dynamic          DYNAMIC          0000000000600e28  \r\n00000e28\r\n\r\n       00000000000001d0  0000000000000010  WA       6     0     8\r\n\r\n  [23] .got              PROGBITS         0000000000600ff8  \r\n00000ff8\r\n\r\n       0000000000000008  0000000000000008  WA       0     0     8\r\n\r\n  [24] .got.plt          PROGBITS         0000000000601000  \r\n00001000\r\n\r\n       0000000000000020  0000000000000008  WA       0     0     8\r\n\r\n  [25] .data             PROGBITS         0000000000601020  \r\n00001020\r\n\r\n       0000000000000010  0000000000000000  WA       0     0     8\r\n\r\n  [26] .bss              NOBITS           0000000000601030  \r\n00001030\r\n\r\n       0000000000000008  0000000000000000  WA       0     0     1\r\n\r\n  [27] .comment          PROGBITS         0000000000000000  \r\n00001030\r\n\r\n       0000000000000034  0000000000000001  MS       0     0     1\r\n\r\n  [28] .shstrtab         STRTAB           0000000000000000  \r\n000018b6\r\n\r\n       000000000000010c  0000000000000000           0     0     1\r\n\r\n  [29] .symtab           SYMTAB           0000000000000000  \r\n00001068\r\n\r\n       0000000000000648  0000000000000018          30    47     8\r\n\r\n  [30] .strtab           STRTAB           0000000000000000  \r\n000016b0\r\n\r\n       0000000000000206  0000000000000000           0     0     1\r\n\r\nKey to Flags:\r\n\r\n  W (write), A (alloc), X (execute), M (merge), S (strings), l \r\n(large)\r\n\r\n  I (info), L (link order), G (group), T (TLS), E (exclude), x \r\n(unknown)\r\n\r\n  O (extra OS processing required) o (OS specific), p (processor \r\nspecific)\r\n\r\n\r\n\r\nThe first line:\r\n\r\n\r\n\r\nThere are 31 section headers, starting at offset 0x19c8\r\n\r\n\r\n\r\nsummarizes the total number of sections in the file, and where \r\nthe address where it starts. Then, comes the listing section by \r\nsection with the following header, is also the format of each \r\nsection output:\r\n\r\n\r\n\r\n[Nr] Name              Type             Address           Offset\r\n\r\n     Size              EntSize          Flags  Link  Info  Align\r\n\r\n\r\n\r\nEach section has two lines with different fields:\r\n\r\n  Nr The index of each section.\r\n\r\n  Name The name of each section.\r\n\r\n  Type This field (in a section header) identifies the type of \r\n  each section. Types classify sections (similar to types in \r\n  programming languages are used by a compiler). \r\n\r\n  Address The starting virtual address of each section. Note that \r\n  the addresses are virtual only when a program runs in an OS \r\n  with support for virtual memory enabled. In our OS, since we \r\n  run on bare metal, the addresses will all be physical.\r\n\r\n  Offset The offset of each section into a file. An [margin:\r\noffset\r\n]offsetoffset is a distance in bytes, from the first byte of a \r\n  file to the start of an object, such as a section or a segment \r\n  in the context of an ELF binary file.\r\n\r\n  Size The size in bytes of each section.\r\n\r\n  EntSize Some sections hold a table of fixed-size entries, such \r\n  as a symbol table. For such a section, this member gives the \r\n  size in bytes of each entry. The member contains 0 if the \r\n  section does not hold a table of fixed-size entries.\r\n\r\n  Flags describes attributes of a section. Flags together with a \r\n  type defines the purpose of a section. Two sections can be of \r\n  the same type, but serve different purposes. For example, even \r\n  though .data and .text share the same type, .data holds the \r\n  initialized data of a program while .text holds executable \r\n  instructions of a program. For that reason, .data is given read \r\n  and write permission, but not executable. Any attempt to \r\n  execute code in .data is denied by the running OS: in Linux, \r\n  such invalid section usage gives a segmentation fault.\r\n\r\n  ELF gives information to enable an OS with such protection \r\n  mechanism. However, running on bare metal, nothing can prevent \r\n  from doing anything. Our OS can execute code in data section, \r\n  and vice versa, writing to code section.\r\n\r\n\r\n                                                                                                                                                                                           [Table 5:\r\nSection Flags\r\n]                                                                                                                                                                                           \r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| Flag  | Descriptions                                                                                                                                                                                                                                                                                                                                                                                        |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| W     | Bytes in this section are writable during execution.                                                                                                                                                                                                                                                                                                                                                |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| A     | Memory is allocated for this section during process execution. \r\nSome control sections do not reside in the memory image of an \r\nobject file; this attribute is off for those sections.                                                                                                                                                                                                               |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| X     | The section contains executable instructions.                                                                                                                                                                                                                                                                                                                                                       |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| M     | The data in the section may be merged to eliminate duplication. \r\nEach element in the section is compared against other elements in \r\nsections with the same name, type and flags. Elements that would \r\nhave identical values at program run-time may be merged.                                                                                                                                      |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| S     | The data elements in the section consist of null-terminated \r\ncharacter strings. The size of each character is specified in the \r\nsection header's EntSize field.                                                                                                                                                                                                                                     |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| l     | Specific large section for x86_64 architecture. This flag is not \r\nspecified in the Generic ABI but in x86_64 ABI.                                                                                                                                                                                                                                                                                   |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| I     | The Info field of this section header holds an index of a section \r\nheader. Otherwise, the number is the index of something else.                                                                                                                                                                                                                                                                    |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| L     | Preserve section ordering when linking. If this section is \r\ncombined with other sections in the output file, it must appear \r\nin the same relative order with respect to those sections, as the \r\nlinked-to section appears with respect to sections the linked-to \r\nsection is combined with. Apply when the Link field of this \r\nsection's header references another section (the linked-to \r\nsection) |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| G     | This section is a member (perhaps the only one) of a section \r\ngroup.                                                                                                                                                                                                                                                                                                                                |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| T     | This section holds Thread-Local Storage, meaning that each thread \r\nhas its own distinct instance of this data. A thread is a \r\ndistinct execution flow of code. A program can have multiple \r\nthreads that pack different pieces of code and execute \r\nseparately, at the same time. We will learn more about threads \r\nwhen writing our kernel.                                                        |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| E     | Link editor is to exclude this section from executable and shared \r\nlibrary that it builds when those objects are not to be further \r\nrelocated.                                                                                                                                                                                                                                                      |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| x     | Unknown flag to readelf. It happens because the linking process \r\ncan be done manually with a linker like GNU ld (we will later \r\nlater). That is, section flags can be specified manually, and \r\nsome flags are for a customized ELF that the open-source readelf \r\ndoesn't know of.                                                                                                                   |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| O     | This section requires special OS-specific processing (beyond the \r\nstandard linking rules) to avoid incorrect behavior. A link \r\neditor encounters sections whose headers contain OS-specific \r\nvalues it does not recognize by Type or Flags values defined by \r\nELF standard, the link editor should combine those sections.                                                                          |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| o     | All bits included in this flag are reserved for operating \r\nsystem-specific semantics.                                                                                                                                                                                                                                                                                                               |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| p     | All bits included in this flag are reserved for \r\nprocessor-specific semantics. If meanings are specified, the \r\nprocessor supplement explains them.                                                                                                                                                                                                                                                  |\r\n+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n\r\n\r\n  Link and Info are numbers that references the indexes of \r\n  sections, symbol table entries, hash table entries. Link field \r\n  holds the index of a section, while Info field holds an index \r\n  of a section, a symbol table entry or a hash table entry, \r\n  depends on the type of a section. \r\n\r\n  Later when writing our OS, we will handcraft the kernel image \r\n  by explicitly linking the object files (produced by gcc) \r\n  through a linker script. We will specify the memory layout of \r\n  sections by specifying at what addresses they will appear in \r\n  the final image. But we will not assign any section flag and \r\n  let the linker take care of it. Nevertheless, knowing which \r\n  flag does what is useful.\r\n\r\n  Align is a value that enforces the offset of a section should \r\n  be divisible by the value. Only 0 and positive integral powers \r\n  of two are allowed. Values 0 and 1 mean the section has no \r\n  alignment constraint.\r\n\r\nOutput of .interp section:\r\n\r\n  \r\n\r\n  [Nr] Name              Type             Address           \r\n  Offset\r\n\r\n       Size              EntSize          Flags  Link  Info  \r\n  Align\r\n\r\n  [ 1] .interp           PROGBITS         0000000000400238  \r\n  00000238\r\n\r\n       000000000000001c  0000000000000000   A       0     0     1\r\n\r\n  \r\n\r\n  Nr is 1.\r\n\r\n  Type is PROGBITS, which means this section is part of the \r\n  program.\r\n\r\n  Address is 0x0000000000400238, which means the program is \r\n  loaded at this virtual memory address at runtime.\r\n\r\n  Offset is 0x00000238 bytes into file.\r\n\r\n  Size is 0x000000000000001c in bytes.\r\n\r\n  EntSize is 0, which means this section does not have any \r\n  fixed-size entry.\r\n\r\n  Flags are A (Allocatable), which means this section consumes \r\n  memory at runtime.\r\n\r\n  Info and Link are 0 and 0, which means this section links to no \r\n  section or entry in any table.\r\n\r\n  Align is 1, which means no alignment.\r\n\r\n\r\n\r\nOutput of the .text section:\r\n\r\n  \r\n\r\n  [14] .text             PROGBITS         00000000004003e0  \r\n  000003e0\r\n\r\n         0000000000000192  0000000000000000  AX       0     0     \r\n  16\r\n\r\n  \r\n\r\n  Nr is 14.\r\n\r\n  Type is PROGBITS, which means this section is part of the \r\n  program.\r\n\r\n  Address is 0x00000000004003e0, which means the program is \r\n  loaded at this virtual memory address at runtime.\r\n\r\n  Offset is 0x000003e0 bytes into file.\r\n\r\n  Size is 0x0000000000000192 in bytes.\r\n\r\n  EntSize is 0, which means this section does not have any \r\n  fixed-size entry.\r\n\r\n  Flags are A (Allocatable) and X (Executable), which means this \r\n  section consumes memory and can be executed as code at runtime.\r\n\r\n  Info and Link are 0 and 0, which means this section links to no \r\n  section or entry in any table.\r\n\r\n  Align is 16, which means the starting address of the section \r\n  should be divisible by 16, or 0x10. Indeed, it is: \\mathtt{0x3e0/0x10=0x3e}\r\n\r\n  . \r\n\r\n  Understand Section in-depth\r\n\r\nIn this section, we will learn different details of section types \r\nand the purposes of special sections e.g. .bss, .text, .data... \r\nby looking at each section one by one. We will also examine the \r\ncontent of each section as a hexdump with the commands:\r\n\r\n\r\n\r\n$ readelf -x <section name|section number> <file>\r\n\r\n\r\n\r\nFor example, if you want to examine the content of section with \r\nindex 25 (the .bss section in the sample output) in the file \r\nhello:\r\n\r\n\r\n\r\n$ readelf -x 25 hello\r\n\r\n\r\n\r\nEquivalently, using name instead of index works:\r\n\r\n\r\n\r\n$ readelf -x .data hello\r\n\r\n\r\n\r\nIf a section contains strings e.g. string symbol table, the flag \r\n-x can be replaced with -p.\r\n\r\n  NULL marks a section header as inactive and does not have an \r\n  associated section. NULL section is always the first entry of \r\n  section header table. It means, any useful section starts from \r\n  1.\r\n\r\n  The sample output of NULL section:\r\n\r\n    \r\n\r\n    [Nr] Name             Type             Address           \r\n    Offset\r\n\r\n         Size             EntSize          Flags  Link  Info  \r\n    Align\r\n\r\n    [ 0]                  NULL             0000000000000000 \r\n    00000000\r\n\r\n         0000000000000000 0000000000000000           0     0     \r\n    0\r\n\r\n    \r\n\r\n  Examining the content, the section is empty:\r\n\r\n  \r\n\r\n   Section '' has no data to dump.\r\n\r\n  \r\n\r\n  NOTE marks a section with special information that other \r\n  programs will check for conformance, compatibility... by a \r\n  vendor or a system builder.\r\n\r\n  In the sample output, we have 2 NOTE sections:\r\n\r\n    \r\n\r\n    [Nr] Name              Type             Address           \r\n    Offset\r\n\r\n         Size              EntSize          Flags  Link  Info  \r\n    Align\r\n\r\n    [ 2] .note.ABI-tag     NOTE             0000000000400254  \r\n    00000254\r\n\r\n         0000000000000020  0000000000000000   A       0     0     \r\n    4\r\n\r\n    [ 3] .note.gnu.build-i NOTE             0000000000400274  \r\n    00000274          \r\n\r\n         0000000000000024  0000000000000000   A       0     0     \r\n    4\r\n\r\n    \r\n\r\n  Examine 2nd section with the command:\r\n\r\n  \r\n\r\n  $ readelf -x 2 hello\r\n\r\n  \r\n\r\n  we have:\r\n\r\n  \r\n\r\n  Hex dump of section '.note.ABI-tag':\r\n\r\n    0x00400254 04000000 10000000 01000000 474e5500 \r\n  ............GNU.\r\n\r\n    0x00400264 00000000 02000000 06000000 20000000 ............ \r\n  ...\r\n\r\n  \r\n\r\n  PROGBITS indicates a section holding the main content of a \r\n  program, either code or data.\r\n\r\n  There are many PROGBITS sections:\r\n\r\n    \r\n\r\n      [Nr] Name              Type             Address           \r\n    Offset\r\n\r\n           Size              EntSize          Flags  Link  Info  \r\n    Align\r\n\r\n      [ 1] .interp           PROGBITS         0000000000400238  \r\n    00000238\r\n\r\n           000000000000001c  0000000000000000   A       0     0   \r\n      1\r\n\r\n      ...\r\n\r\n      [11] .init             PROGBITS         0000000000400390  \r\n    00000390\r\n\r\n           000000000000001a  0000000000000000  AX       0     0   \r\n      4\r\n\r\n      [12] .plt              PROGBITS         00000000004003b0  \r\n    000003b0\r\n\r\n           0000000000000020  0000000000000010  AX       0     0   \r\n      16\r\n\r\n      [13] .plt.got          PROGBITS         00000000004003d0  \r\n    000003d0\r\n\r\n           0000000000000008  0000000000000000  AX       0     0   \r\n      8\r\n\r\n      [14] .text             PROGBITS         00000000004003e0  \r\n    000003e0\r\n\r\n           0000000000000192  0000000000000000  AX       0     0   \r\n      16\r\n\r\n      [15] .fini             PROGBITS         0000000000400574  \r\n    00000574\r\n\r\n           0000000000000009  0000000000000000  AX       0     0   \r\n      4\r\n\r\n      [16] .rodata           PROGBITS         0000000000400580  \r\n    00000580\r\n\r\n           0000000000000004  0000000000000004  AM       0     0   \r\n      4\r\n\r\n      [17] .eh_frame_hdr     PROGBITS         0000000000400584  \r\n    00000584\r\n\r\n           000000000000003c  0000000000000000   A       0     0   \r\n      4\r\n\r\n      [18] .eh_frame         PROGBITS         00000000004005c0  \r\n    000005c0\r\n\r\n           0000000000000114  0000000000000000   A       0     0   \r\n      8\r\n\r\n      ...\r\n\r\n      [23] .got              PROGBITS         0000000000600ff8  \r\n    00000ff8\r\n\r\n           0000000000000008  0000000000000008  WA       0     0   \r\n      8\r\n\r\n      [24] .got.plt          PROGBITS         0000000000601000  \r\n    00001000\r\n\r\n           0000000000000020  0000000000000008  WA       0     0   \r\n      8\r\n\r\n      [25] .data             PROGBITS         0000000000601020  \r\n    00001020\r\n\r\n           0000000000000010  0000000000000000  WA       0     0   \r\n      8\r\n\r\n      [27] .comment          PROGBITS         0000000000000000  \r\n    00001030\r\n\r\n           0000000000000034  0000000000000001  MS       0     0   \r\n      1\r\n\r\n    \r\n\r\n  For our operating system, we only need the following section:\r\n\r\n  .text\r\n\r\n    This section holds all the compiled code of a program. \r\n\r\n  .data\r\n\r\n    This section holds the initialized data of a program. Since \r\n    the data are initialized with actual values, gcc allocates \r\n    the section with actual byte in the executable binary.\r\n\r\n  .rodata\r\n\r\n    This section holds read-only data, such as fixed-size strings \r\n    in a program, e.g. “Hello World”, and others.\r\n\r\n  .bss\r\n\r\n    This section, shorts for Block Started by Symbol, holds \r\n    uninitialized data of a program. Unlike other sections, no \r\n    space is allocated for this section in the image of the \r\n    executable binary on disk. The section is allocated only when \r\n    the program is loaded into main memory.\r\n\r\n  Other sections are mainly needed for dynamic linking, that is \r\n  code linking at runtime for sharing between many programs. To \r\n  enable such feature, an OS as a runtime environment must be \r\n  presented. Since we run our OS on bare metal, we are \r\n  effectively creating such environment. For simplicity, we won't \r\n  add dynamic linking to our OS.\r\n\r\n  SYMTAB and DYNSYM These sections hold symbol table. A symbol \r\n  table is an array of entries that describe symbols in a \r\n  program. A symbol is a name assigned to an entity in a program. \r\n  The types of these entities are also the types of symbols, and \r\n  these are the possible types of an entity:\r\n\r\n  In the sample output, section 5 and 29 are symbol tables:\r\n\r\n    \r\n\r\n    [Nr] Name              Type             Address           \r\n    Offset\r\n\r\n         Size              EntSize          Flags  Link  Info  \r\n    Align\r\n\r\n    [ 5] .dynsym           DYNSYM           00000000004002b8  \r\n    000002b8\r\n\r\n         0000000000000048  0000000000000018   A       6     1     \r\n    8\r\n\r\n    ...\r\n\r\n    [29] .symtab           SYMTAB           0000000000000000  \r\n    00001068\r\n\r\n         0000000000000648  0000000000000018          30    47     \r\n    8\r\n\r\n    \r\n\r\n    To show the symbol table:\r\n\r\n    \r\n\r\n    $ readelf -s hello\r\n\r\n    \r\n\r\n    Output consists of 2 symbol tables, corresponding to the two \r\n    sections above, .dynsym and .symtab:\r\n\r\n    \r\n\r\n    Symbol table '.dynsym' contains 4 entries:\r\n\r\n       Num:    Value          Size Type    Bind   Vis      Ndx \r\n    Name\r\n\r\n         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND \r\n\r\n         1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND \r\n    puts@GLIBC_2.2.5 (2)\r\n\r\n         2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND \r\n    __libc_start_main@GLIBC_2.2.5 (2)\r\n\r\n         3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND \r\n    __gmon_start__\r\n\r\n    Symbol table '.symtab' contains 67 entries:\r\n\r\n       Num:    Value          Size Type    Bind   Vis      Ndx \r\n    Name\r\n\r\n        ..........................................\r\n\r\n        59: 0000000000601040     0 NOTYPE  GLOBAL DEFAULT   26 \r\n    _end\r\n\r\n        60: 0000000000400430    42 FUNC    GLOBAL DEFAULT   14 \r\n    _start\r\n\r\n        61: 0000000000601038     0 NOTYPE  GLOBAL DEFAULT   26 \r\n    __bss_start\r\n\r\n        62: 0000000000400526    32 FUNC    GLOBAL DEFAULT   14 \r\n    main\r\n\r\n        63: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND \r\n    _Jv_RegisterClasses\r\n\r\n        64: 0000000000601038     0 OBJECT  GLOBAL HIDDEN    25 \r\n    __TMC_END__\r\n\r\n        65: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND \r\n    _ITM_registerTMCloneTable\r\n\r\n        66: 00000000004003c8     0 FUNC    GLOBAL DEFAULT   11 \r\n    _init\r\n\r\n    \r\n\r\n  TLS\tThe symbol is associated with a Thread-Local Storage \r\n    entity.\r\n\r\n  Num is the index of an entry in a table.\r\n\r\n  Value is the virtual memory address where the symbol is \r\n    located.\r\n\r\n  Size is the size of the entity associated with a symbol.\r\n\r\n  Type is a symbol type according to table.\r\n\r\n    NOTYPE The type of a symbol is not specified. \r\n\r\n    OBJECT\tThe symbol is associated with a data object. In C, any \r\n      variable definition is of OBJECT type.\r\n\r\n    FUNC The symbol is associated with a function or other \r\n      executable code. \r\n\r\n    SECTION\tThe symbol is associated with a section, and exists \r\n      primarily for relocation.\r\n\r\n    FILE The symbol is the name of a source file associated with \r\n      an executable binary.\r\n\r\n    COMMON\tThe symbol labels an uninitialized variable. That is, \r\n      when a variable in C is defined as global variable without \r\n      an initial value, or as an external variable using the \r\n      extern keyword. In other words, these variables stay in \r\n      .bss section.\r\n\r\n  Bind is the scope of a symbol. \r\n\r\n    LOCAL are symbols that are only visible in the object files \r\n      that defined them. In C, the static modifier marks a symbol \r\n      (e.g. a variable/function) as local to only the file that \r\n      defines it.\r\n\r\n      If we define variables and functions with static modifer:\r\n\r\n        static int global_static_var = 0;\r\n\r\n\r\n\r\nstatic void local_func() {\r\n\r\n}\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    static int local_static_var = 0;\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n      Then we get the static variables listed as local symbols \r\n      after compiling:\r\n\r\n        \r\n\r\n        $ gcc -m32 hello.c -o hello\r\n\r\n        $ readelf -s hello\r\n\r\n        \r\n\r\n        \r\n\r\n        Symbol table '.dynsym' contains 5 entries:\r\n\r\n           Num:    Value  Size Type    Bind   Vis      Ndx Name\r\n\r\n             0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND \r\n\r\n             1: 00000000     0 FUNC    GLOBAL DEFAULT  UND \r\n        puts@GLIBC_2.0 (2)\r\n\r\n             2: 00000000     0 NOTYPE  WEAK   DEFAULT  UND \r\n        __gmon_start__\r\n\r\n             3: 00000000     0 FUNC    GLOBAL DEFAULT  UND \r\n        __libc_start_main@GLIBC_2.0 (2)\r\n\r\n             4: 080484bc     4 OBJECT  GLOBAL DEFAULT   16 \r\n        _IO_stdin_used\r\n\r\n        Symbol table '.symtab' contains 72 entries:\r\n\r\n           Num:    Value  Size Type    Bind   Vis      Ndx Name\r\n\r\n             0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND \r\n\r\n                ......... output omitted .........\r\n\r\n            38: 0804a020     4 OBJECT  LOCAL  DEFAULT   26 \r\n        global_static_var\r\n\r\n            39: 0804840b     6 FUNC    LOCAL  DEFAULT   14 \r\n        local_func\r\n\r\n            40: 0804a024     4 OBJECT  LOCAL  DEFAULT   26 \r\n        local_static_var.1938\r\n\r\n         ......... output omitted .........\r\n\r\n        \r\n\r\n    GLOBAL\tare symbols that are accessible by other object files \r\n      when linking together. These symbols are primarily \r\n      non-static functions and non-static global data. The extern \r\n      modifier marks a symbol as externally defined elsewhere but \r\n      is accessible in the final executable binary, so an extern \r\n      variable is also considered GLOBAL.\r\n\r\n      Similar to the LOCAL example above, the output lists many \r\n      GLOBAL symbols such as main:\r\n\r\n        Num:    Value  Size Type    Bind   Vis      Ndx Name\r\n\r\n        ......... output omitted .........\r\n\r\n         66: 080483e1    10 FUNC    GLOBAL DEFAULT   14 main\r\n\r\n        ......... output omitted .........\r\n\r\n    WEAK are symbols whose definitions can be redefined. \r\n      Normally, a symbol with multiple definitions are reported \r\n      as an error by a compiler. However, this constraint is lax \r\n      when a definition is explicitly marked as weak, which means \r\n      the default implementation can be replaced by a different \r\n      definition at link time.\r\n\r\n      Suppose we have a default implementation of the function \r\n      add:\r\n\r\n        #include <stdio.h>\r\n\r\n\r\n\r\n__attribute__((weak)) int add(int a, int b) {\r\n\r\n    printf(\"warning: function is not implemented.\\n\");\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    printf(\"add(1,2) is %d\\n\", add(1,2));\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n        __attribute__((weak)) is a [margin:\r\nfunction attribute\r\n]function attribute. A function attributefunction attribute is \r\n        extra information for a compiler to handle a function \r\n        differently from a normal function. In this example, weak \r\n        attribute makes the function add a weak function,which \r\n        means the default implementation can be replaced by a \r\n        different definition at link time. Function attribute is \r\n        a feature of a compiler, not standard C.\r\n\r\n        If we do not supply a different function definition in a \r\n        different file (must be in a different file, otherwise \r\n        gcc reports as an error), then the default implementation \r\n        is applied. When the function add is called, it only \r\n        prints the message: \"warning: function not \r\n        implemented\"and returns 0:\r\n\r\n        \r\n\r\n        $ ./hello \r\n\r\n        warning: function is not implemented.\r\n\r\n        add(1,2) is 0\r\n\r\n        \r\n\r\n        However, if we supply a different definition in another \r\n        file e.g. math.c:\r\n\r\n        int add(int a, int b) {\r\n\r\n    return a + b;\r\n\r\n}\r\n\r\n        and compile the two files together:\r\n\r\n        \r\n\r\n        $ gcc math.c hello.c -o hello\r\n\r\n        \r\n\r\n        Then, when running hello, no warning message is printed \r\n        and the correct value is returned.\r\n\r\n        Weak symbol is a mechanism to provide a default \r\n        implementation, but replaceable when a better \r\n        implementation is available (e.g. more specialized and \r\n        optimized) at link-time.\r\n\r\n  Vis is the visibility of a symbol. The following values are \r\n    available:\r\n\r\n    \r\n                                                                                                                                  [Table 6:\r\nSymbol Visibility\r\n]                                                                                                                                   \r\n    +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | Value      | Description                                                                                                                                                                                                                                                                       |\r\n    +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | DEFAULT    | The visibility is specified by the binding type of asymbol. \r\n\r\n• Global and weak symbols are visible outside of their defining \r\n  component (executable file or shared object).\r\n\r\n• Local symbols are hidden. See HIDDEN below.                                                     |\r\n    +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | HIDDEN     | A symbol is hidden when the name is not visible to any other \r\nprogram outside of its running program.                                                                                                                                                                             |\r\n    +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | PROTECTED  | A symbol is protected when it is shared outside of its running \r\nprogram or shared libary and cannot be overridden. That is, there \r\ncan only be one definition for this symbol across running \r\nprograms that use it. No program can define its own definition of \r\nthe same symbol. |\r\n    +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | INTERNAL   | Visibility is processor-specific and is defined by \r\nprocessor-specific ABI.                                                                                                                                                                                                       |\r\n    +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    \r\n\r\n  Ndx is the index of a section that the symbol is in. Aside from \r\n    fixed index numbers that represent section indexes, index has \r\n    these special values:\r\n\r\n    \r\n                                                                                                                                                 [Table 7:\r\nSymbol Index\r\n]                                                                                                                                                 \r\n    +-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | Value                 | Description                                                                                                                                                                                                                                                                                    |\r\n    +-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    +-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | ABS                   | The index will not be changed by any symbol relocation.                                                                                                                                                                                                                                        |\r\n    +-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | COM                   | The index refers to an unallocated common block.                                                                                                                                                                                                                                               |\r\n    +-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | UND                   | The symbol is undefined in the current object file, which means \r\nthe symbol depends on the actual definition in another file. \r\nUndefined symbols appears when the object file refers to symbols \r\nthat are available at runtime, from shared library.                                           |\r\n    +-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | LORESERVE\r\n\r\nHIRESERVE  | LORESERVE is the lower boundary of the reserve indexes. Its value \r\nis 0xff00.\r\n\r\nHIREVERSE is the upper boundary of the reserve indexes. Its value \r\nis 0xffff.\r\n\r\nThe operating system reserves exclusive indexes between LORESERVE \r\nand HIRESERVE, which do not map to any actual section header. |\r\n    +-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | XINDEX                | The index is larger than LORESERVE. The actual value will be \r\ncontained in the section SYMTAB_SHNDX, where each entry is a \r\nmapping between a symbol, whose Ndx field is a XINDEX value, and \r\nthe actual index value.                                                                          |\r\n    +-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    | Others                | Sometimes, values such as ANSI_COM, LARGE_COM, SCOM, SUND appear. \r\nThis means that the index is processor-specific.                                                                                                                                                                            |\r\n    +-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n    \r\n\r\n  Name is the symbol name.\r\n\r\n  A C application program always starts from symbol main. The \r\n  entry for main in the symbol table in .symtab section is:\r\n\r\n    \r\n\r\n    Num:                Value  Size Type    Bind   Vis      Ndx \r\n    Name\r\n\r\n     62:     0000000000400526    32 FUNC    GLOBAL DEFAULT   14 \r\n    main\r\n\r\n    \r\n\r\n  The entry shows that:\r\n\r\n  • main is the 62[superscript:th] entry in the table.\r\n\r\n  • main starts at address 0x0000000000400526.\r\n\r\n  • main consumes 32 bytes.\r\n\r\n  • main is a function.\r\n\r\n  • main is in global scope.\r\n\r\n  • main is visible to other object files that use it.\r\n\r\n  • main is inside the 14[superscript:th] section, which is .text. This is logical, since .text holds all \r\n    program code.\r\n\r\n  STRTAB hold a table of null-terminated strings, called string \r\n  table. The first and last byte of this section is always a NULL \r\n  character. A string table section exists because a string can \r\n  be reused by more than one section to represent symbol and \r\n  section names, so a program like readelf or objdump can display \r\n  various objects in a program, e.g. variable, functions, section \r\n  names, in a human-readable text instead of its raw hex address.\r\n\r\n  In the sample output, section 28 and 30 are of STRTAB type:\r\n\r\n    \r\n\r\n    [Nr] Name              Type             Address           \r\n    Offset\r\n\r\n         Size              EntSize          Flags  Link  Info  \r\n    Align\r\n\r\n    [28] .shstrtab         STRTAB           0000000000000000  \r\n    000018b6\r\n\r\n         000000000000010c  0000000000000000           0     0     \r\n    1\r\n\r\n    [30] .strtab           STRTAB           0000000000000000  \r\n    000016b0\r\n\r\n         0000000000000206  0000000000000000           0     0     \r\n    1\r\n\r\n    \r\n\r\n  .shstrtab holds all the section names.\r\n\r\n  .strtab holds the symbols e.g. variable names, function names, \r\n    struct names, etc., in a C program, but not fixed-size \r\n    null-terminated C strings; the C strings are kept in .rodata \r\n    section.\r\n\r\n  Strings in those section can be inspected with the command:\r\n\r\n    \r\n\r\n    $ readelf -p 29 hello\r\n\r\n    \r\n\r\n    The output shows all the section names, with the offset (also \r\n    the string index) into .shstrtab the table to the left:\r\n\r\n    \r\n\r\n    String dump of section '.shstrtab':  \r\n\r\n      [     1]  .symtab\r\n\r\n      [     9]  .strtab\r\n\r\n      [    11]  .shstrtab\r\n\r\n      [    1b]  .interp\r\n\r\n      [    23]  .note.ABI-tag\r\n\r\n      [    31]  .note.gnu.build-id\r\n\r\n      [    44]  .gnu.hash\r\n\r\n      [    4e]  .dynsym\r\n\r\n      [    56]  .dynstr\r\n\r\n      [    5e]  .gnu.version\r\n\r\n      [    6b]  .gnu.version_r\r\n\r\n      [    7a]  .rela.dyn\r\n\r\n      [    84]  .rela.plt\r\n\r\n      [    8e]  .init\r\n\r\n      [    94]  .plt.got\r\n\r\n      [    9d]  .text\r\n\r\n      [    a3]  .fini\r\n\r\n      [    a9]  .rodata\r\n\r\n      [    b1]  .eh_frame_hdr\r\n\r\n      [    bf]  .eh_frame\r\n\r\n      [    c9]  .init_array\r\n\r\n      [    d5]  .fini_array\r\n\r\n      [    e1]  .jcr\r\n\r\n      [    e6]  .dynamic\r\n\r\n      [    ef]  .got.plt\r\n\r\n      [    f8]  .data\r\n\r\n      [    fe]  .bss\r\n\r\n      [   103]  .comment\r\n\r\n    \r\n\r\n    The actual implementation of a string table is a contiguous \r\n    array of null-terminated strings. The index of a string is \r\n    the position of its first character in the array. For \r\n    example, in the above string table, .symtab is at index 1 in \r\n    the array (NULL character is at index 0). The length of \r\n    .symtab is 7, plus the NULL character, which occurs 8 bytes \r\n    in total. So, .strtab starts at index 9, and so on.\r\n\r\n    \r\n\r\n    \r\n            +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----+\r\n                | 00  | 01  | 02  | 03  | 04  | 05  | 06  | 07  | 08  | 09  | 0a  | 0b  | 0c  | 0d  | 0e  | 0f |\r\n    +-----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----+\r\n    | 00000000  | \\0  | .   | s   | y   | m   | t   | a   | b   | \\0  | .   | s   | t   | r   | t   | a   | b  |\r\n    +-----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----+\r\n                                                                                                                \r\n                +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----+\r\n                | 00  | 01  | 02  | 03  | 04  | 05  | 06  | 07  | 08  | 09  | 0a  | 0b  | 0c  | 0d  | 0e  | 0f |\r\n    +-----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----+\r\n    | 00000010  | \\0  | .   | s   | h   | s   | t   | r   | t   | a   | b   | \\0  | .   | i   | n   | t   | e  |\r\n    +-----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+----+\r\n                                                .... and so on ....                                             \r\n    \r\n\r\n    \r\n\r\n    \r\n\r\n\r\n\r\n    Similarly, the output of .strtab:\r\n\r\n    \r\n\r\n    String dump of section '.strtab':\r\n\r\n      [     1]  crtstuff.c\r\n\r\n      [     c]  __JCR_LIST__\r\n\r\n      [    19]  deregister_tm_clones\r\n\r\n      [    2e]  __do_global_dtors_aux\r\n\r\n      [    44]  completed.7585\r\n\r\n      [    53]  __do_global_dtors_aux_fini_array_entry\r\n\r\n      [    7a]  frame_dummy\r\n\r\n      [    86]  __frame_dummy_init_array_entry\r\n\r\n      [    a5]  hello.c\r\n\r\n      [    ad]  __FRAME_END__\r\n\r\n      [    bb]  __JCR_END__\r\n\r\n      [    c7]  __init_array_end\r\n\r\n      [    d8]  _DYNAMIC\r\n\r\n      [    e1]  __init_array_start\r\n\r\n      [    f4]  __GNU_EH_FRAME_HDR\r\n\r\n      [   107]  _GLOBAL_OFFSET_TABLE_\r\n\r\n      [   11d]  __libc_csu_fini\r\n\r\n      [   12d]  _ITM_deregisterTMCloneTable\r\n\r\n      [   149]  j\r\n\r\n      [   14b]  _edata\r\n\r\n      [   152]  __libc_start_main@@GLIBC_2.2.5\r\n\r\n      [   171]  __data_start\r\n\r\n      [   17e]  __gmon_start__\r\n\r\n      [   18d]  __dso_handle\r\n\r\n      [   19a]  _IO_stdin_used\r\n\r\n      [   1a9]  __libc_csu_init\r\n\r\n      [   1b9]  __bss_start\r\n\r\n      [   1c5]  main\r\n\r\n      [   1ca]  _Jv_RegisterClasses\r\n\r\n      [   1de]  __TMC_END__\r\n\r\n      [   1ea]  _ITM_registerTMCloneTable\r\n\r\n    \r\n\r\n  HASH holds a symbol hash table, which supports symbol table \r\n  access.\r\n\r\n  DYNAMIC holds information for dynamic linking. \r\n\r\n  NOBITS is similar to PROGBITS but occupies no space.\r\n\r\n  .bss section holds uninitialized data, which means the bytes in \r\n  the section can have any value. Until a operating system \r\n  actually loads the section into main memory, there is no need \r\n  to allocate space for the binary image on disk to reduce the \r\n  size of a binary file. Here is the details of .bss from the \r\n  example output:\r\n\r\n  \r\n\r\n  [Nr] Name              Type             Address           \r\n  Offset\r\n\r\n       Size              EntSize          Flags  Link  Info  \r\n  Align\r\n\r\n  [26] .bss              NOBITS           0000000000601038  \r\n  00001038\r\n\r\n       0000000000000008  0000000000000000  WA       0     0     1 \r\n    \r\n\r\n  [27] .comment          PROGBITS         0000000000000000  \r\n  00001038\r\n\r\n       0000000000000034  0000000000000001  MS       0     0     1 \r\n\r\n  \r\n\r\n  In the above output, the size of the section is only 8 bytes, \r\n  while the offsets of both sections are the same, which means \r\n  .bss consumes no byte of the executable binary on disk. \r\n\r\n  Notice that the .comment section has no starting address. This \r\n  means that this section is discarded when the executable binary \r\n  is loaded into memory.\r\n\r\n  REL holds relocation entries without explicit addends. This \r\n  type will be explained in details in [sec:Understand-relocations-with-readelf]\r\n\r\n  RELA holds relocation entries with explicit addends. This type \r\n  will be explained in details in [sec:Understand-relocations-with-readelf]\r\n\r\n  INIT_ARRAY is an array of function pointers for program \r\n  initialization. When an application program runs, before \r\n  getting to main(), initialization code in .init and this \r\n  section are executed first. The first element in this array is \r\n  an ignored function pointer. \r\n\r\n  It might not make sense when we can include initialization code \r\n  in the main() function. However, for shared object files where \r\n  there are no main(), this section ensures that the \r\n  initialization code from an object file executes before any \r\n  other code to ensure a proper environment for main code to run \r\n  properly. It also makes an object file more modularity, as the \r\n  main application code needs not to be responsible for \r\n  initializing a proper environment for using a particular object \r\n  file, but the object file itself. Such a clear division makes \r\n  code cleaner.\r\n\r\n  However, we will not use any .init and INIT_ARRAY sections in \r\n  our operating system, for simplicity, as initializing an \r\n  environment is part of the operating-system domain.\r\n\r\n  To use the INIT_ARRAY, we simply mark a function with the \r\n  attribute constructor:\r\n\r\n    #include <stdio.h>\r\n\r\n\r\n\r\n__attribute__((constructor)) static void init1(){\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\n__attribute__((constructor)) static void init2(){\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    printf(\"hello world\\n\");\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n    The program automatically calls the constructor without \r\n    explicitly invoking it:\r\n\r\n    \r\n\r\n    $ gcc -m32 hello.c -o hello\r\n\r\n    $ ./hello \r\n\r\n    init1\r\n\r\n    init2\r\n\r\n    hello world\r\n\r\n    \r\n\r\n  \r\n\r\n  Optionally, a constructor can be assigned with a priority from \r\n  101 onward. The priorities from 0 to 100 are reserved for gcc. \r\n  If we want init2 to run before init1, we give it a higher \r\n  priority:\r\n\r\n    #include <stdio.h>\r\n\r\n\r\n\r\n__attribute__((constructor(102))) static void init1(){\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\n__attribute__((constructor(101))) static void init2(){\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    printf(\"hello world\\n\");\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n    The call order should be exactly as specified:\r\n\r\n  \r\n\r\n  $ gcc -m32 hello.c -o hello\r\n\r\n  $ ./hello\r\n\r\n  init2\r\n\r\n  init1\r\n\r\n  hello world\r\n\r\n  \r\n\r\n  \r\n\r\n  We can add initialization functions using another method:\r\n\r\n    #include <stdio.h>\r\n\r\n\r\n\r\nvoid init1() {\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\nvoid init2() {\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\n/* Without typedef, init is a definition of a function pointer.\r\n\r\n   With typedef, init is a declaration of a type.*/\r\n\r\ntypedef void (*init)();\r\n\r\n\r\n\r\n__attribute__((section(\".init_array\"))) init init_arr[2] = \r\n{init1, init2};\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    printf(\"hello world!\\n\");\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n    The attribute section(“...”) put a function into a particular \r\n    section rather then the default .text. In this example, it is \r\n    .init_arary. Again, the program automatically calls the \r\n    constructors without explicitly invoking it:\r\n\r\n    \r\n\r\n    $ gcc -m32 hello.c -o hello\r\n\r\n    $ ./hello \r\n\r\n    init1\r\n\r\n  init2\r\n\r\n  hello world!\r\n\r\n    \r\n\r\n  FINI_ARRAY is an array of function pointers for program \r\n  termination, called after exiting main(). If the application \r\n  terminate abnormally, such as through abort() call or a crash, \r\n  the .finit_array is ignored.\r\n\r\n  A destructor is automatically called after exiting main(), if \r\n  one or more available:\r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\n__attribute__((destructor)) static void destructor(){\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    printf(\"hello world\\n\");\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  \r\n\r\n  $ gcc -m32 hello.c -o hello\r\n\r\n  $ ./hello \r\n\r\n  hello world\r\n\r\n  destructor\r\n\r\n  \r\n\r\n  PREINIT_ARRAY is an array of function pointers that are invoked \r\n  before all other initialization functions in INIT_ARRAY.\r\n\r\n  To use the .preinit_array, the only way to put functions into \r\n  this section is to use the attribute section():\r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\nvoid preinit1() {\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\nvoid preinit2() {\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\nvoid init1() {\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\nvoid init2() {\r\n\r\n    printf(\"%s\\n\", __FUNCTION__);\r\n\r\n}\r\n\r\n\r\n\r\n\r\n\r\ntypedef void (*preinit)();\r\n\r\ntypedef void (*init)();\r\n\r\n\r\n\r\n__attribute__((section(\".preinit_array\"))) preinit preinit_arr[2] = \r\n{preinit1, preinit2};\r\n\r\n__attribute__((section(\".init_array\"))) init init_arr[2] = \r\n{init1, init2};\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    printf(\"hello world!\\n\");\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  \r\n\r\n  $ gcc -m32 hello2.c -o hello2\r\n\r\n  $ ./hello2\r\n\r\n  preinit1\r\n\r\n  preinit2\r\n\r\n  init1\r\n\r\n  init2\r\n\r\n  hello world!\r\n\r\n  \r\n\r\n  GROUP defines a section group, which is the same section that \r\n  appears in different object files but when merged into the \r\n  final executable binary file, only one copy is kept and the \r\n  rest in other object files are discarded. This section is only \r\n  relevant in C++ object files, so we will not examine further.\r\n\r\n  SYMTAB_SHNDX is a section containing extended section indexes, \r\n  that are associated with a symbol table. This section only \r\n  appears when the Ndx value of an entry in the symbol table \r\n  exceeds the LORESERVE value. This section then maps between a \r\n  symbol and an actual index value of a section header.\r\n\r\nUpon understanding section types, we can understand the number in \r\nLink and Info fields:\r\n\r\n\r\n\r\n\r\n+-----------------+--------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| Type            | Link                                                                           | Info                                                                                                                                                    |\r\n+-----------------+--------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n+-----------------+--------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| DYNAMIC         | Entries in this section uses the section index of the dynamic \r\nstring table.   | 0                                                                                                                                                       |\r\n+-----------------+--------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| HASH\r\n\r\nGNU_HASH  | The section index of the symbol table to which the hash table \r\napplies.        | 0                                                                                                                                                       |\r\n+-----------------+--------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| REL\r\n\r\nRELA       | The section index of the associated symbol table.                              | The section index to which the relocation applies.                                                                                                      |\r\n+-----------------+--------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| SYMTAB\r\n\r\nDYNSYM  | The section index of the associated string table.                              | One greater than the symbol table index of the last local symbol.                                                                                       |\r\n+-----------------+--------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| GROUP           | The section index of the associated symbol table.                              | The symbol index of an entry in the associated symbol table. The \r\nname of the specified symbol table entry provides a signature for \r\nthe section group. |\r\n+-----------------+--------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| SYMTAB_SHNDX    | The section header index of the associated symbol table.                       |                                                                                                                                                         |\r\n+-----------------+--------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n\r\n\r\n\r\n\r\nVerify that the value of the Link field of a SYMTAB section is \r\nthe index of a STRTAB section.\r\n\r\n\r\n\r\nVerify that the value of the Info field of a SYMTAB section is \r\nthe index of last local symbol + 1. It means, in the symbol \r\ntable, from the index listed by Info field onward, no local \r\nsymbol appears.\r\n\r\n\r\n\r\nVerify that the value of the Info field of a REL section is the \r\nindex of the SYMTAB section.\r\n\r\n\r\n\r\nVerify that the value of the Link field of a REL section is the \r\nindex of the section where relocation is applied. For example. if \r\nthe section is .rel.text, then the relocating section should be \r\n.text.\r\n\r\n  Program header table<sec:Program-header-table>\r\n\r\nA program header tableprogram header table is an array of program \r\nheaders that defines the memory layout of a program at runtime. \r\n\r\nA program headerprogram header is a description of a program \r\nsegment.\r\n\r\nA program segmentprogram segment is a collection of related \r\nsections. A segment contains zero or more sections. An operating \r\nsystem when loading a program, only use segments, not sections. \r\nTo see the information of a program header table, we use the -l \r\noption with readelf:\r\n\r\n\r\n\r\n$ readelf -l <binary file>\r\n\r\n\r\n\r\nSimilar to a section, a program header also has types:\r\n\r\n  PHDR specifies the location and size of the program header \r\n  table itself, both in the file and in the memory image of the \r\n  program\r\n\r\n  INTERP specifies the location and size of a null-terminated \r\n  path name to invoke as an interpreter for linking runtime \r\n  libraries.\r\n\r\n  LOAD specifies a loadable segment. That is, this segment is \r\n  loaded into main memory.\r\n\r\n  DYNAMIC specifies dynamic linking information.\r\n\r\n  NOTE specifies the location and size of auxiliary information.\r\n\r\n  TLS specifies the Thread-Local Storage template, which is \r\n  formed from the combination of all sections with the flag TLS.\r\n\r\n  GNU_STACK indicates whether the program's stack should be made \r\n  executable or not. Linux kernel uses this type.\r\n\r\nA segment also has permission, which is a combination of these 3 \r\nvalues:[float MarginTable:\r\n[MarginTable 3:\r\nSegment Permission\r\n]\r\n\r\n\r\n+-------------+-------------+\r\n| Permission  | Description |\r\n+-------------+-------------+\r\n+-------------+-------------+\r\n| R           | Readable    |\r\n+-------------+-------------+\r\n| W           | Writable    |\r\n+-------------+-------------+\r\n| E           | Executable  |\r\n+-------------+-------------+\r\n\r\n]\r\n\r\n• Read (R)\r\n\r\n• Write (W)\r\n\r\n• Execute (E)\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nThe command to get the program header table:\r\n\r\n  \r\n\r\n  $ readelf -l hello\r\n\r\n  \r\n\r\n  Output:\r\n\r\n  \r\n\r\n  Elf file type is EXEC (Executable file)\r\n\r\n  Entry point 0x400430\r\n\r\n  There are 9 program headers, starting at offset 64\r\n\r\n  \r\n\r\n  Program Headers:\r\n\r\n    Type           Offset             VirtAddr           PhysAddr\r\n\r\n                   FileSiz            MemSiz              Flags  \r\n  Align\r\n\r\n    PHDR           0x0000000000000040 0x0000000000400040 \r\n  0x0000000000400040\r\n\r\n                   0x00000000000001f8 0x00000000000001f8  R E    \r\n  8\r\n\r\n    INTERP         0x0000000000000238 0x0000000000400238 \r\n  0x0000000000400238\r\n\r\n                   0x000000000000001c 0x000000000000001c  R      \r\n  1\r\n\r\n        [Requesting program interpreter: \r\n  /lib64/ld-linux-x86-64.so.2]\r\n\r\n    LOAD           0x0000000000000000 0x0000000000400000 \r\n  0x0000000000400000\r\n\r\n                   0x000000000000070c 0x000000000000070c  R E    \r\n  200000\r\n\r\n    LOAD           0x0000000000000e10 0x0000000000600e10 \r\n  0x0000000000600e10\r\n\r\n                   0x0000000000000228 0x0000000000000230  RW     \r\n  200000\r\n\r\n    DYNAMIC        0x0000000000000e28 0x0000000000600e28 \r\n  0x0000000000600e28\r\n\r\n                   0x00000000000001d0 0x00000000000001d0  RW     \r\n  8\r\n\r\n    NOTE           0x0000000000000254 0x0000000000400254 \r\n  0x0000000000400254\r\n\r\n                   0x0000000000000044 0x0000000000000044  R      \r\n  4\r\n\r\n    GNU_EH_FRAME   0x00000000000005e4 0x00000000004005e4 \r\n  0x00000000004005e4\r\n\r\n                   0x0000000000000034 0x0000000000000034  R      \r\n  4\r\n\r\n    GNU_STACK      0x0000000000000000 0x0000000000000000 \r\n  0x0000000000000000\r\n\r\n                   0x0000000000000000 0x0000000000000000  RW     \r\n  10\r\n\r\n    GNU_RELRO      0x0000000000000e10 0x0000000000600e10 \r\n  0x0000000000600e10\r\n\r\n                   0x00000000000001f0 0x00000000000001f0  R      \r\n  1\r\n\r\n  \r\n\r\n   Section to Segment mapping:\r\n\r\n    Segment Sections...\r\n\r\n     00     \r\n\r\n     01     .interp\r\n\r\n     02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash \r\n  .dynsym .dynstr \r\n\r\n  .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt \r\n  .plt.got .text .fini\r\n\r\n  .rodata .eh_frame_hdr .eh_frame \r\n\r\n     03     .init_array .fini_array .jcr .dynamic .got .got.plt \r\n  .data .bss \r\n\r\n     04     .dynamic \r\n\r\n     05     .note.ABI-tag .note.gnu.build-id \r\n\r\n     06     .eh_frame_hdr \r\n\r\n     07     \r\n\r\n     08     .init_array .fini_array .jcr .dynamic .got \r\n\r\n  \r\n\r\n  In the sample output, LOAD segment appears twice:\r\n\r\n  \r\n\r\n  LOAD           0x0000000000000000 0x0000000000400000 \r\n  0x0000000000400000\r\n\r\n                 0x000000000000070c 0x000000000000070c  R E    \r\n  200000\r\n\r\n  LOAD           0x0000000000000e10 0x0000000000600e10 \r\n  0x0000000000600e10\r\n\r\n                 0x0000000000000228 0x0000000000000230  RW     \r\n  200000\r\n\r\n  \r\n\r\n  Why? Notice the permission: \r\n\r\n  • the upper LOAD has Read and Execute permission. This is a \r\n    text segment. A text segment contains read-only instructions \r\n    and read-only data.\r\n\r\n  • the lower LOAD has Read and Write permission. This is a data \r\n    segment. It means that this segment can be read and written \r\n    to, but is not allowed to be used as executable code, for \r\n    security reason.\r\n\r\n  Then, LOAD contains the following sections:\r\n\r\n  \r\n\r\n     02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash \r\n  .dynsym .dynstr \r\n\r\n  .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt \r\n  .plt.got .text .fini \r\n\r\n  .rodata .eh_frame_hdr .eh_frame \r\n\r\n     03     .init_array .fini_array .jcr .dynamic .got .got.plt \r\n  .data .bss \r\n\r\n  \r\n\r\n  The first number is the index of a program header in program \r\n  header table, and the remaining text is the list of all \r\n  sections within a segment. Unfortunately, readelf does not \r\n  print the index, so a user needs to keep track manually which \r\n  segment is of which index. First segment starts at index 0, \r\n  second at index 1 and so on. LOAD are segments at index 2 and \r\n  3. As can be seen from the two lists of sections, most sections \r\n  are loadable and is available at runtime.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n  Segments vs sections\r\n\r\nAs mentioned earlier, an operating system loads program segments, \r\nnot sections. However, a question arises: Why doesn't the \r\noperating system use sections instead? After all, a section also \r\ncontains similar information to a program segment, such as the \r\ntype, the virtual memory address to be loaded, the size, the \r\nattributes, the flags and align. As explained before, a segment \r\nis the perspective of an operating system, while a section is the \r\nperspective of a linker. To understand why, looking into the \r\nstructure of a segment, we can easily see:\r\n\r\n• A segment is a collection of sections. It means that sections \r\n  are logically grouped together by their attributes. For \r\n  example, all sections in a LOAD segment are always loaded by \r\n  the operating system; all sections have the same permission, \r\n  either a RE (Read + Execute) for executable sections, or RW \r\n  (Read + Write) for data sections.\r\n\r\n• By grouping sections into a segment, it is easier for an \r\n  operating system to batch load sections just once by loading \r\n  the start and end of a segment, instead of loading section by \r\n  section.\r\n\r\n• Since a segment is for loading a program and a section is for \r\n  linking a program, all the sections in a segment is within its \r\n  start and end virtual memory addresses of a segment.\r\n\r\nTo see the last point clearer, consider an example of linking two \r\nobject files. Suppose we have two source files:\r\n\r\n#include <stdio.h>\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    printf(\"Hello World\\n\");\r\n\r\n    return 0;\r\n\r\n}\r\n\r\nand:\r\n\r\nint add(int a, int b) {\r\n\r\n    return a + b;\r\n\r\n}\r\n\r\nNow, compile the two source files as object files:\r\n\r\n\r\n\r\n$ gcc -m32 -c math.c \r\n\r\n$ gcc -m32 -c hello.c\r\n\r\n\r\n\r\nThen, we check the sections of math.o:\r\n\r\n\r\n\r\n$ readelf -S math.o\r\n\r\n\r\n\r\n\r\n\r\nThere are 11 section headers, starting at offset 0x1a8:\r\n\r\nSection Headers:\r\n\r\n  [Nr] Name              Type            Addr     Off    Size   \r\nES Flg Lk Inf Al\r\n\r\n  [ 0]                   NULL            00000000 000000 000000 \r\n00      0   0  0\r\n\r\n  [ 1] .text             PROGBITS        00000000 000034 00000d \r\n00  AX  0   0  1\r\n\r\n  [ 2] .data             PROGBITS        00000000 000041 000000 \r\n00  WA  0   0  1\r\n\r\n  [ 3] .bss              NOBITS          00000000 000041 000000 \r\n00  WA  0   0  1\r\n\r\n  [ 4] .comment          PROGBITS        00000000 000041 000035 \r\n01  MS  0   0  1\r\n\r\n  [ 5] .note.GNU-stack   PROGBITS        00000000 000076 000000 \r\n00      0   0  1\r\n\r\n  [ 6] .eh_frame         PROGBITS        00000000 000078 000038 \r\n00   A  0   0  4\r\n\r\n  [ 7] .rel.eh_frame     REL             00000000 00014c 000008 \r\n08   I  9   6  4\r\n\r\n  [ 8] .shstrtab         STRTAB          00000000 000154 000053 \r\n00      0   0  1\r\n\r\n  [ 9] .symtab           SYMTAB          00000000 0000b0 000090 \r\n10     10   8  4\r\n\r\n  [10] .strtab           STRTAB          00000000 000140 00000c \r\n00      0   0  1\r\n\r\nKey to Flags:\r\n\r\n  W (write), A (alloc), X (execute), M (merge), S (strings)\r\n\r\n  I (info), L (link order), G (group), T (TLS), E (exclude), x \r\n(unknown)\r\n\r\n  O (extra OS processing required) o (OS specific), p (processor \r\nspecific)\r\n\r\n\r\n\r\nAs shown in the output, all the section virtual memory addresses \r\nof every section are set to 0. At this stage, each object file is \r\nsimply a block of binary that contains code and data. Its \r\nexistence is to serve as a material container for the final \r\nproduct, which is the executable binary. As such, the virtual \r\naddresses in hello.o are all zeroes.\r\n\r\nNo segment exists at this stage:\r\n\r\n\r\n\r\n$ readelf -l math.o\r\n\r\nThere are no program headers in this file.\r\n\r\n\r\n\r\nThe same happens to other object file:\r\n\r\n\r\n\r\nThere are 13 section headers, starting at offset 0x224:\r\n\r\nSection Headers:\r\n\r\n  [Nr] Name              Type            Addr     Off    Size   \r\nES Flg Lk Inf Al\r\n\r\n  [ 0]                   NULL            00000000 000000 000000 \r\n00      0   0  0\r\n\r\n  [ 1] .text             PROGBITS        00000000 000034 00002e \r\n00  AX  0   0  1\r\n\r\n  [ 2] .rel.text         REL             00000000 0001ac 000010 \r\n08   I 11   1  4\r\n\r\n  [ 3] .data             PROGBITS        00000000 000062 000000 \r\n00  WA  0   0  1\r\n\r\n  [ 4] .bss              NOBITS          00000000 000062 000000 \r\n00  WA  0   0  1\r\n\r\n  [ 5] .rodata           PROGBITS        00000000 000062 00000c \r\n00   A  0   0  1\r\n\r\n  [ 6] .comment          PROGBITS        00000000 00006e 000035 \r\n01  MS  0   0  1\r\n\r\n  [ 7] .note.GNU-stack   PROGBITS        00000000 0000a3 000000 \r\n00      0   0  1\r\n\r\n  [ 8] .eh_frame         PROGBITS        00000000 0000a4 000044 \r\n00   A  0   0  4\r\n\r\n  [ 9] .rel.eh_frame     REL             00000000 0001bc 000008 \r\n08   I 11   8  4\r\n\r\n  [10] .shstrtab         STRTAB          00000000 0001c4 00005f \r\n00      0   0  1\r\n\r\n  [11] .symtab           SYMTAB          00000000 0000e8 0000b0 \r\n10     12   9  4\r\n\r\n  [12] .strtab           STRTAB          00000000 000198 000013 \r\n00      0   0  1\r\n\r\nKey to Flags:\r\n\r\n  W (write), A (alloc), X (execute), M (merge), S (strings)\r\n\r\n  I (info), L (link order), G (group), T (TLS), E (exclude), x \r\n(unknown)\r\n\r\n  O (extra OS processing required) o (OS specific), p (processor \r\nspecific)\r\n\r\n\r\n\r\n\r\n\r\n$ readelf -l hello.o\r\n\r\nThere are no program headers in this file.\r\n\r\n\r\n\r\nOnly when object files are combined into a final executable \r\nbinary, sections are fully realized:\r\n\r\n\r\n\r\n$ gcc -m32 math.o hello.o -o hello\r\n\r\n$ readelf -S hello.\r\n\r\n\r\n\r\n\r\n\r\nThere are 31 section headers, starting at offset 0x1804:\r\n\r\nSection Headers:\r\n\r\n  [Nr] Name              Type            Addr     Off    Size   \r\nES Flg Lk Inf Al\r\n\r\n  [ 0]                   NULL            00000000 000000 000000 \r\n00      0   0  0\r\n\r\n  [ 1] .interp           PROGBITS        08048154 000154 000013 \r\n00   A  0   0  1\r\n\r\n  [ 2] .note.ABI-tag     NOTE            08048168 000168 000020 \r\n00   A  0   0  4\r\n\r\n  [ 3] .note.gnu.build-i NOTE            08048188 000188 000024 \r\n00   A  0   0  4\r\n\r\n  [ 4] .gnu.hash         GNU_HASH        080481ac 0001ac 000020 \r\n04   A  5   0  4\r\n\r\n  [ 5] .dynsym           DYNSYM          080481cc 0001cc 000050 \r\n10   A  6   1  4\r\n\r\n  [ 6] .dynstr           STRTAB          0804821c 00021c 00004a \r\n00   A  0   0  1\r\n\r\n  [ 7] .gnu.version      VERSYM          08048266 000266 00000a \r\n02   A  5   0  2\r\n\r\n  [ 8] .gnu.version_r    VERNEED         08048270 000270 000020 \r\n00   A  6   1  4\r\n\r\n  [ 9] .rel.dyn          REL             08048290 000290 000008 \r\n08   A  5   0  4\r\n\r\n  [10] .rel.plt          REL             08048298 000298 000010 \r\n08  AI  5  24  4\r\n\r\n  [11] .init             PROGBITS        080482a8 0002a8 000023 \r\n00  AX  0   0  4\r\n\r\n  [12] .plt              PROGBITS        080482d0 0002d0 000030 \r\n04  AX  0   0 16\r\n\r\n  [13] .plt.got          PROGBITS        08048300 000300 000008 \r\n00  AX  0   0  8\r\n\r\n  [14] .text             PROGBITS        08048310 000310 0001a2 \r\n00  AX  0   0 16\r\n\r\n  [15] .fini             PROGBITS        080484b4 0004b4 000014 \r\n00  AX  0   0  4\r\n\r\n  [16] .rodata           PROGBITS        080484c8 0004c8 000014 \r\n00   A  0   0  4\r\n\r\n  [17] .eh_frame_hdr     PROGBITS        080484dc 0004dc 000034 \r\n00   A  0   0  4\r\n\r\n  [18] .eh_frame         PROGBITS        08048510 000510 0000ec \r\n00   A  0   0  4\r\n\r\n  [19] .init_array       INIT_ARRAY      08049f08 000f08 000004 \r\n00  WA  0   0  4\r\n\r\n  [20] .fini_array       FINI_ARRAY      08049f0c 000f0c 000004 \r\n00  WA  0   0  4\r\n\r\n  [21] .jcr              PROGBITS        08049f10 000f10 000004 \r\n00  WA  0   0  4\r\n\r\n  [22] .dynamic          DYNAMIC         08049f14 000f14 0000e8 \r\n08  WA  6   0  4\r\n\r\n  [23] .got              PROGBITS        08049ffc 000ffc 000004 \r\n04  WA  0   0  4\r\n\r\n  [24] .got.plt          PROGBITS        0804a000 001000 000014 \r\n04  WA  0   0  4\r\n\r\n  [25] .data             PROGBITS        0804a014 001014 000008 \r\n00  WA  0   0  4\r\n\r\n  [26] .bss              NOBITS          0804a01c 00101c 000004 \r\n00  WA  0   0  1\r\n\r\n  [27] .comment          PROGBITS        00000000 00101c 000034 \r\n01  MS  0   0  1\r\n\r\n  [28] .shstrtab         STRTAB          00000000 0016f8 00010a \r\n00      0   0  1\r\n\r\n  [29] .symtab           SYMTAB          00000000 001050 000470 \r\n10     30  48  4\r\n\r\n  [30] .strtab           STRTAB          00000000 0014c0 000238 \r\n00      0   0  1\r\n\r\nKey to Flags:\r\n\r\n  W (write), A (alloc), X (execute), M (merge), S (strings)\r\n\r\n  I (info), L (link order), G (group), T (TLS), E (exclude), x \r\n(unknown)\r\n\r\n  O (extra OS processing required) o (OS specific), p (processor \r\nspecific)\r\n\r\n\r\n\r\nEvery loadable section is assigned an address, highlighted in \r\ngreen. The reason each section got its own address is that in \r\nreality, gcc does not combine an object by itself, but invokes \r\nthe linker ld. The linker ld uses the default script that it can \r\nfind in the system to build the executable binary. In the default \r\nscript, a segment is assigned a starting address 0x8048000 and \r\nsections belong to it. Then:\r\n\r\n• \\mathtt{1^{st}\\,section\\,address=starting\\,segment\\,address+section\\,offset=0x8048000+0x154=0x08048154}\r\n\r\n\r\n• \\mathtt{2^{nd}\\,section\\,address=starting\\,segment\\,address+section\\,offset=0x8048000+0x168=0x08048168}\r\n\r\n\r\n• .... and so on until the last loadable section...\r\n\r\nIndeed, the end address of a segment is also the end address of \r\nthe final section. We can see this by listing all the segments:\r\n\r\n\r\n\r\n$ readelf -l hello\r\n\r\n\r\n\r\nAnd check, for example, LOAD segment which starts at 0x08048000 \r\nand end at \\mathtt{0x08048000+0x005fc=0x080485fc}\r\n:\r\n\r\n\r\n\r\nElf file type is EXEC (Executable file)\r\n\r\nEntry point 0x8048310\r\n\r\nThere are 9 program headers, starting at offset 52\r\n\r\nProgram Headers:\r\n\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\nFlg Align\r\n\r\n  PHDR           0x000034 0x08048034 0x08048034 0x00120 0x00120 R \r\nE 0x4\r\n\r\n  INTERP         0x000154 0x08048154 0x08048154 0x00013 0x00013 R \r\n  0x1\r\n\r\n      [Requesting program interpreter: /lib/ld-linux.so.2]\r\n\r\n  LOAD           0x000000 0x08048000 0x08048000 0x005fc 0x005fc R \r\nE 0x1000\r\n\r\n  LOAD           0x000f08 0x08049f08 0x08049f08 0x00114 0x00118 \r\nRW  0x1000\r\n\r\n  DYNAMIC        0x000f14 0x08049f14 0x08049f14 0x000e8 0x000e8 \r\nRW  0x4\r\n\r\n  NOTE           0x000168 0x08048168 0x08048168 0x00044 0x00044 R \r\n  0x4\r\n\r\n  GNU_EH_FRAME   0x0004dc 0x080484dc 0x080484dc 0x00034 0x00034 R \r\n  0x4\r\n\r\n  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 \r\nRW  0x10\r\n\r\n  GNU_RELRO      0x000f08 0x08049f08 0x08049f08 0x000f8 0x000f8 R \r\n  0x1\r\n\r\n Section to Segment mapping:\r\n\r\n  Segment Sections...\r\n\r\n   00     \r\n\r\n   01     .interp \r\n\r\n   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash \r\n.dynsym .dynstr \r\n\r\n.gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .plt.got \r\n.text .fini \r\n\r\n.rodata .eh_frame_hdr .eh_frame \r\n\r\n   03     .init_array .fini_array .jcr .dynamic .got .got.plt \r\n.data .bss \r\n\r\n   04     .dynamic \r\n\r\n   05     .note.ABI-tag .note.gnu.build-id \r\n\r\n   06     .eh_frame_hdr \r\n\r\n   07     \r\n\r\n   08     .init_array .fini_array .jcr .dynamic .got \r\n\r\n\r\n\r\nThe last section in the first LOAD segment is .eh_frame. The \r\nsection starts at 0x08048510, with the offset 0x510 and its size \r\nis 0xec. The end address of .eh_frame should be: \\mathtt{0x08048510+0x510+0xec=0x080485fc}\r\n\r\n, exactly the same as the end address of the first LOAD segment.\r\n\r\nChapter [chap:Linking-and-loading] will explore this whole \r\nprocess in detail.\r\n\r\nRuntime inspection and debug\r\n\r\nA [margin:\r\ndebugger\r\n]debuggerdebugger is a program that allows inspection of a \r\nrunning program. A debugger can start and run a program then stop \r\nat a specific line for examining the state of the program at that \r\npoint. The point where the debugger stop (but not halt) is called \r\na breakpoint.\r\n\r\nWe will be using the GDB - GNU Debugger for debugging our kernel. \r\ngdb is the program name. gdb can do four main kinds of things:\r\n\r\n• Start your program, specifying anything that might affect its \r\n  behavior.\r\n\r\n• Make your program stop on specified conditions.\r\n\r\n• Examine what has happened, when your program has stopped\r\n\r\n• Change things in your program, so you can experiment with \r\n  correcting the effects of one bug and go on to learn about \r\n  another\r\n\r\n  A sample program\r\n\r\nThere must be an existing program for debugging. The good old “\r\nHello World” program suffices for the educational purpose in this \r\nchapter:\r\n\r\n#include <stdio.h>\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    printf(\"Hello World!\\n\");\r\n\r\n    return 0;\r\n\r\n}\r\n\r\nWe compile it with debugging information with the option -g:\r\n\r\n\r\n\r\n$ gcc -m32 -g hello.c -o hello\r\n\r\n\r\n\r\nFinally, we start gdb with the program as argument:\r\n\r\n\r\n\r\n$ gdb hello\r\n\r\n\r\n\r\n  Static inspection of a program\r\n\r\nBefore inspecting a program at runtime, gdb loads it first. Upon \r\nloading into memory (but without running), a lot of useful \r\ninformation can be retrieve for inspection. The commands in this \r\nsection can be used before the program runs. However, they are \r\nalso usable when the program runs and can display even more \r\ninformation.\r\n\r\n  Command: info target/info file/info files\r\n\r\nThis command prints the information of the target being debugged. \r\nA target is the debugging program.\r\n\r\nThe output of the command from hello program, a local target in \r\ndetail:\r\n\r\n  \r\n\r\n  (gdb) info target\r\n\r\n  \r\n\r\n  \r\n\r\n  Symbols from \"/tmp/hello\".\r\n\r\n  Local exec file:\r\n\r\n  \t`/tmp/hello', file type elf32-i386.\r\n\r\n  \tEntry point: 0x8048310\r\n\r\n  \t0x08048154 - 0x08048167 is .interp\r\n\r\n  \t0x08048168 - 0x08048188 is .note.ABI-tag\r\n\r\n  \t0x08048188 - 0x080481ac is .note.gnu.build-id\r\n\r\n  \t0x080481ac - 0x080481cc is .gnu.hash\r\n\r\n  \t0x080481cc - 0x0804821c is .dynsym\r\n\r\n  \t0x0804821c - 0x08048266 is .dynstr\r\n\r\n  \t0x08048266 - 0x08048270 is .gnu.version\r\n\r\n  \t0x08048270 - 0x08048290 is .gnu.version_r\r\n\r\n  \t0x08048290 - 0x08048298 is .rel.dyn\r\n\r\n  \t0x08048298 - 0x080482a8 is .rel.plt\r\n\r\n  \t0x080482a8 - 0x080482cb is .init\r\n\r\n  \t0x080482d0 - 0x08048300 is .plt\r\n\r\n  \t0x08048300 - 0x08048308 is .plt.got\r\n\r\n  \t0x08048310 - 0x080484a2 is .text\r\n\r\n  \t0x080484a4 - 0x080484b8 is .fini\r\n\r\n  \t0x080484b8 - 0x080484cd is .rodata\r\n\r\n  \t0x080484d0 - 0x080484fc is .eh_frame_hdr\r\n\r\n  \t0x080484fc - 0x080485c8 is .eh_frame\r\n\r\n  \t0x08049f08 - 0x08049f0c is .init_array\r\n\r\n  \t0x08049f0c - 0x08049f10 is .fini_array\r\n\r\n  \t0x08049f10 - 0x08049f14 is .jcr\r\n\r\n  \t0x08049f14 - 0x08049ffc is .dynamic\r\n\r\n  \t0x08049ffc - 0x0804a000 is .got\r\n\r\n  \t0x0804a000 - 0x0804a014 is .got.plt\r\n\r\n  \t0x0804a014 - 0x0804a01c is .data\r\n\r\n  \t0x0804a01c - 0x0804a020 is .bss\r\n\r\n  \r\n\r\n  The output displayed reports:\r\n\r\n  • Path of a symbol file. A symbol file is the file that \r\n    contains the debugging information. Usually, this is the same \r\n    file as the binary, but it is common to separate between an \r\n    executable binary and its debugging information into 2 files, \r\n    especially for remote debugging. In the example, it is this \r\n    line:\r\n\r\n    Symbols from \"/tmp/hello\".\r\n\r\n  • The path of the debugging program and its file type. In the \r\n    example, it is this line:\r\n\r\n    Local exec file:\r\n\r\n    \t`/tmp/hello', file type elf32-i386.\r\n\r\n  • The entry point to the debugging program. That is, the very \r\n    first code the program runs. In the example, it is this line:\r\n\r\n    Entry point: 0x8048310\r\n\r\n  • A list of sections with its starting and ending addresses. In \r\n    the example, it is the remaining output.\r\n\r\n\r\n\r\nIf the debugging program runs in a different machine, it is a \r\nremote target and gdb only prints a brief information:\r\n\r\n  \r\n\r\n  (gdb) info target\r\n\r\n  \r\n\r\n  \r\n\r\n  Remote serial target in gdb-specific protocol:\r\n\r\n  Debugging a target over a serial line.\r\n\r\n  \r\n\r\n\r\n\r\n  Command: maint info sections\r\n\r\nThis command is similar to info target but give extra information \r\nabout program sections, specifically the file offset and the \r\nflags of each section.\r\n\r\nHere is the output when running against hello program:\r\n\r\n  \r\n\r\n  (gdb) maint info sections\r\n\r\n  \r\n\r\n  \r\n\r\n  Exec file:\r\n\r\n      `/tmp/hello', file type elf64-x86-64.\r\n\r\n   [0]     0x00400238->0x00400254 at 0x00000238: .interp ALLOC \r\n  LOAD READONLY DATA HAS_CONTENTS\r\n\r\n   [1]     0x00400254->0x00400274 at 0x00000254: .note.ABI-tag \r\n  ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\r\n   [2]     0x00400274->0x00400298 at 0x00000274: \r\n  .note.gnu.build-id ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\r\n   [3]     0x00400298->0x004002b4 at 0x00000298: .gnu.hash ALLOC \r\n  LOAD READONLY DATA HAS_CONTENTS\r\n\r\n   [4]     0x004002b8->0x00400318 at 0x000002b8: .dynsym ALLOC \r\n  LOAD READONLY DATA HAS_CONTENTS\r\n\r\n   [5]     0x00400318->0x00400355 at 0x00000318: .dynstr ALLOC \r\n  LOAD READONLY DATA HAS_CONTENTS\r\n\r\n   [6]     0x00400356->0x0040035e at 0x00000356: .gnu.version \r\n  ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\r\n   [7]     0x00400360->0x00400380 at 0x00000360: .gnu.version_r \r\n  ALLOC LOAD READONLY DATA HAS_CONTENTS\r\n\r\n  ....remaining output omitted....\r\n\r\n  \r\n\r\nThe output is similar to info target, but with more details. Next \r\nto the section names are the section flags, which are attributes \r\nof a section. Here, we can see that the sections with LOAD flag \r\nare from LOAD segment. The command can be combined with the \r\nsection flags for filtered outputs:\r\n\r\n  ALLOBJ displays sections for all loaded object files, including \r\n  shared libraries. Shared libraries are only displayed when the \r\n  program is already running.\r\n\r\n  section names displays only named sections. \r\n\r\n  The command:\r\n\r\n    \r\n\r\n    (gdb) maint info sections .text .data .bss\r\n\r\n    \r\n\r\n    only displays .text, .data and .bss sections:\r\n\r\n    \r\n\r\n    Exec file:\r\n\r\n        `/tmp/hello', file type elf64-x86-64.\r\n\r\n     [13]     0x00400430->0x004005c2 at 0x00000430: .text ALLOC \r\n    LOAD READONLY CODE HAS_CONTENTS\r\n\r\n     [24]     0x00601028->0x00601038 at 0x00001028: .data ALLOC \r\n    LOAD DATA HAS_CONTENTS\r\n\r\n     [25]     0x00601038->0x00601040 at 0x00001038: .bss ALLOC\r\n\r\n    \r\n\r\n  section-flags displays only sections with specified section \r\n  flags. Note that these section flags are specific to gdb, \r\n  though it is based on the section attributes defined \r\n  previously. Currently, gdb understands the following flags:\r\n\r\n  ALLOC Section will have space allocated in the process when \r\n    loaded. Set for all sections except those containing debug \r\n    information. \r\n\r\n  LOAD Section will be loaded from the file into the child \r\n    process memory. Set for pre-initialized code and data, clear \r\n    for .bss sections. \r\n\r\n  RELOC Section needs to be relocated before loading. \r\n\r\n  READONLY Section cannot be modified by the child process. \r\n\r\n  CODE Section contains executable code only. \r\n\r\n  DATA Section contains data only (no executable code). \r\n\r\n  ROM Section will reside in ROM. \r\n\r\n  CONSTRUCTOR Section contains data for constructor/destructor \r\n    lists. \r\n\r\n  HAS_CONTENTS Section is not empty. \r\n\r\n  NEVER_LOAD An instruction to the linker to not output the \r\n    section. \r\n\r\n  COFF_SHARED_LIBRARY A notification to the linker that the \r\n    section contains COFF shared library information. COFF is an \r\n    object file format, similar to ELF. While ELF is the file \r\n    format for an executable binary, COFF is the file format for \r\n    an object file.\r\n\r\n  IS_COMMON Section contains common symbols.\r\n\r\n  We can restrict the output to only display sections that \r\n  contain code with the command:\r\n\r\n    \r\n\r\n    (gdb) maint info sections CODE\r\n\r\n    \r\n\r\n    The output:\r\n\r\n    \r\n\r\n    Exec file:\r\n\r\n        `/tmp/hello', file type elf64-x86-64.\r\n\r\n     [10]     0x004003c8->0x004003e2 at 0x000003c8: .init ALLOC \r\n    LOAD READONLY CODE HAS_CONTENTS\r\n\r\n     [11]     0x004003f0->0x00400420 at 0x000003f0: .plt ALLOC \r\n    LOAD READONLY CODE HAS_CONTENTS\r\n\r\n     [12]     0x00400420->0x00400428 at 0x00000420: .plt.got \r\n    ALLOC LOAD READONLY CODE HAS_CONTENTS\r\n\r\n     [13]     0x00400430->0x004005c2 at 0x00000430: .text ALLOC \r\n    LOAD READONLY CODE HAS_CONTENTS\r\n\r\n     [14]     0x004005c4->0x004005cd at 0x000005c4: .fini ALLOC \r\n    LOAD READONLY CODE HAS_CONTENTS\r\n\r\n    \r\n\r\n  Command: info functions\r\n\r\nThis command lists all function names and their loaded addresses. \r\nThe names can be filtered with a regular expression.\r\n\r\nRun the command, we get the following output:\r\n\r\n  \r\n\r\n  (gdb) info functions\r\n\r\n  \r\n\r\n  \r\n\r\n  All defined functions:\r\n\r\n  File hello.c:\r\n\r\n  int main(int, char **);\r\n\r\n  Non-debugging symbols:\r\n\r\n  0x00000000004003c8  _init\r\n\r\n  0x0000000000400400  puts@plt\r\n\r\n  0x0000000000400410  __libc_start_main@plt\r\n\r\n  0x0000000000400430  _start\r\n\r\n  0x0000000000400460  deregister_tm_clones\r\n\r\n  0x00000000004004a0  register_tm_clones\r\n\r\n  0x00000000004004e0  __do_global_dtors_aux\r\n\r\n  0x0000000000400500  frame_dummy\r\n\r\n  0x0000000000400550  __libc_csu_init\r\n\r\n  0x00000000004005c0  __libc_csu_fini\r\n\r\n  0x00000000004005c4  _fini\r\n\r\n  \r\n\r\n  Command: info variables\r\n\r\nThis command lists all global and static variable names, or \r\nfiltered with a regular expression.\r\n\r\nIf we add a global variable int i into the sample source program \r\nand recompile then run the command, we get the following output:\r\n\r\n  \r\n\r\n  (gdb) info variables\r\n\r\n  \r\n\r\n  \r\n\r\n  All defined variables:\r\n\r\n  \r\n\r\n  File hello.c:\r\n\r\n  int i;\r\n\r\n  \r\n\r\n  Non-debugging symbols:\r\n\r\n  0x00000000004005d0  _IO_stdin_used\r\n\r\n  0x00000000004005e4  __GNU_EH_FRAME_HDR\r\n\r\n  0x0000000000400708  __FRAME_END__\r\n\r\n  0x0000000000600e10  __frame_dummy_init_array_entry\r\n\r\n  0x0000000000600e10  __init_array_start\r\n\r\n  0x0000000000600e18  __do_global_dtors_aux_fini_array_entry\r\n\r\n  0x0000000000600e18  __init_array_end\r\n\r\n  0x0000000000600e20  __JCR_END__\r\n\r\n  0x0000000000600e20  __JCR_LIST__\r\n\r\n  0x0000000000600e28  _DYNAMIC\r\n\r\n  0x0000000000601000  _GLOBAL_OFFSET_TABLE_\r\n\r\n  0x0000000000601028  __data_start\r\n\r\n  0x0000000000601028  data_start\r\n\r\n  0x0000000000601030  __dso_handle\r\n\r\n  0x000000000060103c  __bss_start\r\n\r\n  0x000000000060103c  _edata\r\n\r\n  0x000000000060103c  completed\r\n\r\n  0x0000000000601040  __TMC_END__\r\n\r\n  0x0000000000601040  _end\r\n\r\n  \r\n\r\n  Command: disassemble/disas\r\n\r\nThis command displays the assembly code of the executable file. \r\n\r\ngdb can display the assembly code of a function:\r\n\r\n  \r\n\r\n  (gdb) disassemble main\r\n\r\n  \r\n\r\n  \r\n\r\n  Dump of assembler code for function main:\r\n\r\n     0x0804840b <+0>: \tlea    ecx,[esp+0x4]\r\n\r\n     0x0804840f <+4>: \tand    esp,0xfffffff0\r\n\r\n     0x08048412 <+7>: \tpush   DWORD PTR [ecx-0x4]\r\n\r\n     0x08048415 <+10>:\tpush   ebp\r\n\r\n     0x08048416 <+11>:\tmov    ebp,esp\r\n\r\n     0x08048418 <+13>:\tpush   ecx\r\n\r\n     0x08048419 <+14>:\tsub    esp,0x4\r\n\r\n     0x0804841c <+17>:\tsub    esp,0xc\r\n\r\n     0x0804841f <+20>:\tpush   0x80484c0\r\n\r\n     0x08048424 <+25>:\tcall   0x80482e0 <puts@plt>\r\n\r\n     0x08048429 <+30>:\tadd    esp,0x10\r\n\r\n     0x0804842c <+33>:\tmov    eax,0x0\r\n\r\n     0x08048431 <+38>:\tmov    ecx,DWORD PTR [ebp-0x4]\r\n\r\n     0x08048434 <+41>:\tleave  \r\n\r\n     0x08048435 <+42>:\tlea    esp,[ecx-0x4]\r\n\r\n     0x08048438 <+45>:\tret    \r\n\r\n  End of assembler dump.\r\n\r\n  \r\n\r\n  It would be more useful if source is included:\r\n\r\n  \r\n\r\n  (gdb) disassemble /s main\r\n\r\n  \r\n\r\n  \r\n\r\n  Dump of assembler code for function main:\r\n\r\n  hello.c:\r\n\r\n  4\t{\r\n\r\n     0x0804840b <+0>:\tlea     ecx,[esp+0x4]\r\n\r\n     0x0804840f <+4>:\tand     esp,0xfffffff0\r\n\r\n     0x08048412 <+7>:\tpush    DWORD PTR [ecx-0x4]\r\n\r\n     0x08048415 <+10>:\tpush   ebp\r\n\r\n     0x08048416 <+11>:\tmov    ebp,esp\r\n\r\n     0x08048418 <+13>:\tpush   ecx\r\n\r\n     0x08048419 <+14>:\tsub    esp,0x4\r\n\r\n  5\t    printf(\"Hello World!\\n\");\r\n\r\n     0x0804841c <+17>:\tsub    esp,0xc\r\n\r\n     0x0804841f <+20>:\tpush   0x80484c0\r\n\r\n     0x08048424 <+25>:\tcall   0x80482e0 <puts@plt>\r\n\r\n     0x08048429 <+30>:\tadd    esp,0x10\r\n\r\n  6\t    return 0;\r\n\r\n     0x0804842c <+33>:\tmov    eax,0x0\r\n\r\n  7\t}\r\n\r\n     0x08048431 <+38>:\tmov    ecx,DWORD PTR [ebp-0x4]\r\n\r\n     0x08048434 <+41>:\tleave  \r\n\r\n     0x08048435 <+42>:\tlea    esp,[ecx-0x4]\r\n\r\n     0x08048438 <+45>:\tret    \r\n\r\n  End of assembler dump.\r\n\r\n  \r\n\r\n  Now the high level source (in green text) is included as part \r\n  of the assembly dump. Each line is backed by the corresponding \r\n  assembly code below it. \r\n\r\n\r\n\r\nIf the option /r is added, raw instructions in hex are included, \r\njust like how objdump displays assembly code by default:\r\n\r\n  \r\n\r\n  (gdb) disassemble /rs main\r\n\r\n  \r\n\r\n  \r\n\r\n  Dump of assembler code for function main:\r\n\r\n  hello.c:\r\n\r\n  4\t{\r\n\r\n     0x0804840b <+0>:\t 8d 4c 24 04\t   lea    ecx,[esp+0x4]\r\n\r\n     0x0804840f <+4>:\t 83 e4 f0\t      and    esp,0xfffffff0\r\n\r\n     0x08048412 <+7>:\t ff 71 fc\t      push   DWORD PTR [ecx-0x4]\r\n\r\n     0x08048415 <+10>:\t55\t  push   ebp\r\n\r\n     0x08048416 <+11>:\t89 e5   mov    ebp,esp\r\n\r\n     0x08048418 <+13>:\t51\t  push   ecx\r\n\r\n     0x08048419 <+14>:\t83 ec 04\t      sub    esp,0x4\r\n\r\n  5\t    printf(\"Hello World!\\n\");\r\n\r\n     0x0804841c <+17>:\t83 ec 0c\t      sub    esp,0xc\r\n\r\n     0x0804841f <+20>:\t68 c0 84 04 08\tpush   0x80484c0\r\n\r\n     0x08048424 <+25>:\te8 b7 fe ff ff\tcall   0x80482e0 <puts@plt>\r\n\r\n     0x08048429 <+30>:\t83 c4 10\t      add    esp,0x10\r\n\r\n  6\t    return 0;\r\n\r\n     0x0804842c <+33>:\tb8 00 00 00 00\tmov    eax,0x0\r\n\r\n  7\t}\r\n\r\n     0x08048431 <+38>:\t8b 4d fc\t      mov    ecx,DWORD PTR \r\n  [ebp-0x4]\r\n\r\n     0x08048434 <+41>:\tc9\tleave  \r\n\r\n     0x08048435 <+42>:\t8d 61 fc\t      lea    esp,[ecx-0x4]\r\n\r\n     0x08048438 <+45>:\tc3\tret    \r\n\r\n  End of assembler dump.\r\n\r\n  \r\n\r\n  A function in a specific file can also be specified:\r\n\r\n  \r\n\r\n  (gdb) disassemble /sr 'hello.c'::main\r\n\r\n  \r\n\r\n  \r\n\r\n  Dump of assembler code for function main:\r\n\r\n  hello.c:\r\n\r\n  4\t{\r\n\r\n     0x0804840b <+0>:\t 8d 4c 24 04\t  lea    ecx,[esp+0x4]\r\n\r\n     0x0804840f <+4>:\t 83 e4 f0\t     and    esp,0xfffffff0\r\n\r\n     0x08048412 <+7>:\t ff 71 fc\t     push   DWORD PTR [ecx-0x4]\r\n\r\n     0x08048415 <+10>:\t55\t   push   ebp\r\n\r\n     0x08048416 <+11>:\t89 e5\tmov    ebp,esp\r\n\r\n     0x08048418 <+13>:\t51\t   push   ecx\r\n\r\n     0x08048419 <+14>:\t83 ec 04\t      sub    esp,0x4\r\n\r\n  5\t    printf(\"Hello World!\\n\");\r\n\r\n     0x0804841c <+17>:\t83 ec 0c\t      sub    esp,0xc\r\n\r\n     0x0804841f <+20>:\t68 c0 84 04 08\tpush   0x80484c0\r\n\r\n     0x08048424 <+25>:\te8 b7 fe ff ff\tcall   0x80482e0 <puts@plt>\r\n\r\n     0x08048429 <+30>:\t83 c4 10\t      add    esp,0x10\r\n\r\n  6\t    return 0;\r\n\r\n     0x0804842c <+33>:\tb8 00 00 00 00\tmov    eax,0x0\r\n\r\n  7\t}\r\n\r\n     0x08048431 <+38>:\t8b 4d fc\t      mov    ecx,DWORD PTR \r\n  [ebp-0x4]\r\n\r\n     0x08048434 <+41>:\tc9\tleave  \r\n\r\n     0x08048435 <+42>:\t8d 61 fc\t      lea    esp,[ecx-0x4]\r\n\r\n     0x08048438 <+45>:\tc3\tret    \r\n\r\n  End of assembler dump.\r\n\r\n  \r\n\r\n  The filename must be included in a single quote, and the \r\n  function must be prefixed by double colons e.g. 'hello.c'::main \r\n  to specify disassembling of the function main in the file \r\n  hello.c.\r\n\r\n  Command: x\r\n\r\nThis command examines the content of a given memory range.\r\n\r\nWe can examine the raw content of main:\r\n\r\n  \r\n\r\n  (gdb) x main\r\n\r\n  \r\n\r\n  \r\n\r\n  0x804840b <main>:\t0x04244c8d\r\n\r\n  \r\n\r\n  By default, without any argument, the command only prints the \r\n  content of a single memory address. In this case, that is the \r\n  starting memory address of main.\r\n\r\n\r\n\r\nWith format arguments, the command can print a range of memory in \r\na specific format.\r\n\r\n  \r\n\r\n  (gdb) x/20b main\r\n\r\n  \r\n\r\n  \r\n\r\n  0x804840b <main>:\t   0x8d\t0x4c\t0x24\t0x04\t0x83\t0xe40xf0\t0xff\r\n\r\n  0x8048413 <main+8>:\t 0x71\t0xfc\t0x55\t0x89\t0xe5\t0x510x83\t0xec\r\n\r\n  0x804841b <main+16>:\t0x04\t0x83\t0xec\t0x0c\r\n\r\n  \r\n\r\n  /20b main argument means that the command prints 20 bytes, \r\n  where main starts in memory.\r\n\r\nThe general form for format argument is: /<repeated count><format \r\nletter>\r\n\r\nIf the repeated count is not supplied, by default gdb supplies \r\nthe count as 1. The format letter is one of the following values:\r\n\r\n\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| Letter  | Description                                                                                     |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| o       | Print the memory content in octal format.                                                       |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| x       | Print the memory content in hex format.                                                         |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| d       | Print the memory content in decimal format.                                                     |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| u       | Print the memory content in unsigned decimal format.                                            |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| t       | Print the memory content in binary format.                                                      |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| f       | Print the memory content in float format.                                                       |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| a       | Print the memory content as memory addresses.                                                   |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| i       | Print the memory content as a series of assembly instructions, \r\nsimilar to disassemble command. |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| c       | Print the memory content as an array of ASCII characters.                                       |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n| s       | Print the memory content as a string                                                            |\r\n+---------+-------------------------------------------------------------------------------------------------+\r\n\r\n\r\nDepends on the circumstance, certain format is advantageous than \r\nthe others. For example, if a memory region contains \r\nfloating-point numbers, then it is better to use the format f \r\nthan viewing the number as separated 1-byte hex numbers.\r\n\r\n  Command: print/p\r\n\r\nExamining raw memory is useful but usually it is better to have a \r\nmore human-readable output. This command does precisely the task: \r\nit pretty-prints an expression. An expression can be a global \r\nvariable, a local variable in current stack frame, a function, a \r\nregister, a number...\r\n\r\n  Runtime inspection of a program\r\n\r\nThe main use of a debugger is to examine the state of a program, \r\nwhen it is running. gdb provides a set of useful commands for \r\nretrieving useful runtime information.\r\n\r\n  Command: run\r\n\r\nThis command starts running the program.\r\n\r\nRun the hello program:\r\n\r\n  \r\n\r\n  (gdb) r\r\n\r\n  \r\n\r\n  \r\n\r\n  Starting program: /tmp/hello \r\n\r\n  Hello World!\r\n\r\n  [Inferior 1 (process 1002) exited normally]\r\n\r\n  \r\n\r\n  The program runs successfully and printed the message “Hello \r\n  World”. However, it would not be useful if all gdb can do is \r\n  run a program.\r\n\r\n  Command: break/b\r\n\r\nThis command sets a breakpoint at a location in the high-level \r\nsource code. When gdb runs to a specific location marked by a \r\nbreakpoint, it stops executing for a programmer to inspect the \r\ncurrent state of a program.\r\n\r\nA breakpoint can be set on a line as displayed by an editor. \r\nSuppose we want to set a breakpoint at line 3 of the program, \r\nwhich is the start of main function:\r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\n@|\\color{red}\\bfseries int main(int argc, char *argv[])|@\r\n\r\n{\r\n\r\n    printf(\"Hello World!\\n\");\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  When running a program, instead of running from start to \r\n  finish, gdb stopped at line 3:\r\n\r\n  \r\n\r\n  (gdb) b 3\r\n\r\n  \r\n\r\n  \r\n\r\n  Breakpoint 1 at 0x400535: file hello.c, line 3.\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) r\r\n\r\n  \r\n\r\n  \r\n\r\n  Starting program: /tmp/hello \r\n\r\n  Breakpoint 1, main (argc=1, argv=0x7fffffffdfb8) at hello.c:5\r\n\r\n  5\t    printf(\"Hello World!\\n\");\r\n\r\n  \r\n\r\n  The breakpoint is at line 3, but gdb stopped line 5. The reason \r\n  is that line 3 does not contain code, but a function signature; \r\n  gdb only stops where it can execute code. The code in the \r\n  function starts at line 5, the call to printf, so gdb stops \r\n  there.\r\n\r\n\r\n\r\nLine of code is not always the reliable way to specify a \r\nbreakpoint, as the source code can be changed. What if gdb should \r\nalways stop at main function? In this case, a better method is to \r\nuse the function name directly:\r\n\r\n  \r\n\r\n  b main\r\n\r\n  \r\n\r\n  Then, regardless of how the source code changes, gdb always \r\n  stops at the main function.\r\n\r\n\r\n\r\nSometimes, the debugging program does not contain debug info, or \r\ngdb is debugging assembly code. In that case, a memory address \r\ncan be specified as a stop point. To get the function address, \r\nprint command can be used:\r\n\r\n  \r\n\r\n  (gdb) print main\r\n\r\n  \r\n\r\n  \r\n\r\n  $3 = {int (int, char **)} 0x400526 <main>\r\n\r\n  \r\n\r\n  Knowing the address of main, we can easily set a breakpoint \r\n  with a memory address:\r\n\r\n  \r\n\r\n  b *0x400526\r\n\r\n  \r\n\r\n\r\n\r\ngdb can also set breakpoint in any source file. Suppose that \r\nhello program is composed not just one file but many files e.g. \r\nhello1.c, hello2.c, hello3.c... In that case, simply add the \r\nfilename before either a line number:\r\n\r\n  \r\n\r\n  b hello.c:3\r\n\r\n  \r\n\r\n\r\n\r\nA function name in a specific file can also be set:\r\n\r\n  \r\n\r\n  b hello.c:main\r\n\r\n  \r\n\r\n  Command: next/n\r\n\r\nThis command executes the current line and stops at the next \r\nline. When the current line is a function call, steps over it.\r\n\r\nAfter setting a breakpoint at main, run a program and stop at the \r\nfirst printf:\r\n\r\n  \r\n\r\n  (gdb) r\r\n\r\n  \r\n\r\n  \r\n\r\n  Starting program: /tmp/hello \r\n\r\n  Breakpoint 1, main (argc=1, argv=0x7fffffffdfb8) at hello.c:5\r\n\r\n  5\t    printf(\"Hello World!\\n\");\r\n\r\n  \r\n\r\n  Then, to proceed to the next statement, we use the next \r\n  command:\r\n\r\n  \r\n\r\n  (gdb) n\r\n\r\n  \r\n\r\n  \r\n\r\n  Hello World!\r\n\r\n  6\t    return 0;\r\n\r\n  \r\n\r\n  In the output, the first line shows the output produced after \r\n  executing line 5; then, the next line shows where gdb stops \r\n  currently, which is line 6.\r\n\r\n  Command: step/s\r\n\r\nThis command executes the current line and stops at the next \r\nline. When the current line is a function call, steps into it to \r\nthe first next line in the called function.\r\n\r\nSuppose we have a new function add[footnote:\r\nWhy should we add a new function and function call instead of \r\nusing the existing printf call? Stepping into shared library \r\nfunctions is tricky because to make debugging works, the debug \r\ninfo must be installed and loaded. It is not worth the trouble \r\nfor demonstrating this simple command.\r\n]:\r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\nint add(int a, int b) {\r\n\r\n\treturn a + b;\r\n\r\n}\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n\tadd(1, 2);\r\n\r\n    printf(\"Hello World!\\n\");\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  If step command is used instead of next on the function call \r\n  printf, gdb steps inside the function:\r\n\r\n  \r\n\r\n  (gdb) r\r\n\r\n  \r\n\r\n  \r\n\r\n  Starting program: /tmp/hello \r\n\r\n  Breakpoint 1, main (argc=1, argv=0xffffd154) at hello.c:11\r\n\r\n  11\t    add(1, 2);\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) s\r\n\r\n  \r\n\r\n  \r\n\r\n  add (a=1, b=2) at hello.c:6\r\n\r\n  6\t    return a + b;\r\n\r\n  \r\n\r\n  After executing the command s, gdb stepped into the add \r\n  function where the first statement is a return.\r\n\r\n  Command: ni\r\n\r\nAt the core, gdb operates on assembly instruction. Source line by \r\nline debugging is simply an enhancement to make it friendlier for \r\nprogrammers. Each statement in C translates to one or more \r\nassembly instruction, as shown with objdump and disassemble \r\ncommand. With the debug info available, gdb knows how many \r\ninstructions belong to one line of high-level code; line by line \r\ndebugging is just a execution of assembly instructions of a line \r\nwhen moving from the current line to the next.\r\n\r\nThis command executes the one assembly instruction belongs to the \r\ncurrent line. Until all assembly instructions of the current line \r\nare executed, gdb will not move to the next line. If the current \r\ninstruction is a call, step over it to the next instruction.\r\n\r\nWhen breakpoint is on the printf call and ni is used, it steps \r\nthrough each assembly instruction:\r\n\r\n  \r\n\r\n  (gdb) disassemble /s main\r\n\r\n  \r\n\r\n  \r\n\r\n  Dump of assembler code for function main:\r\n\r\n  hello.c:\r\n\r\n  4\t{\r\n\r\n     0x0804840b <+0>:\t lea    ecx,[esp+0x4]\r\n\r\n     0x0804840f <+4>:\t and    esp,0xfffffff0\r\n\r\n     0x08048412 <+7>:\t push   DWORD PTR [ecx-0x4]\r\n\r\n     0x08048415 <+10>:\tpush   ebp\r\n\r\n     0x08048416 <+11>:\tmov    ebp,esp\r\n\r\n     0x08048418 <+13>:\tpush   ecx\r\n\r\n     0x08048419 <+14>:\tsub    esp,0x4\r\n\r\n  5\t    printf(\"Hello World!\\n\");\r\n\r\n     0x0804841c <+17>:\tsub    esp,0xc\r\n\r\n     0x0804841f <+20>:\tpush   0x80484c0\r\n\r\n     0x08048424 <+25>:\tcall   0x80482e0 <puts@plt>\r\n\r\n     0x08048429 <+30>:\tadd    esp,0x10\r\n\r\n  6\t    return 0;\r\n\r\n  => 0x0804842c <+33>:\tmov    eax,0x0\r\n\r\n  7\t}\r\n\r\n     0x08048431 <+38>:\tmov    ecx,DWORD PTR [ebp-0x4]\r\n\r\n     0x08048434 <+41>:\tleave  \r\n\r\n     0x08048435 <+42>:\tlea    esp,[ecx-0x4]\r\n\r\n     0x08048438 <+45>:\tret    \r\n\r\n  End of assembler dump.\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) r\r\n\r\n  \r\n\r\n  \r\n\r\n  Starting program: /tmp/hello \r\n\r\n  Breakpoint 1, main (argc=1, argv=0xffffd154) at hello.c:5\r\n\r\n  5\t    printf(\"Hello World!\\n\");\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) ni\r\n\r\n  \r\n\r\n  \r\n\r\n  0x0804841f\t5\t    printf(\"Hello World!\\n\");\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) ni\r\n\r\n  \r\n\r\n  \r\n\r\n  0x08048424\t5\t    printf(\"Hello World!\\n\");\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) ni\r\n\r\n  \r\n\r\n  \r\n\r\n  Hello World!\r\n\r\n  0x08048429\t5\t    printf(\"Hello World!\\n\");\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb)\r\n\r\n  \r\n\r\n  \r\n\r\n  6\t    return 0;\r\n\r\n  \r\n\r\n  Upon entering ni, gdb executes current instruction and display \r\n  the next instruction. That's why from the output, gdb only \r\n  displays 3 addresses: 0x0804841f, 0x08048424 and 0x08048429. \r\n  The instruction at 0x0804841c, which is the first instruction \r\n  of printf, is not displayed because it is the first instruction \r\n  that gdb stopped at. Assume that gdb stopped at the first \r\n  instruction of printf at 0x0804841c, the current instruction \r\n  can be displayed using x command:\r\n\r\n  \r\n\r\n  (gdb) x/i $eip\r\n\r\n  \r\n\r\n  \r\n\r\n  => 0x804841c <main+17>: sub    esp,0xc\r\n\r\n  \r\n\r\n  Command: si\r\n\r\nSimilar to ni, this command executes the current assembly \r\ninstruction belongs to the current line. But if the current \r\ninstruction is a call, step into it to the first next instruction \r\nin the called function.\r\n\r\nRecall that the assembly code generated from printf contains a \r\ncall instruction:\r\n\r\n  \r\n\r\n  (gdb) disassemble /s main\r\n\r\n  \r\n\r\n  \r\n\r\n  Dump of assembler code for function main:\r\n\r\n  hello.c:\r\n\r\n  4\t{\r\n\r\n     0x0804840b <+0>:\tlea    ecx,[esp+0x4]\r\n\r\n     0x0804840f <+4>:\tand    esp,0xfffffff0\r\n\r\n     0x08048412 <+7>:\tpush   DWORD PTR [ecx-0x4]\r\n\r\n     0x08048415 <+10>:\tpush   ebp\r\n\r\n     0x08048416 <+11>:\tmov    ebp,esp\r\n\r\n     0x08048418 <+13>:\tpush   ecx\r\n\r\n     0x08048419 <+14>:\tsub    esp,0x4\r\n\r\n  5\t    printf(\"Hello World!\\n\");\r\n\r\n     0x0804841c <+17>:\tsub    esp,0xc\r\n\r\n     0x0804841f <+20>:\tpush   0x80484c0\r\n\r\n     0x08048424 <+25>:\tcall   0x80482e0 <puts@plt>\r\n\r\n     0x08048429 <+30>:\tadd    esp,0x10\r\n\r\n  6\t    return 0;\r\n\r\n  => 0x0804842c <+33>:\tmov    eax,0x0\r\n\r\n  7\t}\r\n\r\n     0x08048431 <+38>:\tmov    ecx,DWORD PTR [ebp-0x4]\r\n\r\n     0x08048434 <+41>:\tleave  \r\n\r\n     0x08048435 <+42>:\tlea    esp,[ecx-0x4]\r\n\r\n     0x08048438 <+45>:\tret    \r\n\r\n  End of assembler dump.\r\n\r\n  \r\n\r\n  We try instruction by instruction stepping again, but this time \r\n  by running si at 0x08048424, where call resides:\r\n\r\n  \r\n\r\n  (gdb) si\r\n\r\n  \r\n\r\n  \r\n\r\n  0x0804841f\t5\t        printf(\"Hello World!\\n\");\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) si\r\n\r\n  \r\n\r\n  \r\n\r\n  0x08048424\t5\t        printf(\"Hello World!\\n\");\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) x/i $eip\r\n\r\n  \r\n\r\n  \r\n\r\n  => 0x8048424 <main+25>:\tcall   0x80482e0 <puts@plt>\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) si\r\n\r\n  \r\n\r\n  \r\n\r\n  0x080482e0 in puts@plt ()\r\n\r\n  \r\n\r\n  The next instruction right after 0x8048424 is the first \r\n  instruction at 0x080482e0 in puts function. In other words, gdb \r\n  stepped into puts instead of stepping over it.\r\n\r\n  Command: until\r\n\r\nThis command executes until the next line is greater than the \r\ncurrent line.\r\n\r\nSuppose we have a function that execute a long loop:\r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\nint add1000() {\r\n\r\n    int total = 0;\r\n\r\n\r\n\r\n    for (int i = 0; i < 1000; ++i){\r\n\r\n        total += i;\r\n\r\n    }\r\n\r\n\r\n\r\n    printf(\"Done adding!\\n\");\r\n\r\n\r\n\r\n    return total;\r\n\r\n}\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    add1000(1, 2);\r\n\r\n    printf(\"Hello World!\\n\");\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  Using next command, we need to press 1000 times for finishing \r\n  the loop. Instead, a faster way is to use until: \r\n\r\n  \r\n\r\n  (gdb) b add1000\r\n\r\n  \r\n\r\n  \r\n\r\n  Breakpoint 1 at 0x8048411: file hello.c, line 4.\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) r\r\n\r\n  \r\n\r\n  \r\n\r\n  Starting program: /tmp/hello \r\n\r\n  Breakpoint 1, add1000 () at hello.c:4\r\n\r\n  4\t    int total = 0;\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) until\r\n\r\n  \r\n\r\n  \r\n\r\n  5\t    for (int i = 0; i < 1000; ++i){\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) until\r\n\r\n  \r\n\r\n  \r\n\r\n  6\t        total += i;\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) until\r\n\r\n  \r\n\r\n  \r\n\r\n  5\t    for (int i = 0; i < 1000; ++i){\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) until\r\n\r\n  \r\n\r\n  \r\n\r\n  8\t    printf(\"Done adding!\\n\");\r\n\r\n  \r\n\r\n  Executing the first until, gdb stopped at line 5 since line 5 \r\n  is greater than line 4. \r\n\r\n  Executing the second until, gdb stopped at line 6 since line 6 \r\n  is greater than line 5.\r\n\r\n  Executing the third until, gdb stopped at line 5 since the loop \r\n  still continues. Because line 5 is less than line 6, with the \r\n  fourth until, gdb kept executing until it does not go back to \r\n  line 5 anymore and stopped at line 8. This is a great way to \r\n  skip over loop in the middle, instead of setting unneeded \r\n  breakpoint.\r\n\r\n  until can be supplied with an argument to explicitly execute to \r\n  a specific line:\r\n\r\n  \r\n\r\n  (gdb) r\r\n\r\n  \r\n\r\n  \r\n\r\n  Starting program: /tmp/hello \r\n\r\n  Breakpoint 1, add1000 () at hello.c:4\r\n\r\n  4\t    int total = 0;\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) until 8\r\n\r\n  \r\n\r\n  \r\n\r\n  add1000 () at hello.c:8\r\n\r\n  8\t    printf(\"Done adding!\\n\");\r\n\r\n  \r\n\r\n  Command: finish\r\n\r\nThis command executes until the end of a function and displays \r\nthe return value. finish is actually just a more convenient \r\nversion of until.\r\n\r\nUsing the add1000 function from the previous example and use \r\nfinish instead of until:\r\n\r\n  \r\n\r\n  (gdb) r\r\n\r\n  \r\n\r\n  \r\n\r\n  Starting program: /tmp/hello \r\n\r\n  Breakpoint 1, add1000 () at hello.c:4\r\n\r\n  4\t    int total = 0;\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) finish\r\n\r\n  \r\n\r\n  \r\n\r\n  Run till exit from #0  add1000 () at hello.c:4\r\n\r\n  Done adding!\r\n\r\n  0x08048466 in main (argc=1, argv=0xffffd154) at hello.c:15\r\n\r\n  15\t    add1000(1, 2);\r\n\r\n  Value returned is $1 = 499500\r\n\r\n  \r\n\r\n  Command: bt\r\n\r\nThis command prints the backtrace of all stack frames. A [margin:\r\nbacktrace\r\n]backtracebacktrace is a list of currently active functions:\r\n\r\nSuppose we have a chain of function calls:\r\n\r\n  void d(int d) { };\r\n\r\nvoid c(int c) { d(0); }\r\n\r\nvoid b(int b) { c(1); }\r\n\r\nvoid a(int a) { b(2); }\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    a(3);\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  bt can visualize such a chain in action:\r\n\r\n  \r\n\r\n  (gdb) b a\r\n\r\n  \r\n\r\n  \r\n\r\n  Breakpoint 1 at 0x8048404: file hello.c, line 9.\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) r\r\n\r\n  \r\n\r\n  \r\n\r\n  Starting program: /tmp/hello \r\n\r\n  Breakpoint 1, a (a=3) at hello.c:9\r\n\r\n  9\tvoid a(int a) { b(2); }\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) s\r\n\r\n  \r\n\r\n  \r\n\r\n  b (b=2) at hello.c:7\r\n\r\n  7\tvoid b(int b) { c(1); }\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) s\r\n\r\n  \r\n\r\n  \r\n\r\n  c (c=1) at hello.c:5\r\n\r\n  5\tvoid c(int c) { d(0); }\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) s\r\n\r\n  \r\n\r\n  \r\n\r\n  d (d=0) at hello.c:3\r\n\r\n  3\tvoid d(int d) { };\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) bt\r\n\r\n  \r\n\r\n  \r\n\r\n  #0  d (d=0) at hello.c:3\r\n\r\n  #1  0x080483eb in c (c=1) at hello.c:5\r\n\r\n  #2  0x080483fb in b (b=2) at hello.c:7\r\n\r\n  #3  0x0804840b in a (a=3) at hello.c:9\r\n\r\n  #4  0x0804841b in main (argc=1, argv=0xffffd154) at hello.c:13\r\n\r\n  \r\n\r\n  Most-recent calls are placed on top and least-recent calls are \r\n  near the bottom. In this case, d is the most current active \r\n  function, so it has the index 0. Next is c, the 2[superscript:nd] active function, has the index 1 and so on with function b, \r\n  function a, and finally function main at the bottom, the \r\n  least-recent function. That is how we read a backtrace.\r\n\r\n  Command: up\r\n\r\nThis command goes up one frame earlier the current frame.\r\n\r\nInstead of staying in d function, we can go up to c function and \r\nlook at its state:\r\n\r\n  \r\n\r\n  (gdb) bt\r\n\r\n  \r\n\r\n  \r\n\r\n  #0  d (d=0) at hello.c:3\r\n\r\n  #1  0x080483eb in c (c=1) at hello.c:5\r\n\r\n  #2  0x080483fb in b (b=2) at hello.c:7\r\n\r\n  #3  0x0804840b in a (a=3) at hello.c:9\r\n\r\n  #4  0x0804841b in main (argc=1, argv=0xffffd154) at hello.c:13\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) up\r\n\r\n  \r\n\r\n  \r\n\r\n  #1  0x080483eb in c (c=1) at hello.c:3\r\n\r\n  3\tvoid b(int b) { c(1); }\r\n\r\n  \r\n\r\n  The output displays the current frame is moved to c and where \r\n  the call to c is made, which is in function b at line 3.\r\n\r\n  Command: down\r\n\r\nSimilar to up, this command goes down one frame later then the \r\ncurrent frame.\r\n\r\nAfter inspecting c function, we can go back to d:\r\n\r\n  \r\n\r\n  (gdb) bt\r\n\r\n  \r\n\r\n  \r\n\r\n  #0  d (d=0) at hello.c:3\r\n\r\n  #1  0x080483eb in c (c=1) at hello.c:5\r\n\r\n  #2  0x080483fb in b (b=2) at hello.c:7\r\n\r\n  #3  0x0804840b in a (a=3) at hello.c:9\r\n\r\n  #4  0x0804841b in main (argc=1, argv=0xffffd154) at hello.c:13\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) up\r\n\r\n  \r\n\r\n  \r\n\r\n  #1  0x080483eb in c (c=1) at hello.c:3\r\n\r\n  3\tvoid b(int b) { c(1); }\r\n\r\n  \r\n\r\n  \r\n\r\n  (gdb) down\r\n\r\n  \r\n\r\n  \r\n\r\n  #0  d (d=0) at hello.c:1\r\n\r\n  1\tvoid d(int d) { };\r\n\r\n  \r\n\r\n  Command: info registers\r\n\r\nThis command lists the current values in commonly used registers. \r\nThis command is useful when debugging assembly and operating \r\nsystem code, as we can inspect the current state of the machine.\r\n\r\nExecuting the command, we can see the commonly used registers:\r\n\r\n  \r\n\r\n  (gdb) info registers \r\n\r\n  \r\n\r\n  \r\n\r\n  eax            0xf7faddbc\t-134554180\r\n\r\n  ecx            0xffffd0c0\t-12096\r\n\r\n  edx            0xffffd0e4\t-12060\r\n\r\n  ebx            0x0\t0\r\n\r\n  esp            0xffffd0a0\t0xffffd0a0\r\n\r\n  ebp            0xffffd0a8\t0xffffd0a8\r\n\r\n  esi            0xf7fac000\t-134561792\r\n\r\n  edi            0xf7fac000\t-134561792\r\n\r\n  eip            0x804841c\t0x804841c <main+17>\r\n\r\n  eflags         0x286\t[ PF SF IF ]\r\n\r\n  cs             0x23\t35\r\n\r\n  ss             0x2b\t43\r\n\r\n  ds             0x2b\t43\r\n\r\n  es             0x2b\t43\r\n\r\n  fs             0x0\t0\r\n\r\n  gs             0x63\t99\r\n\r\n  \r\n\r\n  The above registers suffice for writing our operating system in \r\n  later part.\r\n\r\n  How debuggers work: A brief introduction\r\n\r\n  How breakpoints work\r\n\r\nWhen a programmer places a breakpoint somewhere in his code, what \r\nactually happens is that the first opcode of the first \r\ninstruction of a statement is replaced with another instruction, \r\nint 3 with opcode CCh:\r\n\r\n[float Figure:\r\n[Figure 0.17:\r\nOpcode replacement, with int 3\r\n]\r\n\r\n     \r\n+-----+-----+---------+                +-----+-----+----+\r\n| 83  | ec  |   0c    |  \\rightarrow\r\n  | cc  | ec  | 0c |\r\n+-----+-----+---------+                +-----+-----+----+\r\n+---------------------+                +----------------+\r\n|     sub esp,0x4     |                |     int 3      |\r\n+---------------------+                +----------------+\r\n     \r\n]\r\n\r\nint 3 only costs a single byte, making it efficient for \r\ndebugging. When int 3 instruction is executed, the operating \r\nsystem calls its breakpoint interrupt handler. The handler then \r\nchecks what process reaches a breakpoint, pauses it and notifies \r\nthe debugger it has paused a debugged process. The debugged \r\nprocess is only paused and that means a debugger is free to \r\ninspect its internal state, like a surgeon operates on an \r\nanesthetic patient. Then, the debugger replaces the int 3 opcode \r\nwith the original opcode and executes the original instruction \r\nnormally.\r\n\r\n[float Figure:\r\n[Figure 0.18:\r\nRestore the original opcode, after int 3 was executed\r\n]\r\n\r\n     \r\n+-----+-----+----+                +-----+-----+---------+\r\n| cc  | ec  | 0c |  \\rightarrow\r\n  | 83  | ec  |   0c    |\r\n+-----+-----+----+                +-----+-----+---------+\r\n+----------------+                +---------------------+\r\n|     int 3      |                |     sub esp,0x4     |\r\n+----------------+                +---------------------+\r\n     \r\n]\r\n\r\nIt is simple to see int 3 in action. First, we add an int 3 \r\ninstruction where we need gdb to stop:\r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    asm(\"int 3\");\r\n\r\n    printf(\"Hello World\\n\");\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  int 3 precedes printf, so gdb is expected to stop at printf. \r\n  Next, we compile with debug enable and with Intel syntax:\r\n\r\n  \r\n\r\n  $ gcc -masm=intel -m32 -g hello.c -o hello\r\n\r\n  \r\n\r\n  Finally, start gdb:\r\n\r\n  \r\n\r\n  $ gdb hello\r\n\r\n  \r\n\r\n  Running without setting any breakpoint, gdb stops at printf \r\n  call, as expected:\r\n\r\n  \r\n\r\n  (gdb) r\r\n\r\n  \r\n\r\n  \r\n\r\n  Starting program: /tmp/hello \r\n\r\n  Program received signal SIGTRAP, Trace/breakpoint trap.\r\n\r\n  main (argc=1, argv=0xffffd154) at hello.c:6\r\n\r\n  6\t    printf(\"Hello World\\n\");\r\n\r\n  \r\n\r\n  The blue text indicates that gdb encountered a breakpoint, and \r\n  indeed it stopped at the right place: the printf call, where \r\n  int 3 preceded it.\r\n\r\n  Single stepping\r\n\r\nWhen breakpoint is implemented, it is easy to implement single \r\nstepping: a debugger simply places another int 3 opcode in the \r\nnext instruction. So, when a programmer sets a breakpoint at an \r\ninstruction, the next instruction is automatically set by the \r\ndebugger, thus enable instruction by instruction debugging. \r\nSimilarly, source line by line debugging is just the placements \r\nof the very first opcodes in the two statements with two int 3 \r\nopcodes.\r\n\r\n  How a debugger understands high level source code\r\n\r\nDWARF is a debugging file format used by many compilers and \r\ndebuggers to support source level debugging. DWARF contains \r\ninformation that maps between entities in the executable binary \r\nwith the source files. A program entity can either be data or \r\ncode. A DIE, or [margin:\r\nDebugging Information Entry\r\n]Debugging Information EntryDebugging Information Entry, is a \r\ndescription of a program entity. A DIE consists of a \r\ntag, which specifies the entity \r\nthat the DIE describes, and a list of  attributes that describes \r\nthe entity. Of all the attributes, these two attributes enables \r\nsource-level debugging: \r\n\r\n• Where the entity appears in the source files: which file and \r\n  which line the entity appears.\r\n\r\n• Where the entity appears in the executable binary: in which \r\n  memory address the entity is loaded at runtime. With the \r\n  precise address, gdb can retrieve correct value for a data \r\n  entity, or place a correct breakpoint and stop accordingly for \r\n  a code entity. Without the information of these addresses, gdb \r\n  would not know where the entities are to inspect them.\r\n\r\n\r\n\r\n\r\n+---------------------------------------------------------------------------------------------------------------------------------------------------+                          +------------------------------------------------------------------+\r\n| hello.c                                                                                                                                           |                          | DIE                                                              |\r\n+---------------------------------------------------------------------------------------------------------------------------------------------------+                          +------------------------------------------------------------------+\r\n+--------------------------------------------------------------+------------------------------------------------------------------------------------+                          +------------------------------------------------------------------+\r\n|   Line 1\r\n\r\n  Line 2\r\n\r\n\\Rightarrow\r\n Line 3\r\n\r\n  Line 5\r\n\r\n  Line 6  | #include <stdio.h>\r\n\r\n \r\n\r\nint main(int argc, char *argv[])\r\n\r\n..........\r\n\r\n..........\r\n\r\n  |   \r\n\r\n \r\n\r\n  \\rightarrow\r\n    | ....\r\n\r\n....\r\n\r\nmain in hello.c is at 0x804840b in hello\r\n\r\n....\r\n\r\n.... |\r\n+--------------------------------------------------------------+------------------------------------------------------------------------------------+                          +------------------------------------------------------------------+\r\n                                                                                                                                                                                                                                                   \r\n                                                                                                                                                                                                     \\downarrow\r\n\\uparrow\r\n                          \r\n                                                                                                                                                                                                                                                   \r\n                                                                                                                                                                               +------------------------------------------------------------------+\r\n                                                                                                                                                                               | hello (at 0x804840b)                                             |\r\n                                                                                                                                                                               +------------------------------------------------------------------+\r\n                                                                                                                                                                               +------------------------------------------------------------------+\r\n                                                                                                                                                                               | ...8d 4c 24 04 83 e4 f0 ff 71 fc ....                            |\r\n                                                                                                                                                                               +------------------------------------------------------------------+\r\n\r\n\r\n\r\n\r\n\r\n\r\nIn addition to DIEs, another binary-to-source mapping is the line \r\nnumber table. The line number table maps between a line in the \r\nsource code and at which memory address is the start of the line \r\nin the executable binary.\r\n\r\nIn sum, to successfully enable source-level debugging, a debugger \r\nneeds to know the precise location of the source files and the \r\nload addresses at runtime. Address matching, between the image \r\nlayout of the ELF binary and the address where it is loaded, is \r\nextremely important since debug information relies on correct \r\nloading address at runtime. That is, it assumes the addresses as \r\nrecorded in the binary image at compile-time the same as at \r\nruntime e.g. if the load address for .text section is recorded in \r\nthe executable binary at 0x800000, then when the binary actually \r\nruns, .text should really be loaded at 0x800000 for gdb to be \r\nable to correctly match running instructions with high-level code \r\nstatement. Address mismatching makes debug information useless, \r\nas actual code at one address is displayed as code at another \r\naddress. Without this knowledge, we will not be able to build an \r\noperating system that can be debugged with gdb.\r\n\r\nWhen an executable binary contains debug info, readelf can \r\ndisplay such information in a readable format. Using the good old \r\nhello world program:\r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    printf(\"Hello World\\n\");\r\n\r\n\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  and compile with debug info:\r\n\r\n  \r\n\r\n  $ gcc -m32 -g hello.c -o hello\r\n\r\n  \r\n\r\n  With the binary ready, we can look at the line number table \r\n  with the command:\r\n\r\n  \r\n\r\n  $ readlelf -wL hello\r\n\r\n  \r\n\r\n  -w option prints all the debug information. In combination with \r\n  its sub-option, only specific information is displayed. For \r\n  example, with -L, only the line number table is displayed:\r\n\r\n  \r\n\r\n  Decoded dump of debug contents of section .debug_line:\r\n\r\n  CU: hello.c:\r\n\r\n  File name                            Line number    Starting \r\n  address\r\n\r\n  hello.c                                        6           \r\n  0x804840b\r\n\r\n  hello.c                                        7           \r\n  0x804841c\r\n\r\n  hello.c                                        9           \r\n  0x804842c\r\n\r\n  hello.c                                       10           \r\n  0x8048431\r\n\r\n  \r\n\r\n  From the above output:\r\n\r\n  CU shorts for Compilation Unit, a \r\n    separately compiled source file. In the example, we only have \r\n    one file, hello.c.\r\n\r\n  File name displays the filename of the current compilation \r\n    unit.\r\n\r\n  Line number is the line number in the source file of which the \r\n    line is not an empty line. In the example, line 8 is an empty \r\n    line, so it does not appear.\r\n\r\n  Starting address is the memory address where the line actually \r\n    starts in the executable binary. \r\n\r\n  With such crystal clear information, this is how gdb is able to \r\n  set a breakpoint on a line easily. For placing breakpoints on \r\n  variables and functions, it is time to look at the DIEs. To get \r\n  the DIEs information from an executable binary, run the \r\n  command:\r\n\r\n  \r\n\r\n  $ readlelf -wi hello\r\n\r\n  \r\n\r\n  -wi option lists all the DIE entries. This is one typical DIE \r\n  entry:\r\n\r\n   <0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)\r\n\r\n      <c>   DW_AT_producer    : (indirect string, offset: 0xe): \r\n  GNU C11 5.4.0 20160609 -masm=intel -m32 -mtune=generic \r\n  -march=i686 -g -fstack-protector-strong\r\n\r\n      <10>   DW_AT_language    : 12\t(ANSI C99)\r\n\r\n      <11>   DW_AT_name        : (indirect string, offset: 0xbe): \r\n  hello.c\r\n\r\n      <15>   DW_AT_comp_dir    : (indirect string, offset: 0x97): \r\n  /tmp\r\n\r\n      <19>   DW_AT_low_pc      : 0x804840b\r\n\r\n      <1d>   DW_AT_high_pc     : 0x2e\r\n\r\n      <21>   DW_AT_stmt_list   : 0x0\r\n\r\n  Red This left-most number indicates the current nesting level \r\n    of a DIE entry. 0 is the outer-most level DIE with its entity \r\n    is the compilation unit. This means subsequent DIE entries \r\n    with higher nesting level are all the children of this tag, \r\n    the compilation unit. It makes sense, as all the entities \r\n    must originate from a source file.\r\n\r\n  Blue These numbers in hex format indicate the offsets into \r\n    .debug_info section. Each meaningful information is displayed \r\n    along with its offset. When an attribute references to \r\n    another attribute, the offset is used to precisely identify \r\n    the referenced attribute.\r\n\r\n  Green These names with DW_AT_ prefix are the attributes \r\n    attached to a DIE that describe an entity. Notable \r\n    attributes:\r\n\r\n    DW_AT_name\r\n\r\n    DW_AT_comp_dir The filename of the compilation unit and the \r\n      directory where compilation occurred. Without the filename \r\n      and the path, gdb would not be able to display the \r\n      high-level source, despite the availability of the debug \r\n      info. Debug info only contains the mapping between source \r\n      and binary, not the source code itself.\r\n\r\n    DW_AT_low_pc\r\n\r\n    DW_AT_high_pc The start and end of the current entity, which \r\n      is the compilation unit, in the executable binary. The \r\n      value in DW_AT_low_pc is the starting address. \r\n      DW_AT_high_pc is the size of the compilation unit, when \r\n      adding up to DW_AT_low_pc results in the end address of the \r\n      entity. In this example, code compiled from hello.c starts \r\n      at 0x804840b and end at \\mathtt{0x804840b+0x2e=0x8048439}\r\n. \r\n      To really make sure, we verify with objdump:\r\n\r\n      \r\n\r\n      int main(int argc, char *argv[])\r\n\r\n      {\r\n\r\n       804840b:       8d 4c 24 04             lea    \r\n      ecx,[esp+0x4]\r\n\r\n       804840f:       83 e4 f0                and    \r\n      esp,0xfffffff0\r\n\r\n       8048412:       ff 71 fc                push   DWORD PTR \r\n      [ecx-0x4]\r\n\r\n       8048415:       55                      push   ebp\r\n\r\n       8048416:       89 e5                   mov    ebp,esp\r\n\r\n       8048418:       51                      push   ecx\r\n\r\n       8048419:       83 ec 04                sub    esp,0x4\r\n\r\n          printf(\"Hello World\\n\");\r\n\r\n       804841c:       83 ec 0c                sub    esp,0xc\r\n\r\n       804841f:       68 c0 84 04 08          push   0x80484c0\r\n\r\n       8048424:       e8 b7 fe ff ff          call   80482e0 \r\n      <puts@plt>\r\n\r\n       8048429:       83 c4 10                add    esp,0x10\r\n\r\n          return 0;\r\n\r\n       804842c:       b8 00 00 00 00          mov    eax,0x0\r\n\r\n      }\r\n\r\n       8048431:       8b 4d fc                mov    ecx,DWORD \r\n      PTR [ebp-0x4]\r\n\r\n       8048434:       c9                      leave  \r\n\r\n       8048435:       8d 61 fc                lea    \r\n      esp,[ecx-0x4]\r\n\r\n       8048438:       c3                      ret    \r\n\r\n       8048439:       66 90                   xchg   ax,ax\r\n\r\n       804843b:       66 90                   xchg   ax,ax\r\n\r\n       804843d:       66 90                   xchg   ax,ax\r\n\r\n       804843f:       90                      nop\r\n\r\n      \r\n\r\n      It is true: main starts at 804840b and end at 8048439, \r\n      right after the ret instruction at 8048438. The \r\n      instructions after 8048439 are just padding bytes inserted \r\n      by gcc for alignment, which do not belong to main. Note \r\n      that the output from objdump shows much more code past \r\n      main. It is not counted, as the code is outside of hello.c, \r\n      added by gcc for the operating system. hello.c contains \r\n      only one function: main and this is why hello.c also starts \r\n      and ends the same as main.\r\n\r\n  Pink This number displays the abbreviation form of a tag. An \r\n    abbreviation is the form of a DIE. When debug info is \r\n    displayed with -wi, the DIEs are displayed with their values. \r\n    -wa option shows abbreviations in the .debug_abbrev section:\r\n\r\n    \r\n\r\n    Contents of the .debug_abbrev section:\r\n\r\n      Number TAG (0x0)\r\n\r\n       1      DW_TAG_compile_unit    [has children]\r\n\r\n        DW_AT_producer     DW_FORM_strp\r\n\r\n        DW_AT_language     DW_FORM_data1\r\n\r\n        DW_AT_name         DW_FORM_strp\r\n\r\n        DW_AT_comp_dir     DW_FORM_strp\r\n\r\n        DW_AT_low_pc       DW_FORM_addr\r\n\r\n        DW_AT_high_pc      DW_FORM_data4\r\n\r\n        DW_AT_stmt_list    DW_FORM_sec_offset\r\n\r\n        DW_AT value: 0     DW_FORM value: 0\r\n\r\n    .... more abbreviations ....\r\n\r\n    \r\n\r\n    The output is similar to a DIE output, with only attribute \r\n    names and without any value. We can also say an abbreviation \r\n    is a type of a DIE, as an abbreviation represents the \r\n    structure of a particular DIE. Many DIEs share the same \r\n    abbreviation, or structure, thus they are of the same type. \r\n    An abbreviation number specifies which type a DIE is in the \r\n    abbreviation table above. Abbreviations improve encoding \r\n    efficiency (reduce binary size) because each DIE needs not to \r\n    carry their structure information as pairs of attribute-value[footnote:\r\nFor example, data format such as YAML or JSON encodes its \r\nattribute names along with its values. This simplifies encoding, \r\nbut with overhead.\r\n], but simply refers to an abbreviation for correct decoding.\r\n\r\n  Here are all the DIEs of hello represented as a tree:\r\n\r\n  \r\n\r\n  <Graphics file: C:/Users/Tu Do/os01/book_src/images/06/dwarf_tree.svg>\r\n  <dwarf_tree>\r\n\r\n  \r\n\r\nIn the figure [dwarf_tree], DW_TAG_subprogram represents a \r\nfunction such as main. Its children are the DIEs of argc and \r\nargv. With such precise information, matching source to binary is \r\nan easy job for gdb.\r\n\r\nIf more than one compilation units exist in an executable binary, \r\nthe DIE entries are sorted according to the compilation order \r\nfrom gcc. For example, suppose we have another test.c source file[footnote:\r\nIt can contain anything. Just a sample file.\r\n] and compile it together with hello:\r\n\r\n\r\n\r\n$ gcc -masm=intel -m32 -g test.c hello.c -o hello\r\n\r\n\r\n\r\nThen, the all DIE entries in test.c are displayed before the DIE \r\nentries in hello.c:\r\n\r\n<0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)\r\n\r\n      <c>   DW_AT_producer    : (indirect string, offset: 0x0): \r\n  GNU C11 5.4.0 20160609 \r\n\r\n  -masm=intel -m32 -mtune=generic -march=i686 -g \r\n  -fstack-protector-strong\r\n\r\n      <10>   DW_AT_language    : 12       (ANSI C99)\r\n\r\n      <11>   DW_AT_name        : (indirect string, offset: 0x64): \r\n  test.c\r\n\r\n      <15>   DW_AT_comp_dir    : (indirect string, offset: 0x5f): \r\n  /tmp\r\n\r\n      <19>   DW_AT_low_pc      : 0x804840b\r\n\r\n      <1d>   DW_AT_high_pc     : 0x6\r\n\r\n      <21>   DW_AT_stmt_list   : 0x0\r\n\r\n   <1><25>: Abbrev Number: 2 (DW_TAG_subprogram)\r\n\r\n      <26>   DW_AT_external    : 1\r\n\r\n      <26>   DW_AT_name        : bar\r\n\r\n      <2a>   DW_AT_decl_file   : 1\r\n\r\n      <2b>   DW_AT_decl_line   : 1\r\n\r\n      <2c>   DW_AT_low_pc      : 0x804840b\r\n\r\n      <30>   DW_AT_high_pc     : 0x6\r\n\r\n      <34>   DW_AT_frame_base  : 1 byte block: 9c         \r\n  (DW_OP_call_frame_cfa)\r\n\r\n      <36>   DW_AT_GNU_all_call_sites: 1\r\n\r\n  \r\n\r\n  ....after all DIEs in test.c listed....\r\n\r\n  \r\n\r\n  <0><42>: Abbrev Number: 1 (DW_TAG_compile_unit)\r\n\r\n      <43>   DW_AT_producer    : (indirect string, offset: 0x0): \r\n  GNU C11 5.4.0 20160609 \r\n\r\n  -masm=intel -m32 -mtune=generic -march=i686 -g \r\n  -fstack-protector-strong\r\n\r\n      <47>   DW_AT_language    : 12       (ANSI C99)\r\n\r\n      <48>   DW_AT_name        : (indirect string, offset: 0xc5): \r\n  hello.c\r\n\r\n      <4c>   DW_AT_comp_dir    : (indirect string, offset: 0x5f): \r\n  /tmp\r\n\r\n      <50>   DW_AT_low_pc      : 0x8048411\r\n\r\n      <54>   DW_AT_high_pc     : 0x2e\r\n\r\n      <58>   DW_AT_stmt_list   : 0x35\r\n\r\n  ....then all DIEs in hello.c are listed....\r\n\r\n  \r\n\r\nGroundwork \r\n\r\nBootloader<chap:Bootloader>\r\n\r\nA bootloader loads an OS, or an application [footnote:\r\nMany embedded devices don't use an OS. In embedded systems, the \r\nbootloader is simply included in boot firmware and no bootloader \r\nis needed. \r\n] that runs and communicate directly with hardware. To run an OS, \r\nthe first thing to write is a bootloader. In this chapter, we are \r\ngoing to write a rudimentary bootloader, as our main focus is \r\nwriting an operating system, not a bootloader. More \r\ninterestingly, this chapter will present related tools and \r\ntechniques that are applicable for writing a bootloader as well \r\nas an operating system.\r\n\r\n  x86 Boot Process\r\n\r\nAfter the POST process finished, the CPU's program counter is set \r\nto the address FFFF:0000h for executing BIOS code. BIOS - Basic \r\nInput/Output System is a firmware that performs hardware \r\ninitialization and provides a set of generic subroutines to \r\ncontrol input/output devices. The BIOS checks all available \r\nstorage devices (floppy disks and hard disks) if any device is \r\nbootable, by examining the last two bytes of the first sector \r\nwhether it has the boot record signature of 0x55, 0xAA. If so, \r\nthe BIOS loads the first sector to the address 7C00h, set the \r\nprogram counter to that address and let the CPU executing code \r\nfrom there.\r\n\r\nThe first sector is called Master Boot Record, or MBR. The \r\nprogram in the first sector is called MBR Bootloader.\r\n\r\n  Using BIOS services\r\n\r\nBIOS provides many basic services for controlling the hardware at \r\nthe boot stage. A service is a group of routines that controls a \r\nparticular hardware device, or returns information of current \r\nsystem. Each service is given an interrupt number. To call a BIOS \r\nroutine, an int instruction must be used with an interrupt \r\nnumber. Each BIOS service defines its own numbers for its \r\nroutines; to call a routine, a specific number must be written to \r\na register required by each service. The list of all BIOS \r\ninterrupts is available with Ralf Brown's Interrupt List at: http://www.cs.cmu.edu/~ralf/files.html\r\n.\r\n\r\n[float Figure:\r\n\r\n\r\n[Figure 0.19:\r\nThe boot process.\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/07/simple_boot_process.pdf>\r\n     \r\n]\r\n\r\n  Example: Interrupt call 13h (diskette service) requires number \r\n  of sectors to read, track number, sector number, head number \r\n  and drive number to read from a storage device. The content of \r\n  the sector is stored in memory at the address defined by the \r\n  pair of registers ES:BX. The parameters are stored in registers \r\n  like this:\r\n\r\n  ; Store sector content in the buffer 10FF:0000\r\n\r\nmov     dx, 10FFh\r\n\r\nmov     es, dx\r\n\r\nxor     bx, bx\r\n\r\nmov     al, 2    ; read 2 sector\r\n\r\nmov     ch, 0    ; read track 0\r\n\r\nmov     cl, 2    ; 2nd sector is read\r\n\r\nmov     dh, 0    ; head number\r\n\r\nmov     dl, 0    ; drive number. Drive 0 is floppy drive.\r\n\r\nmov     ah, 0x02 ; read floppy sector function\r\n\r\nint     0x13     ; call BIOS - Read the sector\r\n\r\nThe BIOS is only available in real mode. However, when switching \r\nto protected mode, then BIOS will not be usable anymore and the \r\noperating system code is responsible for controlling hardware \r\ndevices. This is when the operating system stands on its own: it \r\nmust provide its own kernel drivers for talking to hardware.\r\n\r\n  Boot process\r\n\r\n1. BIOS transfers control to MBR bootloader by jumping to \r\n  0000:7c00h, where bootloader is assumed to exist already.\r\n\r\n2. Setup machine environment for booting by properly initialize \r\n  segment registers to enable flat memory model.\r\n\r\n3. Load the kernel:\r\n\r\n  (a) Read kernel from disk.\r\n\r\n  (b) Save it somewhere in the main memory.\r\n\r\n  (c) Jump to the starting code address of the kernel and \r\n    execute.\r\n\r\n4. If error occurs, print a message to notify users something \r\n  went wrong and halt.\r\n\r\n  Example Bootloader\r\n\r\nHere is a simple bootloader that does nothing, except not \r\ncrashing the machine but halt it gracefully. If the virtual \r\nmachine does not halt but text repeatedly flashing, it means the \r\nbootloader does not load properly and the machine crashed. The \r\nmachine crashed because it keeps executing until the near end of \r\nphysical memory (1 MB in real mode), which is FFFF:0000h, which \r\nstarts the whole BIOS boot process all over again. This is \r\neffectively a reset, but not fully, since machine environment \r\nfrom previous run is still reserved. For that reason, it is \r\ncalled a warm reboot. The opposite of warm reboot is cold reboot, \r\nin which the machine environment is reset to initial settings \r\nwhen the computer starts from a powerless state.\r\n\r\n;******************************************\r\n\r\n; bootloader.asm\t\t\r\n\r\n; A Simple Bootloader\r\n\r\n;******************************************\r\n\r\norg 0x7c00\r\n\r\nbits 16\r\n\r\nstart: jmp boot\r\n\r\n\r\n\r\n;; constant and variable definitions\r\n\r\nmsg\tdb\t\"Welcome to My Operating System!\", 0ah, 0dh, 0h\r\n\r\n\r\n\r\nboot:\r\n\r\n  cli\t; no interrupts \t\r\n\r\n  cld\t; all that we need to init\r\n\r\n  hlt\t; halt the system\r\n\r\n\r\n\r\n; We have to be 512 bytes. Clear the rest of the bytes with 0\r\n\r\ntimes 510 - ($-$$) db 0\r\n\r\ndw 0xAA55\t\t\t\t  ; Boot Signiture\r\n\r\n  Compile and load\r\n\r\nWe compile the code with nasm and write it to a disk image:\r\n\r\n\r\n\r\n$ nasm -f bin bootloader.asm -o bootloader\r\n\r\n\r\n\r\nThen, we create a 1.4 MB floppy disk and:\r\n\r\n\r\n\r\n$ dd if=/dev/zero of=disk.img bs=512 count=2880\r\n\r\n\r\n\r\n\r\n\r\n2880+0 records in\r\n\r\n2880+0 records out\r\n\r\n1474560 bytes (1.5 MB, 1.4 MiB) copied, 0.00625622 s, 236 MB/s\r\n\r\n\r\n\r\nThen, we write the bootloader to the 1[superscript:st]sector:\r\n\r\n\r\n\r\n$ dd conv=notrunc if=bootloader of=disk.img bs=512 count=1 seek=0\r\n\r\n\r\n\r\n\r\n\r\n1+0 records in\r\n\r\n1+0 records out\r\n\r\n512 bytes copied, 0.000102708 s, 5.0 MB/s\r\n\r\n\r\n\r\nThe option conv=notrunc preserves the original size of the floppy \r\ndisk. Without this option, the 1.4 MB disk image will be \r\ncompletely replaced by the new disk.img with only 512 bytes, and \r\nwe do not want that happens. \r\n\r\nIn the past, developing an operating system is complicated \r\nbecause a programmer needs to understand specific hardware he is \r\nusing. Even though x86 was ubiquitous, the minute differences \r\nbetween models made some code written for a machine not run on \r\nanother. Further, if you use the same physical computer you write \r\nyour operating system take very long between runs, and also \r\ndifficult to debug. Fortunately, today we can uniformly produce a \r\nvirtual machine with a particular specification and avoid the \r\nincompatibility issue altogether, thus making an OS easier to \r\nwrite and test since everyone can reproduce the same machine \r\nenvironment. \r\n\r\nWe will be using QEMU, a generic and open source machine emulator \r\nand virtualizer. QEMU can emulate various types of machine, not \r\nlimited to x86_64 only. Debug is easy since you can connect GDB \r\nto a virtual machine to debug code that runs on it, through \r\nQEMU's built-in GDB server. QEMU can use disk.img as a boot \r\ndevice e.g. a floppy disk:\r\n\r\n\r\n\r\n$ qemu-system-i386 -machine q35 -fda disk.img -gdb tcp::26000 -S\r\n\r\n\r\n\r\n• With option -machine q35, QEMU emulates a q35 machine model \r\n  from Intel.[footnote:\r\nThe following command lists all supported emulated machines from \r\nQEMU:\r\n\r\nqemu-system-i386 -machine help\r\n].\r\n\r\n• With option -fda disk.img, QEMU uses disk.img as a floppy disk \r\n  image.\r\n\r\n• With option -gdb tcp::26000, QEMU allows gdb to connect to the \r\n  virtual machine for remote debugging through a tcp socket with \r\n  port 26000.\r\n\r\n• With option -S, QEMU waits for gdb to connect before it starts \r\n  running.\r\n\r\nAfter the command is executed, a new console window that displays \r\nthe screen output of the virtual machine. Open another terminal, \r\nrun gdb and set the current architecture to i8086, since we are \r\nrunning in 16-bit mode:\r\n\r\n\r\n\r\n(gdb) set architecture i8086\r\n\r\n\r\n\r\n\r\n\r\nwarning: A handler for the OS ABI \"GNU/Linux\" is not built into \r\nthis configuration\r\n\r\nof GDB. Attempting to continue with the default i8086 settings.\r\n\r\nThe target architecture is assumed to be i8086\r\n\r\n\r\n\r\nThen, connect gdb to the waiting virtual machine with this \r\ncommand:\r\n\r\n\r\n\r\n(gdb) target remote localhost:26000\r\n\r\n\r\n\r\n\r\n\r\nRemote debugging using localhost:26000\r\n\r\n0x0000fff0 in ?? ()\r\n\r\n\r\n\r\nThen, place a breakpoint at 0x7c00:\r\n\r\n\r\n\r\n(gdb) b *0x7c00\r\n\r\n\r\n\r\n\r\n\r\nBreakpoint 1 at 0x7c00\r\n\r\n\r\n\r\nNote the asterisk before the memory address. Without the asterisk, gdb \r\ntreats the address as a symbol in a program rather than an \r\naddress. Then, for convenience, we use a split layout for viewing \r\nthe assembly code and registers together:\r\n\r\n\r\n\r\n(gdb) layout asm\r\n\r\n(gdb) layout reg\r\n\r\n\r\n\r\nFinally, run the program:\r\n\r\n\r\n\r\n(gdb) c\r\n\r\n\r\n\r\nIf the virtual machine successfully runs the bootloader, this is \r\nwhat the QEMU screen should look like:\r\n\r\n[float Figure:\r\n[Figure 0.20:\r\nBoot succeeded.\r\n]\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/07/boot_succeeded.png>\r\n]\r\n\r\n  Debugging\r\n\r\nIf, for some reason, the sample bootloader cannot get to such \r\nscreen and gdb does not stop at 0x7c00, then the following \r\nscenarios are likely:\r\n\r\n• The bootloader is invalid: the message “Boot failed: not a \r\n  bootable disk” appears for floppy disk booting. Make sure the \r\n  boot signature is at the last 2 bytes of the 512-byte first \r\n  sector.\r\n\r\n• The machine cannot find a boot disk: the message “Boot failed: \r\n  not a bootable disk” appears for floppy disk booting. Make sure \r\n  the bootloader is correctly written to the first sector. It can \r\n  be verify by check the disk with hd:\r\n\r\n  \r\n\r\n  $ hd disk.img | less\r\n\r\n  \r\n\r\n  If the first 512 bytes are all zeroes, then it is likely that \r\n  the bootloader is incorrectly written to another sector.\r\n\r\n• The machine crashes: When such scenario happens, it reset back \r\n  to the beginning at FFFF:0000h. If the QEMU machine starts \r\n  without waiting for gdb, then the console output window keeps \r\n  flashing as the machine is repeatedly reset. It is likely some \r\n  instruction in the bootloader code causing the fault.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nPrint a welcome message\r\n\r\n  We loaded the bootloader successfully. But, it needs to do \r\n  something useful other than halting our machine. The easiest \r\n  thing to do is printing something on screen, like how an \r\n  introduction to all programming language starts with “Hello \r\n  World”. Our bootloader prints “Welcome to my operating system”[footnote:\r\nOr whatever message you want.\r\n]. In this part, we will build a simple I/O library that allows \r\n  us to set a cursor anywhere on the screen and print text there.\r\n\r\n  First, create a file io.asm for I/O related routines. Then, \r\n  write the following routines:\r\n\r\n  1. MovCursor \r\n\r\n    Purpose: Move a cursor to a specific location on screen and \r\n    remember this location.\r\n\r\n    Parameters: \r\n\r\n    • bh = Y coordinate\r\n\r\n    • bl = X coordinate. \r\n\r\n    Return: None\r\n\r\n  2. PutChar \r\n\r\n    Purpose: Print a character on screen, at the cursor position \r\n    previously set by MovCursor .\r\n\r\n    Parameters:\r\n\r\n    • al = Character to print\r\n\r\n    • bl = text color\r\n\r\n    • cx = number of times the character is repeated\r\n\r\n    Return: None\r\n\r\n  3. Print \r\n\r\n    Purpose: Print a string.\r\n\r\n    Parameters:\r\n\r\n    • ds:si = Zero terminated string\r\n\r\n    Return: None\r\n\r\n  Test the routines by putting each in the bootloader source, \r\n  compile and run. To debug, run GDB and set a breakpoint at a \r\n  specific routine. The end result is that Print should display a \r\n  welcome message on screen.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n  Loading a program from bootloader\r\n\r\nNow that we get the feel of how to use the BIOS services, it is \r\ntime for something more complicated. We will place our kernel on \r\n2[superscript:nd] sector onward, and our bootloader reads 30 sectors starting from \r\n2[superscript:nd] sector. Why 30 sectors? Our kernel will grow gradually, so we \r\nwill preserve 30 sectors and save us time for modifying the \r\nbootloader each time the kernel size expands another sector.\r\n\r\nThe primary responsibility of a bootloader is to read an \r\noperating system from some storage device e.g. hard disk, then \r\nloads it into main memory and transfer the control to the loaded \r\noperating system, similar to how the BIOS reads and loads a \r\nbootloader. At the moment, our bootloader does nothing more than \r\njust an assembly program loaded by the BIOS. To make our \r\nbootloader a real one, it must perform well the above two tasks: \r\nread and load an operating system.\r\n\r\n  Floppy Disk Anatomy\r\n\r\nTo read from a storage device, we must understand how the device \r\nworks, and the provided interface for controlling it. First of \r\nall, a floppy disk is a storage device, similar to RAM, but can \r\nstore information even when a computer is turned off, thus is \r\ncalled persistent storage device[margin:\r\npersistent storage device\r\n]persistent storage device. A floppy disk also a persistent \r\nstorage device, thus it provides a storage space up to 1.4 MB, or \r\n1,474,560 bytes. When reading from a floppy disk, the smallest \r\nunit that can be read is a sectorsector, a group of 512 \r\ncontiguous bytes. A group of 18 sectors is a tracktrack. Each \r\nside of a floppy disk consists of 80 tracks. A floppy drive is \r\nrequired to read a floppy disk. Inside a floppy drive contains an \r\narm with 2 heads, each head reads a side of a floppy drive; head \r\n0 writes the upper side and head 1 writes the lower side of a \r\nfloppy disk.\r\n\r\n[float MarginFigure:\r\n\r\n\r\n[MarginFigure 8:\r\nSector and Track.\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/07/Disk-structure.pdf>\r\n     \r\n]\r\n\r\nWhen a floppy drive writes data to a brand new floppy disk, track \r\n0 on the upper side is written first, by head 0. When the upper \r\ntrack 0 is full, the lower track 0 is used by head 1. When both \r\nthe upper and lower side of a track 0 are full, it goes back to \r\nhead 0 for writing data again, but this time the upper side of \r\ntrack 1 and so on, until no space left on the device. The same \r\nprocedure is also applied for reading data from floppy disk.[float MarginFigure:\r\n\r\n\r\n[MarginFigure 9:\r\nFloppy disk platter with 2 sides.\r\n]\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/07/Disk_heads.pdf>\r\n]\r\n\r\n  Read and load sectors from a floppy disk<sub:Read-and-load>\r\n\r\nFirst, we need to a sample program for writing into the 2[superscript:nd] sector, so we can experiment with floppy disk reading:\r\n\r\n;******************************************\r\n\r\n; sample.asm\t\t\r\n\r\n; A Sample Program\r\n\r\n;******************************************\r\n\r\nmov eax, 1\r\n\r\nadd eax, 1\r\n\r\nSuch a program is good enough. To simplify and for the purpose of \r\ndemonstration, we will use the same floppy disk that holds the \r\nbootloader to hold our operating system. The operating system \r\nimage starts from the 2[superscript:nd] sector, as the 1[superscript:st] sector is already in use by the bootloader. We compile and write \r\nit to the 2[superscript:nd] sector with dd:\r\n\r\n\r\n\r\n$ nasm -f bin sample.asm -o sample\r\n\r\n$ dd if=sample of=disk.img bs=512 count=1 seek=0\r\n\r\n\r\n\r\n[float Figure:\r\n\r\n\r\n[Figure 0.21:\r\nThe bootloader and the sample program on floppy disk.\r\n]\r\n\r\n     \r\n+---------------------------+---------------------------+------------+---------------------------+\r\n| 1[superscript:st] sector  | 2[superscript:nd] sector  |   .....    | 30[superscript:th] sector |\r\n+---------------------------+---------------------------+------------+---------------------------+\r\n|        bootloader         |          sample           |   ....     |          (empty)          |\r\n+---------------------------+---------------------------+------------+---------------------------+\r\n     \r\n]\r\n\r\nNext, we need to fix the bootloader for reading from the floppy \r\ndisk and load a number of arbitrary sectors. Before doing so, a \r\nbasic understanding of floppy disk is required. To read data from \r\ndisk, interrupt 13 with AH = 02 is a routine for reading sectors \r\nfrom disk into memory:\r\n\r\nAH = 02\r\n\r\nAL = number of sectors to read (1-128 dec.)\r\n\r\nCH = track/cylinder number (0-1023 dec., see below)\r\n\r\nCL = sector number (1-17 dec.)\r\n\r\nDH = head number (0-15 dec.)\r\n\r\nDL = drive number (0=A:, 1=2nd floppy, 80h=drive 0, 81h=drive 1)\r\n\r\nES:BX = pointer to buffer\r\n\r\n\r\n\r\nReturn:\r\n\r\n   AH = status (see INT 13,STATUS)\r\n\r\n   AL = number of sectors read\r\n\r\n   CF = 0 if successful\r\n\r\n      = 1 if error\r\n\r\nApply the above routine, the bootloader can read the 2[superscript:nd] sector:\r\n\r\n;******************************************\r\n\r\n; Bootloader.asm\t\t\r\n\r\n; A Simple Bootloader\r\n\r\n;******************************************\r\n\r\norg 0x7c00\r\n\r\nbits 16\r\n\r\nstart: jmp boot\r\n\r\n\r\n\r\n;; constant and variable definitions\r\n\r\n  msg\tdb\t\"Welcome to My Operating System!\", 0ah, 0dh, 0h\r\n\r\n\r\n\r\nboot:\r\n\r\n  cli\t; no interrupts \t\r\n\r\n  cld\t; all that we need to init\r\n\r\n\r\n\r\n  mov\tax, 0x50\r\n\r\n\r\n\r\n  ;; set the buffer\r\n\r\n  mov\tes, ax\r\n\r\n  xor\tbx, bx\r\n\r\n\r\n\r\n  mov\tal, 2\t\t\t\t\t; read 2 sector\r\n\r\n  mov\tch, 0\t\t\t\t\t; track 0\r\n\r\n  mov\tcl, 2\t\t\t\t\t; sector to read (The second sector)\r\n\r\n  mov\tdh, 0\t\t\t\t\t; head number\r\n\r\n  mov\tdl, 0\t\t\t\t\t; drive number\r\n\r\n\r\n\r\n  mov\tah, 0x02\t\t\t     ; read sectors from disk\r\n\r\n  int\t0x13\t\t\t\t\t ; call the BIOS routine\r\n\r\n  jmp\t0x50:0x0\t\t\t\t; jump and execute the sector!\r\n\r\n\r\n\r\n  hlt\t; halt the system\r\n\r\n\r\n\r\n  ; We have to be 512 bytes. Clear the rest of the bytes with 0\r\n\r\ntimes 510 - ($-$$) db 0\r\n\r\ndw 0xAA55\t\t\t\t  ; Boot Signiture\r\n\r\nThe above code jumps to the address 0x50:00 (which is 0x500). To \r\ntest the code, load it on a QEMU virtual machine and connect \r\nthrough gdb, then place a breakpoint at 0x500. If gdb stops at \r\nthe address, with the assembly listing is the same code as in \r\nsample.asm, then the bootloader successfully loaded the program. \r\nThis is an important milestone, as we ensure that our operating \r\nsystem are loaded and ran properly.\r\n\r\n  Improve productivity with scripts\r\n\r\n  Automate build with GNU Make\r\n\r\nUp to this point, the whole development process felt repetitive: \r\nwhenever a change is made, the same commands are entered again. \r\nThe commands are also complex. Ctrl+r helps, but it still feels \r\ntedious. \r\n\r\nGNU Make is a program that controls and automates the process of \r\nbuilding a complex software. For a small program, like a single C \r\nsource file, invoking gcc is quick and easy. However, soon your \r\nsoftware will be more complex, with multiples spanning multiple \r\ndirectories, it is a chore to manually build and link files. To \r\nsolve such problem, a tool was created to automate away this \r\nproblem and is called a build system. GNU Make is one such of \r\ntools. There are various build systems out there, but GNU Make is \r\nthe most popular in Linux world, as it is used for building the \r\nLinux kernel.\r\n\r\nFor a comprehensive introduction to make, please refer to the \r\nofficial Introduction to Make: \r\nhttps://www.gnu.org/software/make/manual/html_node/Introduction.html#Introduction\r\n. And that's enough for our project. You can also download the \r\nmanual in different formats e.g. PDF from the official manual \r\npage: https://www.gnu.org/software/make/manual/ .\r\n\r\nWith Makefile, we can build simpler commands and save time:\r\n\r\nall: bootloader bootdisk\r\n\r\n\r\n\r\nbootloader:\r\n\r\n\tnasm -f bin bootloader.asm -o bootloader.o\r\n\r\n\r\n\r\nkernel:\r\n\r\n\tnasm -f bin sample.asm -o bootloader.o\r\n\r\n\r\n\r\nbootdisk: bootloader.o kernel.o\r\n\r\n\tdd if=/dev/zero of=disk.img bs=512 count=2880\r\n\r\n\tdd conv=notrunc if=bootloader.o of=disk.img bs=512 count=1 \r\nseek=0\r\n\r\n\tdd conv=notrunc if=sample.o of=disk.img bs=512 count=1 seek=1\r\n\r\nNow, with a single command, we can build from start to finish a \r\ndisk image with a bootloader at 1[superscript:st]sector and the sample program at 2[superscript:nd]sector:\r\n\r\n\r\n\r\n$ make bootdisk\r\n\r\n\r\n\r\n\r\n\r\nnasm -f bin bootloader.asm -o bootloader.o\r\n\r\nnasm -f bin sample.asm -o bootloader.o\r\n\r\ndd if=/dev/zero of=disk.img bs=512 count=2880\r\n\r\n2880+0 records in\r\n\r\n2880+0 records out\r\n\r\n1474560 bytes (1.5 MB, 1.4 MiB) copied, 0.00482188 s, 306 MB/s\r\n\r\ndd conv=notrunc if=bootloader.o of=disk.img bs=512 count=1 seek=0\r\n\r\n0+1 records in\r\n\r\n0+1 records out\r\n\r\n10 bytes copied, 7.0316e-05 s, 142 kB/s\r\n\r\ndd conv=notrunc if=sample.o of=disk.img bs=512 count=1 seek=1\r\n\r\n0+1 records in\r\n\r\n0+1 records out\r\n\r\n10 bytes copied, 0.000208375 s, 48.0 kB/s\r\n\r\n\r\n\r\nLooking at the Makefile, we can see a few problems: \r\n\r\nFirst, the name disk.img are all over the place. When we want to \r\nchange the disk image name e.g. floppy_disk.img, all the places \r\nwith the name disk.img must be changed manually. To solve this \r\nproblem, we use a variable, and every appearance of disk.img is \r\nreplaced with the reference to the variable. This way, only one \r\nplace that is changed - the variable definition - all other \r\nplaces are updated automatically. The following variables are \r\nadded:\r\n\r\nBOOTLOADER=bootloader.o\r\n\r\nOS=sample.o\r\n\r\nDISK_IMG=disk.img.o\r\n\r\nThe second problem is, the name bootloader and sample appears as \r\npart of the filenames of the source files e.g. bootloader.asm and \r\nsample.asm, as well as the filenames of the binary files e.g. \r\nbootloader and sample. Similar to disk.img, when a name changed, \r\nevery reference of that name must also be changed manually for \r\nboth the names of the source files and the names of the binary \r\nfiles e.g. if we change bootloader.asm to loader.asm, then the \r\nobject file bootloader.o needs changing to loader.o. To solve \r\nthis problem, instead of changing filenames manually, we create a \r\nrule that automatically generate the filenames of one extension \r\nto another. In this case, we want any source file that starts \r\nwith .asm to have its equivalent binary files, without any \r\nextension e.g. bootloader.asm \\rightarrow\r\n bootloader.o. Such \r\ntransformation is common, so GNU Make provides built-in \r\nfunctions: wildcard and patsubst for solving such problems:\r\n\r\nBOOTLOADER_SRCS := $(wildcard *.asm)\r\n\r\nBOOTLOADER_OBJS := $(patsubst %.asm, %.o, $(BOOTLOADER_SRCS))\r\n\r\nwildcard matches any .asm file in the current directory, then \r\nassigned the list of matched files into the variable \r\nBOOTLOADER_SRCS. In this case, BOOTLOADER_SRCS is assigned the \r\nvalue: \r\n\r\nbootloader.asm sample.asm\r\n\r\npatsubst substitutes any filename starts with .asm into a \r\nfilename .o e.g. bootloader.asm \\rightarrow\r\n bootloader.o. After \r\npatsubsts runs, we get a list of object files in BOOTLOADER_OBJS:\r\n\r\nbootloader.o sample.o\r\n\r\nFinally, a recipe for building from .asm to .o are needed:\r\n\r\n%.o: %.asm\r\n\r\n\tnasm -f bin $< -o $@\r\n\r\n• $< is a special variable that refers to the input of the \r\n  recipe: %.asm.\r\n\r\n• $@ is a special variable that refers to the output of the \r\n  recipe: %.o.\r\n\r\nWhen the recipe is executed, the variables are replaced with the \r\nactual values. For example, if a transformation is bootloader.asm \r\n\\rightarrow\r\n bootloader.o, then the actual command executed when \r\nreplace the placeholders in the recipe is: \r\n\r\nnasm -f bin bootloader.asm -o bootloader.o\r\n\r\nWith the recipe, all the .asm files are built automatically with \r\nthe nasm command into .o files and we no longer need a separate \r\nrecipe for each object files. Putting it all together with the \r\nnew variables, we get a better Makefile:\r\n\r\nBOOTLOADER=bootloader.o\r\n\r\nOS=sample.o\r\n\r\nDISK_IMG=disk.img\r\n\r\n\r\n\r\nBOOTLOADER_SRCS := $(wildcard *.asm)\r\n\r\nBOOTLOADER_OBJS := $(patsubst %.asm, %.o, $(BOOTLOADER_SRCS))\r\n\r\n\r\n\r\nall: bootdisk\r\n\r\n\r\n\r\n%.o: %.asm\r\n\r\n\tnasm -f bin $< -o $@\r\n\r\n\r\n\r\nbootdisk:  $(BOOTLOADER_OBJS)\r\n\r\n\tdd if=/dev/zero of=$(DISK_IMG) bs=512 count=2880\r\n\r\n\tdd conv=notrunc if=$(BOOTLOADER) of=$(DISK_IMG) bs=512 count=1 \r\nseek=0\r\n\r\n\tdd conv=notrunc if=$(OS) of=$(DISK_IMG) bs=512 count=1 seek=1\r\n\r\nFrom here on, any .asm file is compiled automatically, without an \r\nexplicit recipe for each file. \r\n\r\nThe object files are in the same directory as the source files, \r\nmaking it more difficult when working with the source tree. \r\nIdeally, object files and source files should live in different \r\ndirectories. We want a better organized directory layout like \r\nFigure [project-layout].[float MarginFigure:\r\n[MarginFigure 10:\r\nA better project layout\r\n]<project-layout>\r\n\r\n.\r\n\r\n├── bootloader\r\n\r\n│   └── bootloader.asm\r\n\r\n│   └── Makefile\r\n\r\n├── build\r\n\r\n│   ├── bootloader\r\n\r\n│   │   └── bootloader.o\r\n\r\n│   ├── disk.img\r\n\r\n│   └── os\r\n\r\n│       └── sample.o\r\n\r\n├── Makefile\r\n\r\n└── os\r\n\r\n    ├── Makefile\r\n\r\n    └── sample.asm\r\n\r\nThe layout can be displayed with tree command:\r\n\r\n$ tree\r\n]\r\n\r\nbootloader/ directory holds bootloader source files; os/ holds \r\noperating system source files that we are going to write later; \r\nbuild/ holds the object files for both the bootloader, the os and \r\nthe final disk image disk.img. Notice that bootloader/ directory \r\nalso has its own Makefile. This Makefile will be responsible for \r\nbuilding everything in bootloader/ directory, while the top-level \r\nMakefile is released from the burden of building the bootloader, \r\nbut only the disk image. The content of the Makefile in \r\nbootloader/ directory should be:\r\n\r\nBUILD_DIR=@|\\color{blue}\\bfseries ../build/|@bootloader\r\n\r\n\r\n\r\nBOOTLOADER_SRCS := $(wildcard *.asm)\r\n\r\nBOOTLOADER_OBJS := $(patsubst %.asm, $(BUILD_DIR)/%.o, \r\n$(BOOTLOADER_SRCS))\r\n\r\n\r\n\r\nall: $(BOOTLOADER_OBJS)\r\n\r\n\r\n\r\n$(BUILD_DIR)/%.o: %.asm\r\n\r\n\tnasm -f bin $< -o $@[float MarginFigure:\r\n[MarginFigure 11:\r\nMakefile in bootloader/\r\n]\r\n\r\n.\r\n\r\n├── bootloader\r\n\r\n│   └── bootloader.asm\r\n\r\n│   └── Makefile\r\n\r\n├── build\r\n\r\n│   ├── bootloader\r\n\r\n│   │   └── bootloader.o\r\n\r\n│   ├── disk.img\r\n\r\n│   └── os\r\n\r\n│       └── sample.o\r\n\r\n├── Makefile\r\n\r\n└── os\r\n\r\n    ├── Makefile\r\n\r\n    └── sample.asm\r\n]\r\n\r\nBasically everything related to the bootloader in the top-level \r\nMakefile are extracted into this Makefile. When make runs this \r\nMakefile, bootloader.o should be built and put into ../build/ \r\ndirectory. As a good practice, all references to ../build/ go \r\nthrough BUILD_DIR variable. The recipe for transforming from .asm \r\n\\rightarrow\r\n .o is also updated with proper paths, else it will \r\nnot work. \r\n\r\n• %.asm refers to the assembly source files in the current \r\n  directory.\r\n\r\n• $(BUILD_DIR)/%.o refers to the output object files in the build \r\n  directory in the path ../build/.\r\n\r\nThe entire recipe implements the transformation from \r\n<source_file.asm> \\rightarrow\r\n ../build/<object_file.o>. Note \r\nthat all paths must be correct. If we try to build object files \r\nin a different directory e.g. current directory, it will not work \r\nsince there is no such recipe exists to build objects at such a \r\npath.\r\n\r\nWe also create a similar Makefile for os/ directory:[float MarginFigure:\r\n[MarginFigure 12:\r\nMakefile in os/\r\n]\r\n\r\n.\r\n\r\n├── bootloader\r\n\r\n│   └── bootloader.asm\r\n\r\n│   └── Makefile\r\n\r\n├── build\r\n\r\n│   ├── bootloader\r\n\r\n│   │   └── bootloader.o\r\n\r\n│   ├── disk.img\r\n\r\n│   └── os\r\n\r\n│       └── sample.o\r\n\r\n├── Makefile\r\n\r\n└── os\r\n\r\n    ├── Makefile\r\n\r\n    └── sample.asm\r\n]\r\n\r\nBUILD_DIR=../build/os\r\n\r\n\r\n\r\nOS_SRCS := $(wildcard *.asm)\r\n\r\nOS_OBJS := $(patsubst %.asm, $(BUILD_DIR)/%.o, $(OS_SRCS))\r\n\r\n\r\n\r\nall: $(OS_OBJS)\r\n\r\n\r\n\r\n$(BUILD_DIR)/%.o: %.asm\r\n\r\n\tnasm -f bin $< -o $@\r\n\r\nFor now, it looks almost identical to the Makefile for \r\nbootloader. In the next chapter, we will update it for C code. \r\nThen, we update the top-level Makefile:[float MarginFigure:\r\n[MarginFigure 13:\r\nTop-level Makefile\r\n]\r\n\r\n.\r\n\r\n├── bootloader\r\n\r\n│   └── bootloader.asm\r\n\r\n│   └── Makefile\r\n\r\n├── build\r\n\r\n│   ├── bootloader\r\n\r\n│   │   └── bootloader.o\r\n\r\n│   ├── disk.img\r\n\r\n│   └── os\r\n\r\n│       └── sample.o\r\n\r\n├── Makefile\r\n\r\n└── os\r\n\r\n    ├── Makefile\r\n\r\n    └── sample.asm\r\n]\r\n\r\nBUILD_DIR=build\r\n\r\nBOOTLOADER=$(BUILD_DIR)/bootloader/bootloader.o\r\n\r\nOS=$(BUILD_DIR)/os/sample.o\r\n\r\nDISK_IMG=disk.img\r\n\r\n\r\n\r\nall: bootdisk\r\n\r\n\r\n\r\n.PHONY: bootdisk bootloader os\r\n\r\n\r\n\r\nbootloader:\r\n\r\n\tmake -C bootloader\r\n\r\n\r\n\r\nos:\r\n\r\n\tmake -C os\r\n\r\n\r\n\r\nbootdisk: bootloader os\r\n\r\n\tdd if=/dev/zero of=$(DISK_IMG) bs=512 count=2880\r\n\r\n\tdd conv=notrunc if=$(BOOTLOADER) of=$(DISK_IMG) bs=512 count=1 \r\nseek=0\r\n\r\n\tdd conv=notrunc if=$(OS) of=$(DISK_IMG) bs=512 count=1 seek=1\r\n\r\nThe build process is now truly modularized:\r\n\r\n• bootloader and os builds are now delegated to child Makefile of \r\n  respective components. -C option tells make to execute with a \r\n  Makefile in a supplied directory. In this case, the directories \r\n  are bootloader/ and os/.\r\n\r\n• The target all of the top-level Makefile is only responsible \r\n  for bootdisk target, which is the primary target of this \r\n  Makefile. \r\n\r\nIn many cases, a target is not always a filename, but is just a \r\nname for a recipe to be always executed when requested. If a \r\nfilename is of the same name as a target and the file is \r\nup-to-date, make does not execute the target. To solve this \r\nproblem, .PHONY specifies that some targets are not files. All \r\nphony targets will then run when requested, regardless of files \r\nof the same names.\r\n\r\nTo save time entering the command for starting up a QEMU virtual \r\nmachine, we also add a target to the top-level Makefile:\r\n\r\nqemu:\r\n\r\n\tqemu-system-i386 -machine q35 -fda $(DISK_IMG) -gdb tcp::26000 \r\n-S\r\n\r\nOne last problem is project cleaning. At the moment, object files \r\nneed removing manually and this is a repetitive process. Instead, \r\nlet the Makefile of each component takes care of cleaning its \r\nobject files, then top-level Makefile performs project cleaning \r\nby calling the component Makefile to do the jobs. Each Makefile \r\nis added with a clean target at the end:\r\n\r\n• Bootloader Makefile:\r\n\r\n  clean:\r\n\r\n\trm $(BUILD_DIR)/*\r\n\r\n• OS Makefile:\r\n\r\n  clean:\r\n\r\n\trm $(BUILD_DIR)/*\r\n\r\n• Top-level Makefile:\r\n\r\n  clean:\r\n\r\n\tmake -C bootloader clean\r\n\r\n\tmake -C os clean\r\n\r\nSimply invoking make clean at the project root, all object files \r\nthe are removed.\r\n\r\n  GNU Make Syntax summary\r\n\r\nGNU Make, at its core, is a domain-specific language for build \r\nautomation. As any programming language, it needs a way to define \r\ndata and code. In a Makefile, variables carry data. A variable \r\nvalue is either hard coded or evaluated from invoking a shell \r\nsuch as Bash. All variable values in Make has the same type: a \r\nstring of text. Number 3 is not a number, but textual \r\nrepresentation of the symbol 3. Here are common ways how to \r\ndefine data in a Makefile:\r\n\r\n\r\n+------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| Syntax                                                                                         | Description                                                                                                                                                                                                                                                                                |\r\n+------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n+------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| A = 1\r\n\r\nB = 2\r\n\r\nC = $$(expr $(A) + $(B))\r\n\r\n\r\n⇒ A is 1, B is 2, C is 3.                             | Declare a variable and assign a textual value to it. the double \r\ndollar sign $$ means the enclosing expression evaluating by a \r\nshell, defined by /bin/sh. In this case, the enclosing expression \r\nis (expr $(A) + $(B)) and is evaluated by Bash.\r\n                                        |\r\n+------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| PATH = /bin\r\n\r\nPATH := $PATH:/usr/bin\r\n\r\n\r\n⇒ PATH is /bin/:/usr/bin                                 | Declare a variable and assign to it. However, the difference is \r\nthat the = syntax does not allow refer to a variable to use \r\nitself as a value in the right hand side, while this syntax does.\r\n                                                                                           |\r\n+------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| PATH = /bin\r\n\r\nPATH += /usr/bin\r\n\r\n\r\n⇒ PATH is /bin/:/usr/bin                                       | Append a new value at the end of a variable. Equivalent to: \r\n\r\nPATH := $PATH:/usr/bin\r\n                                                                                                                                                                                                      |\r\n+------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| CFLAGS ?= -o\r\n\r\n\r\n⇒ CFLAGS is assigned the value -o if it was not defined.                        | This syntax is called conditional reference. Set a variable to a \r\nvalue if it is undefined. This is useful if a user wants to \r\nsupply different value for a variable from the command line e.g. \r\nadd debugging option to CFLAGS. Otherwise, Make uses the default \r\ndefined by ?=.\r\n         |\r\n+------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| SRCS = lib1.c lib2.c main.c\r\n\r\nOBJS := $(SRC:.o=.c)\r\n\r\n\r\n⇒ OBJS has the value lib1.o lib2.o main.o  | This syntax is called substitution reference. A part of \r\nreferenced variable is replaced with something else. In this \r\ncase, all the .c extension is replaced by .o extension, thus \r\ncreating a list of object files for OBJS variable from the list \r\nof source files from SRCS variable.\r\n |\r\n+------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n\r\n\r\nCode in GNU Make is a collection of recipes that it can run. Each \r\nrecipe is analogous to a function in a programming language, and \r\ncan be called like a regular function. Each recipe carries a \r\nseries of shell commands to be executed by a shell e.g. Bash. A \r\nrecipe has the following format:\r\n\r\ntarget: prerequisites\r\n\r\n\tcommand\r\n\r\nEach target is analogous to a function name. Each prerequisite is \r\na call another target. Each command is one of Make's built-in \r\ncommands or a command that is executable by a shell. All \r\nprerequisites must be satisfied before entering main body of target\r\n; that is, each prerequisite must not return any error. If any \r\nerror is returned, Make terminates the whole build process and \r\nprints an error on the command line.\r\n\r\nEach time make runs, by default if no target is supplied, it \r\nstarts with all target, go through every prerequisites and \r\nfinally the body of all. all is analogous to main in other \r\nprogramming languages. However, if make is given a target, it \r\nwill start from that target instead of main. This feature is \r\nuseful to automate multiple aspects in a project. For example, \r\none target is for building the project, one target is for \r\ngenerating the documents e.g. test reports, another target for \r\nrunning the whole test suite and all runs every main targets.\r\n\r\n\r\n\r\n  Automate debugging steps with GDB script<sub:Automate-debugging-steps>\r\n\r\nFor the convenience, we save GDB configuration to .gdbinit file \r\nat the project root directory. This configuration is just a \r\ncollection of GDB commands and a few extra commands. When gdb \r\nruns, it first loads the .gdbinit file at home directory, then \r\nthe .gdbinit file at the current directory. Why shouldn't we put \r\ncommands in ~/.gdbinit? Because these commands are specific to \r\nonly this projec e.g. not all programs are required a remote \r\nconnection.\r\n\r\nOur first configuration:\r\n\r\ndefine hook-stop\r\n\r\n    # Translate the segment:offset into a physical address\r\n\r\n    printf \"[%4x:%4x] \", $cs, $eip\r\n\r\n    x/i $cs*16+$eip\r\n\r\nend\r\n\r\nThe above script displays the memory address in [segment:offset] \r\nformat, which is necessary for debugging our bootloader and \r\noperating system code.\r\n\r\nIt is better to use Intel syntax:\r\n\r\nset disassembly-flavor intel\r\n\r\nThe following commands set a more convenient layout for debugging \r\nassembly code:\r\n\r\nlayout asm\r\n\r\nlayout reg\r\n\r\nWe are currently debugging bootloader code, so it is a good idea \r\nto first set it to 16-bit:\r\n\r\nset architecture i8086\r\n\r\nEvery time the QEMU virtual machine starts, gdb must always \r\nconnect to port 26000. To avoid the trouble of manually \r\nconnecting to the virtual machine, add the command:\r\n\r\ntarget remote localhost:26000\r\n\r\nDebugging the bootloader needs a breakpoint at 0x7c00, where our \r\nbootloader code starts:\r\n\r\nb *0x7c00\r\n\r\nNow, whenever gdb starts, it automatically set correct \r\narchitecture based on code, automatically connects to the virtual \r\nmachine[footnote:\r\nThe QEMU virtual machine should have already been started before \r\nstarting gdb.\r\n], displays output in a convenient layout and set a necessary \r\nbreakpoint. All that need to do is run the program. \r\n\r\nLinking and loading on bare metal<chap:Linking-and-loading>\r\n\r\n[margin:\r\nRelocation\r\n]RelocationRelocation is the process of replacing symbol \r\nreferences with its actual symbolic definitions in an object \r\nfile. A symbol reference is the memory address of a symbol.\r\n\r\nIf the definition is hard to understand, consider a similar \r\nanalogy: house relocation. Suppose that a programmer bought a new \r\nhouse and the new house is empty. He must buy furnitures and \r\nappliances to fulfill daily needs and thus, he made a list of \r\nitems to buy, and where to place them. To visualize the \r\nplacements of new items, he draws a blueprint of the house and \r\nthe respective places of all items. He then travels to the shops \r\nto buy goods. Whenever he visit a shop and sees matched items, he \r\ntells the shop owner to note them down. After done selecting, he \r\ntells the shop owner to pick up a brand new item instead of the \r\nobjects on display, then give the address for delivering the \r\ngoods to his new house. Finally, when the goods arrive, he places \r\nthe items where he planned at the beginning.\r\n\r\n\r\n\r\nNow that house relocation is clear, object relocation is similar:\r\n\r\n• The list of items represents the relocation table, where the \r\n  memory location for each symbol (item) is predetermined.\r\n\r\n• Each item represents a pair of symbol definition and its symbol \r\n  address.\r\n\r\n• Each shop represents a compiled object file.\r\n\r\n• Each item on display represents a symbol definition and \r\n  references in the object file.\r\n\r\n• The new address, where all the goods are delivered, represents \r\n  the final executable binary or the final object file. Since the \r\n  items on display are not for sale, the shop owner delivers \r\n  brand new goods instead. Similarly, the object files are not \r\n  merged together, but copied all over a new file, the \r\n  object/executable file.\r\n\r\n• Finally, the goods are placed in the positions according to the \r\n  shopping list made from the beginning. Similarly, the symbol \r\n  definitions are placed appropriately in its respective section \r\n  and the symbol references of the final object/executable file \r\n  are replaced with the actual memory addresses of the symbol \r\n  definitions.\r\n\r\n\r\n\r\n\r\n\r\n  Understand relocations with readelf<sec:Understand-relocations-with-readelf>\r\n\r\nEarlier, when we explore object sections, there exists sections \r\nthat begins with .rel. These sections are relocation tables that \r\nmaps between a symbol and its location in the final object file \r\nor the final executable binary[footnote:\r\nA .rel section is equivalent to a list of items in the house \r\nanalogy.\r\n]. \r\n\r\nSuppose that a function foo is defined in another object file, so \r\nmain.c declares it as extern:\r\n\r\nint i;\r\n\r\nvoid foo();\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    i = 5;\r\n\r\n    foo();\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n\r\n\r\nvoid foo() {}\r\n\r\nWhen we compile main.c as object file with this command:\r\n\r\n\r\n\r\n$ gcc -m32 -masm=intel -c main.c\r\n\r\n\r\n\r\nThen, we can inspect the relocation tables with this command:\r\n\r\n\r\n\r\n$ readelf -r main.o\r\n\r\n\r\n\r\nThe output:\r\n\r\n\r\n\r\nRelocation section '.rel.text' at offset 0x1cc contains 2 \r\nentries:\r\n\r\n Offset     Info    Type            Sym.Value  Sym. Name\r\n\r\n00000013  00000801 R_386_32          00000004   i\r\n\r\n0000001c  00000a02 R_386_PC32        0000002e   foo\r\n\r\nRelocation section '.rel.eh_frame' at offset 0x1dc contains 2 \r\nentries:\r\n\r\n Offset     Info    Type            Sym.Value  Sym. Name\r\n\r\n00000020  00000202 R_386_PC32        00000000   .text\r\n\r\n0000004c  00000202 R_386_PC32        00000000   .text\r\n\r\n\r\n\r\n  Offset \r\n\r\nAn [margin:\r\noffset\r\n]offsetoffset is the location into a section of a binary file, \r\nwhere the actual memory address of a symbol definition is \r\nreplaced. The section with .rel prefix determines which section \r\nto offset into. For example, .rel.text is the relocation table of \r\nsymbols whose address needs correcting in .text section, at a \r\nspecific offset into .text section. In the example output:\r\n\r\n\r\n\r\n0000001c  00000a02 R_386_PC32        0000002e   foo\r\n\r\n\r\n\r\nThe blue number indicates there exists a reference of symbol foo \r\nthat is 1c bytes into .text section. To see it clearer, we \r\nrecompile main.c with option -g into the file main_debug.o, then \r\nrun objdump on it and got:\r\n\r\n\r\n\r\nDisassembly of section .text:\r\n\r\n00000000 <main>:\r\n\r\nint i;\r\n\r\nvoid foo();\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n   0:   8d 4c 24 04             lea    ecx,[esp+0x4]\r\n\r\n   4:   83 e4 f0                and    esp,0xfffffff0\r\n\r\n   7:   ff 71 fc                push   DWORD PTR [ecx-0x4]\r\n\r\n   a:   55                      push   ebp\r\n\r\n   b:   89 e5                   mov    ebp,esp\r\n\r\n   d:   51                      push   ecx\r\n\r\n   e:   83 ec 04                sub    esp,0x4\r\n\r\n    i = 5;\r\n\r\n  11:   c7 05 00 00 00 00 05    mov    DWORD PTR ds:0x0,0x5\r\n\r\n  18:   00 00 00 \r\n\r\n    foo();\r\n\r\n  1b:   e8 fc ff ff ff          call   1c <main+0x1c>\r\n\r\n    return 0;\r\n\r\n  20:   b8 00 00 00 00          mov    eax,0x0\r\n\r\n}\r\n\r\n  25:   83 c4 04                add    esp,0x4\r\n\r\n  28:   59                      pop    ecx\r\n\r\n  29:   5d                      pop    ebp\r\n\r\n  2a:   8d 61 fc                lea    esp,[ecx-0x4]\r\n\r\n  2d:   c3                      ret    \r\n\r\n....irrelevant content omitted....\r\n\r\n\r\n\r\nThe byte at 1b[superscript:] is the opcode e8, the call instruction; byte at 1c is the value \r\nfc. Why is the operand value for e8 is 0xfffffffc, which is \r\nequivalent to -4, but the translated instruction call 1c?\r\n\r\n  Info \r\n\r\nInfo specifies index of a symbol in the symbol table and the type \r\nof relocation to perform. \r\n\r\n\r\n\r\n0000001c  00000a02 R_386_PC32        0000002e   foo\r\n\r\n\r\n\r\nThe pink number is the index of symbol foo in the symbol table, \r\nand the green number is the relocation type. The numbers are \r\nwritten in hex format. In the example, 0a means 10 in decimal, \r\nand symbol foo is indeed at index 10:\r\n\r\n\r\n\r\n10: 0000002e     6 FUNC    GLOBAL DEFAULT    1 foo\r\n\r\n\r\n\r\n  Type \r\n\r\nType represents the type value in textual form. Looking at the \r\ntype of foo:\r\n\r\n\r\n\r\n0000001c  00000a02 R_386_PC32        0000002e   foo\r\n\r\n\r\n\r\nThe green number is type in its numeric form, and R_386_PC32 is \r\nthe name assigned to that value. Each value represents a \r\nrelocation method of calculation. For example, with the type \r\nR_386_PC32, the following formula is applied for relocation \r\n(Inteli386 psABI): \r\n\r\nRelocated\\,Offset=S+A-P\r\n\r\n\r\nTo understand the formula, it is necessary to understand symbol \r\nvalues.\r\n\r\n  Sym.Value \r\n\r\nThis field shows the symbol value. A symbol value is a value \r\nassigned to a symbol, whose meaning depends on the Ndx field:\r\n\r\n  A symbol whose section index is COMMON, its symbol value holds \r\n  alignment constraints.\r\n\r\n  In the symbol table, the variable i is identified as COM \r\n  (uninitialized variable):[footnote:\r\nThe command for listing symbol table is (assume the object file \r\nis hello.o):\r\n\r\nreadelf -s hello.o\r\n]\r\n\r\n    \r\n\r\n    Symbol table '.symtab' contains 16 entries:\r\n\r\n       Num:    Value  Size Type    Bind   Vis      Ndx Name\r\n\r\n         0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND \r\n\r\n         1: 00000000     0 FILE    LOCAL  DEFAULT  ABS hello2.c\r\n\r\n         2: 00000000     0 SECTION LOCAL  DEFAULT    1 \r\n\r\n         3: 00000000     0 SECTION LOCAL  DEFAULT    3 \r\n\r\n         4: 00000000     0 SECTION LOCAL  DEFAULT    4 \r\n\r\n         5: 00000000     0 SECTION LOCAL  DEFAULT    5 \r\n\r\n         6: 00000000     0 SECTION LOCAL  DEFAULT    7 \r\n\r\n         7: 00000000     0 SECTION LOCAL  DEFAULT    8 \r\n\r\n         8: 00000000     0 SECTION LOCAL  DEFAULT   10 \r\n\r\n         9: 00000000     0 SECTION LOCAL  DEFAULT   12 \r\n\r\n        10: 00000000     0 SECTION LOCAL  DEFAULT   14 \r\n\r\n        11: 00000000     0 SECTION LOCAL  DEFAULT   15 \r\n\r\n        12: 00000000     0 SECTION LOCAL  DEFAULT   13 \r\n\r\n        13: 00000004     4 OBJECT  GLOBAL DEFAULT  COM i\r\n\r\n        14: 00000000    46 FUNC    GLOBAL DEFAULT    1 main\r\n\r\n        15: 0000002e     6 FUNC    GLOBAL DEFAULT    1 foo\r\n\r\n    \r\n\r\n    so its symbol value is a memory alignment for assigning a \r\n    proper memory address that conforms to the alignment in the \r\n    final memory address. In the case of i, the value is 4, so \r\n    the starting memory address of i in the final binary file \r\n    will be a multiple of 4.\r\n\r\n  A symbol whose Ndx identifies a specific section, its symbol \r\n  value holds a section offset.\r\n\r\n  In the symbol table, main and foo belong to section 1:\r\n\r\n    \r\n\r\n    14: 00000000    46 FUNC    GLOBAL DEFAULT    1 main\r\n\r\n    15: 0000002e     6 FUNC    GLOBAL DEFAULT    1 foo\r\n\r\n    \r\n\r\n    which is .text[footnote:\r\n.text holds program code and read-only data.\r\n] section[footnote:\r\nThe command for listing sections is (assume the object file is \r\nhello.o):\r\n\r\nreadelf -S hello.o\r\n]:\r\n\r\n    \r\n\r\n    There are 20 section headers, starting at offset 0x558:\r\n\r\n    Section Headers:\r\n\r\n      [Nr] Name              Type            Addr     Off    Size \r\n      ES Flg Lk Inf Al\r\n\r\n      [ 0]                   NULL            00000000 000000 \r\n    000000 00      0   0  0\r\n\r\n      [ 1] .text             PROGBITS        00000000 000034 \r\n    000034 00  AX  0   0  1\r\n\r\n      [ 2] .rel.text         REL             00000000 000414 \r\n    000010 08   I 18   1  4\r\n\r\n      [ 3] .data             PROGBITS        00000000 000068 \r\n    000000 00  WA  0   0  1\r\n\r\n      [ 4] .bss              NOBITS          00000000 000068 \r\n    000000 00  WA  0   0  1\r\n\r\n      [ 5] .debug_info       PROGBITS        00000000 000068 \r\n    000096 00      0   0  1\r\n\r\n    ..... remaining output omitted for clarity....\r\n\r\n    \r\n\r\n  In the final executable and shared object files, instead of the \r\n  above values, a symbol value holds a memory address.\r\n\r\n  After compiling hello.o into the final executable hello, the \r\n  symbol table now contains the memory address for each symbol[footnote:\r\nThe command to compile the object file hello.o into the \r\nexecutable hello:\r\n\r\ngcc -g -m32 -masm=intel hello.o -o hello\r\n]:\r\n\r\n    \r\n\r\n    Symbol table '.symtab' contains 75 entries:\r\n\r\n       Num:    Value  Size Type    Bind   Vis      Ndx Name\r\n\r\n         0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND \r\n\r\n         1: 08048154     0 SECTION LOCAL  DEFAULT    1 \r\n\r\n         2: 08048168     0 SECTION LOCAL  DEFAULT    2 \r\n\r\n         3: 08048188     0 SECTION LOCAL  DEFAULT    3 \r\n\r\n    ....output omitted...\r\n\r\n        64: 08048409     6 FUNC    GLOBAL DEFAULT   14 foo\r\n\r\n        65: 0804a020     0 NOTYPE  GLOBAL DEFAULT   26 _end\r\n\r\n        66: 080482e0     0 FUNC    GLOBAL DEFAULT   14 _start\r\n\r\n        67: 08048488     4 OBJECT  GLOBAL DEFAULT   16 _fp_hw\r\n\r\n        68: 0804a01c     4 OBJECT  GLOBAL DEFAULT   26 i\r\n\r\n        69: 0804a018     0 NOTYPE  GLOBAL DEFAULT   26 \r\n    __bss_start\r\n\r\n        70: 080483db    46 FUNC    GLOBAL DEFAULT   14 main\r\n\r\n    ...ouput omitted...\r\n\r\n    \r\n\r\n    Unlike the values of the symbols foo, i and main as in the \r\n    hello.o object file, the complete memory addresses are in \r\n    place.\r\n\r\nNow it suffices to understand relocation types. Previously, we \r\nmentioned the type R_386_PC32. The following formula is applied \r\nfor relocation (Inteli386 psABI): \r\n\r\nRelocated\\,Offset=S+A-P\r\n\r\n\r\nwhere\r\n\r\n  S represents the value of the symbol. In the final executable \r\n  binary, it is the address of the symbol.\r\n\r\n  A represents the addend, an extra value added to the value of a \r\n  symbol.\r\n\r\n  P Represents the memory address to be fixed.\r\n\r\n  Relocate Offset is the distance between a relocating location[footnote:\r\nwhere the referenced memory address is to be fixed.\r\n] and the actual memory location of a symbol definition, or a \r\n  memory address.\r\n\r\nBut why do we waste time in calculating a distance instead of \r\nreplacing with a direct memory address? The reason is that x86 \r\narchitecture does not use employ any addressing mode that uses an \r\nabsolute memory address, as listed in table [mod-rm-32]. All \r\naddressing modes in x86 are relative. In some assembly language, \r\nan absolute address can be used simply because it is a syntactic \r\nsugar that is later transformed into one of the relative \r\naddressing mode provided by the x86 hardware by the assembler.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nFor the foo symbol:\r\n\r\n  \r\n\r\n  0000001c  00000a02 R_386_PC32        0000002e   foo\r\n\r\n  \r\n\r\n  The distance between the usage of foo in main.o and its \r\n  definition, applying the formula S+A-P\r\n is: \\mathtt{2e+0-1c=12}\r\n\r\n  . That is, the place where memory fixing starts is 0x12 or 18 \r\n  bytes away from the definition of the symbol foo. However, to \r\n  make an instruction works properly, we must also subtract 4 \r\n  from 0x12 and results in 0xe. Why the extra -4? Because the \r\n  relative address starts at the end of an instruction, not the \r\n  address where memory fixing starts. For that reason, we must \r\n  also exclude the 4 bytes of the overwritten address. \r\n\r\n  Indeed, looking at the objdump output of the object file \r\n  hello.o:\r\n\r\n  \r\n\r\n  Disassembly of section .text:\r\n\r\n  00000000 <main>:\r\n\r\n     0:   8d 4c 24 04             lea    ecx,[esp+0x4]\r\n\r\n     4:   83 e4 f0                and    esp,0xfffffff0\r\n\r\n     7:   ff 71 fc                push   DWORD PTR [ecx-0x4]\r\n\r\n     a:   55                      push   ebp\r\n\r\n     b:   89 e5                   mov    ebp,esp\r\n\r\n     d:   51                      push   ecx\r\n\r\n     e:   83 ec 04                sub    esp,0x4\r\n\r\n    11:   c7 05 00 00 00 00 05    mov    DWORD PTR ds:0x0,0x5\r\n\r\n    18:   00 00 00 \r\n\r\n    1b:   e8 fc ff ff ff          call   1c <main+0x1c>\r\n\r\n    20:   b8 00 00 00 00          mov    eax,0x0\r\n\r\n    25:   83 c4 04                add    esp,0x4\r\n\r\n    28:   59                      pop    ecx\r\n\r\n    29:   5d                      pop    ebp\r\n\r\n    2a:   8d 61 fc                lea    esp,[ecx-0x4]\r\n\r\n    2d:   c3                      ret    \r\n\r\n  0000002e <foo>:\r\n\r\n    2e:   55                      push   ebp\r\n\r\n    2f:   89 e5                   mov    ebp,esp\r\n\r\n    31:   90                      nop\r\n\r\n    32:   5d                      pop    ebp\r\n\r\n    33:   c3                      ret    \r\n\r\n  \r\n\r\n  The place where memory fixing starts is after the opcode e8, \r\n  with the mock value fc ff ff ff, which is -4 in decimal. \r\n  However, the assembly code, the value is displayed as 1c. the \r\n  memory address right after e8. The reason is that the \r\n  instruction e8 starts at 1b and ends at 20[footnote:\r\nThe end of an instruction is the memory address right after its \r\nlast operand. The whole instruction e8 spans from the address 1b \r\nto the address 1f. \r\n]. -4 means 4 bytes backward from the end of instruction, that \r\n  is: \\mathtt{20-4=1c}\r\n. After linking, the output of the final \r\n  executable file is displayed with the actual memory fixing:\r\n\r\n  \r\n\r\n  080483db <main>:\r\n\r\n   80483db:       8d 4c 24 04             lea    ecx,[esp+0x4]\r\n\r\n   80483df:       83 e4 f0                and    esp,0xfffffff0\r\n\r\n   80483e2:       ff 71 fc                push   DWORD PTR \r\n  [ecx-0x4]\r\n\r\n   80483e5:       55                      push   ebp\r\n\r\n   80483e6:       89 e5                   mov    ebp,esp\r\n\r\n   80483e8:       51                      push   ecx\r\n\r\n   80483e9:       83 ec 04                sub    esp,0x4\r\n\r\n   80483ec:       c7 05 1c a0 04 08 05    mov    DWORD PTR \r\n  ds:0x804a01c,0x5\r\n\r\n   80483f3:       00 00 00 \r\n\r\n   80483f6:       e8 0e 00 00 00          call   8048409 <foo>\r\n\r\n   80483fb:       b8 00 00 00 00          mov    eax,0x0\r\n\r\n   8048400:       83 c4 04                add    esp,0x4\r\n\r\n   8048403:       59                      pop    ecx\r\n\r\n   8048404:       5d                      pop    ebp\r\n\r\n   8048405:       8d 61 fc                lea    esp,[ecx-0x4]\r\n\r\n   8048408:       c3                      ret \r\n\r\n  \r\n\r\n  08048409 <foo>:\r\n\r\n   8048409:       55                      push   ebp\r\n\r\n   804840a:       89 e5                   mov    ebp,esp\r\n\r\n   804840c:       90                      nop\r\n\r\n   804840d:       5d                      pop    ebp\r\n\r\n   804840e:       c3                      ret    \r\n\r\n   804840f:       90                      nop\r\n\r\n  \r\n\r\n  In the final output, the opcode e8 previously at 1b now starts \r\n  at the address 80483f6. The mock value fc ff ff ff is replaced \r\n  with the actual value 0e 00 00 00 using the same calculating \r\n  method from its object file: opcode e8 is at 80483f6. The \r\n  definition of foo is at 8048409. The offset from the next \r\n  address after e8 is \\mathtt{8048409+0-80483f7-4=0e}\r\n. However, \r\n  for readability, the assembly is displayed as call 8048409 \r\n  <foo>, since GNU as[footnote:\r\nOr any current assembler in use today.\r\n] assembler allows specifying the actual memory address of a \r\n  symbol definition. Such address is later translated into \r\n  relative addressing mode, saving the programmer the trouble of \r\n  calculating offset manually.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n  Sym. Name\r\n\r\nThis field displays the name of a symbol to be relocated. The \r\nnamed symbol is the same as written in a high level language such \r\nas C.\r\n\r\n  Crafting ELF binary with linker scripts\r\n\r\nA [margin:\r\nlinker\r\n]linkerlinker is a program that combines separated object files \r\ninto a final binary file. When gcc is invoked, it runs ld \r\nunderneath to turn object files into the final executable file..\r\n\r\nA linker script[margin:\r\nlinker script\r\n]linker script is a text file that instructs how a linker should \r\ncombine object files. When gcc runs, it uses its default linker \r\nscript to build the memory layout of a compiled binary file. \r\nStandardized memory layout is called object file format e.g. ELF \r\nincludes program headers, section headers and their attributes. \r\nThe default linker script is made for running in the current \r\noperating system environment[footnote:\r\nTo view the default script, use --verbose option:\r\n\r\nld --verbose\r\n]. Running on bare metal, the default script cannot be used as it \r\nis not designed for such environment. For that reason, a \r\nprogrammer needs to supply his own linker script for such \r\nenvironments.\r\n\r\nEvery linker script consists of a series of commands with the \r\nfollowing format:\r\n\r\nCOMMAND \r\n\r\n{\r\n\r\n  sub-command 1\r\n\r\n  sub-command 2\r\n\r\n  .... more sub-command....\r\n\r\n}\r\n\r\nEach sub-command is specific to only the top-level command. The \r\nsimplest linker script needs only one command: SECTION, that \r\nconsumes input sections from object files and produces output \r\nsections of the final binary file[footnote:\r\nRecall that sections are chunks of code or data, or both.\r\n]. \r\n\r\n  Example linker script\r\n\r\nHere is a minimal example of a linker script:\r\n\r\nSECTIONS                      /* Command */\r\n\r\n{\r\n\r\n   . = 0x10000;               /* sub-command 1 */\r\n\r\n   .text : { *(.text) }       /* sub-command 2 */\r\n\r\n   . = 0x8000000;             /* sub-command 3 */\r\n\r\n   .data : { *(.data) }       /* sub-command 4 */\r\n\r\n   .bss : { *(.bss) }         /* sub-command 5 */\r\n\r\n} \r\n\r\nCode Dissection: \r\n\r\n\r\n\r\n\r\n+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| Code                   | Description                                                                                                                                                                                                                                                                                    |\r\n+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| SECTION                | Top-level command that declares a list of custom program \r\nsections. ld provides a set of such commands.                                                                                                                                                                                        |\r\n+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| . = 0x10000;           | Set location counter to the address 0x10000. Location counter \r\nspecifies the base address for subsequent commands. In this \r\nexample, subsequent commands will use 0x10000 onward.                                                                                                              |\r\n+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| .text : { *(.text) }   | Since location counter is set to 0x10000, the output .text in the \r\nfinal binary file will starts at the address 0x10000. This \r\ncommand combines all .text sections from all object files with \r\n*(.text) syntax into a final .text section. The * is the wildcard \r\nwhich matches any file name. |\r\n+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| . = 0x8000000;         | Again, the location counter is set to 0x8000000. Subsequent \r\ncommands will use this address for working with sections.                                                                                                                                                                         |\r\n+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| .data : { *(.data) }   | All .data section are combined into one .data section in the \r\nfinal binary file.                                                                                                                                                                                                               |\r\n+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n| .bss : { *(.bss) }     | All .bss section are combined into one .bss section in the final \r\nbinary file.                                                                                                                                                                                                                 |\r\n+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\r\n\r\n\r\n\r\n\r\nThe addresses 0x10000 and 0x8000000 are called Virtual Memory \r\nAddress. A virtual memory address[margin:\r\nvirtual memory address\r\n]virtual memory address is the address where a section is loaded \r\nin memory when a program runs. To use the linker script, we save \r\nit as a file e.g. main.lds[footnote:\r\n.lds is the extension for linker script.\r\n]; then, we need a sample program in a file, e.g. main.c: \r\n\r\nvoid test() {}\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n  \r\n\r\n    return 0;\r\n\r\n}\r\n\r\nThen, we compile the file and explicitly invoke ld with the \r\nlinker script:\r\n\r\n\r\n\r\n$ gcc -m32 -g -c main.c\r\n\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\r\n\r\n\r\nIn the ld command, the options are similar to gcc:\r\n\r\n\r\n+---------+--------------------------------------------------------------------------------------------------------------+\r\n| Option  | Description                                                                                                  |\r\n+---------+--------------------------------------------------------------------------------------------------------------+\r\n+---------+--------------------------------------------------------------------------------------------------------------+\r\n| -m      | Specify object file format that ld produces. In the example, \r\nelf_i386 means a 32-bit ELF is to be produced. |\r\n+---------+--------------------------------------------------------------------------------------------------------------+\r\n| -o      | Specify the name of the final executable binary.                                                             |\r\n+---------+--------------------------------------------------------------------------------------------------------------+\r\n| -T      | Specify the linker script to use. In the example, it is main.lds.                                            |\r\n+---------+--------------------------------------------------------------------------------------------------------------+\r\n\r\n\r\nThe remaining input is a list of object files for linking. After \r\nthe command ld is executed, the final executable binary - main - \r\nis produced. If we try running it:\r\n\r\n\r\n\r\n$ ./main\r\n\r\nSegmentation fault \r\n\r\n\r\n\r\nThe reason is that when linking manually, the entry address must \r\nbe explicitly set, or else ld sets it to the start of .text \r\nsection by default. We can verify from the readelf output:\r\n\r\n\r\n\r\n$ readelf -h main\r\n\r\n\r\n\r\n\r\n\r\nELF Header:\r\n\r\n  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 \r\n\r\n  Class:                             ELF64\r\n\r\n  Data:                              2's complement, little \r\nendian\r\n\r\n  Version:                           1 (current)\r\n\r\n  OS/ABI:                            UNIX - System V\r\n\r\n  ABI Version:                       0\r\n\r\n  Type:                              EXEC (Executable file)\r\n\r\n  Machine:                           Advanced Micro Devices \r\nX86-64\r\n\r\n  Version:                           0x1\r\n\r\n  Entry point address:               0x10000\r\n\r\n  Start of program headers:          64 (bytes into file)\r\n\r\n  Start of section headers:          2098144 (bytes into file)\r\n\r\n  Flags:                             0x0\r\n\r\n  Size of this header:               64 (bytes)\r\n\r\n  Size of program headers:           56 (bytes)\r\n\r\n  Number of program headers:         3\r\n\r\n  Size of section headers:           64 (bytes)\r\n\r\n  Number of section headers:         14\r\n\r\n  Section header string table index: 11\r\n\r\n\r\n\r\nThe entry point address is set to 0x10000, which is the beginning \r\nof .text section. Using objdump to examine the address:\r\n\r\n\r\n\r\n$ objdump -z -M intel -S -D prog | less\r\n\r\n\r\n\r\nwe see that the address 0x10000 does not start at main function \r\nwhen the program runs:\r\n\r\n\r\n\r\nDisassembly of section .text:\r\n\r\n00010000 <test>:\r\n\r\nint a = 5;\r\n\r\nint i;\r\n\r\nvoid test(){}\r\n\r\n   10000:       55                      push   ebp\r\n\r\n   10001:       89 e5                   mov    ebp,esp\r\n\r\n   10003:       90                      nop\r\n\r\n   10004:       5d                      pop    ebp\r\n\r\n   10005:       c3                      ret    \r\n\r\n00010006 <main>:\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n   10006:       55                      push   ebp\r\n\r\n   10007:       89 e5                   mov    ebp,esp\r\n\r\n    \r\n\r\n    return 0;\r\n\r\n   10009:       b8 00 00 00 00          mov    eax,0x0\r\n\r\n}\r\n\r\n   1000e:       5d                      pop    ebp\r\n\r\n   1000f:       c3                      ret    \r\n\r\n\r\n\r\nThe start of .text section at 0x10000 is the function test, not \r\nmain! To enable the program to run at main properly, we need to \r\nset the entry point in the linker script with the following line \r\nat the beginning of the file:\r\n\r\nENTRY(main)\r\n\r\nRecompile the executable binary file main again. This time, the \r\noutput from readelf is different:\r\n\r\n\r\n\r\nELF Header:\r\n\r\n  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 \r\n\r\n  Class:                             ELF32\r\n\r\n  Data:                              2's complement, little \r\nendian\r\n\r\n  Version:                           1 (current)\r\n\r\n  OS/ABI:                            UNIX - System V\r\n\r\n  ABI Version:                       0\r\n\r\n  Type:                              EXEC (Executable file)\r\n\r\n  Machine:                           Intel 80386\r\n\r\n  Version:                           0x1\r\n\r\n  Entry point address:               0x10006\r\n\r\n  Start of program headers:          52 (bytes into file)\r\n\r\n  Start of section headers:          9168 (bytes into file)\r\n\r\n  Flags:                             0x0\r\n\r\n  Size of this header:               52 (bytes)\r\n\r\n  Size of program headers:           32 (bytes)\r\n\r\n  Number of program headers:         3\r\n\r\n  Size of section headers:           40 (bytes)\r\n\r\n  Number of section headers:         14\r\n\r\n  Section header string table index: 11\r\n\r\n\r\n\r\nThe program now executes code at the address 0x10006 when it \r\nstarts. 0x10006 is where main starts! To make sure we really \r\nstarts at main, we run the program with gdb, set two breakpoints \r\nat main and test functions:\r\n\r\n\r\n\r\n$ gdb ./main\r\n\r\n\r\n\r\n\r\n\r\n.... output omitted ....\r\n\r\nReading symbols from ./main...done.\r\n\r\n\r\n\r\n\r\n\r\n(gdb) b test\r\n\r\n\r\n\r\n\r\n\r\nBreakpoint 1 at 0x10003: file main.c, line 1.\r\n\r\n\r\n\r\n\r\n\r\n(gdb) b main\r\n\r\n\r\n\r\n\r\n\r\nBreakpoint 2 at 0x10009: file main.c, line 5.\r\n\r\n\r\n\r\n\r\n\r\n(gdb) r\r\n\r\n\r\n\r\n\r\n\r\nStarting program: /tmp/main \r\n\r\nBreakpoint 2, main (argc=-11493, argv=0x0) at main.c:5\r\n\r\n5\t    return 0;\r\n\r\n\r\n\r\nAs displayed in the output, gdb stopped at the 2[superscript:nd] breakpoint first. Now, we run the program normally, without gdb: \r\n\r\n\r\n\r\n$ ./main\r\n\r\nSegmentation fault \r\n\r\n\r\n\r\nWe still get a segmentation fault. It is to be expected, as we \r\nran a custom binary without C runtime support from the operating \r\nsystem. The last statement in the main function: return 0, simply \r\nreturns to a random place[footnote:\r\nReturn address is above the current ebp. However, when we enter \r\nmain, no return value is pushed on the stack. So, when return is \r\nexecuted, it simply retrieves any value above ebp and use as a \r\nreturn address.\r\n]. The C runtime ensures that the program exit properly. In \r\nLinux, the _exit() function is implicitly called when main \r\nreturns. To fix this problem, we simply change the program to \r\nexit properly:\r\n\r\nvoid test() {}\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    asm(\"mov eax, 0x1\\n\"\r\n\r\n        \"mov ebx, 0x0\\n\"\r\n\r\n        \"int 0x80\");\r\n\r\n}\r\n\r\nInline assembly is required because interrupt 0x80 is defined for \r\nsystem calls in Linux. Since the program uses no library, there \r\nis no other way to call system functions, aside from using \r\nassembly. However, when writing our operating system, we will not \r\nneed such code, as there is no environment for exiting properly \r\nyet.\r\n\r\nNow that we can precisely control where the program runs \r\ninitially, it is easy to bootstrap the kernel from the \r\nbootloader. Before we move on to the next section, note how \r\nreadelf and objdump can be applied to debug a program even before \r\nit runs.\r\n\r\n  Understand the custom ELF structure\r\n\r\nIn the example, we manage to create a runnable ELF executable \r\nbinary from a custom linker script, as opposed to the default one \r\nprovided by gcc. To make it convenient to look into its \r\nstructure:\r\n\r\n\r\n\r\n$ readelf -e main\r\n\r\n\r\n\r\n-e option is the combination of 3 options -h -l -S: \r\n\r\n\r\n\r\n....... ELF header output omitted .......\r\n\r\nSection Headers:\r\n\r\n  [Nr] Name              Type            Addr     Off    Size   \r\nES Flg Lk Inf Al\r\n\r\n  [ 0]                   NULL            00000000 000000 000000 \r\n00      0   0  0\r\n\r\n  [ 1] .text             PROGBITS        00010000 001000 000010 \r\n00  AX  0   0  1\r\n\r\n  [ 2] .eh_frame         PROGBITS        00010010 001010 000058 \r\n00   A  0   0  4\r\n\r\n  [ 3] .debug_info       PROGBITS        00000000 001068 000087 \r\n00      0   0  1\r\n\r\n  [ 4] .debug_abbrev     PROGBITS        00000000 0010ef 000074 \r\n00      0   0  1\r\n\r\n  [ 5] .debug_aranges    PROGBITS        00000000 001163 000020 \r\n00      0   0  1\r\n\r\n  [ 6] .debug_line       PROGBITS        00000000 001183 000038 \r\n00      0   0  1\r\n\r\n  [ 7] .debug_str        PROGBITS        00000000 0011bb 000078 \r\n01  MS  0   0  1\r\n\r\n  [ 8] .comment          PROGBITS        00000000 001233 000034 \r\n01  MS  0   0  1\r\n\r\n  [ 9] .shstrtab         STRTAB          00000000 00133a 000074 \r\n00      0   0  1\r\n\r\n  [10] .symtab           SYMTAB          00000000 001268 0000c0 \r\n10     11  10  4\r\n\r\n  [11] .strtab           STRTAB          00000000 001328 000012 \r\n00      0   0  1\r\n\r\nKey to Flags:\r\n\r\n  W (write), A (alloc), X (execute), M (merge), S (strings)\r\n\r\n  I (info), L (link order), G (group), T (TLS), E (exclude), x \r\n(unknown)\r\n\r\n  O (extra OS processing required) o (OS specific), p (processor \r\nspecific)\r\n\r\nProgram Headers:\r\n\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\nFlg Align\r\n\r\n  LOAD           0x001000 0x00010000 0x00010000 0x00068 0x00068 R \r\nE 0x1000\r\n\r\n  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 \r\nRW  0x10\r\n\r\n Section to Segment mapping:\r\n\r\n  Segment Sections...\r\n\r\n   00     .text .eh_frame \r\n\r\n   01     \r\n\r\n\r\n\r\nThe structure is incredibly simple. Both the segment and section \r\nlistings can be contained within one screen. This is not the case \r\nwith default ELF executable binary. From the output, there are \r\nonly 11 sections, and only two are loaded at runtime: .text and \r\n.eh_frame because both section are assigned with an actual memroy \r\naddresses, 0x10000 and 0x10010 respectively. The remaining \r\nsections are assigned with 0 in the final executable binary[footnote:\r\nAs opposed to the object files, where memory addresses are always \r\n0 and only assigned with actual values in the linking process.\r\n], which mean they are not loaded at runtime. It makes sense, as \r\nthose sections are related to versioning[footnote:\r\nIt is the .comment section. It can be viewed with the comment \r\nreadelf -p .comment main.\r\n], debugging[footnote:\r\nThe ones starts with .debug prefix.\r\n] and linking[footnote:\r\nThe symbol tables and string table.\r\n].\r\n\r\nThe program segment header table is even simpler. It only \r\ncontains 2 segments: LOAD and GNU_STACK. By default, if the \r\nlinker script does not supply the instructions for building \r\nprogram segments, ld provides reasonable default segments. As in \r\nthis case, .text should be in the LOAD segment. GNU_STACK segment \r\nis a GNU extension used by the Linux kernel to control the state \r\nof the program stack. We will not need this segment, along with \r\n.eh_frame, which is for exception handling, as we write our own \r\noperating system from scratch. To achieve these goals, we will \r\nneed to create our own program headers instead of letting ld \r\nhandles the task, and instruct ld to remove .eh_frame.\r\n\r\n  Manipulate the program segments\r\n\r\nFirst, we need to craft our own program header table by using the \r\nfollowing syntax:\r\n\r\nPHDRS\r\n\r\n{\r\n\r\n  <name> <type> [ FILEHDR ] [ PHDRS ] [ AT ( address ) ]\r\n\r\n        [ FLAGS ( flags ) ] ;\r\n\r\n}\r\n\r\nPHDRS command, similar to SECTION command, but for declaring a \r\nlist of custom program segments with a predefined syntax. \r\n\r\n  name is the header name for later referenced by a section \r\n  declared in SECTION command.\r\n\r\n  type is the ELF segment type, as described in section [sec:Program-header-table]\r\n  , with added prefix PT_. For example, instead of NULL or LOAD \r\n  as displayed by readelf, it is PT_NULL or PT_LOAD.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nWith only name and type, we can create any number of program \r\nsegments. For example, we can add the NULL program segment and \r\nremove the GNU_STACK segment:\r\n\r\n  PHDRS\r\n\r\n{\r\n\r\n    null PT_NULL;\r\n\r\n    code PT_LOAD;\r\n\r\n}\r\n\r\n\r\n\r\nSECTIONS\r\n\r\n{\r\n\r\n    . = 0x10000;\r\n\r\n    .text : { *(.text) } :code\r\n\r\n    . = 0x8000000;\r\n\r\n    .data : { *(.data) }\r\n\r\n    .bss : { *(.bss) }\r\n\r\n}\r\n\r\n  The content of PHDRS command tells that the final executable \r\n  binary contains 2 program segments: NULL and LOAD. The NULL \r\n  segment is given the name null and LOAD segment given the name \r\n  code to signify this LOAD segment contains program code. Then, \r\n  to put a section into a segment, we use the syntax :<phdr>, \r\n  where phdr is the name given to a segment earlier. In this \r\n  example, .text section is put into code segment. We compile and \r\n  see the result (assuming main.o compiled earlier remains):\r\n\r\n  \r\n\r\n  $ ld -m elf_i386 -o main -T main.lds main.o\r\n\r\n  $ readelf -l main\r\n\r\n  \r\n\r\n  \r\n\r\n  Elf file type is EXEC (Executable file)\r\n\r\n  Entry point 0x10000\r\n\r\n  There are 2 program headers, starting at offset 52\r\n\r\n  Program Headers:\r\n\r\n    Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\n  Flg Align\r\n\r\n    NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000 \r\n      0x4\r\n\r\n    LOAD           0x001000 0x00010000 0x00010000 0x00010 0x00010 \r\n  R E 0x1000\r\n\r\n   Section to Segment mapping:\r\n\r\n    Segment Sections...\r\n\r\n     00     \r\n\r\n     01     .text .eh_frame\r\n\r\n  \r\n\r\n  Those 2 segments are now NULL and LOAD instead of LOAD and \r\n  GNU_STACK.\r\n\r\n\r\n\r\nWe can add as many segments of the same type, as long as they are \r\ngiven different names:\r\n\r\n  PHDRS\r\n\r\n{\r\n\r\n    null1 PT_NULL;\r\n\r\n    null2 PT_NULL;\r\n\r\n    code1 PT_LOAD;\r\n\r\n    code2 PT_LOAD;\r\n\r\n}\r\n\r\n\r\n\r\nSECTIONS\r\n\r\n{\r\n\r\n    . = 0x10000;\r\n\r\n    .text : { *(.text) } :code1\r\n\r\n    .eh_frame : { *(.eh_frame) } :code2\r\n\r\n    . = 0x8000000;\r\n\r\n    .data : { *(.data) }\r\n\r\n    .bss : { *(.bss) }\r\n\r\n}\r\n\r\n  After amending the PHDRS content earlier with this new segment \r\n  listing, we put .text into code1 segment and .eh_frame into \r\n  code2 segment, we compile and see the new segments:\r\n\r\n  \r\n\r\n  $ ld -m elf_i386 -o main -T main.lds main.o\r\n\r\n  $ readelf -l main\r\n\r\n  \r\n\r\n  \r\n\r\n  Elf file type is EXEC (Executable file)\r\n\r\n  Entry point 0x10000\r\n\r\n  There are 4 program headers, starting at offset 52\r\n\r\n  Program Headers:\r\n\r\n    Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\n  Flg Align\r\n\r\n    NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000 \r\n      0x4\r\n\r\n    NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000 \r\n      0x4\r\n\r\n    LOAD           0x001000 0x00010000 0x00010000 0x00010 0x00010 \r\n  R E 0x1000\r\n\r\n    LOAD           0x001010 0x00010010 0x00010010 0x00058 0x00058 \r\n  R   0x1000\r\n\r\n   Section to Segment mapping:\r\n\r\n    Segment Sections...\r\n\r\n     00     \r\n\r\n     01     \r\n\r\n     02     .text \r\n\r\n     03     .eh_frame \r\n\r\n  \r\n\r\n  Now .text and .eh_frame are in different segments.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n  FILEHDR is an optional keyword, when added specifies that a \r\n  program segment includes the ELF file header of the executable \r\n  binary. However, this attribute should only added for the first \r\n  program segment, as it drastically alters the size and starting \r\n  address of a segment because the ELF header is always at the \r\n  beginning of a binary file, recall that a segment starts at the \r\n  address of its first content, which is in most of the cases \r\n  (except for this case, which is the file header), the first \r\n  section.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nAdding the FILEHDR keyword changes the size of NULL segment:\r\n\r\n  PHDRS\r\n\r\n{\r\n\r\n    null PT_NULL FILEHDR;\r\n\r\n    code PT_LOAD;\r\n\r\n}\r\n\r\n..... content is the same .....\r\n\r\n  We link it again and see the result:\r\n\r\n  \r\n\r\n  $ ld -m elf_i386 -o main -T main.lds main.o\r\n\r\n  $ readelf -l main\r\n\r\n  \r\n\r\n  \r\n\r\n  Elf file type is EXEC (Executable file)\r\n\r\n  Entry point 0x10000\r\n\r\n  There are 2 program headers, starting at offset 52\r\n\r\n  Program Headers:\r\n\r\n    Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\n  Flg Align\r\n\r\n    NULL           0x000000 0x00000000 0x00000000 0x00034 0x00034 \r\n  R   0x4\r\n\r\n    LOAD           0x001000 0x00010000 0x00010000 0x00068 0x00068 \r\n  R E 0x1000\r\n\r\n   Section to Segment mapping:\r\n\r\n    Segment Sections...\r\n\r\n     00     \r\n\r\n     01     .text .eh_frame \r\n\r\n  \r\n\r\n  In previous examples, the file size and memory size of the NULL \r\n  section are always 0, now they are both 34 bytes, which is the \r\n  size of an ELF header.\r\n\r\n\r\n\r\nIf we assign FILEHDR to a non-starting segment, its size and \r\nstarting address changes significantly:\r\n\r\n  PHDRS\r\n\r\n{\r\n\r\n    null PT_NULL;\r\n\r\n    code PT_LOAD FILEHDR;\r\n\r\n}\r\n\r\n..... content is the same .....\r\n\r\n  \r\n\r\n  $ ld -m elf_i386 -o main -T main.lds main.o\r\n\r\n  $ readelf -l main\r\n\r\n  \r\n\r\n  \r\n\r\n  Elf file type is EXEC (Executable file)\r\n\r\n  Entry point 0x10000\r\n\r\n  There are 2 program headers, starting at offset 52\r\n\r\n  Program Headers:\r\n\r\n    Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\n  Flg Align\r\n\r\n    NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000 \r\n      0x4\r\n\r\n    LOAD           0x000000 0x0000f000 0x0000f000 0x01068 0x01068 \r\n  R E 0x1000\r\n\r\n   Section to Segment mapping:\r\n\r\n    Segment Sections...\r\n\r\n     00     \r\n\r\n     01     .text .eh_frame \r\n\r\n  \r\n\r\n  The size of the LOAD segment in the previous example is only \r\n  0x68, the same size as the total sizes of .text and .eh_frame \r\n  sections in it. But now, it is 0x01068, got 0x1000 bytes \r\n  larger. What is the reason for these extra bytes? A simple \r\n  answer: segment alignment. From the output, the alignment of \r\n  this segment is 0x1000; it means that regardless of which \r\n  address is the start of this segment, it must be divisible by \r\n  0x1000. For that reason, the starting address of LOAD is 0xf000 \r\n  because it is divisible by 0x1000.\r\n\r\n  Another question arises: why is the starting address 0xf000 \r\n  instead of 0x10000? .text is the first section, which starts at \r\n  0x10000, so the segment should start at 0x10000. The reason is \r\n  that we include FILEHDR as part of the segment, it must expand \r\n  to include the ELF file header, which is at the very start of \r\n  an ELF executable binary. To satisfy this constraint and the \r\n  alignment constraint, 0xf000 is the closest address. Note that \r\n  the virtual and physical memory addresses are the addresses at \r\n  runtime, not the locations of the segment in the file on disk. \r\n  As the FileSiz field shows, the segment only consumes 0x1068 \r\n  bytes on disk. Figure [LOAD_segment] illustrates the difference \r\n  between the memory layouts with and without FILEHDR keyword.\r\n\r\n  [float Figure:\r\n[Figure 0.22:\r\nLOAD segment on disk and in memory.\r\n]<LOAD_segment>\r\n\r\n[float Figure:\r\n[Sub-Figure a:\r\nWithout FILEHDR.\r\n]\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/08/no_FILEHDR.pdf>\r\n]\r\n\r\n[float Figure:\r\n[Sub-Figure b:\r\nWith FILEHDR.\r\n]\r\n\r\n<Graphics file: C:/Users/Tu Do/os01/book_src/images/08/with_FILEHDR.pdf>\r\n]\r\n]\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n  PHDRS is an optional keyword, when added specifies that a \r\n  program segment is a program segment header table.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nThe first segment of the default executable binary generated by \r\ngcc is a PHDR since the program segment header table appears \r\nright after the ELF header. It is also a convenient segment to \r\nput the ELF header into using the FILEHDR keyword. We replace the \r\nunused NULL segment earlier with a PHDR segment:\r\n\r\n  PHDRS\r\n\r\n{\r\n\r\n    headers PT_PHDR FILEHDR PHDRS;\r\n\r\n    code PT_LOAD FILEHDR;\r\n\r\n}\r\n\r\n..... content is the same .....\r\n\r\n  \r\n\r\n  $ ld -m elf_i386 -o main -T main.lds main.o\r\n\r\n  $ readelf -l main\r\n\r\n  \r\n\r\n  \r\n\r\n  Elf file type is EXEC (Executable file)\r\n\r\n  Entry point 0x10000\r\n\r\n  There are 2 program headers, starting at offset 52\r\n\r\n  Program Headers:\r\n\r\n    Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\n  Flg Align\r\n\r\n    PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 \r\n  R   0x4\r\n\r\n    LOAD           0x001000 0x00010000 0x00010000 0x00068 0x00068 \r\n  R E 0x1000\r\n\r\n   Section to Segment mapping:\r\n\r\n    Segment Sections...\r\n\r\n     00     \r\n\r\n     01     .text .eh_frame \r\n\r\n  \r\n\r\n  As shown in the output, the first segment is of type PHDR. Its \r\n  size is 0x74, which includes:\r\n\r\n  • 0x34 bytes for ELF header.\r\n\r\n  • 0x40 bytes for the program segment header table, with 2 \r\n    entries, each is 0x20 bytes (32 bytes) in length.\r\n\r\n  The above number is consistent with ELF header output:\r\n\r\n  \r\n\r\n  ELF Header:\r\n\r\n    Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 \r\n\r\n    Class:                             ELF32\r\n\r\n  ....... output omitted ......\r\n\r\n    Size of this header:               52 (bytes)   --> 0x34 \r\n  bytes\r\n\r\n    Size of program headers:           32 (bytes)   --> 0x20 \r\n  bytes each program header\r\n\r\n    Number of program headers:         2            --> 0x40 \r\n  bytes in total\r\n\r\n    Size of section headers:           40 (bytes)  \r\n\r\n    Number of section headers:         12\r\n\r\n    Section header string table index: 9\r\n\r\n  \r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n  AT ( address ) specifies the load memory address where the \r\n  segment is placed. Every segment or section has a virtual \r\n  memory address and a load memory address:\r\n\r\n  • A [margin:\r\nvirtual memory address\r\n]virtual memory addressvirtual memory address is a starting \r\n    address of a segment or a section when a program is in memory \r\n    and running. The memory address is called virtual because it \r\n    does not map to the actual memory cell that corresponds to \r\n    the address number, but any random memory cell, which depends \r\n    on how the underlying operating system translates the \r\n    address. For example, the virtual memory address 0x1 might \r\n    map to the memory cell with the physical address 0x1000.\r\n\r\n  • A load memory address[margin:\r\nload memory address\r\n]load memory address is the physical memory address, where a \r\n    program is loaded but not yet running.\r\n\r\n  The load memory address is specified by AT syntax. Normally \r\n  both types of addresses are the same, and the physical address \r\n  can be ignored. They differ when loading and running are \r\n  purposely divided into two distinct phases that require \r\n  different address regions. \r\n\r\n  For example, a program can be designed to load into a ROM[footnote:\r\nRead-Only Memory\r\n] at a fixed address. But when loading into RAM for a bare-metal \r\n  application or an operating system to use, the program needs a \r\n  load address that accommodates the addressing scheme of the \r\n  target application or operating system. \r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nWe can specify a load memory address for the segment LOAD with AT \r\nsyntax:\r\n\r\n  PHDRS\r\n\r\n{\r\n\r\n    headers PT_PHDR FILEHDR PHDRS AT(0x500);\r\n\r\n    code PT_LOAD;\r\n\r\n}\r\n\r\n..... content is the same .....\r\n\r\n  \r\n\r\n  $ ld -m elf_i386 -o main -T main.lds main.o\r\n\r\n  $ readelf -l main\r\n\r\n  \r\n\r\n  \r\n\r\n  Elf file type is EXEC (Executable file)\r\n\r\n  Entry point 0x4000\r\n\r\n  There are 2 program headers, starting at offset 52\r\n\r\n  Program Headers:\r\n\r\n    Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\n  Flg Align\r\n\r\n    PHDR           0x000000 0x00000000 0x00000500 0x00074 0x00074 \r\n  R   0x4\r\n\r\n    LOAD           0x001000 0x00004000 0x00002000 0x00068 0x00068 \r\n  R E 0x1000\r\n\r\n   Section to Segment mapping:\r\n\r\n    Segment Sections...\r\n\r\n     00     \r\n\r\n     01     .text .eh_frame \r\n\r\n  \r\n\r\n  It depends on an operating system whether to use the address or \r\n  not. For our operating system, the virtual memory address and \r\n  load are the same, so an explicit load address is none of our \r\n  concern.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\n  FLAGS (flags) assigns permissions to a segment. Each flag is an \r\n  integer that represents a permission and can be combined with \r\n  OR operations. Possible values:\r\n\r\n  \r\n+-------------+--------+-------------+\r\n  | Permission  | Value  | Description |\r\n  +-------------+--------+-------------+\r\n  +-------------+--------+-------------+\r\n  | R           | 1      | Readable    |\r\n  +-------------+--------+-------------+\r\n  | W           | 2      | Writable    |\r\n  +-------------+--------+-------------+\r\n  | E           | 4      | Executable  |\r\n  +-------------+--------+-------------+\r\n  \r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nWe can create a LOAD segment with Read, Write and Execute \r\npermissions enabled:\r\n\r\n  PHDRS\r\n\r\n{\r\n\r\n    headers PT_PHDR FILEHDR PHDRS AT(0x500);\r\n\r\n    code PT_LOAD FILEHDR FLAGS(0x1 | 0x2 | 0x4);\r\n\r\n}\r\n\r\n..... content is the same .....\r\n\r\n  \r\n\r\n  $ ld -m elf_i386 -o main -T main.lds main.o\r\n\r\n  $ readelf -l main\r\n\r\n  \r\n\r\n  \r\n\r\n  Elf file type is EXEC (Executable file)\r\n\r\n  Entry point 0x0\r\n\r\n  There are 2 program headers, starting at offset 52\r\n\r\n  Program Headers:\r\n\r\n    Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\n  Flg Align\r\n\r\n    PHDR           0x000000 0x00000000 0x00000500 0x00074 0x00074 \r\n  R   0x4\r\n\r\n    LOAD           0x001000 0x00000000 0x00000000 0x00010 0x00010 \r\n  RWE 0x1000\r\n\r\n   Section to Segment mapping:\r\n\r\n    Segment Sections...\r\n\r\n     00     \r\n\r\n     01     .text .eh_frame\r\n\r\n  \r\n\r\n  LOAD segment now gets all the RWE permissions, as shown above.\r\n\r\n\r\n-------------------------------------------\r\n\r\n\r\nFinally, we want to remove the .eh_frame or any unwanted section, \r\nwe add a special section called /DISCARD/:\r\n\r\n... program segment header table remains the same ...\r\n\r\n\r\n\r\nSECTIONS\r\n\r\n{\r\n\r\n    /* . = 0x10000; */\r\n\r\n    .text : { *(.text) } :code\r\n\r\n    . = 0x8000000;\r\n\r\n    .data : { *(.data) }\r\n\r\n    .bss : { *(.bss) }\r\n\r\n    /DISCARD/ : { *(.eh_frame) }\r\n\r\n}\r\n\r\nAny section putting in /DISCARD/ disappears in the final \r\nexecutable binary:\r\n\r\n\r\n\r\n$ ld -m elf_i386 -o main -T main.lds main.o\r\n\r\n$ readelf -l main\r\n\r\n\r\n\r\n\r\n\r\nElf file type is EXEC (Executable file)\r\n\r\nEntry point 0x0\r\n\r\nThere are 2 program headers, starting at offset 52\r\n\r\nProgram Headers:\r\n\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\nFlg Align\r\n\r\n  PHDR           0x000000 0x00000000 0x00000500 0x00074 0x00074 R \r\n  0x4\r\n\r\n  LOAD           0x001000 0x00000000 0x00000000 0x00010 0x00010 R \r\nE 0x1000\r\n\r\n Section to Segment mapping:\r\n\r\n  Segment Sections...\r\n\r\n   00     \r\n\r\n   01     .text \r\n\r\n\r\n\r\nAs can be seen, .eh_frame is nowhere to be found.\r\n\r\n  C Runtime: Hosted vs Freestanding<sec:C-Runtime>\r\n\r\nThe purpose of .init, .init_array, .fini_array and .preinit_array \r\nsection is to initialize a C Runtime environment that supports \r\nthe C standard libraries. Why does C need a runtime environment, \r\nwhen it is supposed to be a compiled language? The reason is that \r\nmany of the standard functions depend on the underlying operating \r\nsystem, which is of itself a big runtime environment. For \r\nexample, I/O related functions such as reading from keyboard with \r\ngets(), reading from file with open(), printing on screen with \r\nprintf(), managing system memory with malloc(), free(), etc. \r\n\r\nA C implementation cannot provide such routines without a running \r\noperating system, which is a hosted environment. A hosted environment\r\nhosted environment is a runtime environment that:\r\n\r\n• provides a default implementation of C libraries that includes \r\n  system-dependent data and routines.\r\n\r\n• perform resource allocations to prepare an environment for a \r\n  program to run. \r\n\r\nThis process is similar to the hardware initialization process:\r\n\r\n• When first powered up, a desktop computer loads its basic \r\n  system routines from a read-only memory stored on the \r\n  motherboard.\r\n\r\n• Then, it starts initializing an environment, such as setting \r\n  default values for various registers in CPU and devices, before \r\n  executing the any code. \r\n\r\nIn contrast, a freestanding environmentfreestanding environment \r\nis an environment that does not provide system-dependent data and \r\nroutines. As a consequence, almost no C library exists and the \r\nenvironment can run code compiled written from pure C syntax. For \r\na free standing environment to become a host environment, it must \r\nimplement standard C system routines. But for a conforming \r\nfreestanding environment, it only needs these header files \r\navailable: <float.h>, <limits.h>, <stadarg.h> and <stddef.h> \r\n(according to GCC manual).\r\n\r\nFor a typical desktop x86 program, C runtime environment is \r\ninitialized by a compiler so a program runs normal. However, for \r\nan embedded platform where a program runs directly on it, this is \r\nnot the case. The typical C runtime environment used in desktop \r\noperating systems cannot be used on the embedded platforms, \r\nbecause architectural differences and resource constraints. As \r\nsuch, the software writer must implement a custom C runtime \r\nenvironment suitable for the targeted platform. For the embedded \r\nplatform, \r\n\r\nIn writing our operating system, the first step is to create a \r\nfreestanding environment before creating a hosted one.\r\n\r\n  Debuggable bootloader on bare metal\r\n\r\nCurrently, the bootloader is compiled as a flat binary file. \r\nAlthough gdb can display the assembly code, it is not always the \r\nsame as the source code. In the assembly source code, there \r\nexists variable names and labels. These symbols are lost when \r\ncompiled as a flat binary file, making debugging more difficult. \r\nAnother issue is the mismatch between the written assembly source \r\ncode and the displayed assembly source code. The written code \r\nmight contain higher level syntax that is assembler-specific and \r\nis generated into lower-level assembly code as displayed by gdb. \r\nFinally, with debug information available, the command next/n and \r\nprev/p can be used instead of ni and si.\r\n\r\nTo enable debug information, we modify the bootloader Makefile:\r\n\r\n1. The bootloader must be compiled as a ELF binary. Open the \r\n  Makefile in bootloader/ directory and change this line under \r\n  $(BUILD_DIR)/%.o: %.asm recipe:\r\n\r\n  nasm -f bin $< -o $@\r\n\r\n  to this line:\r\n\r\n  nasm -f elf $< -F dwarf -g -o $@\r\n\r\n  In the updated recipe, bin format is replaced with elf format \r\n  to enable debugging information to be properly produced.-F \r\n  option specifies the debug information format, which is dwarf \r\n  in this case. Finally, -g option causes nasm to actually \r\n  generate debug information in selected format.\r\n\r\n2. Then, ld consumes the ELF bootloader binary and produces \r\n  another ELF bootloader binary, with proper starting memory \r\n  address of .text section that match the actual address of the \r\n  bootloader at runtime, when QEMU virtual machine loads it at \r\n  0x7c00. We need ld because when compiled by nasm, the starting \r\n  address is assumed to be 0, not 0x7c00.\r\n\r\n3. Finally, we use objcopy to separate extract only the flat \r\n  binary content as the original bootloader by adding this line \r\n  to $(BUILD_DIR)/%.o: %.asm:\r\n\r\n  objcopy -O binary $(BUILD_DIR)/bootloader.o.elf $@\r\n\r\n  objcopy, as its name implies, is a program that copies and \r\n  translates object files. Here, we copy the original ELF \r\n  bootloader and translate it into a flat binary file.\r\n\r\nThe updated recipe should look like:\r\n\r\n$(BUILD_DIR)/%.o: %.asm\r\n\r\n\tnasm -f elf $< -F dwarf -g -o $@\r\n\r\n\tld -m elf_i386 -T bootloader.lds $@ -o $@.elf\r\n\r\n\tobjcopy -O binary  $(BUILD_DIR)/bootloader.o.elf $@\r\n\r\nNow we test the bootloader with debug information available:\r\n\r\n1. Start the QEMU machine:\r\n\r\n  \r\n\r\n  $ make qemu\r\n\r\n  \r\n\r\n2. Start gdb with the debug information stored in \r\n  bootloader.o.elf:\r\n\r\n  \r\n\r\n  $ gdb build/bootloader/bootloader.o.elf\r\n\r\n  \r\n\r\n  After getting into gdb, press the Enter key and if the sample \r\n  .gdbinit section [sub:Automate-debugging-steps] is used, the \r\n  output should look like:\r\n\r\n  \r\n\r\n  ---Type <return> to continue, or q <return> to quit---\r\n\r\n  [f000:fff0] 0x0000fff0 in ?? ()\r\n\r\n  Breakpoint 1 at 0x7c00: file bootloader.asm, line 6.\r\n\r\n  (gdb)\r\n\r\n  \r\n\r\n  gdb now understand where the instruction at address 0x7c00 is \r\n  in the assembly source file, thanks to the debug information.\r\n\r\n  Debuggable program on bare metal\r\n\r\nThe process of building a debug-ready executable binary is \r\nsimilar to that of a bootloader, except more involved. Recall \r\nthat for a debugger to work properly, its debugging information \r\nmust contain correct address mappings between memory addresses \r\nand the source code. gcc stores such mapping information in DIE \r\nentries, in which it tells gdb at which code address corresponds \r\nto a line in a source file, so that breakpoints work properly.\r\n\r\nBut first, we need a sample C source file, a very simple one:\r\n\r\nvoid main() {}\r\n\r\nBecause this is a free standing environment, standard libraries \r\nthat involve system functions such as printf() would not work, \r\nbecause a C runtime does not exist. At this stage, the goal is to \r\ncorrectly jump to main with source code displayed properly in \r\ngdb, so no fancy C code is needed yet.\r\n\r\nThe next step is updating os/Makefile:\r\n\r\nBUILD_DIR=../build_os\r\n\r\nOS=$(BUILD_DIR)/os\r\n\r\n\r\n\r\n@|\\color{red}\\bfseries CFLAGS+=-ffreestanding -nostdlib -gdwarf-4 \r\n-m32 -ggdb3|@\r\n\r\n\r\n\r\nOS_SRCS := $(wildcard *.c)\r\n\r\nOS_OBJS := $(patsubst %.c, $(BUILD_DIR)/%.o, $(OS_SRCS))\r\n\r\n\r\n\r\nall: $(OS)\r\n\r\n\r\n\r\n$(BUILD_DIR)/%.o: %.c\r\n\r\n\tgcc $(CFLAGS) -m32 -c  $< -o $@\r\n\r\n\r\n\r\n$(OS): $(OS_OBJS)\r\n\r\n\tld -m elf_i386 -T@|\\color{blue}\\bfseries os.lds|@ $(OS_OBJS) -o \r\n$@\r\n\r\n\r\n\r\nclean:\r\n\r\n\trm $(OS_OBJS)\r\n\r\nWe updated the Makefile with the following changes:\r\n\r\n• Add a CFLAGS variable for passing options to gcc.\r\n\r\n• Instead of the rule to build assembly source code earlier, it \r\n  is replaced with a C version with a recipe to build C source \r\n  files. The CFLAGS variable makes the gcc command in the recipe \r\n  looks cleaner regardless how many options are added.\r\n\r\n• Add a linking command for building the final executable binary \r\n  of the operating system with a custom linker script os.lds.\r\n\r\nEverything looks good, except for the linker script part. Why is \r\nit needed? The linker script is required for controlling at which \r\nphysical memory address the operating system binary appears in \r\nthe memory, so the linker can jump to the operating system code \r\nand execute it. To complete this requirement, the default linker \r\nscript used by gcc would not work as it assumes the compiled \r\nexecutable runs inside an existing operating system, while we are \r\nwriting an operating system itself.\r\n\r\nThe next question is, what will be the content in the linker \r\nscript? To answer this question, we must understand what goals to \r\nachieve with the linker script:\r\n\r\n• For the bootloader to correctly jump to and execute the \r\n  operating system code.\r\n\r\n• For gdb to debug correctly with the operating system source \r\n  code.\r\n\r\nTo achieve the goals, we must devise a design of a suitable \r\nmemory layout for the operating system. Recall that the \r\nbootloader developed in chapter [chap:Bootloader] can already \r\nload a simple binary compiled from the sample Assembly program \r\nsample.asm. To load the operating system, we can simply throw \r\nbinary compiled from sample.asm with the binary compiled from \r\nos.c above.\r\n\r\nIf only it is that simple. The idea is correctly, but not enough. \r\nThe goals implies the following constraints:\r\n\r\n1. The operating system code is written in C and compiled as an \r\n  ELF executable binary. It means, the bootloader needs to \r\n  retrieve correct entry address from the ELF header.\r\n\r\n2. To debug properly with gdb, the debug info must contain \r\n  correct mappings between instruction addresses and source code.\r\n\r\nThanks to the understanding of ELF and DWARF acquire in the \r\nearlier chapters, we can certainly modify the bootloader and \r\ncreate an executable binary that satisfy the above constraint. We \r\nwill solve these problems one by one.\r\n\r\n  Loading an ELF binary from a bootloader\r\n\r\nEarlier we examined that an ELF header contains a entry address \r\nof a program. That information is 0x18 bytes away from the \r\nbeginning of an ELF header, according to man elf :\r\n\r\ntypedef struct {\r\n\r\n               unsigned char e_ident[EI_NIDENT];\r\n\r\n               uint16_t      e_type;\r\n\r\n               uint16_t      e_machine;\r\n\r\n               uint32_t      e_version;\r\n\r\n               ElfN_Addr     e_entry;\r\n\r\n               ElfN_Off      e_phoff;\r\n\r\n               ElfN_Off      e_shoff;\r\n\r\n               uint32_t      e_flags;\r\n\r\n               uint16_t      e_ehsize;\r\n\r\n               uint16_t      e_phentsize;\r\n\r\n               uint16_t      e_phnum;\r\n\r\n               uint16_t      e_shentsize;\r\n\r\n               uint16_t      e_shnum;\r\n\r\n               uint16_t      e_shstrndx;\r\n\r\n           } ElfN_Ehdr;\r\n\r\nThe offset from the start of the struct to the start of e_entry \r\nis:\r\n\r\n• 16 bytes of e_ident[EI_NIDENT]:\r\n\r\n  #define EI_NIDENT 16\r\n\r\n• 2 bytes of e_type\r\n\r\n• 2 bytes of e_machine\r\n\r\n• 4 bytes of e_version\r\n\r\n\\mathtt{Offset=16+2+2+4=24=0x18}\r\n\r\n\r\ne_entry is of type ElfN_Addr, in which N is either 32 or 64. We \r\nare writing 32-bit operating system, in this case N=32\r\n and so \r\nElfN_Addr is Elf32_Addr, which is 4 bytes long.\r\n\r\nWith any program, such as this simple one:\r\n\r\n  #include <stdio.h>\r\n\r\n\r\n\r\nint main(int argc, char *argv[])\r\n\r\n{\r\n\r\n    printf(\"hello world!\\n\");\r\n\r\n    return 0;\r\n\r\n}\r\n\r\n  We can retrieve the entry address with a human-readable \r\n  presentation using readelf:\r\n\r\n  \r\n\r\n  $ gcc hello.c -o hello\r\n\r\n  $ readelf -h hello\r\n\r\n  \r\n\r\n  \r\n\r\n  ELF Header:\r\n\r\n    Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 \r\n\r\n    .... output omitted ....\r\n\r\n    Entry point address:               0x400430\r\n\r\n    .... output omitted ....\r\n\r\n  \r\n\r\n  Or in raw binary with hd:\r\n\r\n  \r\n\r\n  $ hd hello | less\r\n\r\n  \r\n\r\n  \r\n\r\n  00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  \r\n  |.ELF............|\r\n\r\n  00000010  02 00 3e 00 01 00 00 00  30 04 40 00 00 00 00 00  \r\n  |..>.....0.@.....|\r\n\r\n  .........\r\n\r\n  \r\n\r\n  The offset 0x18 is the start of the least-significant byte of \r\n  e_entry, which is 0x30, followed by 04 40 00, together in \r\n  reverse makes the address 0x00400430.\r\n\r\nNow that we know where the position of the entry address in the \r\nELF header, it is easy to modify the bootloader made in section [sub:Read-and-load]\r\n to retrieve and jump to the address:\r\n\r\n;******************************************\r\n\r\n; Bootloader.asm\r\n\r\n; A Simple Bootloader\r\n\r\n;******************************************\r\n\r\nbits 16\r\n\r\nstart: jmp boot\r\n\r\n\r\n\r\n;; constant and variable definitions\r\n\r\nmsg\tdb\t\"Welcome to My Operating System!\", 0ah, 0dh, 0h\r\n\r\n\r\n\r\nboot:\r\n\r\n  cli\t; no interrupts\r\n\r\n  cld\t; all that we need to init\r\n\r\n\r\n\r\n  mov\t\tax, 50h\r\n\r\n\r\n\r\n  ;; set the buffer\r\n\r\n  mov\tes, ax\r\n\r\n  xor\tbx, bx\r\n\r\n\r\n\r\n  mov\tal, 2\t\t\t\t\t      ; read 2 sector\r\n\r\n  mov\tch, 0                          ; we are reading the second \r\nsector past us,\r\n\r\n                                        ; so its still on track 0\r\n\r\n  mov\tcl, 2\t\t\t\t\t      ; sector to read (The second sector)\r\n\r\n  mov\tdh, 0\t\t\t\t\t      ; head number\r\n\r\n  mov\tdl, 0\t\t\t\t\t      ; drive number. Remember Drive 0 is floppy \r\ndrive.\r\n\r\n\r\n\r\n  mov\tah, 0x02\t\t\t           ; read floppy sector function\r\n\r\n  int\t0x13\t\t\t\t\t       ; call BIOS - Read the sector\r\n\r\n  jmp\t@|\\color{blue}\\bfseries [500h + 18h]|@\t\t\t\t  ; jump and \r\nexecute the sector!\r\n\r\n\r\n\r\n  hlt\t; halt the system\r\n\r\n\r\n\r\n  ; We have to be 512 bytes. Clear the rest of the bytes with 0\r\n\r\n  times 510 - ($-$$) db 0\r\n\r\n  dw 0xAA55\t\t\t\t  ; Boot Signiture\r\n\r\nIt is as simple as that! First, we load the operating system \r\nbinary at 0x500, then we retrieve the entry address at the offset \r\n0x18 from 0x500, by first calculating the expression 500h+18h=518h\r\n\r\n to get the actual in-memory address, then retrieve the content \r\nby dereference it.\r\n\r\nThe first part is done. For the next part, we need to build an \r\nELF operating system image for the bootloader to load. The first \r\nstep is to create a linker script:\r\n\r\n@|\\color{blue}\\bfseries ENTRY(main);|@\r\n\r\n\r\n\r\nPHDRS\r\n\r\n{\r\n\r\n  headers PT_PHDR FILEHDR PHDRS;\r\n\r\n  code PT_LOAD;\r\n\r\n}\r\n\r\n\r\n\r\nSECTIONS\r\n\r\n{\r\n\r\n  .text @|\\color{blue}\\bfseries 0x500|@: { *(.text)  } :code\r\n\r\n  .data :  { *(.data)  }\r\n\r\n  .bss :  { *(.bss) }\r\n\r\n  /DISCARD/ : { *(.eh_frame) }\r\n\r\n}\r\n\r\nThe script is straight-forward and remains almost the same as \r\nbefore. The only differences are:\r\n\r\n• main are explicitly specified as the entry point by specifying \r\n  ENTRY(main).\r\n\r\n• .text is explicitly specified with 0x500 as its virtual memory \r\n  address since we load the operating system image at 0x500.\r\n\r\nAfter putting the script, we compile with make and it should work \r\nsmoothly:\r\n\r\n\r\n\r\n$ make clean; make\r\n\r\n$ readelf -l  build/os/os \r\n\r\n\r\n\r\n\r\n\r\nElf file type is EXEC (Executable file)\r\n\r\nEntry point 0x500\r\n\r\nThere are 2 program headers, starting at offset 52\r\n\r\nProgram Headers:\r\n\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\nFlg Align\r\n\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R \r\n  0x4\r\n\r\n  LOAD           0x000500 0x00000500 0x00000500 0x00040 0x00040 R \r\nE 0x1000\r\n\r\n Section to Segment mapping:\r\n\r\n  Segment Sections...\r\n\r\n   00     \r\n\r\n   01     .text\r\n\r\n\r\n\r\nAll looks good, until we run it. We begin by starting the QEMU \r\nvirtual machine:\r\n\r\n\r\n\r\n$ make qemu\r\n\r\n\r\n\r\nThen, start gdb and load the debug info (which is also in the \r\nsame binary file) and set a breakpoint at main:\r\n\r\n\r\n\r\n(gdb) symbol-file build/os/os\r\n\r\nReading symbols from build/os/os...done.\r\n\r\n(gdb) b main\r\n\r\nBreakpoint 2 at 0x500\r\n\r\n\r\n\r\nThen we start the program:\r\n\r\n\r\n\r\n(gdb) symbol-file build/os/os\r\n\r\nReading symbols from build/os/os...done.\r\n\r\n(gdb) b main\r\n\r\nBreakpoint 2 at 0x500\r\n\r\n\r\n\r\nKeep the programming running until it stops at main:\r\n\r\n\r\n\r\n(gdb) c\r\n\r\nContinuing.\r\n\r\n[ 0:7c00] \r\n\r\nBreakpoint 1, 0x00007c00 in ?? ()\r\n\r\n(gdb) c\r\n\r\nContinuing.\r\n\r\n[ 0: 500] \r\n\r\nBreakpoint 2, main () at main.c:1\r\n\r\n\r\n\r\nAt this point, we switch the layout to the C source code instead \r\nof the registers:\r\n\r\n\r\n\r\n(gdb) layout split\r\n\r\n\r\n\r\nlayout split creates a layout that consists of 3 smaller windows: \r\n\r\n• Source window at the top.\r\n\r\n• Assembly window in the middle.\r\n\r\n• Command window at the bottom.\r\n\r\nAfter the command, the layout should look like this:\r\n\r\n\r\n\r\n   \r\n┌──main.c───────────────────────────────────────────────────────┐\r\n\r\nB+>│1       void main(){}                                         \r\n │\r\n\r\n   │2                                                             \r\n │\r\n\r\n   │3                                                             \r\n │\r\n\r\n   │4                                                             \r\n │\r\n\r\n   │5                                                             \r\n │\r\n\r\n   │6                                                             \r\n │\r\n\r\n   │7                                                             \r\n │\r\n\r\n   │8                                                             \r\n │\r\n\r\n   │9                                                             \r\n │\r\n\r\n   │10                                                            \r\n │\r\n\r\n   │11                                                            \r\n │\r\n\r\n   │12                                                            \r\n │\r\n\r\n   │13                                                            \r\n │\r\n\r\n   │14                                                            \r\n │\r\n\r\n   │15                                                            \r\n │\r\n\r\n   │16                                                            \r\n │\r\n\r\n   \r\n└───────────────────────────────────────────────────────────────┘\r\n\r\nB+>│0x500 <main>    jg     0x547                                  \r\n │\r\n\r\n   │0x502 <main+2>  dec    sp                                     \r\n │\r\n\r\n   │0x503 <main+3>  inc    si                                     \r\n │\r\n\r\n   │0x504 <main+4>  add    WORD PTR [bx+di],ax                    \r\n │\r\n\r\n   │0x506           add    WORD PTR [bx+si],ax                    \r\n │\r\n\r\n   │0x508           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   │0x50a           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   │0x50c           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   │0x50e           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   │0x510           add    al,BYTE PTR [bx+si]                    \r\n │\r\n\r\n   │0x512           add    ax,WORD PTR [bx+si]                    \r\n │\r\n\r\n   │0x514           add    WORD PTR [bx+si],ax                    \r\n │\r\n\r\n   │0x516           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   │0x518           add    BYTE PTR [di],al                       \r\n │\r\n\r\n   │0x51a           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   │0x51c           xor    al,0x0                                 \r\n │\r\n\r\n   │0x51e           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   \r\n└───────────────────────────────────────────────────────────────┘\r\n\r\nremote Thread 1 In: main                            L1    PC: \r\n0x500 \r\n\r\n[f000:fff0] 0x0000fff0 in ?? ()\r\n\r\nBreakpoint 1 at 0x7c00\r\n\r\n(gdb) symbol-file build/os/os\r\n\r\nReading symbols from build/os/os...done.\r\n\r\n(gdb) b main\r\n\r\nBreakpoint 2 at 0x500: file main.c, line 1.\r\n\r\n(gdb) c\r\n\r\nContinuing.\r\n\r\n[   0:7c00] \r\n\r\nBreakpoint 1, 0x00007c00 in ?? ()\r\n\r\n(gdb) c\r\n\r\nContinuing.\r\n\r\n[   0: 500] \r\n\r\nBreakpoint 2, main () at main.c:1\r\n\r\n(gdb) layout split\r\n\r\n(gdb) \r\n\r\n\r\n\r\nSomething wrong is going on here. It is not the generated \r\nassembly code for function call as it is known in section [sub:Function-Call-and]\r\n. It is definitely wrong, verified with objdump:\r\n\r\n\r\n\r\n$ objdump -D build/os/os | less\r\n\r\n\r\n\r\n\r\n\r\n/home/tuhdo/workspace/os/build/os/os:     file format elf32-i386\r\n\r\nDisassembly of section .text:\r\n\r\n00000500 <main>:\r\n\r\n 500:   55                      push   %ebp\r\n\r\n 501:   89 e5                   mov    %esp,%ebp\r\n\r\n 503:   90                      nop\r\n\r\n 504:   5d                      pop    %ebp\r\n\r\n 505:   c3                      ret\r\n\r\n.... remaining output omitted ....\r\n\r\n\r\n\r\nThe assembly code of main is completely different. This is why \r\nunderstanding assembly code and its relation to high-level \r\nlanguages are important. Without the knowledge, we would have \r\nused gdb as a simple source-level debugger without bother looking \r\nat the assembly code from the split layout. As a consequence, the \r\ntrue cause of the non-working code could never been discovered.\r\n\r\n  Debugging the memory layout\r\n\r\nWhat is the reason for the incorrect Assembly code in main \r\ndisplayed by gdb? There can only be one cause: the bootloader \r\njumped to the wrong addresses. But why was the address wrong? We \r\nmade the .text section at address 0x500, in which main code is in \r\nthe first byte for executing, and instructed the bootloader to \r\nretrieve the address at the offset 0x18, then jump to the entry \r\naddress.[float MarginFigure:\r\n[MarginFigure 14:\r\nMemory state after loading 2[superscript:nd] sector.\r\n]<mem-state-bootloader>\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/08/mem_after_load_os.pdf>\r\n     \r\n]\r\n\r\nThen, it might be possible for the bootloader to load the \r\noperating system address at the wrong address. But then, we \r\nexplicitly set the load address to 50h:00, which is 0x500, and so \r\nthe correct address was used. After the bootloader loas the 2[superscript:nd] sector, the in-memory state should look like the figure [mem-state-bootloader]\r\n:\r\n\r\nWhat is the reason for the incorrect Assembly code in main \r\ndisplayed by gdb? There can only be one cause: the bootloader \r\njumped to the wrong addresses. But why was the address wrong? We \r\nmade the .text section at address 0x500, in which main code is in \r\nthe first byte for executing, and instructed the bootloader to \r\nretrieve the address at the offset 0x18, then jump to the entry \r\naddress.\r\n\r\nThen, it might be possible for the bootloader to load the \r\noperating system address at the wrong address. But then, we \r\nexplicitly set the load address to 50h:00, which is 0x500, and so \r\nthe correct address was used. After the bootloader loads the 2[superscript:nd] sector, the in-memory state should look like the figure [mem-state-bootloader]\r\n.\r\n\r\nHere is the problem: 0x500 is the start of the ELF header. The \r\nbootloader actually loads the 2[superscript:nd] sector, which stores the executable as a whole, to 0x500. \r\nClearly, .text section, where main resides, is far from 0x500. \r\nSince the in-memory entry address of the executable binary is \r\n0x500, .text should be at \\mathtt{0x500+0x500=0xa00}\r\n. However, \r\nthe entry address recorded in the ELF header remains 0x500 and as \r\na result, the bootloader jumped there instead of 0xa00. This is \r\none of the issues that must be fixed.\r\n\r\nThe other issue is the mapping between debug info and the memory \r\naddress. Because the debug info is compiled with the assumed \r\noffset 0x500 that is the start of .text section, but due to \r\nactual loading, the offset is pushed another 0x500 bytes, making \r\nthe address actually is at 0xa00. This memory mismatch renders \r\nthe debug info useless.\r\n\r\n[float Figure:\r\n[Figure 0.23:\r\nWrong symbol-memory mappings in debug info.\r\n]\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/08/debug_info_bad_position.pdf>\r\n     \r\n]\r\n\r\nIn summary, we have 2 problems to overcome:\r\n\r\n• Fix the entry address to account for the extra offset when \r\n  loading into memory.\r\n\r\n• Fix the debug info to account for the extra offset when loading \r\n  into memory.\r\n\r\nFirst, we need to know the actual layout of the compiled \r\nexecutable binary:l\r\n\r\n\r\n\r\n$ readelf -l build/os/os\r\n\r\n\r\n\r\n\r\n\r\nElf file type is EXEC (Executable file)\r\n\r\nEntry point 0x500\r\n\r\nThere are 2 program headers, starting at offset 52\r\n\r\nProgram Headers:\r\n\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\nFlg Align\r\n\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R \r\n  0x4\r\n\r\n  LOAD           0x000500 0x00000500 0x00000500 0x00040 0x00040 R \r\nE 0x1000\r\n\r\n Section to Segment mapping:\r\n\r\n  Segment Sections...\r\n\r\n   00     \r\n\r\n   01     .text\r\n\r\n\r\n\r\nNotice the Offset and the VirtAddress fields: both have the same \r\nvalue. This is problematic, as the entry address and the memory \r\naddresses in the debug info depend on VirtAddr field, but the \r\nOffset having the same value destroys the validity of VirtAddr[footnote:\r\nThe offset is the distance in bytes between the beginning of the \r\nfile, the address 0, to the beginning address of a segment or a \r\nsection.\r\n] because it means that the real in-memory address will always be \r\ngreater than the VirtAddr. \r\n\r\nIf we try to adjust the virtual memory address of the .text \r\nsection in the linker script os.lds, whatever value we set also \r\nsets the Offset to the same value, until we set it to some value \r\nequal or greater than 0x1074:\r\n\r\n\r\n\r\nElf file type is EXEC (Executable file)\r\n\r\nEntry point 0x1074\r\n\r\nThere are 2 program headers, starting at offset 52\r\n\r\nProgram Headers:\r\n\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\nFlg Align\r\n\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R \r\n  0x4\r\n\r\n  LOAD           0x000074 0x00001074 0x00001074 0x00006 0x00006 R \r\nE 0x1000\r\n\r\n Section to Segment mapping:\r\n\r\n  Segment Sections...\r\n\r\n   00     \r\n\r\n   01     .text \r\n\r\n\r\n\r\nIf we adjust the virtual address to 0x1073, both the Offset and \r\nVirtAddr still share the same value:\r\n\r\n\r\n\r\nElf file type is EXEC (Executable file)\r\n\r\nEntry point 0x1073\r\n\r\nThere are 2 program headers, starting at offset 52\r\n\r\nProgram Headers:\r\n\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\nFlg Align\r\n\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R \r\n  0x4\r\n\r\n  LOAD           0x001073 0x00001073 0x00001073 0x00006 0x00006 R \r\nE 0x1000\r\n\r\n Section to Segment mapping:\r\n\r\n  Segment Sections...\r\n\r\n   00     \r\n\r\n   01     .text \r\n\r\n\r\n\r\nThe key to answer such phenonemon is in the Align field. The \r\nvalue 0x1000 indicates that the offset address of the segment \r\nshould be divisible by 0x1000, or if the distance between segment \r\nis divisible by 0x1000, the linker removes such distance to save \r\nthe binary size. We can do some experiments to verify this claim[footnote:\r\nAll the outputs are produced by the command:\r\n\r\n$ readelf -l build/os/os\r\n]:\r\n\r\n• By setting the virtual address of .text to 0x0 to 0x73 (in \r\n  os.lds), the offset starts from 0x1000 to 0x1073, accordingly. \r\n  For example, by setting it to 0x0:\r\n\r\n  \r\n\r\n  Elf file type is EXEC (Executable file)\r\n\r\n  Entry point 0x0\r\n\r\n  There are 2 program headers, starting at offset 52\r\n\r\n  Program Headers:\r\n\r\n    Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\n  Flg Align\r\n\r\n    PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 \r\n  R   0x4\r\n\r\n    LOAD           0x001000 0x00000000 0x00000000 0x00006 0x00006 \r\n  R E 0x1000\r\n\r\n   Section to Segment mapping:\r\n\r\n    Segment Sections...\r\n\r\n     00     \r\n\r\n     01     .text \r\n\r\n  \r\n\r\n  By default, if we do not specify any virtual address, the \r\n  offset stays at 0x1000 because 0x1000 is the perfect offset to \r\n  satisfy the alignment constraint. Any addition from 0x1 to 0x73 \r\n  makes the segment misaligned, but the linker keeps it anyway \r\n  because it is told so.\r\n\r\n• By setting the virtual address of .text to 0x74 (in os.lds):\r\n\r\n  \r\n\r\n  Elf file type is EXEC (Executable file)\r\n\r\n  Entry point 0x74\r\n\r\n  There are 2 program headers, starting at offset 52\r\n\r\n  Program Headers:\r\n\r\n    Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\n  Flg Align\r\n\r\n    PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 \r\n  R   0x4\r\n\r\n    LOAD           0x000074 0x00000074 0x00000074 0x00006 0x00006 \r\n  R E 0x1000\r\n\r\n   Section to Segment mapping:\r\n\r\n    Segment Sections...\r\n\r\n     00     \r\n\r\n     01     .text \r\n\r\n  \r\n\r\n  PHDR is 0x74 bytes in size, so if LOAD starts at 0x1074, the \r\n  distance between the PHDR segment and LOAD segment is \\mathtt{0x1074-0x74=0x1000}\r\n\r\n   bytes. To save space, it removes that extra 0x1000 bytes.\r\n\r\n• By setting the virtual address of .text to any value between \r\n  0x75 and 0x1073 (in os.lds), the offset takes the exact values \r\n  specified, as can be seen in the case of setting to 0x1073 \r\n  above.\r\n\r\n• By setting the virtual address of .text to any value equal or \r\n  greater than 0x1074: it starts all over again at 0x74, where \r\n  the distance is equal to 0x1000 bytes.\r\n\r\nNow we get a hint how to control the values of Offset and \r\nVirtAddr to produce a desired binary layout. What we need is to \r\nchange the Align field to a value with smaller value for finer \r\ngrain control. It might work out with a binary layout like this:\r\n\r\n\r\n\r\nElf file type is EXEC (Executable file)\r\n\r\nEntry point 0x600\r\n\r\nThere are 2 program headers, starting at offset 52\r\n\r\nProgram Headers:\r\n\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\nFlg Align\r\n\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R \r\n  0x4\r\n\r\n  LOAD           0x000100 0x00000600 0x00000600 0x00006 0x00006 R \r\nE 0x100\r\n\r\n Section to Segment mapping:\r\n\r\n  Segment Sections...\r\n\r\n   00     \r\n\r\n   01     .text \r\n\r\n\r\n\r\n\r\n\r\nThe binary will look like figure [fig-good-bin-layout] in memory:\r\n\r\n[float Figure:\r\n\r\n\r\n[Figure 0.24:\r\nA good binary layout.\r\n]<fig-good-bin-layout>\r\n\r\n     <Graphics file: C:/Users/Tu Do/os01/book_src/images/08/binary_layout_good.pdf>\r\n     \r\n]\r\n\r\nIf we set the Offset field to 0x100 from the beginning of the \r\nfile and the VirtAddr to 0x600, when loading in memory, the \r\nactual memory of .text is \\mathtt{0x500+0x100=0x600}\r\n; 0x500 is \r\nthe memory location where the bootloader loads into the physical \r\nmemory and 0x100 is the offset from the end of ELF header to \r\n.text. The entry address and the debug info will then take the \r\nvalue 0x600 from the VirtAddr field above, which totally matches \r\nthe actual physical layout. We can do it by changing os.lds as \r\nfollow:\r\n\r\n@|\\color{blue}\\bfseries ENTRY(main);|@\r\n\r\n\r\n\r\nPHDRS\r\n\r\n{\r\n\r\n  headers PT_PHDR FILEHDR PHDRS;\r\n\r\n  code PT_LOAD;\r\n\r\n}\r\n\r\n\r\n\r\nSECTIONS\r\n\r\n{\r\n\r\n  .text @|\\color{blue}\\bfseries 0x600|@: @|\\color{magenta} \r\nALIGN(0x100)|@ { *(.text)  } :code\r\n\r\n  .data :  { *(.data)  }\r\n\r\n  .bss :  { *(.bss) }\r\n\r\n  /DISCARD/ : { *(.eh_frame) }\r\n\r\n}\r\n\r\nThe ALIGN keyword, as it implies, tells the linker to align a \r\nsection, thus the segment containing it. However, to make the \r\nALIGN keyword has any effect, automatic alignment must be \r\ndisabled. According to man ld:\r\n\r\n\r\n\r\n-n\r\n\r\n--nmagic\r\n\r\n    Turn off page alignment of sections, and disable linking \r\nagainst shared    \r\n\r\n    libraries.  If the output format supports Unix style magic \r\nnumbers, mark the \r\n\r\n    output as \"NMAGIC\"\r\n\r\n\r\n\r\nThat is, by default, each section is aligned by an operating \r\nsystem page, which is 4096, or 0x1000 bytes in size. The -n or \r\n-nmagic option disables this behavior, which is needed. We amend \r\nthe ld command used in os/Makefile:\r\n\r\n..... above content omitted ....\r\n\r\n$(OS): $(OS_OBJS)\r\n\r\n\tld -m elf_i386 @|\\color{red}\\bfseries -nmagic|@ -Tos.lds \r\n$(OS_OBJS) -o $@\r\n\r\nFinally, we also need to update the top-level Makefile to write \r\nmore than one sector into the disk image for the operating system \r\nbinary, as its size exceeds one sector:\r\n\r\n\r\n\r\n$ ls -l build/os/os\r\n\r\n-rwxrwxr-x 1 tuhdo tuhdo 9060 Feb 13 21:37 build/os/os\r\n\r\n\r\n\r\nWe update the rule so that the sectors are automatically \r\ncalculated:\r\n\r\n..... above content omitted ....\r\n\r\nbootdisk:  bootloader os\r\n\r\n\tdd if=/dev/zero of=$(DISK_IMG) bs=512 count=2880\r\n\r\n\tdd conv=notrunc if=$(BOOTLOADER) of=$(DISK_IMG) bs=512 count=1 \r\nseek=0\r\n\r\n\tdd conv=notrunc if=$(OS) of=$(DISK_IMG) bs=512 count=$$(($(shell \r\nstat --printf=\"%s\" $(OS))/512)) seek=1\r\n\r\nAfter updating the everything, recompiling the executable binary \r\nand we get the desired offset and virtual memory at 0x100 and \r\n0x600, respectively:\r\n\r\n\r\n\r\nElf file type is EXEC (Executable file)\r\n\r\nEntry point 0x600\r\n\r\nThere are 2 program headers, starting at offset 52\r\n\r\nProgram Headers:\r\n\r\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  \r\nFlg Align\r\n\r\n  PHDR           0x000000 0x00000000 0x00000000 0x00074 0x00074 R \r\n  0x4\r\n\r\n  LOAD           0x000100 0x00000600 0x00000600 0x00006 0x00006 R \r\nE 0x100\r\n\r\n Section to Segment mapping:\r\n\r\n  Segment Sections...\r\n\r\n   00     \r\n\r\n   01     .text \r\n\r\n\r\n\r\n  Testing the new binary\r\n\r\nFirst, we start the QEMU machine:\r\n\r\n\r\n\r\n$ make qemu\r\n\r\n\r\n\r\nIn another terminal, we start gdb, loading the debug info and set \r\na breakpoint at main:\r\n\r\n\r\n\r\n$ gdb\r\n\r\n\r\n\r\nThe following output should be produced:\r\n\r\n\r\n\r\n---Type <return> to continue, or q <return> to quit---\r\n\r\n[f000:fff0] 0x0000fff0 in ?? ()\r\n\r\nBreakpoint 1 at 0x7c00\r\n\r\nBreakpoint 2 at 0x600: file main.c, line 1.\r\n\r\n\r\n\r\n\r\n\r\nThen, let gdb runs until it hits the main function, then we \r\nchange to the split layout between source and assembly:\r\n\r\n\r\n\r\n(gdb) layout split\r\n\r\n\r\n\r\nThe final terminal output should look like this:\r\n\r\n\r\n\r\n   \r\n┌──main.c───────────────────────────────────────────────────────┐\r\n\r\nB+>│1       void main(){}                                         \r\n │\r\n\r\n   │2                                                             \r\n │\r\n\r\n   │3                                                             \r\n │\r\n\r\n   │4                                                             \r\n │\r\n\r\n   │5                                                             \r\n │\r\n\r\n   │6                                                             \r\n │\r\n\r\n   │7                                                             \r\n │\r\n\r\n   │8                                                             \r\n │\r\n\r\n   │9                                                             \r\n │\r\n\r\n   │10                                                            \r\n │\r\n\r\n   │11                                                            \r\n │\r\n\r\n   │12                                                            \r\n │\r\n\r\n   │13                                                            \r\n │\r\n\r\n   │14                                                            \r\n │\r\n\r\n   │15                                                            \r\n │\r\n\r\n   │16                                                            \r\n │\r\n\r\n   \r\n└───────────────────────────────────────────────────────────────┘\r\n\r\nB+>│0x600 <main>    push   bp                                     \r\n │\r\n\r\n   │0x601 <main+1>  mov    bp,sp                                  \r\n │\r\n\r\n   │0x603 <main+3>  nop                                           \r\n │\r\n\r\n   │0x604 <main+4>  pop    bp                                     \r\n │\r\n\r\n   │0x605 <main+5>  ret                                           \r\n │\r\n\r\n   │0x606           aaa                                           \r\n │\r\n\r\n   │0x607           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   │0x609           add    BYTE PTR [si],al                       \r\n │\r\n\r\n   │0x60b           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   │0x60d           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   │0x60f           add    BYTE PTR [si],al                       \r\n │\r\n\r\n   │0x611           add    ax,bp                                  \r\n │\r\n\r\n   │0x613           push   ss                                     \r\n │\r\n\r\n   │0x614           add    BYTE PTR [bx+si],al                    \r\n │\r\n\r\n   │0x616           or     al,0x67                                \r\n │\r\n\r\n   │0x618           adc    al,BYTE PTR [bx+si]                    \r\n │\r\n\r\n   │0x61a           add    BYTE PTR [bx+si+0x2],al                \r\n │\r\n\r\n   \r\n└───────────────────────────────────────────────────────────────┘\r\n\r\nremote Thread 1 In: main                            L1    PC: \r\n0x600 \r\n\r\n(gdb) c\r\n\r\nContinuing.\r\n\r\n[   0:7c00] \r\n\r\nBreakpoint 1, 0x00007c00 in ?? ()\r\n\r\n(gdb) c\r\n\r\nContinuing.\r\n\r\n[   0: 600] \r\n\r\nBreakpoint 2, main () at main.c:1\r\n\r\n(gdb) layout split\r\n\r\n\r\n\r\nNow, the displayed assembly is the same as in objdump, except the \r\nregisters are 16-bit ones. This is normal, as gdb is operating in \r\n16-bit mode, while objdump displays code in 32-bit mode. To make \r\nsure, we verify the raw opcode by using x command:\r\n\r\n\r\n\r\n(gdb) x/16xb 0x600\r\n\r\n\r\n\r\n\r\n\r\n0x600 <main>:   0x55    0x89    0xe5    0x90    0x5d    0xc3    \r\n0x37\r\n\r\n        0x00\r\n\r\n0x608:  0x00    0x00    0x04    0x00    0x00    0x00    0x00    \r\n0x00\r\n\r\n\r\n\r\nFrom the assembly window, main stops at the address 0x605. As \r\nsuch, the corresponding bytes from 0x600 to 0x605 are highlighted \r\nin red from the output of the command x/16xb 0x600. Then, the raw \r\nopcode from the objdump output:\r\n\r\n\r\n\r\n$ objdump -z -M intel -S -D build/os/os | less\r\n\r\n\r\n\r\n\r\n\r\nbuild/os/os:     file format elf32-i386\r\n\r\nDisassembly of section .text:\r\n\r\n00000600 <main>:\r\n\r\nvoid main(){}\r\n\r\n 600:   55                      push   ebp\r\n\r\n 601:   89 e5                   mov    ebp,esp\r\n\r\n 603:   90                      nop\r\n\r\n 604:   5d                      pop    ebp\r\n\r\n 605:   c3                      ret    \r\n\r\nDisassembly of section .debug_info:\r\n\r\n...... output omitted ......\r\n\r\n\r\n\r\nBoth raw opcode displayed by the two programs are the same. In \r\nthis case, it proved that gdb correctly jumped to the address of \r\nmain for a proper debugging. This is an extremely important \r\nmilestone. Being able to debug in bare metal will help \r\ntremendously in writing an operating system, as a debugger allows \r\na programmer to inspect the internal state of a running machine \r\nat each step to verify his code, step by step, to gradually build \r\nup a solid understanding. Some professional programmers do not \r\nlike debuggers, but it is because they understand their domain \r\ndeep enough to not need to rely on a debugger to verify their \r\ncode. When encountering new domains, a debugger is indispensable \r\nlearning tool because of its verifiability.\r\n\r\nHowever, even with the aid of debugger, writing an operating \r\nsystem is still not a walk in the park. The debugger may give the \r\naccess to the machine at one point in time, but it does not give \r\nthe cause. To find out the root cause, is up to the ability of a \r\nprogrammer. Later in the book, we will learn how to use other \r\ndebugging techniques, such as using QEMU logging facility to \r\ndebug CPU exceptions.\r\n\r\nKernel Programming\r\n\r\nx86 Descriptors\r\n\r\n  Basic operating system concepts\r\n\r\nThe first and foremost, OS manages hardware resources. It's easy \r\nto see the core features of an OS based on Von Neumann diagram:\r\n\r\n  CPU management: allows programs to share CPU for multitasking. \r\n\r\n  Memory management: allocates enough storage for programs to \r\n  run. \r\n\r\n  Devices management: detects and communicates with different \r\n  devices\r\n\r\nAny OS should be good at the above fundamentals tasks. \r\n\r\nAnother important feature of an OS is to provide an software \r\ninterface layer, that hides away hardware interfaces, to \r\ninterface with applications that run on top of that OS. The \r\nbenefits of such a layer:\r\n\r\n• reusability: that is, the same software API can be reused \r\n  across programs, thus simplifying software development process\r\n\r\n• separation of concerns: bugs appear either in application \r\n  programs, or in the OS; a programmer needs to isolate where the \r\n  bugs are.\r\n\r\n• simplify software development process: provides an easier to \r\n  use software interface layer with a uniform access to hardware \r\n  resources across devices, instead of directly using the \r\n  hardware interface of a particular device.\r\n\r\n  Hardware Abstraction Layer\r\n\r\nThere are so many hardware devices out there, so it's best to \r\nleave the hardware engineers how the devices talk to an OS. To \r\nachieve this goal, the OS only provides a set of agreed software \r\ninterfaces between itself and the device driver writers and is \r\ncalled Hardware Abstraction Layer.\r\n\r\nIn C, this software interface is implemented through a structure \r\nfunction pointers. \r\n\r\n[illustrate with Linux example]\r\n\r\n  System programming interface \r\n\r\nSystem programming interfaces are standard interfaces that an OS \r\nprovides application programs to use its services. For example, \r\nif a program wishes to read a file on disk, then it must call a \r\nfunction like open() and let the OS handle the details of talking \r\nto the hard disk for retrieving the file.\r\n\r\n  The need for an Operating System \r\n\r\nIn a way, OS is an overhead, but a necessary one, for a user to \r\ntell a computer what to do. When resources in a computer system \r\n(CPU, GPU, memory, hard drive...) became big and more \r\ncomplicated, it's tedious to manually manage all the resources.\r\n\r\nImagine we have to manually load programs on a computer with 3 GB \r\nof RAM. We would have to load programs at various fix addresses, \r\nand for each program a size must be manually calculated to avoid \r\nwasting memory resource, and enough for programs to not \r\noverriding each other.\r\n\r\nOr, when we want to give computer input through the keyboard, \r\nwithout an OS, an application also has to carry code to \r\nfacilitate the communication with keyboard hardware; each \r\napplication then handles such keyboard communication on its own. \r\nWhy should there be such duplications across applications for \r\nsuch standard feature? If you write an accounting software, why \r\nshould a programmer concern writing a keyboard driver, totally \r\nirrelevant to the problem domain?\r\n\r\nThat's why a crucial job of an OS is to hide the complexity of \r\nhardware devices, so a program is freed from the burden of \r\nmaintaining its own code for hardware communication by having a \r\nstandardized set of interfaces and thus, reduce potential bugs \r\nalong with faster development time.\r\n\r\nTo write an OS effectively, a programmer need to understand well \r\nthe underlying computer architecture that programmer are writing \r\nan OS for. The first reason is, many OS concepts are supported by \r\nthe architecture e.g. the concepts of virtual memory are well \r\nsupported by x86 architecture. If the underlying computer \r\narchitecture is not well-understood, OS developers are doomed to \r\nreinvent it in your OS, and such software-implemented solutions \r\nrun slower than the hardware version.\r\n\r\n  Drivers\r\n\r\nDrivers are programs that enable an OS to communicate and use \r\nfeatures of hardware devices. For example, a keyboard driver \r\nenables an OS to get input from keyboard; or a network driver \r\nallows a network card to send and receive data packets to and \r\nfrom the Internet.\r\n\r\nIf you only write application programs, you may wonder how can \r\nsoftware control hardware devices? As mentioned in Chapter 2, \r\nthrough the hardware-software interface: by writing to a device's \r\nregisters or to write to ports of a device, through the use of \r\nCPU's instructions. \r\n\r\n  Userspace and kernel space\r\n\r\nKernel space refers to the working environment of an OS that only \r\nthe kernel can access. Kernel space includes the direct \r\ncommunication with hardware, or manipulate privileged memory \r\nregions (such as kernel code and data).\r\n\r\nIn contrast, userspace refers to less privileged processes that \r\nrun above the OS, and is supervised by the OS. To access the \r\nkernel facility, user program must go through the standardized \r\nsystem programming interfaces provided by the OS.\r\n\r\n  Memory Segment\r\n\r\n  Segment Descriptor\r\n\r\n  Types of Segment Descriptors\r\n\r\n  Code and Data descriptors\r\n\r\n  Task Descriptor\r\n\r\n  Interrupt Descriptor\r\n\r\n  Descriptor Scope\r\n\r\n  Global Descriptor\r\n\r\n  Local Descriptor\r\n\r\n  Segment Selector\r\n\r\n  Enhancement: Bootloader with descriptors\r\n\r\nProcess\r\n\r\n  Concepts\r\n\r\n  Process\r\n\r\n  Task\r\n\r\nA task is a unit of work that an OS needs to do, similar to how \r\nhuman have tasks to do daily. From a user point of view, a task \r\nfor a computer to do can be web browsing, document editing, \r\ngaming, sending and receiving emails... Since a CPU can only \r\nexecute sequentially, one instruction after another (fetching \r\nfrom main memory), there must be some way to do many meaningful \r\ntasks at once. For that reason, the computer must share the \r\nresources e.g. registers, stack, memory... between tasks, since \r\nwe have many tasks but single and limited resources.\r\n\r\n  Process\r\n\r\nProcess is a data structure that keeps track of the execution \r\nstate of a task. Task is a general concept, and process is the \r\nimplementation of a task. In a general-purpose OS, a task is \r\nusually a program. For example, when you run Firefox, a process \r\nstructure is created to keep track of where the stack and the \r\nheap allocated for firefox are, where Firefox's code area is and \r\nwhich instruction EIP is holding to execute next... The typical \r\nprocess structure looks like this:\r\n\r\n[insert process image]\r\n\r\nProcess is a virtual computer, but much more primitive than the \r\nvirtual machine in virtualization software like Virtual Box, and \r\nthat's a good thing. Imagine having to run a full-fledged virtual \r\nmachine for every task; how wasteful of machine resources that \r\nwould be.. In the view of a running process, its code executes as \r\nif it runs directly on hardware. Each process has its own set of \r\nregister values, which are kept tracked by the OS, and its own \r\ncontiguous virtual memory space (which is discontiguous in actual \r\nphysical memory). The code in a process is given virtual memory \r\naddresses to read and write from.\r\n\r\n[ illustrate: - a process looks like a mini Von Neumann - with \r\ncontiguous memory, each with a color; each cell of a process \r\nmapped to distant memory cell in physical memory ]\r\n\r\nA process can run so much until the OS tells it to temporary stop \r\nfor other tasks to use the hardware resources. The suspended \r\nprocess can then wait until further notice from the OS. This \r\nwhole switching process is so fast that a computer user think his \r\ncomputer actually runs tasks in parallel. The program that does \r\nthe switching between tasks is called a *scheduler*.\r\n\r\n  Scheduler\r\n\r\nAn OS needs to perform a wide range of different functionalities, \r\ne.g. web browsing, document editing, gaming... A scheduler \r\ndecides which tasks got to run before the others and, for how \r\nlong, in an efficient manner. Scheduler enables your computer to \r\nbecome a time sharing system, because tasks share CPU execution \r\ntime and no one process can monopolize the CPU (in practice, it \r\nstill happens regularly). Without a scheduler, only a single task \r\ncan be performed at a time.\r\n\r\n  Context switch\r\n\r\nWhen a process is prepared to be switched out for another process \r\nto take its place, certain hardware resources i.e. current open \r\nfiles, current register values... must be backed up to later \r\nresume that process's execution.\r\n\r\n  Priority\r\n\r\nPriority is an important metric for OS to decide which task is \r\nscheduled to run before the others to allocate appropriate CPU \r\nexecution time for each task.\r\n\r\n  Preemptive vs Non-preemptive\r\n\r\nA preemptive OS can interrupt an executing process and switch to \r\nanother process.\r\n\r\nA non-preemtive OS, a task runs until its completion.\r\n\r\n  Process states\r\n\r\nState is a particular condition of a process, triggered by an \r\naction from the scheduler. A process goes through various states \r\nduring its life cycle. A process typically has these states:\r\n\r\n  Run indicating CPU is executing code in this process.\r\n\r\n  Sleep (or Suspended): indicating CPU is executing some process \r\n  else.\r\n\r\n  Destroyed: process is done and waiting to be destroyed \r\n  completely.\r\n\r\n  procfs\r\n\r\n  Threads\r\n\r\nThreads are units of work inside a process that shares the \r\nexecution environment. A process creates a whole new execution \r\nenvironment with code of its own:\r\n\r\n[illustration between process and thread, with each process is a \r\nbig rectangle box and threads nested boxes point to different \r\ncode region]\r\n\r\nInstead of creating a completely new process structure in memory, \r\nOS simply let the thread uses some of the resources of the parent \r\nprocess that created it. A thread has its own registers, program \r\ncounter, stack pointer, and its own call stack. Everything else \r\nis shared between the threads, such as an address space, heap, \r\nstatic data, and code segments, and file descriptors. Because \r\nthread simply reuses existing resources and involve no context \r\nswitching, it is much faster to create and switch between \r\nprocesses.\r\n\r\nHowever, note that the above scheme is just an implementation of \r\nthread concept. You can completely treat thread the same as \r\nprocess (hence you can call all processes threads and vice \r\nversa). Or you can just back up some resources, whlie leaving \r\nsome resources shared. It's up to the OS designer to distinguish \r\nbetween threads and processes. Threads are usually implemented as \r\na component of a process.\r\n\r\nOn Linux, a thread is simply a process that shares resources with \r\nits parent process; for that reason, a Linux thread is also \r\ncalled *light-weight process*. Or put it another way, a thread in \r\nLinux is merely an implementation of a single-threaded process \r\nthat execute its main program code. A multi-threaded program in \r\nLinux is just a process with shared with its single-threaded \r\nchildren processes, each points to different code region of its \r\nparent process.\r\n\r\n[TODO: turn the above table into a diagram]\r\n\r\nOn Windows, threads and processes are two separated entities, so \r\nthe above description for Linux does not apply. However, the \r\ngeneral idea: a thread shares the execution environment, holds.\r\n\r\n  Task: x86 concept of a process\r\n\r\n  Task Data Structure\r\n\r\n  Task State Segment\r\n\r\n  Task Descriptor\r\n\r\n  Process Implementation\r\n\r\n  Requirements\r\n\r\n  Major Plan\r\n\r\n  Stage 1: Switch to a task from bootloader\r\n\r\n  Stage 2: Switch to a task with one function from kernel\r\n\r\n  Stage 3: Switch to a task with many functions from kernel\r\n\r\nTo implement the concept of a process, a kernel needs to be able \r\nto save and restore its machine states for different tasks. \r\n\r\n  Description\r\n\r\n  [Describe task switching mechanism involving LDT and GDT]\r\n\r\n  qasdfasdf asd\r\n\r\n  Constraints\r\n\r\n  Design\r\n\r\n  Implementation plan\r\n\r\n  Milestone: Code Refactor\r\n\r\nInterrupt\r\n\r\nMemory management\r\n\r\n  Address Space\r\n\r\nAddress space is the set of all addressable memory locations. \r\nThere are 2 types of address spaces in physical memory address:\r\n\r\n• One for memory: \r\n\r\n• One for I/O:\r\n\r\nEach process has its own address space to do whatever it wants, \r\nas long as the physical memory is not exhausted. This address \r\nspace is called virtual memory.\r\n\r\n  Virtual Memory\r\n\r\nPhysical memory is a contagious memory locations that has a \r\nsimple mapping between a physical memory address and its \r\ncorresponding location in memory, decoded by memory controller. \r\nOn the other hand, *virtual memory* does not have direct mapping \r\nbetween a memory address and the corresponding physical memory \r\nlocation, even though it appears contagious from the view of an \r\nuserspace program. Instead, virtual memory address is translated \r\nby OS into an actual physical memory address. For that reason, \r\neven addresses appear next to each other in virtual memory space, \r\nthey are scattered through out the physical memory.\r\n\r\nWhy virtual memory is needed? Because virtual memory reduces the \r\ncomplexity of programming, by giving each program an illusion \r\nthat it has its own separate \"physical\" memory to work with. \r\nWithout virtual memory, programs must know and agree with each \r\nother their own memory regions to not accidentally destroy each \r\nother.\r\n\r\n[illustration a world without virtual memory]\r\n\r\nVirtual memory also enables a more secured OS, as application \r\nprograms cannot manipulate main memory directly, so malicious \r\nprograms won't cause havocs by destroying main memory and \r\npossibly hardware devices, by gaining access to hardware I/O \r\nports.\r\n\r\nAnother benefit is that virtual memory can extend beyond physical \r\nmemory, by storing its data to hard disk. By swapping some of \r\nunused memory (i.e. inactive memory of a sleeping process), the \r\nsystem gains some free memory to continue running, so no data is \r\ndestroyed. Otherwise, the OS is forced to kill a random user \r\nprocess to free up some memory, and you may lose unsaved work \r\nthat belongs to the killed process. However, this process can \r\nsignificantly slow down the whole system because of Von Neumann \r\nbottleneck. In the old days, when memory was scarce, it was \r\nuseful.\r\n\r\nFile System\r\n\r\nFile system is a mechanism on how raw bytes in a storage device \r\ncan be meaningfully managed. That is, a group of bytes at \r\nspecific locations in a storage device can be allocated for a \r\npurpose e.g. storing raw ASCII document, and later the exact \r\nchunks of bytes can be retrieved correctly. File system manages \r\nmany such groups of bytes. It's helpful to think a file system as \r\na database that maps between high level information and specific \r\nlocations in a hard disk, similar to how business information is \r\nmapped to a specific row in a table. The high level information \r\nthat is relevant to a file system is organized as *files* and \r\n*directories*.\r\n\r\n[illustration between a file system and a database table to see \r\nhow they are similar]\r\n\r\nFile is an entity that includes two components: metadata and the \r\nactual raw data. Metadata is the information describes the \r\nproperties of the raw data associated with the file; raw data are \r\nreal content of a file. Directory is a file that holds a group of \r\nfiles and also child directories. Together, they create a file \r\nhierarchy system as commonly seen in Windows or Linux.\r\n\r\n  Example: Ex2 filesystem\r\n\r\n\r\n\r\n[LaTeX Command: printindex]\r\n\r\nBibliography\r\n\r\nReferences\r\n\r\n[Hardy 2005] G. H. Hardy, \"A Mathematician's Apology\", University of Alberta Mathematical Sciences Society (2005), 13.\r\n\r\n[Intel 2016] Intel, IntelÂ® 64 and IA-32 Architectures Optimization Reference Manual (Intel, 2016).\r\n\r\n[Kovitz 1999] Benjamin L. Kovitz, \"Practical Software Requirements\", Manning (1999), 53.\r\n\r\n[Peirce 1933] Charles Sanders Peirce, \"Collected Papers v. 4\" (1933).\r\n\r\n[Wakerly 1999] John F. Wakerly, \"Digital Design: Principles and Practices\", Prentice Hall (1999), 86.\r\n\r\n\r\n\r\n"
  },
  {
    "path": "book_src/images/.rid",
    "content": "58f78e5b0bedbe9328c87d501e6feda8"
  },
  {
    "path": "book_src/images/02/layer_translation.graphml",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:java=\"http://www.yworks.com/xml/yfiles-common/1.0/java\" xmlns:sys=\"http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0\" xmlns:x=\"http://www.yworks.com/xml/yfiles-common/markup/2.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:y=\"http://www.yworks.com/xml/graphml\" xmlns:yed=\"http://www.yworks.com/xml/yed/3\" xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd\">\r\n  <!--Created by yEd 3.16.1-->\r\n  <key attr.name=\"Description\" attr.type=\"string\" for=\"graph\" id=\"d0\"/>\r\n  <key for=\"port\" id=\"d1\" yfiles.type=\"portgraphics\"/>\r\n  <key for=\"port\" id=\"d2\" yfiles.type=\"portgeometry\"/>\r\n  <key for=\"port\" id=\"d3\" yfiles.type=\"portuserdata\"/>\r\n  <key attr.name=\"url\" attr.type=\"string\" for=\"node\" id=\"d4\"/>\r\n  <key attr.name=\"description\" attr.type=\"string\" for=\"node\" id=\"d5\"/>\r\n  <key for=\"node\" id=\"d6\" yfiles.type=\"nodegraphics\"/>\r\n  <key for=\"graphml\" id=\"d7\" yfiles.type=\"resources\"/>\r\n  <key attr.name=\"url\" attr.type=\"string\" for=\"edge\" id=\"d8\"/>\r\n  <key attr.name=\"description\" attr.type=\"string\" for=\"edge\" id=\"d9\"/>\r\n  <key for=\"edge\" id=\"d10\" yfiles.type=\"edgegraphics\"/>\r\n  <graph edgedefault=\"directed\" id=\"G\">\r\n    <data key=\"d0\"/>\r\n    <node id=\"n0\">\r\n      <data key=\"d5\"/>\r\n      <data key=\"d6\">\r\n        <y:ShapeNode>\r\n          <y:Geometry height=\"33.02857142857143\" width=\"192.66666666666666\" x=\"21.33333333333333\" y=\"412.48571428571427\"/>\r\n          <y:Fill hasColor=\"false\" transparent=\"false\"/>\r\n          <y:BorderStyle color=\"#000000\" raised=\"false\" type=\"line\" width=\"1.0\"/>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"94.33333333333333\" y=\"14.514285714285734\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"94.33333333333333\" y=\"14.514285714285734\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" height=\"18.701171875\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"112.73828125\" x=\"39.96419270833333\" y=\"7.163699776785734\">Assembly Language<y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:Shape type=\"rectangle\"/>\r\n        </y:ShapeNode>\r\n      </data>\r\n    </node>\r\n    <node id=\"n1\">\r\n      <data key=\"d5\"/>\r\n      <data key=\"d6\">\r\n        <y:ShapeNode>\r\n          <y:Geometry height=\"33.02857142857143\" width=\"192.66666666666666\" x=\"21.33333333333333\" y=\"463.48571428571427\"/>\r\n          <y:Fill hasColor=\"false\" transparent=\"false\"/>\r\n          <y:BorderStyle color=\"#000000\" raised=\"false\" type=\"line\" width=\"1.0\"/>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"94.33333333333333\" y=\"14.514285714285734\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"94.33333333333333\" y=\"14.514285714285734\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" height=\"18.701171875\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"68.037109375\" x=\"62.31477864583333\" y=\"7.163699776785734\">Logic Gates<y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:Shape type=\"rectangle\"/>\r\n        </y:ShapeNode>\r\n      </data>\r\n    </node>\r\n    <node id=\"n2\">\r\n      <data key=\"d5\"/>\r\n      <data key=\"d6\">\r\n        <y:ShapeNode>\r\n          <y:Geometry height=\"33.02857142857143\" width=\"192.66666666666666\" x=\"21.33333333333333\" y=\"514.4857142857143\"/>\r\n          <y:Fill hasColor=\"false\" transparent=\"false\"/>\r\n          <y:BorderStyle color=\"#000000\" raised=\"false\" type=\"line\" width=\"1.0\"/>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"94.33333333333333\" y=\"14.514285714285734\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"94.33333333333333\" y=\"14.514285714285734\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" height=\"18.701171875\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"38.001953125\" x=\"77.33235677083333\" y=\"7.163699776785734\">Circuit<y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:Shape type=\"rectangle\"/>\r\n        </y:ShapeNode>\r\n      </data>\r\n    </node>\r\n    <node id=\"n3\">\r\n      <data key=\"d5\"/>\r\n      <data key=\"d6\">\r\n        <y:ShapeNode>\r\n          <y:Geometry height=\"30.0\" width=\"175.0\" x=\"260.0\" y=\"439.0\"/>\r\n          <y:Fill hasColor=\"false\" transparent=\"false\"/>\r\n          <y:BorderStyle color=\"#000000\" raised=\"false\" type=\"line\" width=\"1.0\"/>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"85.5\" y=\"13.0\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"85.5\" y=\"13.0\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" height=\"18.701171875\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"72.021484375\" x=\"51.4892578125\" y=\"5.6494140625\">Mathematics<y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:Shape type=\"rectangle\"/>\r\n        </y:ShapeNode>\r\n      </data>\r\n    </node>\r\n    <node id=\"n4\">\r\n      <data key=\"d5\"/>\r\n      <data key=\"d6\">\r\n        <y:ShapeNode>\r\n          <y:Geometry height=\"30.0\" width=\"175.0\" x=\"489.0\" y=\"439.0\"/>\r\n          <y:Fill hasColor=\"false\" transparent=\"false\"/>\r\n          <y:BorderStyle color=\"#000000\" raised=\"false\" type=\"line\" width=\"1.0\"/>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"85.5\" y=\"13.0\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"85.5\" y=\"13.0\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" height=\"18.701171875\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"93.3671875\" x=\"40.81640625\" y=\"5.6494140625\">Problem Domain<y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:Shape type=\"rectangle\"/>\r\n        </y:ShapeNode>\r\n      </data>\r\n    </node>\r\n    <node id=\"n5\">\r\n      <data key=\"d5\"/>\r\n      <data key=\"d6\">\r\n        <y:ShapeNode>\r\n          <y:Geometry height=\"33.02857142857143\" width=\"192.66666666666666\" x=\"21.33333333333333\" y=\"361.48571428571427\"/>\r\n          <y:Fill hasColor=\"false\" transparent=\"false\"/>\r\n          <y:BorderStyle color=\"#000000\" raised=\"false\" type=\"line\" width=\"1.0\"/>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"94.33333333333333\" y=\"14.514285714285734\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" hasText=\"false\" height=\"4.0\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"4.0\" x=\"94.33333333333333\" y=\"14.514285714285734\">\r\n            <y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:NodeLabel alignment=\"center\" autoSizePolicy=\"content\" fontFamily=\"Dialog\" fontSize=\"12\" fontStyle=\"plain\" hasBackgroundColor=\"false\" hasLineColor=\"false\" height=\"18.701171875\" horizontalTextPosition=\"center\" iconTextGap=\"4\" modelName=\"custom\" textColor=\"#000000\" verticalTextPosition=\"bottom\" visible=\"true\" width=\"132.748046875\" x=\"29.959309895833325\" y=\"7.163699776785734\">Programming Language<y:LabelModel>\r\n              <y:SmartNodeLabelModel distance=\"4.0\"/>\r\n            </y:LabelModel>\r\n            <y:ModelParameter>\r\n              <y:SmartNodeLabelModelParameter labelRatioX=\"0.0\" labelRatioY=\"0.0\" nodeRatioX=\"0.0\" nodeRatioY=\"0.0\" offsetX=\"0.0\" offsetY=\"0.0\" upX=\"0.0\" upY=\"-1.0\"/>\r\n            </y:ModelParameter>\r\n          </y:NodeLabel>\r\n          <y:Shape type=\"rectangle\"/>\r\n        </y:ShapeNode>\r\n      </data>\r\n    </node>\r\n    <edge id=\"e0\" source=\"n4\" target=\"n3\">\r\n      <data key=\"d9\"/>\r\n      <data key=\"d10\">\r\n        <y:PolyLineEdge>\r\n          <y:Path sx=\"0.0\" sy=\"0.0\" tx=\"0.0\" ty=\"0.0\"/>\r\n          <y:LineStyle color=\"#000000\" type=\"line\" width=\"1.0\"/>\r\n          <y:Arrows source=\"none\" target=\"standard\"/>\r\n          <y:BendStyle smoothed=\"false\"/>\r\n        </y:PolyLineEdge>\r\n      </data>\r\n    </edge>\r\n    <edge id=\"e1\" source=\"n3\" target=\"n0\">\r\n      <data key=\"d9\"/>\r\n      <data key=\"d10\">\r\n        <y:PolyLineEdge>\r\n          <y:Path sx=\"-87.5\" sy=\"0.0\" tx=\"0.0\" ty=\"0.0\">\r\n            <y:Point x=\"241.928\" y=\"429.0\"/>\r\n          </y:Path>\r\n          <y:LineStyle color=\"#000000\" type=\"line\" width=\"1.0\"/>\r\n          <y:Arrows source=\"none\" target=\"standard\"/>\r\n          <y:BendStyle smoothed=\"false\"/>\r\n        </y:PolyLineEdge>\r\n      </data>\r\n    </edge>\r\n    <edge id=\"e2\" source=\"n3\" target=\"n1\">\r\n      <data key=\"d9\"/>\r\n      <data key=\"d10\">\r\n        <y:PolyLineEdge>\r\n          <y:Path sx=\"-87.5\" sy=\"0.0\" tx=\"0.0\" ty=\"0.0\">\r\n            <y:Point x=\"246.0\" y=\"480.0\"/>\r\n            <y:Point x=\"246.0\" y=\"480.0\"/>\r\n          </y:Path>\r\n          <y:LineStyle color=\"#000000\" type=\"line\" width=\"1.0\"/>\r\n          <y:Arrows source=\"none\" target=\"standard\"/>\r\n          <y:BendStyle smoothed=\"false\"/>\r\n        </y:PolyLineEdge>\r\n      </data>\r\n    </edge>\r\n    <edge id=\"e3\" source=\"n3\" target=\"n2\">\r\n      <data key=\"d9\"/>\r\n      <data key=\"d10\">\r\n        <y:PolyLineEdge>\r\n          <y:Path sx=\"-87.42633599999994\" sy=\"0.0\" tx=\"0.0\" ty=\"0.0\">\r\n            <y:Point x=\"246.06534400000007\" y=\"531.0\"/>\r\n          </y:Path>\r\n          <y:LineStyle color=\"#000000\" type=\"line\" width=\"1.0\"/>\r\n          <y:Arrows source=\"none\" target=\"standard\"/>\r\n          <y:BendStyle smoothed=\"false\"/>\r\n        </y:PolyLineEdge>\r\n      </data>\r\n    </edge>\r\n    <edge id=\"e4\" source=\"n0\" target=\"n1\">\r\n      <data key=\"d9\"/>\r\n      <data key=\"d10\">\r\n        <y:PolyLineEdge>\r\n          <y:Path sx=\"0.0\" sy=\"0.0\" tx=\"0.0\" ty=\"0.0\"/>\r\n          <y:LineStyle color=\"#000000\" type=\"line\" width=\"1.0\"/>\r\n          <y:Arrows source=\"none\" target=\"standard\"/>\r\n          <y:BendStyle smoothed=\"false\"/>\r\n        </y:PolyLineEdge>\r\n      </data>\r\n    </edge>\r\n    <edge id=\"e5\" source=\"n1\" target=\"n2\">\r\n      <data key=\"d9\"/>\r\n      <data key=\"d10\">\r\n        <y:PolyLineEdge>\r\n          <y:Path sx=\"0.0\" sy=\"0.0\" tx=\"0.0\" ty=\"0.0\"/>\r\n          <y:LineStyle color=\"#000000\" type=\"line\" width=\"1.0\"/>\r\n          <y:Arrows source=\"none\" target=\"standard\"/>\r\n          <y:BendStyle smoothed=\"false\"/>\r\n        </y:PolyLineEdge>\r\n      </data>\r\n    </edge>\r\n    <edge id=\"e6\" source=\"n5\" target=\"n0\">\r\n      <data key=\"d9\"/>\r\n      <data key=\"d10\">\r\n        <y:PolyLineEdge>\r\n          <y:Path sx=\"0.0\" sy=\"0.0\" tx=\"0.0\" ty=\"0.0\"/>\r\n          <y:LineStyle color=\"#000000\" type=\"line\" width=\"1.0\"/>\r\n          <y:Arrows source=\"none\" target=\"standard\"/>\r\n          <y:BendStyle smoothed=\"false\"/>\r\n        </y:PolyLineEdge>\r\n      </data>\r\n    </edge>\r\n    <edge id=\"e7\" source=\"n3\" target=\"n5\">\r\n      <data key=\"d9\"/>\r\n      <data key=\"d10\">\r\n        <y:PolyLineEdge>\r\n          <y:Path sx=\"-87.5\" sy=\"0.0\" tx=\"0.0\" ty=\"0.0\">\r\n            <y:Point x=\"228.7378773333334\" y=\"378.0\"/>\r\n          </y:Path>\r\n          <y:LineStyle color=\"#000000\" type=\"line\" width=\"1.0\"/>\r\n          <y:Arrows source=\"none\" target=\"standard\"/>\r\n          <y:BendStyle smoothed=\"false\"/>\r\n        </y:PolyLineEdge>\r\n      </data>\r\n    </edge>\r\n  </graph>\r\n  <data key=\"d7\">\r\n    <y:Resources/>\r\n  </data>\r\n</graphml>\r\n"
  },
  {
    "path": "book_src/images/03/.rid",
    "content": "f5ac4f7ffcf0c9cdf703fb078663fd4e"
  },
  {
    "path": "book_src/images/04/.rid",
    "content": "49527604b893e9c9a27465cd47b4ab70"
  },
  {
    "path": "book_src/images/04/modrm32.tex",
    "content": "\\begin{tabular}{|>{\\raggedright}p{4cm}|>{\\raggedright}p{1cm}|>{\\raggedright}p{1cm}|>{\\raggedright}p{1cm}|>{\\raggedright}p{1cm}|>{\\raggedright}p{1cm}|>{\\raggedright}p{1cm}|>{\\raggedright}p{1cm}|>{\\raggedright}p{1cm}|>{\\raggedright}p{1cm}|>{\\raggedright}p{1cm}|}\r\n\\hline \r\n\\multicolumn{3}{|l|}{\\textsf{\\footnotesize{}r8(/r)}} & \\textsf{\\footnotesize{}AL} & \\textsf{\\footnotesize{}CL} & \\textsf{\\footnotesize{}DL} & \\textsf{\\footnotesize{}BL} & \\textsf{\\footnotesize{}AH} & \\textsf{\\footnotesize{}CH} & \\textsf{\\footnotesize{}DH} & \\textsf{\\footnotesize{}BH }\\tabularnewline\r\n\\multicolumn{3}{|l|}{\\textsf{\\footnotesize{}r16(/r)}} & \\textsf{\\footnotesize{}AX} & \\textsf{\\footnotesize{}CX } & \\textsf{\\footnotesize{}DX } & \\textsf{\\footnotesize{}BX} & \\textsf{\\footnotesize{}SP } & \\textsf{\\footnotesize{}BP} & \\textsf{\\footnotesize{}SI} & \\textsf{\\footnotesize{}DI }\\tabularnewline\r\n\\multicolumn{3}{|l|}{\\textsf{\\footnotesize{}r32(/r)}} & \\textsf{\\footnotesize{}EAX} & \\textsf{\\footnotesize{}ECX} & \\textsf{\\footnotesize{}EDX } & \\textsf{\\footnotesize{}EBX } & \\textsf{\\footnotesize{}ESP} & \\textsf{\\footnotesize{}EBP} & \\textsf{\\footnotesize{}ESI} & \\textsf{\\footnotesize{}EDI}\\tabularnewline\r\n\\multicolumn{3}{|l|}{\\textsf{\\footnotesize{}mm(/r)}} & \\textsf{\\footnotesize{}MM0} & \\textsf{\\footnotesize{}MM1} & \\textsf{\\footnotesize{}MM2} & \\textsf{\\footnotesize{}MM3} & \\textsf{\\footnotesize{}MM4} & \\textsf{\\footnotesize{}MM5} & \\textsf{\\footnotesize{}MM6} & \\textsf{\\footnotesize{}MM7}\\tabularnewline\r\n\\multicolumn{3}{|l|}{\\textsf{\\footnotesize{}xmm(/r) }} & \\textsf{\\footnotesize{}XMM0} & \\textsf{\\footnotesize{}XMM1} & \\textsf{\\footnotesize{}XMM2} & \\textsf{\\footnotesize{}XMM3} & \\textsf{\\footnotesize{}XMM4} & \\textsf{\\footnotesize{}XMM5} & \\textsf{\\footnotesize{}XMM6} & \\textsf{\\footnotesize{}XMM7}\\tabularnewline\r\n\\multicolumn{3}{|l|}{\\textsf{\\footnotesize{}(In decimal) /digit (Opcode)}} & \\textsf{\\footnotesize{}0} & \\textsf{\\footnotesize{}1 } & \\textsf{\\footnotesize{}2} & \\textsf{\\footnotesize{}3} & \\textsf{\\footnotesize{}4} & \\textsf{\\footnotesize{}5} & \\textsf{\\footnotesize{}6} & \\textsf{\\footnotesize{}7 }\\tabularnewline\r\n\\multicolumn{3}{|l|}{\\textsf{\\footnotesize{}(In binary) REG =}} & \\textsf{\\footnotesize{}000} & \\textsf{\\footnotesize{}001} & \\textsf{\\footnotesize{}010} & \\textsf{\\footnotesize{}011} & \\textsf{\\footnotesize{}100} & \\textsf{\\footnotesize{}101} & \\textsf{\\footnotesize{}110} & \\textsf{\\footnotesize{}111}\\tabularnewline\r\n\\hline \r\n\\textsf{\\textbf{\\footnotesize{}~~~~~~~Effective Address}} & \\textsf{\\textbf{\\footnotesize{}~~Mod}} & \\textsf{\\textbf{\\footnotesize{}~~R/M}} & \\multicolumn{8}{c|}{\\textsf{\\textbf{\\footnotesize{}Values of ModR/M Byte (In Hexadecimal)}}}\\tabularnewline\r\n\\hline \r\n\\texttt{\\footnotesize{}{[}EAX{]}} & \\texttt{\\footnotesize{}00} & \\texttt{\\footnotesize{}000} & \\texttt{\\footnotesize{}00} & \\texttt{\\footnotesize{}08} & \\texttt{\\footnotesize{}10} & \\texttt{\\footnotesize{}18} & \\texttt{\\footnotesize{}20} & \\texttt{\\footnotesize{}28} & \\texttt{\\footnotesize{}30} & \\texttt{\\footnotesize{}38}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}ECX{]}} &  & \\texttt{\\footnotesize{}001} & \\texttt{\\footnotesize{}01} & \\texttt{\\footnotesize{}09} & \\texttt{\\footnotesize{}11} & \\texttt{\\footnotesize{}19} & \\texttt{\\footnotesize{}21} & \\texttt{\\footnotesize{}29} & \\texttt{\\footnotesize{}31} & \\texttt{\\footnotesize{}39}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EDX{]}} &  & \\texttt{\\footnotesize{}010} & \\texttt{\\footnotesize{}02} & \\texttt{\\footnotesize{}0A} & \\texttt{\\footnotesize{}12} & \\texttt{\\footnotesize{}1A} & \\texttt{\\footnotesize{}22} & \\texttt{\\footnotesize{}2A} & \\texttt{\\footnotesize{}32} & \\texttt{\\footnotesize{}3A}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EBX{]}} &  & \\texttt{\\footnotesize{}011} & \\texttt{\\footnotesize{}03} & \\texttt{\\footnotesize{}0B} & \\texttt{\\footnotesize{}13} & \\texttt{\\footnotesize{}1B} & \\texttt{\\footnotesize{}23} & \\texttt{\\footnotesize{}2B} & \\texttt{\\footnotesize{}33} & \\texttt{\\footnotesize{}3B}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}-\\--{]}{[}-\\--{]}\\textsuperscript{1}} &  & \\texttt{\\footnotesize{}100} & \\texttt{\\footnotesize{}04} & \\texttt{\\footnotesize{}0C} & \\texttt{\\footnotesize{}14} & \\texttt{\\footnotesize{}1C} & \\texttt{\\footnotesize{}24} & \\texttt{\\footnotesize{}2C} & \\texttt{\\footnotesize{}34} & \\texttt{\\footnotesize{}3C}\\tabularnewline\r\n\\texttt{\\footnotesize{}disp32\\textsuperscript{2}} &  & \\texttt{\\footnotesize{}101} & \\texttt{\\footnotesize{}05} & \\texttt{\\footnotesize{}0D} & \\texttt{\\footnotesize{}15} & \\texttt{\\footnotesize{}1D} & \\texttt{\\footnotesize{}25} & \\texttt{\\footnotesize{}2D} & \\texttt{\\footnotesize{}35} & \\texttt{\\footnotesize{}3D}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}ESI{]}} &  & \\texttt{\\footnotesize{}110} & \\texttt{\\footnotesize{}06} & \\texttt{\\footnotesize{}0E} & \\texttt{\\footnotesize{}16} & \\texttt{\\footnotesize{}1E} & \\texttt{\\footnotesize{}26} & \\texttt{\\footnotesize{}2E} & \\texttt{\\footnotesize{}36} & \\texttt{\\footnotesize{}3E}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EDI{]}} &  & \\texttt{\\footnotesize{}111} & \\texttt{\\footnotesize{}07} & \\texttt{\\footnotesize{}0F} & \\texttt{\\footnotesize{}17} & \\texttt{\\footnotesize{}1F} & \\texttt{\\footnotesize{}27} & \\texttt{\\footnotesize{}2F} & \\texttt{\\footnotesize{}37} & \\texttt{\\footnotesize{}3F}\\tabularnewline\r\n\\hline \r\n\\texttt{\\footnotesize{}{[}EAX{]} + disp8\\textsuperscript{\\textsf{\\footnotesize{}3}}} & \\texttt{\\footnotesize{}01} & \\texttt{\\footnotesize{}000} & \\texttt{\\footnotesize{}40} & \\texttt{\\footnotesize{}48} & \\texttt{\\footnotesize{}50} & \\texttt{\\footnotesize{}58} & \\texttt{\\footnotesize{}60} & \\texttt{\\footnotesize{}68} & \\texttt{\\footnotesize{}70} & \\texttt{\\footnotesize{}78}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}ECX{]} + disp8} &  & \\texttt{\\footnotesize{}001} & \\texttt{\\footnotesize{}41} & \\texttt{\\footnotesize{}49} & \\texttt{\\footnotesize{}51} & \\texttt{\\footnotesize{}59} & \\texttt{\\footnotesize{}61} & \\texttt{\\footnotesize{}69} & \\texttt{\\footnotesize{}71} & \\texttt{\\footnotesize{}79}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EDX{]} + disp8} &  & \\texttt{\\footnotesize{}010} & \\texttt{\\footnotesize{}42} & \\texttt{\\footnotesize{}4A} & \\texttt{\\footnotesize{}52} & \\texttt{\\footnotesize{}5A} & \\texttt{\\footnotesize{}62} & \\texttt{\\footnotesize{}6A} & \\texttt{\\footnotesize{}72} & \\texttt{\\footnotesize{}7A}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EBX{]} + disp8} &  & \\texttt{\\footnotesize{}011} & \\texttt{\\footnotesize{}43} & \\texttt{\\footnotesize{}4B} & \\texttt{\\footnotesize{}53} & \\texttt{\\footnotesize{}5B} & \\texttt{\\footnotesize{}63} & \\texttt{\\footnotesize{}6B} & \\texttt{\\footnotesize{}73} & \\texttt{\\footnotesize{}7B}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}-\\--{]}{[}-\\--{]} + disp8} &  & \\texttt{\\footnotesize{}100} & \\texttt{\\footnotesize{}44} & \\texttt{\\footnotesize{}4C} & \\texttt{\\footnotesize{}54} & \\texttt{\\footnotesize{}5C} & \\texttt{\\footnotesize{}64} & \\texttt{\\footnotesize{}6C} & \\texttt{\\footnotesize{}74} & \\texttt{\\footnotesize{}7C}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EBP{]} + disp8} &  & \\texttt{\\footnotesize{}101} & \\texttt{\\footnotesize{}45} & \\texttt{\\footnotesize{}4D} & \\texttt{\\footnotesize{}55} & \\texttt{\\footnotesize{}5D} & \\texttt{\\footnotesize{}65} & \\texttt{\\footnotesize{}6D} & \\texttt{\\footnotesize{}75} & \\texttt{\\footnotesize{}7D}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}ESI{]} + disp8} &  & \\texttt{\\footnotesize{}110} & \\texttt{\\footnotesize{}46} & \\texttt{\\footnotesize{}4E} & \\texttt{\\footnotesize{}56} & \\texttt{\\footnotesize{}5E} & \\texttt{\\footnotesize{}66} & \\texttt{\\footnotesize{}6E} & \\texttt{\\footnotesize{}76} & \\texttt{\\footnotesize{}7E}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EDI{]} + disp8} &  & \\texttt{\\footnotesize{}111} & \\texttt{\\footnotesize{}47} & \\texttt{\\footnotesize{}4F} & \\texttt{\\footnotesize{}57} & \\texttt{\\footnotesize{}5F} & \\texttt{\\footnotesize{}67} & \\texttt{\\footnotesize{}6F} & \\texttt{\\footnotesize{}77} & \\texttt{\\footnotesize{}7F}\\tabularnewline\r\n\\hline \r\n\\texttt{\\footnotesize{}{[}EAX{]} + disp32} & \\texttt{\\footnotesize{}10} & \\texttt{\\footnotesize{}000} & \\texttt{\\footnotesize{}80} & \\texttt{\\footnotesize{}88} & \\texttt{\\footnotesize{}90} & \\texttt{\\footnotesize{}98} & \\texttt{\\footnotesize{}A0} & \\texttt{\\footnotesize{}A8} & \\texttt{\\footnotesize{}B0} & \\texttt{\\footnotesize{}B8}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}ECX{]} + disp32} &  & \\texttt{\\footnotesize{}001} & \\texttt{\\footnotesize{}81} & \\texttt{\\footnotesize{}89} & \\texttt{\\footnotesize{}91} & \\texttt{\\footnotesize{}99} & \\texttt{\\footnotesize{}A1} & \\texttt{\\footnotesize{}A9} & \\texttt{\\footnotesize{}B1} & \\texttt{\\footnotesize{}B9}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EDX{]} + disp32} &  & \\texttt{\\footnotesize{}010} & \\texttt{\\footnotesize{}82} & \\texttt{\\footnotesize{}8A} & \\texttt{\\footnotesize{}92} & \\texttt{\\footnotesize{}9A} & \\texttt{\\footnotesize{}A2} & \\texttt{\\footnotesize{}AA} & \\texttt{\\footnotesize{}B2} & \\texttt{\\footnotesize{}BA}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EBX{]} + disp32} &  & \\texttt{\\footnotesize{}011} & \\texttt{\\footnotesize{}83} & \\texttt{\\footnotesize{}8B} & \\texttt{\\footnotesize{}93} & \\texttt{\\footnotesize{}9B} & \\texttt{\\footnotesize{}A3} & \\texttt{\\footnotesize{}AB} & \\texttt{\\footnotesize{}B3} & \\texttt{\\footnotesize{}BB}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}-\\--{]}{[}-\\--{]} + disp32} &  & \\texttt{\\footnotesize{}100} & \\texttt{\\footnotesize{}84} & \\texttt{\\footnotesize{}8C} & \\texttt{\\footnotesize{}94} & \\texttt{\\footnotesize{}9C} & \\texttt{\\footnotesize{}A4} & \\texttt{\\footnotesize{}AC} & \\texttt{\\footnotesize{}B4} & \\texttt{\\footnotesize{}BC}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EBP{]} + disp32} &  & \\texttt{\\footnotesize{}101} & \\texttt{\\footnotesize{}85} & \\texttt{\\footnotesize{}8D} & \\texttt{\\footnotesize{}95} & \\texttt{\\footnotesize{}9D} & \\texttt{\\footnotesize{}A5} & \\texttt{\\footnotesize{}AD} & \\texttt{\\footnotesize{}B5} & \\texttt{\\footnotesize{}BD}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}ESI{]} + disp32} &  & \\texttt{\\footnotesize{}110} & \\texttt{\\footnotesize{}86} & \\texttt{\\footnotesize{}8E} & \\texttt{\\footnotesize{}96} & \\texttt{\\footnotesize{}9E} & \\texttt{\\footnotesize{}A6} & \\texttt{\\footnotesize{}AE} & \\texttt{\\footnotesize{}B6} & \\texttt{\\footnotesize{}BE}\\tabularnewline\r\n\\texttt{\\footnotesize{}{[}EDI{]} + disp32} &  & \\texttt{\\footnotesize{}111} & \\texttt{\\footnotesize{}87} & \\texttt{\\footnotesize{}8F} & \\texttt{\\footnotesize{}97} & \\texttt{\\footnotesize{}9F} & \\texttt{\\footnotesize{}A7} & \\texttt{\\footnotesize{}AF} & \\texttt{\\footnotesize{}B7} & \\texttt{\\footnotesize{}BF}\\tabularnewline\r\n\\hline \r\n\\texttt{\\footnotesize{}EAX/AX/AL/MM0/XMM0} & \\texttt{\\footnotesize{}11} & \\texttt{\\footnotesize{}000} & \\texttt{\\footnotesize{}C0} & \\texttt{\\footnotesize{}C8} & \\texttt{\\footnotesize{}D0} & \\texttt{\\footnotesize{}D8} & \\texttt{\\footnotesize{}E0} & \\texttt{\\footnotesize{}E8} & \\texttt{\\footnotesize{}F0} & \\texttt{\\footnotesize{}F8}\\tabularnewline\r\n\\texttt{\\footnotesize{}ECX/CX/CL/MM/XMM1} &  & \\texttt{\\footnotesize{}001} & \\texttt{\\footnotesize{}C1} & \\texttt{\\footnotesize{}C9} & \\texttt{\\footnotesize{}D1} & \\texttt{\\footnotesize{}D9} & \\texttt{\\footnotesize{}E1} & \\texttt{\\footnotesize{}E9} & \\texttt{\\footnotesize{}F1} & \\texttt{\\footnotesize{}F9}\\tabularnewline\r\n\\texttt{\\footnotesize{}EDX/DX/DL/MM2/XMM2} &  & \\texttt{\\footnotesize{}010} & \\texttt{\\footnotesize{}C2} & \\texttt{\\footnotesize{}CA} & \\texttt{\\footnotesize{}D2} & \\texttt{\\footnotesize{}DA} & \\texttt{\\footnotesize{}E2} & \\texttt{\\footnotesize{}EA} & \\texttt{\\footnotesize{}F2} & \\texttt{\\footnotesize{}FA}\\tabularnewline\r\n\\texttt{\\footnotesize{}EBX/BX/BL/MM3/XMM3} &  & \\texttt{\\footnotesize{}011} & \\texttt{\\footnotesize{}C3} & \\texttt{\\footnotesize{}CB} & \\texttt{\\footnotesize{}D3} & \\texttt{\\footnotesize{}DB} & \\texttt{\\footnotesize{}E3} & \\texttt{\\footnotesize{}EB} & \\texttt{\\footnotesize{}F3} & \\texttt{\\footnotesize{}FB}\\tabularnewline\r\n\\texttt{\\footnotesize{}ESP/SP/AH/MM4/XMM4} &  & \\texttt{\\footnotesize{}100} & \\texttt{\\footnotesize{}C4} & \\texttt{\\footnotesize{}CC} & \\texttt{\\footnotesize{}D4} & \\texttt{\\footnotesize{}DC} & \\texttt{\\footnotesize{}E4} & \\texttt{\\footnotesize{}EC} & \\texttt{\\footnotesize{}F4} & \\texttt{\\footnotesize{}FC}\\tabularnewline\r\n\\texttt{\\footnotesize{}EBP/BP/CH/MM5/XMM5} &  & \\texttt{\\footnotesize{}101} & \\texttt{\\footnotesize{}C5} & \\texttt{\\footnotesize{}CD} & \\texttt{\\footnotesize{}D5} & \\texttt{\\footnotesize{}DD} & \\texttt{\\footnotesize{}E5} & \\texttt{\\footnotesize{}ED} & \\texttt{\\footnotesize{}F5} & \\texttt{\\footnotesize{}FD}\\tabularnewline\r\n\\texttt{\\footnotesize{}ESI/SI/DH/MM6/XMM6} &  & \\texttt{\\footnotesize{}110} & \\texttt{\\footnotesize{}C6} & \\texttt{\\footnotesize{}CE} & \\texttt{\\footnotesize{}D6} & \\texttt{\\footnotesize{}DE} & \\texttt{\\footnotesize{}E6} & \\texttt{\\footnotesize{}EE} & \\texttt{\\footnotesize{}F6} & \\texttt{\\footnotesize{}FE}\\tabularnewline\r\n\\texttt{\\footnotesize{}EDI/DI/BH/MM7/XMM7} &  & \\texttt{\\footnotesize{}111} & \\texttt{\\footnotesize{}C7} & \\texttt{\\footnotesize{}CF} & \\texttt{\\footnotesize{}D7} & \\texttt{\\footnotesize{}DF} & \\texttt{\\footnotesize{}E7} & \\texttt{\\footnotesize{}EF} & \\texttt{\\footnotesize{}F7} & \\texttt{\\footnotesize{}FF}\\tabularnewline\r\n\\hline \r\n\\multicolumn{11}{>{\\raggedright}p{14.5cm}}{\\begin{enumerate}\r\n\\item \\textsf{\\footnotesize{}The {[}-\\--{]}{[}-\\--{]} nomenclature means\r\na SIB follows the ModR/M byte.}{\\footnotesize \\par}\r\n\\item \\textsf{\\footnotesize{}The disp32 nomenclature denotes a 32-bit displacement\r\nthat follows the ModR/M byte (or the SIB byte if one is present) and\r\nthat is added to the index.}{\\footnotesize \\par}\r\n\\item \\textsf{\\footnotesize{}The disp8 nomenclature denotes an 8-bit displacement\r\nthat follows the ModR/M byte (or the SIB byte if one is present) and\r\nthat is sign-extended and added to the index.}\\end{enumerate}\r\n}\\tabularnewline\r\n\\end{tabular}\r\n"
  },
  {
    "path": "book_src/images/05/.rid",
    "content": "a9ba32dde86cd02374a576cbb347870e"
  },
  {
    "path": "book_src/images/06/.rid",
    "content": "978a2a6b43366794c5decbda95bf1e08"
  },
  {
    "path": "book_src/images/07/.rid",
    "content": "58310471881fcc80a0b152521c5de8ae"
  },
  {
    "path": "book_src/images/08/.rid",
    "content": "1ba7fe42ddc5a44d1184e22d6c2fa796"
  },
  {
    "path": "book_src/references.bib",
    "content": "% This file was created with JabRef 2.10.\r\n% Encoding: UTF8\r\n\r\n\r\n@InBook{Hardy,\r\n  Title                    = {A Mathematician's Apology},\r\n  Author                   = {G. H. Hardy},\r\n  Chapter                  = {10},\r\n  Pages                    = {13},\r\n  Publisher                = {University of Alberta Mathematical Sciences Society},\r\n  Year                     = {2005},\r\n\r\n  Owner                    = {Tu Do},\r\n  Timestamp                = {2016.10.05}\r\n}\r\n\r\n@Book{intel_opt_manual,\r\n  Title                    = {Intel® 64 and IA-32 Architectures Optimization Reference Manual},\r\n  Author                   = {Intel},\r\n  Pages                    = {672},\r\n  Publisher                = {Intel},\r\n  Year                     = {2016},\r\n\r\n  Owner                    = {Tu Do},\r\n  Timestamp                = {2016.12.12}\r\n}\r\n\r\n@Book{Intel_volume1,\r\n  Title                    = {Intel 64 and IA-32 Architectures Software Developer's Manual Volume 1: Basic Architecture},\r\n  Author                   = {Intel},\r\n  Pages                    = {482},\r\n  Publisher                = {Intel},\r\n  Year                     = {2016},\r\n\r\n  Owner                    = {Tu Do},\r\n  Timestamp                = {2016.12.15}\r\n}\r\n\r\n@InBook{Kovitz_psr,\r\n  Title                    = {Practical Software Requirements},\r\n  Author                   = {Benjamin L. Kovitz},\r\n  Chapter                  = {3},\r\n  Pages                    = {53},\r\n  Publisher                = {Manning},\r\n  Year                     = {1999},\r\n\r\n  __markedentry            = {[Tu Do:]},\r\n  Owner                    = {Tu Do},\r\n  Timestamp                = {2016.09.20}\r\n}\r\n\r\n@InBook{Peirce,\r\n  Title                    = {Collected Papers v. 4},\r\n  Author                   = {Charles Sanders Peirce},\r\n  Chapter                  = {A Boolean Algebra with One Constant},\r\n  Year                     = {1933},\r\n\r\n  Owner                    = {Tu Do},\r\n  Timestamp                = {2016.09.28}\r\n}\r\n\r\n@Manual{74hc00_data_sheet,\r\n  Title                    = {74HC00; 74HCT00 Quad 2-input NAND gate},\r\n  Author                   = {NXP Semiconductors},\r\n  Edition                  = {7},\r\n  Month                    = {November},\r\n  Organization             = {NXP Semiconductors},\r\n  Year                     = {2015},\r\n\r\n  Owner                    = {Tu Do},\r\n  Timestamp                = {2017.01.16}\r\n}\r\n\r\n@InBook{John_digital,\r\n  Title                    = {Digital Design: Principles and Practices},\r\n  Author                   = {John F. Wakerly},\r\n  Chapter                  = {3},\r\n  Pages                    = {86},\r\n  Publisher                = {Prentice Hall},\r\n  Year                     = {1999},\r\n\r\n  Owner                    = {Tu Do},\r\n  Timestamp                = {2016.09.27}\r\n}\r\n\r\n"
  },
  {
    "path": "code/chapter7/os/.gdbinit",
    "content": "define hook-stop\n    # Translate the segment:offset into a physical address\n    printf \"[%4x:%4x] \", $cs, $eip\nend\nset architecture i8086\nlayout asm\nlayout reg\nset disassembly-flavor intel\ntarget remote localhost:26000\nsymbol-file build/os/os\nb *0x7c00\nb main\n"
  },
  {
    "path": "code/chapter7/os/Makefile",
    "content": "BUILD_DIR=build\nBOOTLOADER=$(BUILD_DIR)/bootloader/bootloader.o\nOS=$(BUILD_DIR)/os/sample.o\nDISK_IMG=disk.img\n\nall: bootdisk\n\n.PHONY: bootdisk bootloader os\n\nbootloader:\n\tmake -C bootloader\n\nos:\n\tmake -C os\n\nbootdisk: bootloader os\n\tdd if=/dev/zero of=$(DISK_IMG) bs=512 count=2880\n\tdd conv=notrunc if=$(BOOTLOADER) of=$(DISK_IMG) bs=512 count=1 seek=0\n\tdd conv=notrunc if=$(OS) of=$(DISK_IMG) bs=512 count=1 seek=1\n\nqemu:\n\tqemu-system-i386 -machine q35 -fda $(DISK_IMG) -gdb tcp::26000 -S\n\nclean:\n\tmake -C bootloader clean\n\tmake -C os clean\n"
  },
  {
    "path": "code/chapter7/os/bootloader/Makefile",
    "content": "BUILD_DIR=../build/bootloader\n\nBOOTLOADER_SRCS := $(wildcard *.asm)\nBOOTLOADER_OBJS := $(patsubst %.asm, $(BUILD_DIR)/%.o, $(BOOTLOADER_SRCS))\n\nall: $(BOOTLOADER_OBJS)\n\n$(BUILD_DIR)/%.o: %.asm\n\tnasm -f bin $< -o $@\n\nclean:\n\trm $(BUILD_DIR)/*\n"
  },
  {
    "path": "code/chapter7/os/bootloader/bootloader.asm",
    "content": ";******************************************\n; Bootloader.asm\n; A Simple Bootloader\n;******************************************\nbits 16\nstart: jmp boot\n\n;; constant and variable definitions\nmsg\tdb\t\"Welcome to My Operating System!\", 0ah, 0dh, 0h\n\nboot:\n  cli\t; no interrupts\n  cld\t; all that we need to init\n\n  mov\t\tax, 50h\n\n  ; ;; set the buffer\n\tmov\tes, ax\n\txor\tbx, bx\n\n  mov\tal, 2\t\t\t\t\t      ; read 2 sector\n\tmov\tch, 0\t\t\t\t\t      ; we are reading the second sector past us, so its still on track 0\n\tmov\tcl, 2\t\t\t\t\t      ; sector to read (The second sector)\n\tmov\tdh, 0\t\t\t\t\t      ; head number\n\tmov\tdl, 0\t\t\t\t\t      ; drive number. Remember Drive 0 is floppy drive.\n\n  mov\tah, 0x02\t\t\t      ; read floppy sector function\n\tint\t0x13\t\t\t\t\t      ; call BIOS - Read the sector\n  jmp 0x50:0x0     \t\t\t\t; jump and execute the sector!\n\n  hlt\t; halt the system\n\n  ; We have to be 512 bytes. Clear the rest of the bytes with 0\n  times 510 - ($-$$) db 0\n  dw 0xAA55\t\t\t\t  ; Boot Signiture\n"
  },
  {
    "path": "code/chapter7/os/os/Makefile",
    "content": "BUILD_DIR=../build/os\n\nOS_SRCS := $(wildcard *.asm)\nOS_OBJS := $(patsubst %.asm, $(BUILD_DIR)/%.o, $(OS_SRCS))\n\nall: $(OS_OBJS)\n\n$(BUILD_DIR)/%.o: %.asm\n\tnasm -f bin $< -o $@\n\nclean:\n\trm $(BUILD_DIR)/*\n"
  },
  {
    "path": "code/chapter7/os/os/sample.asm",
    "content": ";******************************************\n; sample.asm\t\t\n; A Sample Program\n;******************************************\nmov eax, 1\nadd eax, 1\n"
  },
  {
    "path": "code/chapter8/os/.gdbinit",
    "content": "define hook-stop\n    # Translate the segment:offset into a physical address\n    printf \"[%4x:%4x] \", $cs, $eip\nend\nset architecture i8086\nlayout asm\nlayout reg\nset disassembly-flavor intel\ntarget remote localhost:26000\nsymbol-file build/os/os\nb *0x7c00\nb main\n"
  },
  {
    "path": "code/chapter8/os/Makefile",
    "content": "BUILD_DIR=build\nBOOTLOADER=$(BUILD_DIR)/bootloader/bootloader.o\nOS=$(BUILD_DIR)/os/os\nDISK_IMG=disk.img\n\nall: bootdisk\n\n.PHONY: bootloader os\n\nbootloader:\n\tmake -C bootloader\n\nos:\n\tmake -C os\n\nbootdisk:  bootloader os\n\t# @echo \"size is $(size)\"\n\t# @echo \"count is $(count)\"\n\tdd if=/dev/zero of=$(DISK_IMG) bs=512 count=2880\n\tdd conv=notrunc if=$(BOOTLOADER) of=$(DISK_IMG) bs=512 count=1 seek=0\n\t# dd conv=notrunc if=$(OS) of=$(DISK_IMG) bs=512 count=1 seek=1\n\tdd conv=notrunc if=$(OS) of=$(DISK_IMG) bs=512 count=$$(($(shell stat --printf=\"%s\" $(OS))/512)) seek=1\n\nqemu:\n\tqemu-system-i386 -machine q35 -fda $(DISK_IMG) -gdb tcp::26000 -S\n\nclean:\n\tmake -C bootloader clean\n\tmake -C os clean\n"
  },
  {
    "path": "code/chapter8/os/bootloader/Makefile",
    "content": "BUILD_DIR=../build/bootloader\n\nBOOTLOADER_SRCS := $(wildcard *.asm)\nBOOTLOADER_OBJS := $(patsubst %.asm, $(BUILD_DIR)/%.o, $(BOOTLOADER_SRCS))\n\nall: $(BOOTLOADER_OBJS)\n\n$(BUILD_DIR)/%.o: %.asm\n\tnasm -f elf $< -F dwarf -g -o $@\n\tld -m elf_i386 -T bootloader.lds $@ -o $@.elf\n\tobjcopy -O binary  $(BUILD_DIR)/bootloader.o.elf $@\n\nclean:\n\trm $(BUILD_DIR)/*\n"
  },
  {
    "path": "code/chapter8/os/bootloader/bootloader.asm",
    "content": ";******************************************\n; Bootloader.asm\n; A Simple Bootloader\n;******************************************\nbits 16\nstart: jmp boot\n\n;; constant and variable definitions\nmsg\tdb\t\"Welcome to My Operating System!\", 0ah, 0dh, 0h\n\nboot:\n  cli\t; no interrupts\n  cld\t; all that we need to init\n\n  mov\t\tax, 50h\n\n  ; ;; set the buffer\n\tmov\tes, ax\n\txor\tbx, bx\n\n  mov\tal, 17\t\t\t\t\t      ; read 2 sector\n\tmov\tch, 0\t\t\t\t\t      ; we are reading the second sector past us, so its still on track 0\n\tmov\tcl, 2\t\t\t\t\t      ; sector to read (The second sector)\n\tmov\tdh, 0\t\t\t\t\t      ; head number\n\tmov\tdl, 0\t\t\t\t\t      ; drive number. Remember Drive 0 is floppy drive.\n\n  mov\tah, 0x02\t\t\t      ; read floppy sector function\n\tint\t0x13\t\t\t\t\t      ; call BIOS - Read the sector\n  jmp\t[500h + 0x18]\t\t\t\t; jump and execute the sector!\n\n  hlt\t; halt the system\n\n  ; We have to be 512 bytes. Clear the rest of the bytes with 0\n  times 510 - ($-$$) db 0\n  dw 0xAA55\t\t\t\t  ; Boot Signiture\n"
  },
  {
    "path": "code/chapter8/os/bootloader/bootloader.lds",
    "content": "OUTPUT(bootloader);\n\nPHDRS\n{\n  headers PT_NULL;\n  text PT_LOAD FILEHDR PHDRS ;\n  data PT_LOAD ;\n}\n\n\nSECTIONS\n{\n  . = SIZEOF_HEADERS;\n  .text 0x7c00:  {  *(.text)  } :text\n  .data :  {  *(.data)  } :data\n}\n"
  },
  {
    "path": "code/chapter8/os/os/Makefile",
    "content": "BUILD_DIR=../build/os\nOS=$(BUILD_DIR)/os\n\nCFLAGS+=-ffreestanding -nostdlib  -m32 -gdwarf-4 -ggdb3\n\nOS_SRCS := $(wildcard *.c)\nOS_OBJS := $(patsubst %.c, $(BUILD_DIR)/%.o, $(OS_SRCS))\n\nall: $(OS)\n\n$(BUILD_DIR)/%.o: %.c\n\tgcc $(CFLAGS) -m32 -c  $< -o $@\n\n$(OS): $(OS_OBJS)\n\tld -m elf_i386 -nmagic -Tos.lds $(OS_OBJS) -o $@\n\t# objcopy --only-keep-debug $(OS) $(OS).dbg\n\t# strip -g $(OS)\n\nclean:\n\trm $(BUILD_DIR)/*\n"
  },
  {
    "path": "code/chapter8/os/os/main.c",
    "content": "void main(){}\n"
  },
  {
    "path": "code/chapter8/os/os/os.lds",
    "content": "ENTRY(main);\n\nPHDRS\n{\n  headers PT_PHDR FILEHDR PHDRS;\n  code PT_LOAD;\n}\n\nSECTIONS\n{\n  .text 0x600: ALIGN(0x100)  { *(.text)  } :code\n  .data :  { *(.data)  }\n  .bss :  { *(.bss) }\n  /DISCARD/ : { *(.eh_frame) }\n}\n"
  },
  {
    "path": "code/chapter8/os/os/sample.asm",
    "content": ";******************************************\n; sample.asm\t\t\n; A Sample Program\n;******************************************\nmov eax, 1\nadd eax, 1\n"
  }
]